import apilo invoices
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
importApiloInvoices();
|
|
||||||
function importApiloInvoices()
|
function importApiloInvoices()
|
||||||
{
|
{
|
||||||
$apilo_config = loadApiloConfiguration();
|
$apilo_config = loadApiloConfiguration();
|
||||||
@@ -21,12 +20,13 @@ function importApiloInvoices()
|
|||||||
|
|
||||||
brecho(count($invoices->documents));
|
brecho(count($invoices->documents));
|
||||||
|
|
||||||
|
$platforms = loadApiloPlatformsList($apilo_config['token']);
|
||||||
|
|
||||||
if (isset($invoices->documents) && count($invoices->documents) > 0) {
|
if (isset($invoices->documents) && count($invoices->documents) > 0) {
|
||||||
foreach ($invoices->documents as $invoice) {
|
foreach ($invoices->documents as $invoice) {
|
||||||
addapiloInvoice($invoice);
|
addapiloInvoice($invoice, $platforms, $apilo_config['token']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,13 +40,12 @@ function loadApiloConfiguration()
|
|||||||
}
|
}
|
||||||
return $config;
|
return $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadApiloInvoices($token, $offset)
|
function loadApiloInvoices($token, $offset)
|
||||||
{
|
{
|
||||||
$url = "https://twinpol.apilo.com/rest/api/finance/documents/";
|
$url = "https://twinpol.apilo.com/rest/api/finance/documents/";
|
||||||
$params = [
|
$params = [
|
||||||
'type' => 1,
|
'type' => 1,
|
||||||
'limit' => 100,
|
'limit' => 50,
|
||||||
'offset' => $offset
|
'offset' => $offset
|
||||||
];
|
];
|
||||||
$url .= '?' . http_build_query($params);
|
$url .= '?' . http_build_query($params);
|
||||||
@@ -68,7 +67,6 @@ function loadApiloInvoices($token, $offset)
|
|||||||
}
|
}
|
||||||
return json_decode($response);
|
return json_decode($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
function refreshApiloToken($refreshToken, $clientId, $clientSecret)
|
function refreshApiloToken($refreshToken, $clientId, $clientSecret)
|
||||||
{
|
{
|
||||||
$url = "https://api.apilo.com/oauth/token";
|
$url = "https://api.apilo.com/oauth/token";
|
||||||
@@ -116,11 +114,21 @@ function refreshApiloToken($refreshToken, $clientId, $clientSecret)
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
function addApiloInvoice($invoice, $platforms, $token)
|
||||||
function addApiloInvoice($invoice)
|
|
||||||
{
|
{
|
||||||
$db = $GLOBALS['db'];
|
$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';
|
$invoiceType = 'normal';
|
||||||
if (isset($invoice->type)) {
|
if (isset($invoice->type)) {
|
||||||
switch ($invoice->type) {
|
switch ($invoice->type) {
|
||||||
@@ -134,6 +142,7 @@ function addApiloInvoice($invoice)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$customerName = '';
|
$customerName = '';
|
||||||
$customerNip = '';
|
$customerNip = '';
|
||||||
$customerCity = '';
|
$customerCity = '';
|
||||||
@@ -167,7 +176,6 @@ function addApiloInvoice($invoice)
|
|||||||
|
|
||||||
$currency = isset($invoice->originalCurrency) ? $invoice->originalCurrency : 'PLN';
|
$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");
|
$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;
|
$saleDate = isset($invoice->soldAt) ? date("Y-m-d", strtotime(substr($invoice->soldAt,0,10))) : $issueDate;
|
||||||
|
|
||||||
@@ -182,7 +190,7 @@ function addApiloInvoice($invoice)
|
|||||||
$invoiceType,
|
$invoiceType,
|
||||||
$issueDate,
|
$issueDate,
|
||||||
$saleDate,
|
$saleDate,
|
||||||
'apilo',
|
$orderSource,
|
||||||
isset($invoice->orderId) ? $invoice->orderId : '',
|
isset($invoice->orderId) ? $invoice->orderId : '',
|
||||||
$db->quote($customerName),
|
$db->quote($customerName),
|
||||||
$customerNip,
|
$customerNip,
|
||||||
@@ -267,7 +275,44 @@ function addApiloInvoiceProduct($invoiceId, $item)
|
|||||||
error_log("Błąd dodawania pozycji faktury apilo: " . $db->last_error);
|
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)
|
function brecho($msg)
|
||||||
{
|
{
|
||||||
echo '<br><pre>';
|
echo '<br><pre>';
|
||||||
|
|||||||
@@ -68,6 +68,16 @@ $job_strings = array(
|
|||||||
2 => 'importBaselinkerCorrectingInvoices'
|
2 => 'importBaselinkerCorrectingInvoices'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function importApiloInvoices() {
|
||||||
|
try {
|
||||||
|
$GLOBALS['db']->query("USE preDb_0dcc87940d3655fa574b253df04ca1c3;");
|
||||||
|
require_once('modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php');
|
||||||
|
importApiloInvoices();
|
||||||
|
return true;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
function importBaselinkerInvoices()
|
function importBaselinkerInvoices()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user