Skip to content

Commit

Permalink
Aggiornamenti vari
Browse files Browse the repository at this point in the history
- Fix per l’invio della fattura elettronica con aggiunta metodo di pagamento
- Aggiunta dell’userAgent alle chiamate API
- Inserimento versione nei log
  • Loading branch information
websuvius committed Feb 1, 2023
1 parent 1f7cfa7 commit 71d2914
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 95 deletions.
2 changes: 1 addition & 1 deletion fattureincloud/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>fattureincloud</name>
<displayName><![CDATA[FattureInCloud]]></displayName>
<version><![CDATA[2.1.2]]></version>
<version><![CDATA[2.1.3]]></version>
<description><![CDATA[Collega il tuo negozio Prestashop al tuo account FattureInCloud! Sincronizza gli ordini, le anagrafiche ed emetti fatture!]]></description>
<author><![CDATA[FattureInCloud]]></author>
<tab><![CDATA[billing_invoicing]]></tab>
Expand Down
97 changes: 66 additions & 31 deletions fattureincloud/fattureincloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct()
{
$this->name = 'fattureincloud';
$this->tab = 'billing_invoicing';
$this->version = '2.1.2';
$this->version = '2.1.3';
$this->author = 'FattureInCloud';
$this->need_instance = 1;

Expand Down Expand Up @@ -156,15 +156,17 @@ public function getContent()

if (Configuration::get('FATTUREINCLOUD_ACCESS_TOKEN') == "" || Configuration::get('FATTUREINCLOUD_REFRESH_TOKEN') == "") {
$fic_client = new FattureInCloudClient();
$fic_client->setUserAgent("FattureInCloud/Prestashop/" . $this->version);
$device_code_request = $fic_client->oauthDevice();

$this->context->smarty->assign('device_code_request', $device_code_request);

$output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/connect.tpl');
} elseif (Configuration::get('FATTUREINCLOUD_COMPANY_ID') == "" && Configuration::get('FATTUREINCLOUD_ACCESS_TOKEN') != "" && Configuration::get('FATTUREINCLOUD_REFRESH_TOKEN') != "") {
$fic_client = new FattureInCloudClient();
$fic_client->setUserAgent("FattureInCloud/Prestashop/" . $this->version);
$fic_client->setAccessToken(Configuration::get('FATTUREINCLOUD_ACCESS_TOKEN'));

$controlled_companies_request = $fic_client->getControlledCompanies();

$this->context->smarty->assign('controlled_companies_request', $controlled_companies_request);
Expand Down Expand Up @@ -759,21 +761,7 @@ public function hookActionValidateOrder($params)
*/
public function composeOrder($order_id)
{
$order_to_create = $this->mapPrestashopToFicDocument($order_id);

$order_to_create['data']['type'] = "order";

if (Configuration::get('FATTUREINCLOUD_ORDERS_SUFFIX')) {
$order_to_create['data']['numeration'] = Configuration::get('FATTUREINCLOUD_ORDERS_SUFFIX');
}

if (Configuration::get('FATTUREINCLOUD_ORDERS_UPDATE_STORAGE')) {
foreach ($order_to_create['data']['items_list'] as $key => $item) {
if (isset($item['product_id'])) {
$order_to_create['data']['items_list'][$key]['stock'] = true;
}
}
}
$order_to_create = $this->mapPrestashopToFicDocument("order", $order_id);

return $order_to_create;
}
Expand Down Expand Up @@ -875,18 +863,8 @@ public function hookActionOrderStatusPostUpdate($order)
*/
public function composeInvoice($order_id)
{
$invoice_to_create = $this->mapPrestashopToFicDocument($order_id);

$invoice_to_create['data']['type'] = "invoice";

if (Configuration::get('FATTUREINCLOUD_INVOICES_SUFFIX')) {
$invoice_to_create['data']['numeration'] = Configuration::get('FATTUREINCLOUD_INVOICES_SUFFIX');
}

if (Configuration::get('FATTUREINCLOUD_INVOICES_SEND_TO_SDI')) {
$invoice_to_create['data']['ei_invoice'] = true;
}

$invoice_to_create = $this->mapPrestashopToFicDocument("invoice", $order_id);

return $invoice_to_create;
}

Expand Down Expand Up @@ -978,10 +956,41 @@ public function getPaymentAccountIDByName($payment_name)
}
}

/**
* Get MPXX code by Prestashop payment module
*/
public function getEIPaymentCodeByModule($payment_module)
{

$ei_payment = array();

$ei_payment['payment_method'] = "MP08";

if ($payment_module == 'ps_cashondelivery') {
$ei_payment['payment_method'] = "MP01";
} elseif ($payment_module == 'ps_checkpayment') {
$ei_payment['payment_method'] = "MP02";
} elseif (strpos($payment_module, 'wire') !== false) {
$ei_payment['payment_method'] = "MP05";

$payment_module_details = Module::getInstanceByName($payment_module);

if (!empty($payment_module_details->owner)) {
$ei_payment['bank_beneficiary'] = $payment_module_details->owner;
}

if (!empty($payment_module_details->details)) {
$ei_payment['bank_iban'] = $payment_module_details->details;
}
}

return $ei_payment;
}

/**
* Map Prestashop order to FattureInCloud issuedDocument model
*/
public function mapPrestashopToFicDocument($order_id)
public function mapPrestashopToFicDocument($document_type, $order_id)
{
$order = new Order($order_id);

Expand Down Expand Up @@ -1199,6 +1208,10 @@ public function mapPrestashopToFicDocument($order_id)
$this->writeLog("ERROR - Ricerca prodotto fallita: " . json_encode($check_product_request));
} elseif ($check_product_request['total'] == 1) {
$item['product_id'] = $check_product_request['data'][0]['id'];

if ($document_type == "order" && Configuration::get('FATTUREINCLOUD_ORDERS_UPDATE_STORAGE')) {
$item['stock'] = true;
}
}
}

Expand Down Expand Up @@ -1381,6 +1394,7 @@ public function mapPrestashopToFicDocument($order_id)

$document = array(
"data" => array(
"type" => $document_type,
"currency" => $currency,
"entity" => $entity,
"items_list" => $items,
Expand All @@ -1407,6 +1421,25 @@ public function mapPrestashopToFicDocument($order_id)
$document["data"]["notes"] = $notes;
}

if ($document_type == "order") {

if (Configuration::get('FATTUREINCLOUD_ORDERS_SUFFIX')) {
$document['data']['numeration'] = Configuration::get('FATTUREINCLOUD_ORDERS_SUFFIX');
}

} elseif ($document_type == "invoice") {

if (Configuration::get('FATTUREINCLOUD_INVOICES_SUFFIX')) {
$document['data']['numeration'] = Configuration::get('FATTUREINCLOUD_INVOICES_SUFFIX');
}

if (Configuration::get('FATTUREINCLOUD_INVOICES_SEND_TO_SDI')) {
$document['data']['e_invoice'] = true;
$document['data']['ei_data'] = $this->getEIPaymentCodeByModule($order->module);
}

}

return $document;
}

Expand Down Expand Up @@ -1503,6 +1536,8 @@ public function initFattureInCloudClient()
$fic_client->setAccessToken(Configuration::get('FATTUREINCLOUD_ACCESS_TOKEN'));
$fic_client->setRefreshToken(Configuration::get('FATTUREINCLOUD_REFRESH_TOKEN'));

$fic_client->setUserAgent("FattureInCloud/Prestashop/" . $this->version);

$refresh_token_request = $fic_client->oauthRefreshToken();

if (isset($refresh_token_request['access_token'])) {
Expand Down Expand Up @@ -1541,7 +1576,7 @@ public function writeLog($line)
}
}

$log_line = "[" . date("Y/m/d h:i:s", time()) . "] " . $line . "\n";
$log_line = "[" . date("Y/m/d h:i:s", time()) . "] [v" . $this->version . "] " . $line . "\n";
file_put_contents($current_log_file, $log_line, FILE_APPEND);
}
}
10 changes: 9 additions & 1 deletion fattureincloud/libs/fattureincloudClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class FattureInCloudClient
{
private $apiBaseUrl = "https://api-v2.fattureincloud.it";
private $userAgent;
private $clientId = "NTRjNjY0MjU1YzBiODFhYmI4MDc0NGFm";
private $scope = "entity.clients:a issued_documents.orders:a issued_documents.invoices:a settings:a products:a";
private $redirectUri = "https://fattureincloud.it/connetti";
Expand Down Expand Up @@ -62,6 +63,11 @@ public function setCompanyId($companyId)
$this->companyId = $companyId;
}

public function setUserAgent($userAgent)
{
$this->userAgent = $userAgent;
}

public function oauthDevice()
{
$data = array(
Expand Down Expand Up @@ -277,6 +283,7 @@ private function makeGenericRequest($endpoint, $body = null, $method = "GET")
break;
}

curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl . '/'. $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $this->accessToken , "Accept: application/json", "Content-Type: application/json"));
Expand Down Expand Up @@ -376,7 +383,8 @@ private function makeCompanyRequest($endpoint, $body = null, $method = "GET")
}
break;
}


curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl . '/c/' . $this->companyId . '/'. $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $this->accessToken , "Accept: application/json", "Content-Type: application/json"));
Expand Down
148 changes: 86 additions & 62 deletions fattureincloud/views/js/fattureincloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,112 @@ var fattureincloud = function() {

var initConnect = function(interval, device_code) {

$(".connect-error").click(function() {
location.reload();
});

pollForAccessToken(interval, device_code);
try {

$(".connect-error").click(function() {
location.reload();
});

pollForAccessToken(interval, device_code);

} catch(e) {
console.error(e);
}
}

var pollForAccessToken = function(interval, device_code) {

$.ajax({
url: admin_controller_link,
data: {
ajax: true,
action: 'getaccesstoken',
device_code: device_code
},
dataType: "json",
success : function(result){

if (typeof result.error !== "undefined") {
if (result.error == "authorization_pending") {
setTimeout(function() {
pollForAccessToken(interval, device_code);
}, interval * 1000);
try {

$.ajax({
url: admin_controller_link,
data: {
ajax: true,
action: 'getaccesstoken',
device_code: device_code
},
dataType: "json",
success : function(result){

} else {
if (typeof result.error !== "undefined") {
if (result.error == "authorization_pending") {
setTimeout(function() {
pollForAccessToken(interval, device_code);
}, interval * 1000);

$(".connect-container").addClass("hidden");
} else {

$(".connect-container").addClass("hidden");

var errorDetails = result.error + ": " + result.error_description;
$(".connect-error-details").html(errorDetails);
$(".connect-error").removeClass("hidden");
}

var errorDetails = result.error + ": " + result.error_description;
$(".connect-error-details").html(errorDetails);
$(".connect-error").removeClass("hidden");
} else {
location.reload();
}

} else {
location.reload();
}
}
});
});

} catch(e) {
console.error(e);
}
}

var initSelectCompany = function() {

$(".connect-error").click(function() {
location.reload();
});

$("button[data-company-id]").click(function() {

$("button[data-company-id]").addClass("disabled");
$("button[data-company-id]").prop("disabled", true);
try {

saveCompanyId($(this).data("company-id"));
});
$(".connect-error").click(function() {
location.reload();
});

$("button[data-company-id]").click(function() {

$("button[data-company-id]").addClass("disabled");
$("button[data-company-id]").prop("disabled", true);

saveCompanyId($(this).data("company-id"));
});

} catch(e) {
console.error(e);
}

}

var saveCompanyId = function(company_id) {
$.ajax({
url: admin_controller_link,
data: {
ajax: true,
action: 'savecompanyid',
company_id: company_id
},
dataType: "json",
success : function(result){

if (typeof result.error !== "undefined") {

try {

$.ajax({
url: admin_controller_link,
data: {
ajax: true,
action: 'savecompanyid',
company_id: company_id
},
dataType: "json",
success : function(result){

$(".connect-container").addClass("hidden");

var errorDetails = result.error + ": " + result.error_description;
$(".connect-error-details").html(errorDetails);
$(".connect-error").removeClass("hidden");

} else {
location.reload();
if (typeof result.error !== "undefined") {

$(".connect-container").addClass("hidden");

var errorDetails = result.error + ": " + result.error_description;
$(".connect-error-details").html(errorDetails);
$(".connect-error").removeClass("hidden");

} else {
location.reload();
}
}
}
});
});

} catch(e) {
console.error(e);
}
}

return {
Expand Down

0 comments on commit 71d2914

Please sign in to comment.