236 lines
7.5 KiB
PHP
236 lines
7.5 KiB
PHP
<?php
|
|
// serie faktur baselinker
|
|
// FV Polska (stare) - 15356
|
|
// FV Polska - 7688
|
|
// FV Niemcy - 15357
|
|
// FVKOR Polska - 17314
|
|
// FVKOR domyślna - 7689
|
|
|
|
|
|
/*
|
|
DELETE p
|
|
FROM ecommerce_invoices_products p
|
|
LEFT JOIN ecommerce_invoices i ON p.invoice_id = i.id
|
|
WHERE i.id IS NULL;
|
|
*/
|
|
function importFV($seriesId)
|
|
{
|
|
$baselinker_config = loadConfiguration();
|
|
|
|
$db = $GLOBALS['db'];
|
|
|
|
$IMPORT_START_ID = 1531694;
|
|
|
|
$dbRes = $db->query("SELECT id FROM ecommerce_invoices WHERE origin = 'allegro' AND type = 'normal' AND series_id = '$seriesId' ORDER BY id DESC LIMIT 1");
|
|
$lastImportId = $db->fetchByAssoc($dbRes)['id'];
|
|
if ($lastImportId == null) {
|
|
$lastImportId = $IMPORT_START_ID;
|
|
} else {
|
|
$lastImportId++; //get next
|
|
}
|
|
|
|
$invoicesRes = loadInvoices($baselinker_config['token'], $lastImportId, $seriesId);
|
|
$invoices = $invoicesRes->invoices;
|
|
|
|
usort($invoices, function ($a, $b) {
|
|
return $a->date_add - $b->date_add;
|
|
});
|
|
|
|
if (count($invoices) > 0) {
|
|
foreach ($invoices as $invoice) {
|
|
$invoice->order_source = getInvoiceSource($baselinker_config['token'], $invoice->order_id);
|
|
$invoice->total_price_vat = $invoice->total_price_brutto - $invoice->total_price_netto;
|
|
$invoice->series_id = $seriesId;
|
|
addInvoice($invoice);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function importFVKOR($seriesId)
|
|
{
|
|
$baselinker_config = loadConfiguration();
|
|
$db = $GLOBALS['db'];
|
|
|
|
$IMPORT_CORRECTION_START_ID = 2106464;
|
|
|
|
$dbRes = $db->query("SELECT id FROM ecommerce_invoices WHERE origin = 'Allegro' AND type = 'correcting' AND series_id = '$seriesId' ORDER BY id DESC LIMIT 1");
|
|
$lastImportId = $db->fetchByAssoc($dbRes)['id'];
|
|
if ($lastImportId == null) {
|
|
$lastImportId = $IMPORT_CORRECTION_START_ID;
|
|
} else {
|
|
$lastImportId++; //get next
|
|
}
|
|
|
|
$invoicesRes = loadInvoices($baselinker_config['token'], $lastImportId, $seriesId);
|
|
|
|
$invoices = $invoicesRes->invoices;
|
|
|
|
usort($invoices, function ($a, $b) {
|
|
return $a->date_add - $b->date_add;
|
|
});
|
|
|
|
if (count($invoices) > 0) {
|
|
foreach ($invoices as $invoice) {
|
|
$invoice->order_source = getInvoiceSource($baselinker_config['token'], $invoice->correcting_to_invoice_id);
|
|
$total = getCorrectTotals($invoice);
|
|
$invoice->series_id = $seriesId;
|
|
$invoice->total_price_netto = $total['netto'];
|
|
$invoice->total_price_brutto = $total['brutto'];
|
|
$invoice->total_price_vat = $total['vat'];
|
|
$invoice->order_source = $total['order_source'];
|
|
addInvoice($invoice);
|
|
}
|
|
}
|
|
}
|
|
function addInvoice($i)
|
|
{
|
|
$db = $GLOBALS['db'];
|
|
$name = strlen($i->invoice_nip) > 0 ?
|
|
$i->invoice_company :
|
|
$i->invoice_fullname;
|
|
$db->query("SET NAMES utf8mb4");
|
|
$query = sprintf(
|
|
"INSERT INTO ecommerce_invoices VALUES ('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%f', '%s', '%s', null, null, null, '%s');",
|
|
$i->invoice_id,
|
|
$i->number,
|
|
$i->type,
|
|
date("Y-m-d", $i->date_add),
|
|
date("Y-m-d", $i->date_sell),
|
|
$i->order_source,
|
|
$i->order_id,
|
|
$name,
|
|
$i->invoice_nip,
|
|
$i->invoice_city,
|
|
$i->invoice_postcode,
|
|
$i->invoice_address,
|
|
$i->invoice_country,
|
|
$i->invoice_country_code,
|
|
$i->currency,
|
|
$i->total_price_netto,
|
|
$i->total_price_brutto,
|
|
$i->total_price_vat,
|
|
'',
|
|
$i->correcting_to_invoice_id,
|
|
$i->series_id
|
|
);
|
|
$db->query($query);
|
|
|
|
|
|
if ($db->last_error) {
|
|
return;
|
|
}
|
|
// delete products for this invoice if exists
|
|
$db->query(sprintf("DELETE FROM ecommerce_invoices_products WHERE invoice_id='%s'", $i->invoice_id));
|
|
foreach ($i->items as $p) {
|
|
addProduct($i->invoice_id, $p);
|
|
}
|
|
}
|
|
function addProduct($invoiceId, $p)
|
|
{
|
|
$db = $GLOBALS['db'];
|
|
$id = $p->is_shipment == 1 ? '165f364e-9301-25ac-5906-58e38f1de4ca' : $db->fetchByAssoc($db->query(sprintf("SELECT id FROM ecmproducts WHERE code ='%s'", $p->sku)))['id'];
|
|
// generate random uuid v4
|
|
$uuid = sprintf(
|
|
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0x0fff) | 0x4000,
|
|
mt_rand(0, 0x3fff) | 0x8000,
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff)
|
|
);
|
|
$query = sprintf(
|
|
"INSERT INTO ecommerce_invoices_products VALUES('%s', '%s', '%s', '%s', '%d', '%f', '%f', '%f', '%f', '%s')",
|
|
$uuid,
|
|
$id,
|
|
$invoiceId,
|
|
$p->order_product_id,
|
|
$p->quantity,
|
|
$p->price_netto,
|
|
$p->price_brutto,
|
|
$p->price_brutto - $p->price_netto,
|
|
$p->tax_rate,
|
|
$p->sku
|
|
);
|
|
$db->query($query);
|
|
}
|
|
function loadConfiguration()
|
|
{
|
|
global $db;
|
|
$dbRes = $db->query("SELECT * FROM config WHERE category='baselinker'");
|
|
$config = [];
|
|
while ($row = $db->fetchByAssoc($dbRes)) {
|
|
$config[$row['name']] = $row['value'];
|
|
}
|
|
return $config;
|
|
}
|
|
function loadInvoices($token, $lastImportId, $seriesId)
|
|
{
|
|
$methodParams = '{
|
|
"id_from": ' . $lastImportId . ',
|
|
"get_external_invoices": false,
|
|
"series_id": ' . $seriesId . '
|
|
}';
|
|
$apiParams = [
|
|
"method" => "getInvoices",
|
|
"parameters" => $methodParams
|
|
];
|
|
|
|
$curl = curl_init("https://api.baselinker.com/connector.php");
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-BLToken: " . $token]);
|
|
curl_setopt($curl, CURLOPT_VERBOSE, 0);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
|
|
$response = json_decode(curl_exec($curl));
|
|
return $response;
|
|
}
|
|
function getInvoiceSource($token, $orderId)
|
|
{
|
|
$methodParams = '{
|
|
"order_id": ' . $orderId . '
|
|
}';
|
|
$apiParams = [
|
|
"method" => "getOrders",
|
|
"parameters" => $methodParams
|
|
];
|
|
|
|
$curl = curl_init("https://api.baselinker.com/connector.php");
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-BLToken: " . $token]);
|
|
curl_setopt($curl, CURLOPT_VERBOSE, 0);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
|
|
$res = json_decode(curl_exec($curl));
|
|
return $res->orders[0]->order_source;
|
|
}
|
|
function getCorrectTotals($invoice)
|
|
{
|
|
$db = $GLOBALS['db'];
|
|
$i = $db->fetchByAssoc($db->query(sprintf("SELECT * FROM ecommerce_invoices WHERE id='%s'", $invoice->correcting_to_invoice_id)));
|
|
if (!isset($i)) {
|
|
die(); // w tym przypadku nie wszystkie FV są zaciągnięte. Trzeba przerwać skrypt i odpalić następnym razem
|
|
}
|
|
$c = $db->fetchByAssoc($db->query(sprintf("SELECT SUM(total_netto) as total_netto, SUM(total_brutto) as total_brutto FROM ecommerce_invoices WHERE corrected_invoice_id='%s'", $invoice->correcting_to_invoice_id)));
|
|
$total_netto = $invoice->total_price_netto - (float) $i['total_netto'] - (float) $c['total_netto'];
|
|
$total_brutto = $invoice->total_price_brutto - (float) $i['total_brutto'] - (float) $c['total_brutto'];
|
|
$total_vat = $total_brutto - $total_netto;
|
|
return array(
|
|
'netto' => $total_netto,
|
|
'brutto' => $total_brutto,
|
|
'vat' => $total_vat,
|
|
'order_source' => $i['origin']
|
|
);
|
|
}
|
|
|
|
function brecho($msg)
|
|
{
|
|
echo '<br><pre>';
|
|
var_dump($msg);
|
|
echo PHP_EOL;
|
|
echo '</pre><br>';
|
|
}
|