apilo - refresh token fix
This commit is contained in:
@@ -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'];
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user