<?php
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'];
}