WIP: schedulers..
This commit is contained in:
@@ -1,36 +1,31 @@
|
||||
<?php
|
||||
// enable error reporting
|
||||
error_reporting(LC_ALL);
|
||||
ini_set('display_errors', 1);
|
||||
//error_reporting(LC_ALL);
|
||||
//ini_set('display_errors', 1);
|
||||
// ?XDEBUG_SESSION_START=PHPSTORM
|
||||
importInvoices();
|
||||
function importInvoices()
|
||||
//importInvoices();
|
||||
function apilo_importInvoices()
|
||||
{
|
||||
$apilo_config = loadApiloConfiguration();
|
||||
$apilo_config = apilo_loadConfiguration();
|
||||
$db = $GLOBALS['db'];
|
||||
|
||||
$GLOBALS['log']->info('----- Importing invoices from Apilo, config', $apilo_config);
|
||||
|
||||
$dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'");
|
||||
$offset = intval($db->fetchByAssoc($dbRes)['last_id']);
|
||||
|
||||
$GLOBALS['log']->info('----- Importing invoices from Apilo, offset', $offset);
|
||||
|
||||
$invoices = loadApiloInvoices($apilo_config['token'], $offset);
|
||||
$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
|
||||
if (isset($invoices->error)) {
|
||||
if (refreshApiloToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
|
||||
$apilo_config = loadApiloConfiguration();
|
||||
$invoices = loadApiloInvoices($apilo_config['token'], $offset);
|
||||
if (apilo_refreshToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
|
||||
//$apilo_config = apilo_loadConfiguration();
|
||||
//$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
brecho(count($invoices->documents));
|
||||
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, documents count', count($invoices->documents));
|
||||
|
||||
$GLOBALS['log']->info('----- Importing invoices from Apilo, documents count', count($invoices->documents));
|
||||
|
||||
$platforms = loadApiloPlatformsList($apilo_config['token']);
|
||||
$platforms = apilo_loadPlatformsList($apilo_config['token']);
|
||||
|
||||
if (!$platforms) {
|
||||
return false;
|
||||
@@ -38,15 +33,15 @@ function importInvoices()
|
||||
|
||||
if (isset($invoices->documents) && count($invoices->documents) > 0) {
|
||||
foreach ($invoices->documents as $invoice) {
|
||||
addapiloInvoice($invoice, $platforms, $apilo_config['token']);
|
||||
apilo_addInvoice($invoice, $platforms, $apilo_config['token']);
|
||||
}
|
||||
}
|
||||
$GLOBALS['log']->info('----- Importing invoices from Apilo, all processed, return TRUE');
|
||||
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, all processed, return TRUE');
|
||||
return true;
|
||||
}
|
||||
function loadApiloConfiguration()
|
||||
function apilo_loadConfiguration()
|
||||
{
|
||||
global $db;
|
||||
$db = $GLOBALS['db'];
|
||||
$dbRes = $db->query("SELECT * FROM config WHERE category='apilo'");
|
||||
$config = [];
|
||||
while ($row = $db->fetchByAssoc($dbRes)) {
|
||||
@@ -54,7 +49,7 @@ function loadApiloConfiguration()
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
function loadApiloInvoices($token, $offset)
|
||||
function apilo_loadInvoices($token, $offset)
|
||||
{
|
||||
$url = "https://twinpol.apilo.com/rest/api/finance/documents/";
|
||||
$params = [
|
||||
@@ -81,7 +76,7 @@ function loadApiloInvoices($token, $offset)
|
||||
}
|
||||
return json_decode($response);
|
||||
}
|
||||
function refreshApiloToken($refreshToken, $clientId, $clientSecret)
|
||||
function apilo_refreshToken($refreshToken, $clientId, $clientSecret)
|
||||
{
|
||||
$url = "https://twinpol.apilo.com/rest/auth/token/";
|
||||
|
||||
@@ -126,11 +121,11 @@ function refreshApiloToken($refreshToken, $clientId, $clientSecret)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function addApiloInvoice($invoice, $platforms, $token)
|
||||
function apilo_addInvoice($invoice, $platforms, $token)
|
||||
{
|
||||
$db = $GLOBALS['db'];
|
||||
|
||||
$platformId = loadApiloOrderPlatformId($token, $invoice->orderId);
|
||||
$platformId = apilo_loadOrderPlatformId($token, $invoice->orderId);
|
||||
if (!$platformId) {
|
||||
return false;
|
||||
}
|
||||
@@ -231,12 +226,12 @@ function addApiloInvoice($invoice, $platforms, $token)
|
||||
if (isset($invoice->documentItems) && is_array($invoice->documentItems)) {
|
||||
$db->query(sprintf("DELETE FROM ecommerce_invoices_products WHERE invoice_id='%s'", $db->quote($invoice->id)));
|
||||
foreach ($invoice->documentItems as $item) {
|
||||
addApiloInvoiceProduct($invoice->id, $item);
|
||||
apilo_addInvoiceProduct($invoice->id, $item);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function addApiloInvoiceProduct($invoiceId, $item)
|
||||
function apilo_addInvoiceProduct($invoiceId, $item)
|
||||
{
|
||||
$db = $GLOBALS['db'];
|
||||
|
||||
@@ -290,7 +285,7 @@ function addApiloInvoiceProduct($invoiceId, $item)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function loadApiloPlatformsList($token) {
|
||||
function apilo_loadPlatformsList($token) {
|
||||
$url = "https://twinpol.apilo.com/rest/api/orders/platform/map/";
|
||||
$headers = [
|
||||
'Authorization: Bearer ' . $token,
|
||||
@@ -309,7 +304,7 @@ function loadApiloPlatformsList($token) {
|
||||
}
|
||||
return json_decode($response);
|
||||
}
|
||||
function loadApiloOrderPlatformId($token, $orderId) {
|
||||
function apilo_loadOrderPlatformId($token, $orderId) {
|
||||
$url = "https://twinpol.apilo.com/rest/api/orders/".$orderId."/";
|
||||
$headers = [
|
||||
'Authorization: Bearer ' . $token,
|
||||
|
||||
Reference in New Issue
Block a user