apilo - refresh token fix

This commit is contained in:
2025-11-25 10:27:03 +00:00
parent 7140100a08
commit 97274ccde1
5 changed files with 103 additions and 70 deletions

View File

@@ -1,2 +1,80 @@
<?php
echo 'TEMU';
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set('display_errors', 1);
$filename = getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/TEMU/files/10-2025.csv';
$handle = fopen($filename, 'r');
if ($handle === false) {
die('Cannot open file: ' . $filename);
}
$invoices = [];
$documentsNo = [];
$isFirstRow = true;
while (($data = fgetcsv($handle, 0, ",")) !== false) {
if ($isFirstRow) {
$isFirstRow = false;
continue;
}
for ($i = 10; $i <= 20; $i++) {
$data[$i] = str_replace(',', '.', $data[$i]);
}
if ($data[6] != '23%') {
die('VAT inny niż 23%! '.$data[6]);
}
if ($data[21] != 'PLN') {
die('Waluta inna niż PLN! '.$data[21]);
}
if (!isset($invoices[$data[22]])) {
$inv = array();
$inv['document_no'] = $data[22];
$inv['order_no'] = $data[0];
$inv['parent_name'] = 'TemuCustomer';
$inv['parent_hash'] = substr(md5($inv['parent_name']), 0, 20);
$inv['parent_short_name'] = substr($inv['parent_name'], 0, 40);
$inv['parent_address_city'] = '';
$inv['parent_address_postalcode'] = '';
$inv['parent_address_street'] = '';
$inv['parent_nip'] = '';
$inv['category'] = "Sprzedaż";
$inv['register_date'] = $data[3];
$inv['sell_date'] = $data[3];
$inv['total_netto'] = floatval($data[10]) + floatval($data[11]) + floatval($data[12]) + floatval($data[13]);
$inv['total_vat'] = floatval($data[20]);
$inv['vat_calculated'] = round($inv['total_netto'] * 0.23,2);
$inv['total_brutto'] = $inv['total_netto'] + $inv['total_vat'];
if (substr($data[22], 0, 2) == 'CN') {
$inv['type'] = 'correcting';
} else {
$inv['type'] = 'normal';
}
} else {
$inv = $invoices[$data[22]];
$inv['total_netto'] += floatval($data[10]) + floatval($data[11]) + floatval($data[12]) + floatval($data[13]);
$inv['total_vat'] += floatval($data[20]);
$inv['vat_calculated'] = round($inv['total_netto'] * 0.23,2);
$inv['total_brutto'] = $inv['total_netto'] + $inv['total_vat'];
}
$invoices[$data[22]] = $inv;
}
fclose($handle);
$db = $GLOBALS['db'];
$db->query("SET NAMES utf8mb4");
foreach ($invoices as $inv) {
$query = sprintf("INSERT INTO ecommerce_invoices VALUES ('%s', '%s', '%s', '%s', '%s', 'temu', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%f', null, null, null, null, null, null);",
getId(), $inv['document_no'], $inv['type'], $inv['register_date'], $inv['sell_date'],
$inv['order_no'], 'TemuCustomer', $inv['parent_nip'], null, null, '',
'', null, 'PLN', $inv['total_netto'], $inv['total_brutto'], $inv['total_vat']);
$res = $db->query($query);
if (!$res) {
echo "Query error: ".$query.PHP_EOL;
}
}
function getId() {
$db = $GLOBALS['db'];
$res = $db->fetchByAssoc($db->query("SELECT UUID() as id;"));
return $res['id'];
}

View File

@@ -3,27 +3,27 @@
//error_reporting(LC_ALL);
//ini_set('display_errors', 1);
// ?XDEBUG_SESSION_START=PHPSTORM
//apilo_importInvoices();
function apilo_importInvoices()
{
$apilo_config = apilo_loadConfiguration();
$db = $GLOBALS['db'];
$dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'");
$offset = intval($db->fetchByAssoc($dbRes)['last_id']);
$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
brecho($apilo_config);
if (isset($invoices->errors)) {
if (isset($invoices->error)) {
if (apilo_refreshToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
//$apilo_config = apilo_loadConfiguration();
//$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
$apilo_config = apilo_loadConfiguration();
$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
} else {
return false;
}
}
brecho($invoices);
brecho(count($invoices->documents));
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, documents count', count($invoices->documents));
@@ -56,7 +56,7 @@ function apilo_loadInvoices($token, $offset)
$url = "https://twinpol.apilo.com/rest/api/finance/documents/";
$params = [
'type' => 1,
'limit' => 50,
'limit' => 100,
'offset' => $offset
];
$url .= '?' . http_build_query($params);
@@ -105,19 +105,19 @@ function apilo_refreshToken($refreshToken, $clientId, $clientSecret)
$err = curl_error($curl);
curl_close($curl);
brecho($response);
brecho($httpCode);
if ($httpCode !== 200) {
if ($httpCode !== 201) {
return false;
}
$tokenData = json_decode($response, true);
brecho($tokenData);
var_dump($tokenData);
if (isset($tokenData['accessToken'])) {
global $db;
$db->query("UPDATE config SET value='" . $tokenData['access_token'] . "' WHERE category='apilo' AND name='access_token'");
if (isset($tokenData['refresh_token'])) {
$db->query("UPDATE config SET value='" . $tokenData['refresh_token'] . "' WHERE category='apilo' AND name='refresh_token'");
$db->query("UPDATE config SET value='" . $tokenData['accessToken'] . "' WHERE category='apilo' AND name='token'");
if (isset($tokenData['refreshToken'])) {
$db->query("UPDATE config SET value='" . $tokenData['refreshToken'] . "' WHERE category='apilo' AND name='refreshToken'");
}
return true;
}

View File

@@ -1,108 +1,60 @@
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*****************************************************************************
* The contents of this file are subject to the RECIPROCAL PUBLIC LICENSE
* Version 1.1 ("License"); You may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/rpl.php. Software distributed under the
* License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
* either express or implied.
*
* You may:
* a) Use and distribute this code exactly as you received without payment or
* a royalty or other fee.
* b) Create extensions for this code, provided that you make the extensions
* publicly available and document your modifications clearly.
* c) Charge for a fee for warranty or support or for accepting liability
* obligations for your customers.
*
* You may NOT:
* a) Charge for the use of the original code or extensions, including in
* electronic distribution models, such as ASP (Application Service
* Provider).
* b) Charge for the original source code or your extensions other than a
* nominal fee to cover distribution costs where such distribution
* involves PHYSICAL media.
* c) Modify or delete any pre-existing copyright notices, change notices,
* or License text in the Licensed Software
* d) Assert any patent claims against the Licensor or Contributors, or
* which would in any way restrict the ability of any third party to use the
* Licensed Software.
*
* You must:
* a) Document any modifications you make to this code including the nature of
* the change, the authors of the change, and the date of the change.
* b) Make the source code for any extensions you deploy available via an
* Electronic Distribution Mechanism such as FTP or HTTP download.
* c) Notify the licensor of the availability of source code to your extensions
* and include instructions on how to acquire the source code and updates.
* d) Grant Licensor a world-wide, non-exclusive, royalty-free license to use,
* reproduce, perform, modify, sublicense, and distribute your extensions.
*
* The Original Code is: CommuniCore
* Olavo Farias
* 2006-04-7 olavo.farias@gmail.com
*
* The Initial Developer of the Original Code is CommuniCore.
* Portions created by CommuniCore are Copyright (C) 2005 CommuniCore Ltda
* All Rights Reserved.
********************************************************************************/
global $mod_strings, $current_user;
global $mod_strings, $current_user;
if(ACLController::checkAccess('EcmInvoiceOuts', "edit", true)) $module_menu [] = Array("index.php?module=".'EcmInvoiceOuts'."&action=EditView&return_module=".'EcmInvoiceOuts'."&return_action=DetailView", translate('LNK_NEW_'.'ECMQUOTE', 'EcmInvoiceOuts'),"CreateEcmInvoiceOuts", 'EcmInvoiceOuts');
if (ACLController::checkAccess('EcmInvoiceOuts', "edit", true)) $module_menu [] = array("index.php?module=" . 'EcmInvoiceOuts' . "&action=EditView&return_module=" . 'EcmInvoiceOuts' . "&return_action=DetailView", translate('LNK_NEW_' . 'ECMQUOTE', 'EcmInvoiceOuts'), "CreateEcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=index&return_module=EcmInvoiceOuts&return_action=DetailView", translate('LNK_ECMQUOTES_LIST','EcmInvoiceOuts'),"EcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=Report_INTRASTAT", "Raport INTRASTAT","EcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=ecommerce", "Faktury E-Commerce","EcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=bimit_invoiceSummary", "Analiza faktur","EcmInvoiceOuts", 'EcmInvoiceOuts');
if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=index&return_module=EcmInvoiceOuts&return_action=DetailView", translate('LNK_ECMQUOTES_LIST', 'EcmInvoiceOuts'), "EcmInvoiceOuts", 'EcmInvoiceOuts');
if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=Report_INTRASTAT", "Raport INTRASTAT", "EcmInvoiceOuts", 'EcmInvoiceOuts');
if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=ecommerce", "Faktury E-Commerce", "EcmInvoiceOuts", 'EcmInvoiceOuts');
if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=bimit_invoiceSummary", "Analiza faktur", "EcmInvoiceOuts", 'EcmInvoiceOuts');

View File

@@ -27,6 +27,8 @@ if (isset($_REQUEST['import_baselinker'])) {
include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/baselinkerInvoiceDetails.php');
} else if (isset($_REQUEST['apilo_products'])) {
include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/checkApiloProducts.php');
} else if (isset($_REQUEST['temu'])) {
include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/TEMU/temuInvoicesListView.php');
} else {
include_once(getcwd().'/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php');
}