From 97274ccde123dcb6533e8ae90f79712f2c553796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Tue, 25 Nov 2025 10:27:03 +0000 Subject: [PATCH] apilo - refresh token fix --- index.php | 1 + .../TEMU/temuInvoicesListView.php | 80 ++++++++++++++++++- .../importApiloInvoices.php | 28 +++---- modules/EcmInvoiceOuts/Menu.php | 62 ++------------ modules/EcmInvoiceOuts/ecommerce.php | 2 + 5 files changed, 103 insertions(+), 70 deletions(-) diff --git a/index.php b/index.php index 3c597033..21413e9e 100755 --- a/index.php +++ b/index.php @@ -1,4 +1,5 @@ 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']; +} \ No newline at end of file diff --git a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php index 6f4381a9..1e06873b 100644 --- a/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php +++ b/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php @@ -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; } diff --git a/modules/EcmInvoiceOuts/Menu.php b/modules/EcmInvoiceOuts/Menu.php index 6f7fe3b2..84291fd0 100755 --- a/modules/EcmInvoiceOuts/Menu.php +++ b/modules/EcmInvoiceOuts/Menu.php @@ -1,108 +1,60 @@