import apilo invoices
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
importApiloInvoices();
|
||||
function importApiloInvoices()
|
||||
{
|
||||
$apilo_config = loadApiloConfiguration();
|
||||
@@ -21,12 +20,13 @@ function importApiloInvoices()
|
||||
|
||||
brecho(count($invoices->documents));
|
||||
|
||||
$platforms = loadApiloPlatformsList($apilo_config['token']);
|
||||
|
||||
if (isset($invoices->documents) && count($invoices->documents) > 0) {
|
||||
foreach ($invoices->documents as $invoice) {
|
||||
addapiloInvoice($invoice);
|
||||
addapiloInvoice($invoice, $platforms, $apilo_config['token']);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -40,13 +40,12 @@ function loadApiloConfiguration()
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
function loadApiloInvoices($token, $offset)
|
||||
{
|
||||
$url = "https://twinpol.apilo.com/rest/api/finance/documents/";
|
||||
$params = [
|
||||
'type' => 1,
|
||||
'limit' => 100,
|
||||
'limit' => 50,
|
||||
'offset' => $offset
|
||||
];
|
||||
$url .= '?' . http_build_query($params);
|
||||
@@ -68,7 +67,6 @@ function loadApiloInvoices($token, $offset)
|
||||
}
|
||||
return json_decode($response);
|
||||
}
|
||||
|
||||
function refreshApiloToken($refreshToken, $clientId, $clientSecret)
|
||||
{
|
||||
$url = "https://api.apilo.com/oauth/token";
|
||||
@@ -116,11 +114,21 @@ function refreshApiloToken($refreshToken, $clientId, $clientSecret)
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function addApiloInvoice($invoice)
|
||||
function addApiloInvoice($invoice, $platforms, $token)
|
||||
{
|
||||
$db = $GLOBALS['db'];
|
||||
|
||||
$platformId = loadApiloOrderPlatformId($token, $invoice->orderId);
|
||||
$platformDescription = 'ERROR';
|
||||
foreach ($platforms as $platform) {
|
||||
if ($platform->id == $platformId) {
|
||||
$platformDescription = $platform->description;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$orderSource = 'Apilo | '.$platformDescription;
|
||||
|
||||
$invoiceType = 'normal';
|
||||
if (isset($invoice->type)) {
|
||||
switch ($invoice->type) {
|
||||
@@ -134,6 +142,7 @@ function addApiloInvoice($invoice)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$customerName = '';
|
||||
$customerNip = '';
|
||||
$customerCity = '';
|
||||
@@ -167,7 +176,6 @@ function addApiloInvoice($invoice)
|
||||
|
||||
$currency = isset($invoice->originalCurrency) ? $invoice->originalCurrency : 'PLN';
|
||||
|
||||
// Daty
|
||||
$issueDate = isset($invoice->invoicedAt) ? date("Y-m-d", strtotime(substr($invoice->invoicedAt, 0, 10))) : date("Y-m-d");
|
||||
$saleDate = isset($invoice->soldAt) ? date("Y-m-d", strtotime(substr($invoice->soldAt,0,10))) : $issueDate;
|
||||
|
||||
@@ -182,7 +190,7 @@ function addApiloInvoice($invoice)
|
||||
$invoiceType,
|
||||
$issueDate,
|
||||
$saleDate,
|
||||
'apilo',
|
||||
$orderSource,
|
||||
isset($invoice->orderId) ? $invoice->orderId : '',
|
||||
$db->quote($customerName),
|
||||
$customerNip,
|
||||
@@ -267,7 +275,44 @@ function addApiloInvoiceProduct($invoiceId, $item)
|
||||
error_log("Błąd dodawania pozycji faktury apilo: " . $db->last_error);
|
||||
}
|
||||
}
|
||||
|
||||
function loadApiloPlatformsList($token) {
|
||||
$url = "https://twinpol.apilo.com/rest/api/orders/platform/map/";
|
||||
$headers = [
|
||||
'Authorization: Bearer ' . $token,
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json'
|
||||
];
|
||||
$curl = curl_init($url);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
|
||||
$response = curl_exec($curl);
|
||||
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
curl_close($curl);
|
||||
if ($httpCode !== 200) {
|
||||
return (object)['error' => 'HTTP Error: ' . $httpCode];
|
||||
}
|
||||
return json_decode($response);
|
||||
}
|
||||
function loadApiloOrderPlatformId($token, $orderId) {
|
||||
$url = "https://twinpol.apilo.com/rest/api/orders/".$orderId."/";
|
||||
$headers = [
|
||||
'Authorization: Bearer ' . $token,
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json'
|
||||
];
|
||||
$curl = curl_init($url);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
|
||||
$response = curl_exec($curl);
|
||||
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
curl_close($curl);
|
||||
if ($httpCode !== 200) {
|
||||
return (object)['error' => 'HTTP Error: ' . $httpCode];
|
||||
}
|
||||
return json_decode($response)->platformAccountId;
|
||||
}
|
||||
function brecho($msg)
|
||||
{
|
||||
echo '<br><pre>';
|
||||
|
||||
Reference in New Issue
Block a user