import apilo

This commit is contained in:
2025-09-29 17:18:32 +00:00
parent f709c11d00
commit 601b31439c
12 changed files with 238 additions and 39 deletions

View File

@@ -1,10 +1,12 @@
<?php
function importApiloInvoices()
// ?XDEBUG_SESSION_START=PHPSTORM
//importApiloInvoices();
function importInvoices()
{
$apilo_config = loadApiloConfiguration();
$db = $GLOBALS['db'];
$dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin = 'apilo'");
$dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'");
$offset = intval($db->fetchByAssoc($dbRes)['last_id']);
$invoices = loadApiloInvoices($apilo_config['token'], $offset);
@@ -12,16 +14,18 @@ function importApiloInvoices()
if (isset($invoices->error)) {
if (refreshApiloToken($apilo_config['refresh_token'], $apilo_config['client_id'], $apilo_config['client_secret']) == true) {
$apilo_config = loadApiloConfiguration();
$invoices = loadApiloInvoices($apilo_config['access_token'], $offset);
$invoices = loadApiloInvoices($apilo_config['token'], $offset);
} else {
die('Nie udało się odświeżyć tokena apilo');
return false;
}
}
brecho(count($invoices->documents));
$platforms = loadApiloPlatformsList($apilo_config['token']);
if (!$platforms) {
return false;
}
if (isset($invoices->documents) && count($invoices->documents) > 0) {
foreach ($invoices->documents as $invoice) {
addapiloInvoice($invoice, $platforms, $apilo_config['token']);
@@ -29,7 +33,6 @@ function importApiloInvoices()
}
return true;
}
function loadApiloConfiguration()
{
global $db;
@@ -101,17 +104,14 @@ function refreshApiloToken($refreshToken, $clientId, $clientSecret)
$tokenData = json_decode($response, true);
if (isset($tokenData['access_token'])) {
// Zapisujemy nowy token w bazie
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'");
}
return true;
}
return false;
}
function addApiloInvoice($invoice, $platforms, $token)
@@ -119,6 +119,9 @@ function addApiloInvoice($invoice, $platforms, $token)
$db = $GLOBALS['db'];
$platformId = loadApiloOrderPlatformId($token, $invoice->orderId);
if (!$platformId) {
return false;
}
$platformDescription = 'ERROR';
foreach ($platforms as $platform) {
if ($platform->id == $platformId) {
@@ -210,8 +213,7 @@ function addApiloInvoice($invoice, $platforms, $token)
$db->query($query);
if ($db->last_error) {
error_log("Błąd dodawania faktury apilo: " . $db->last_error);
return;
return false;
}
if (isset($invoice->documentItems) && is_array($invoice->documentItems)) {
@@ -227,7 +229,7 @@ function addApiloInvoiceProduct($invoiceId, $item)
$productId = '';
if (isset($item->sku) && !empty($item->sku)) {
$productResult = $db->query(sprintf("SELECT id FROM ecmproducts WHERE code = '%s'", $db->quote($item->sku)));
$productResult = $db->query(sprintf("SELECT id FROM ecmproducts WHERE code = '%s'", trim($db->quote($item->sku))));
$productRow = $db->fetchByAssoc($productResult);
if ($productRow) {
$productId = $productRow['id'];
@@ -272,7 +274,7 @@ function addApiloInvoiceProduct($invoiceId, $item)
$db->query($query);
if ($db->last_error) {
error_log("Błąd dodawania pozycji faktury apilo: " . $db->last_error);
return false;
}
}
function loadApiloPlatformsList($token) {
@@ -290,7 +292,7 @@ function loadApiloPlatformsList($token) {
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($httpCode !== 200) {
return (object)['error' => 'HTTP Error: ' . $httpCode];
return false;
}
return json_decode($response);
}
@@ -309,7 +311,7 @@ function loadApiloOrderPlatformId($token, $orderId) {
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($httpCode !== 200) {
return (object)['error' => 'HTTP Error: ' . $httpCode];
return false;
}
return json_decode($response)->platformAccountId;
}