$invoice) {
$add = true;
foreach ($invoice['items'] as $item) {
if (!checkProduct($item[10])) {
brecho("Brak produktu: " . $item[10]);
$add = false;
}
}
if ($add && !checkInvoice($invoice['id'])) {
addInvoice($invoice);
}
}
brecho("Koniec. Smutne.");
function addInvoice($i) {
brecho($i['document_no']);
$db = $GLOBALS['db'];
$db->query("SET NAMES utf8mb4");
$query = sprintf("INSERT INTO ecommerce_invoices VALUES ('%d', '%s', '%s', '%s', '%s', 'amazon', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%f', '%s');",
$i['id'], $i['document_no'], 'normal', $i['register_date']->format("Y-m-d"), $i['sell_date']->format("Y-m-d"),
$i['order_no'], 'AmazonCustomer', $i['parent_nip'], $i['parent_address_city'], $i['parent_address_postalcode'], '',
'', $i['parent_address_country'], $i['currency'], $i['total_netto'], $i['total_brutto'], $i['total_vat'], $i['url']);
$db->query($query);
brecho($query);
foreach ($i['items'] as $p) {
addProduct($i['id'], $p);
}
}
function addProduct($invoiceId, $p) {
$db = $GLOBALS['db'];
$id = $db->fetchByAssoc($db->query(sprintf("SELECT id FROM ecmproducts WHERE code ='%s' OR amazon_code = '%s'", $p[10], $p[10])))['id'];
$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', '%d', '%f', '%f', '%f', '%f', '%s')",
$uuid, $id, $invoiceId, $p[11], $p[25], $p[23], $p[24], $p[13]*100, $p[10]);
$db->query($query);
}
function checkProduct($code)
{
$db = $GLOBALS['db'];
$res = $db->query(sprintf("SELECT id FROM ecmproducts WHERE code='%s' OR amazon_code='%s' AND deleted=0", $code, $code));
return $res->num_rows > 0;
}
function checkInvoice($id) {
$db = $GLOBALS['db'];
$res = $db->query(sprintf("SELECT id FROM ecommerce_invoices WHERE id = '%s'", $id));
return $res->num_rows > 0;
}
function brecho($msg)
{
echo "
";
var_dump($msg);
echo "
";
}