Add php files

This commit is contained in:
2025-05-12 15:44:39 +00:00
parent c951760058
commit 82d5804ac4
9534 changed files with 2638137 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set('display_errors', 1);
/*
DELETE FROM ecommerce_invoices_products WHERE invoice_id IN (
SELECT id FROM ecommerce_invoices WHERE origin='amazon b2b' AND register_date LIKE '2025-04-%');
DELETE FROM ecommerce_invoices WHERE origin='amazon b2b' AND register_date LIKE '2025-04-%';
*/
$filename = getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/amazonWDT/files/04-2025.csv';
$handle = fopen($filename, 'r');
if ($handle === false) {
die('Cannot open file: ' . $filename);
}
$b2b = [];
while (($data = fgetcsv($handle, 0, "\t")) !== false) {
if ($data[1] == 'WDT - B2B' && $data[8] == 'PL' && $data[9] != 'PL') {
$b2b[] = $data;
if ($data[20] != 'PLN') {
echo '~PLN';
die();
}
}
}
fclose($handle);
$groupedB2B = [];
foreach ($b2b as $el) {
if ($el[6] == 'Sprzedaz - Transport') {
$el[10] = '00195';
$el[11] = '1';
}
$groupedB2B[$el[21]][] = $el;
}
foreach ($groupedB2B as $invoiceNo => $invoice) {
if (checkInvoice($invoiceNo)) {
echo 'Invoice already exists' . PHP_EOL;
continue;
}
$totalNetto = 0;
$totalVat = 0;
$totalBrutto = 0;
foreach ($invoice as $item) {
$totalNetto += str_replace(',', '.', $item[17]);
$totalVat += str_replace(',', '.', $item[18]);
$totalBrutto += str_replace(',', '.', $item[19]);
}
$db = $GLOBALS['db'];
$db->query("SET NAMES utf8mb4");
$i = $invoice[0];
$type = '';
foreach ($invoice as $item) {
if ($item[6] == 'Sprzedaz - Produkt') {
$type = 'normal';
break;
} else if ($item[6] == 'Zwrot - Produkt') {
$type = 'correcting';
break;
}
}
$invoiceId = getId();
$query = sprintf("INSERT INTO ecommerce_invoices VALUES ('%s', '%s', '%s', '%s', '%s', 'amazon b2b', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%f', null, null, null, null, null);",
$invoiceId, $i[21], $type, formatDate($i[5]), formatDate($i[5]),
null, 'AmazonCustomer', $i[22], null, null, '',
'', null, 'PLN', $totalNetto, $totalBrutto, $totalVat);
$res = $db->query($query);
if (!$res) {
echo "Query error: ".$query.PHP_EOL;
}
foreach ($invoice as $p) {
// if last character is dot, remove it
if (substr($p[10], -1) == '.') {
$p[10] = substr($p[10], 0, -1);
}
if (substr($p[10], -1) == '-') {
$p[10] = substr($p[10], 0, -1);
}
if (checkProduct($p[10])) {
echo 'Cannot find product: '. $p[10] . PHP_EOL;
continue;
}
$productId = $db->fetchByAssoc($db->query(sprintf("SELECT id FROM ecmproducts WHERE code='%s' OR amazon_code='%s' AND deleted=0", $p[10], $p[10])))['id'];
$quantity = str_replace(',', '.', $p[11]);
$vatValue = str_replace(',', '.', $p[12]) * 100;
$netto = str_replace(',', '.', $p[17]) / $quantity;
$vat = str_replace(',', '.', $p[18]) / $quantity;
$brutto = str_replace(',', '.', $p[19]) / $quantity;
$query = sprintf("INSERT INTO ecommerce_invoices_products VALUES('%s', '%s', '%s', null, '%f', '%f', '%f', '%f', '%f', '%s')",
getId(), $productId, $invoiceId, $quantity, $netto, $brutto, $vat, $vatValue, $p[10]);
$res = $db->query($query);
if (!$res) {
echo "Query error: ".$query.PHP_EOL;
}
}
}
echo 'Koniec. Smutne.';
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 ? false : true;
}
function checkInvoice($number) {
$db = $GLOBALS['db'];
$res = $db->query(sprintf("SELECT id FROM ecommerce_invoices WHERE document_no = '%s'", $number));
return $res->num_rows > 0;
}
function formatDate($date) {
$dateTime = DateTime::createFromFormat('d-m-Y', $date);
return $dateTime ? $dateTime->format('Y-m-d') : false;
}
function getId() {
$db = $GLOBALS['db'];
$res = $db->fetchByAssoc($db->query("SELECT UUID() as id;"));
return $res['id'];
}

View File

@@ -0,0 +1,92 @@
<?php
$filename = getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/amazonWDT/files/amazon-wz-04-2025.csv';
$handle = fopen($filename, 'r');
if ($handle === false) {
die('Cannot open file: ' . $filename);
}
$products = [];
$db = $GLOBALS['db'];
global $app_list_strings;
while (($data = fgetcsv($handle, 0, ";")) !== false) {
if ($data[2] != 'SKU' && $data[2] != '' && $data[3] != '') {
$p = array();
$p['product_code'] = $data[2];
if (substr($p['product_code'], -1) == '.' || substr($p['product_code'], -1) == '-') {
$p['product_code'] = substr($p['product_code'], 0, -1);
}
$p['quantity'] = floatval(str_replace(' ', '', $data[3]));
$p['total'] = floatval(str_replace(' ', '', str_replace(',', '.', $data[5])));
$p['price_netto'] = round($p['total'] / $p['quantity'], 2);
$fromDb = $db->fetchByAssoc($db->query("SELECT * FROM ecmproducts WHERE deleted=0 AND (code = '" . $p['product_code'] . "' OR amazon_code = '" . $p['product_code'] . "')"));
if (empty($fromDb)) {
echo 'Nie znaleziono produktu: ' . $p['product_code'] . PHP_EOL;
die('KONIEC');
}
$p['product_id'] = $fromDb['id'];
$p['name'] = $fromDb['name'];
$p['unit_id'] = $fromDb['unit_id'];
$p['unit_name'] = $app_list_strings['ecmproducts_unit_dom'][$p['unit_id']];
$p['ecmvat_name'] = '0%';
$p['ecmvat_value'] = 0;
$p['ecmvat_id'] = '9b783d21-5548-6653-e1d6-49610eb3f9dd';
$products[] = $p;
}
}
$mergedProducts = [];
foreach ($products as $product) {
if (!isset($mergedProducts[$product['product_id']])) {
$mergedProducts[$product['product_id']] = $product;
} else {
$mergedProducts[$product['product_id']]['quantity'] += $product['quantity'];
$mergedProducts[$product['product_id']]['total'] += $product['total'];
$mergedProducts[$product['product_id']]['price'] = round($mergedProducts[$product['product_id']]['total'] / $mergedProducts[$product['product_id']]['quantity'], 2);
}
}
$products = array_values($mergedProducts);
fclose($handle);
$sessionId = create_guid();
$_SESSION[$sessionId] = $products;
echo '<input title="Wystaw WZ" class="button primary" type="button" name="Edit" id="edit_button" value="Wystaw WZ"
onclick="window.open(\'index.php?module=EcmStockDocOuts&action=EditView&amazonWZ=' . $sessionId . '\')">';
function getStockStates($products)
{
$db = $GLOBALS['db'];
$query = "SELECT s.product_id, s.quantity FROM ecmstockstates AS s WHERE s.stock_id = 'add8ef6f-3d1f-5ccf-c486-64719142f096' AND s.product_id IN ('" . implode('\',\'', $products) . "')";
$res = $db->query($query);
$stockStates = array();
while ($row = $db->fetchByAssoc($res)) {
$stockStates[$row['product_id']] = isset($row['quantity']) ? intval($row['quantity']) : 0;
}
return $stockStates;
}
function getProductsForWZ($invoiceIds)
{
$db = $GLOBALS['db'];
$query = "SELECT ip.ecmproduct_id, SUM(ip.quantity) AS quantity, ip.price_netto
FROM ecommerce_invoices_products AS ip
WHERE invoice_id IN ('" . implode('\',\'', $invoiceIds) . "')
AND ip.ecmproduct_id NOT IN ('', '165f364e-9301-25ac-5906-58e38f1de4ca')
GROUP BY ip.ecmproduct_id, ip.price_netto;";
$res = $db->query($query);
$products = array();
while ($row = $db->fetchByAssoc($res)) {
$products[] = array(
'ecmproduct_id' => $row['ecmproduct_id'],
'quantity' => $row['quantity'],
'price_netto' => $row['price_netto'],
);
}
return $products;
}

View File

@@ -0,0 +1,123 @@
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set('display_errors', 1);
/*
DELETE FROM ecommerce_invoices_products WHERE invoice_id IN (
SELECT id FROM ecommerce_invoices WHERE origin='amazon vat local' AND register_date LIKE '2025-02-%');
DELETE FROM ecommerce_invoices WHERE origin='amazon vat local' AND register_date LIKE '2025-02-%';
*/
$filename = getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/amazonWDT/files/04-2025.csv';
$handle = fopen($filename, 'r');
if ($handle === false) {
die('Cannot open file: ' . $filename);
}
$localVat = [];
while (($data = fgetcsv($handle, 0, "\t")) !== false) {
if ($data[1] == 'VAT - Lokalny' && $data[8] == 'PL' && $data[9] == 'PL') {
$localVat[] = $data;
if ($data[20] != 'PLN') {
echo '~PLN';
}
}
}
fclose($handle);
$groupedLocalVat = [];
foreach ($localVat as $vat) {
if ($vat[6] == 'Sprzedaz - Transport') {
$vat[10] = '00195';
$vat[11] = '1';
}
$groupedLocalVat[$vat[21]][] = $vat;
}
foreach ($groupedLocalVat as $invoiceNo => $invoice) {
if (checkInvoice($invoiceNo)) {
echo 'Invoice already exists' . PHP_EOL;
continue;
}
$totalNetto = 0;
$totalVat = 0;
$totalBrutto = 0;
foreach ($invoice as $item) {
$totalNetto += str_replace(',', '.', $item[17]);
$totalVat += str_replace(',', '.', $item[18]);
$totalBrutto += str_replace(',', '.', $item[19]);
}
$db = $GLOBALS['db'];
$db->query("SET NAMES utf8mb4");
$i = $invoice[0];
$type = '';
foreach ($invoice as $item) {
if ($item[6] == 'Sprzedaz - Produkt') {
$type = 'normal';
break;
} else if ($item[6] == 'Zwrot - Produkt') {
$type = 'correcting';
break;
}
}
$invoiceId = getId();
$query = sprintf("INSERT INTO ecommerce_invoices VALUES ('%s', '%s', '%s', '%s', '%s', 'amazon vat local', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%f', null, null, null, null, null);",
$invoiceId, $i[21], $type, formatDate($i[5]), formatDate($i[5]),
null, 'AmazonCustomer', $i[22], null, null, '',
'', null, 'PLN', $totalNetto, $totalBrutto, $totalVat);
$res = $db->query($query);
if (!$res) {
echo "Query error: ".$query.PHP_EOL;
}
foreach ($invoice as $p) {
// if last character is dot, remove it
if (substr($p[10], -1) == '.') {
$p[10] = substr($p[10], 0, -1);
}
if (checkProduct($p[10])) {
echo 'Cannot find product: '. $p[10] . PHP_EOL;
continue;
}
$productId = $db->fetchByAssoc($db->query(sprintf("SELECT id FROM ecmproducts WHERE code='%s' OR amazon_code='%s' AND deleted=0", $p[10], $p[10])))['id'];
$quantity = str_replace(',', '.', $p[11]);
$vatValue = str_replace(',', '.', $p[12]) * 100;
$netto = str_replace(',', '.', $p[17]) / $quantity;
$vat = str_replace(',', '.', $p[18]) / $quantity;
$brutto = str_replace(',', '.', $p[19]) / $quantity;
$query = sprintf("INSERT INTO ecommerce_invoices_products VALUES('%s', '%s', '%s', null, '%f', '%f', '%f', '%f', '%f', '%s')",
getId(), $productId, $invoiceId, $quantity, $netto, $brutto, $vat, $vatValue, $p[10]);
$res = $db->query($query);
if (!$res) {
echo "Query error: ".$query.PHP_EOL;
}
}
}
echo 'Koniec. Smutne.';
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 ? false : true;
}
function checkInvoice($number) {
$db = $GLOBALS['db'];
$res = $db->query(sprintf("SELECT id FROM ecommerce_invoices WHERE document_no = '%s'", $number));
return $res->num_rows > 0;
}
function formatDate($date) {
$dateTime = DateTime::createFromFormat('d-m-Y', $date);
return $dateTime ? $dateTime->format('Y-m-d') : false;
}
function getId() {
$db = $GLOBALS['db'];
$res = $db->fetchByAssoc($db->query("SELECT UUID() as id;"));
return $res['id'];
}

View File

@@ -0,0 +1,53 @@
<?php
// read all csv files in directory
$dir = getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/amazonWDT/files/';
$files = scandir($dir);
$csvFiles = [];
foreach ($files as $file) {
if (strpos($file, '.csv') !== false && strpos($file, '2025') == false) {
$csvFiles[] = $file;
}
}
// read files from csvFiles[] one by one
$report = [];
$sum = 0;
foreach ($csvFiles as $file) {
$filename = $dir . $file;
$handle = fopen($filename, 'r');
if ($handle === false) {
die('Cannot open file: ' . $filename);
}
// && ($data[10] == 'FR00148_1000_amz_de' || $data[10] == 'FR00148')
while (($data = fgetcsv($handle, 0, "\t")) !== false) {
if ($data[1] == 'Przemieszczenie - WDT' && $data[8] == 'PL' && $data[9] == 'CZ' && ($data[10] == 'FR00148_1000_amz_de' || $data[10] == 'FR00148') ) {
$sum += intval($data[11]);
$report[] = $data;
}
}
fclose($handle);
}
var_dump($sum);
//var_dump($report);
die();
$outputFilename = $dir . '../report.csv';
$outputHandle = fopen($outputFilename, 'w');
if ($outputHandle === false) {
die('Cannot open file for writing: ' . $outputFilename);
}
// Write header row if needed
if (!empty($report)) {
fputcsv($outputHandle, array_keys($report[0]), "\t");
}
// Write data rows
foreach ($report as $row) {
fputcsv($outputHandle, $row, "\t");
}
fclose($outputHandle);
?>

View File

@@ -0,0 +1,140 @@
<?php
$invoices = getInvoices('2024');
$products = array();
foreach ($invoices as $invoice) {
foreach ($invoice['products'] as $product) {
$products[] = $product['ecmproduct_id'];
}
}
$states = getStockStates($products);
$canProceed = [];
$cantProceed = [];
$missingProducts = [];
$msg = [];
foreach ($invoices as $invoice) {
if ($DEBUG) {
$msg[] = "Sprawdzam fakture: " . $invoice['document_no'] . "<br>";
}
$proceed = true; // hope :)
foreach ($invoice['products'] as $product) {
if ($DEBUG) {
$msg[] = "&nbsp;&nbsp;&nbsp;Sprawdzam produkt: " . $product['code'] . " (Ilość: " . $product['quantity'] . ")<br>";
$msg[] = "&nbsp;&nbsp;&nbsp;&nbspStan magazynowy: " . $states[$product['ecmproduct_id']] . "<br>";
}
if ($states[$product['ecmproduct_id']] < $product['quantity']) {
if ($DEBUG) {
$msg[] = "&nbsp;&nbsp;&nbsp;&nbsp<b>Nie ma wystarczającej ilości produktu w magazynie</b><br>";
}
if (!isset($missingProducts[$product['code']])) {
$missingProducts[$product['code']] = array();
$missingProducts[$product['code']]['id'] = $product['ecmproduct_id'];
$missingProducts[$product['code']]['code'] = $product['code'];
$missingProducts[$product['code']]['missing'] = $product['quantity'] - $states[$product['ecmproduct_id']];
} else {
$missingProducts[$product['code']]['missing'] += $product['quantity'] - $states[$product['ecmproduct_id']];
}
$proceed = false;
break;
}
}
if ($proceed) {
$canProceed[] = $invoice;
foreach ($invoice['products'] as $product) {
$states[$product['ecmproduct_id']] -= $product['quantity'];
}
} else {
$cantProceed[] = $invoice;
}
}
// create list of invoice ids from canProceed
$invoiceIds = array();
foreach ($canProceed as $invoice) {
$invoiceIds[] = $invoice['id'];
}
$sessionId = create_guid();
$_SESSION[$sessionId] = $invoiceIds;
if (count($cantProceed) == 0) {
echo '<input title="Wystaw WZ" class="button primary" type="button" name="Edit" id="edit_button" value="Wystaw WZ"
onclick="window.open(\'index.php?module=EcmStockDocOuts&action=EditView&ecommerceWZ=' . $sessionId . '\')">';
} else {
var_dump($cantProceed);
}
die();
function getInvoices($date)
{
$db = $GLOBALS['db'];
$invoices = array();
// fomat date from d.m.y to mysql format
$query = "SELECT ip.ecmproduct_id, ip.code, i.document_no, SUM(ip.quantity) AS quantity, i.register_date, i.id
FROM ecommerce_invoices_products AS ip
INNER JOIN ecommerce_invoices AS i ON ip.invoice_id = i.id AND i.register_date LIKE '$date%' AND i.type='normal'
AND i.origin IN ('amazon vat local')
WHERE ip.ecmproduct_id != '' AND ip.ecmproduct_id !='165f364e-9301-25ac-5906-58e38f1de4ca'
AND i.ecmstockdocout_id IS NULL
GROUP BY i.id, ip.ecmproduct_id
ORDER BY i.register_date;";
$res = $db->query($query);
while ($row = $db->fetchByAssoc($res)) {
if (!isset($invoices[$row['document_no']])) {
$invoices[$row['document_no']] = array();
$invoices[$row['document_no']]['document_no'] = $row['document_no'];
$invoices[$row['document_no']]['register_date'] = $row['register_date'];
$invoices[$row['document_no']]['id'] = $row['id'];
$invoices[$row['document_no']]['products'] = array();
$invoices[$row['document_no']]['products'][] = array(
'ecmproduct_id' => $row['ecmproduct_id'],
'code' => $row['code'],
'quantity' => $row['quantity'],
);
} else {
$invoices[$row['document_no']]['products'][] = array(
'ecmproduct_id' => $row['ecmproduct_id'],
'code' => $row['code'],
'quantity' => $row['quantity'],
);
}
}
$invoices = array_values($invoices);
usort($invoices, function ($a, $b) {
return $a['id'] - $b['id'];
});
return $invoices;
}
function getStockStates($products)
{
$db = $GLOBALS['db'];
$query = "SELECT s.product_id, s.quantity FROM ecmstockstates AS s WHERE s.stock_id = 'add8ef6f-3d1f-5ccf-c486-64719142f096' AND s.product_id IN ('" . implode('\',\'', $products) . "')";
$res = $db->query($query);
$stockStates = array();
while ($row = $db->fetchByAssoc($res)) {
$stockStates[$row['product_id']] = isset($row['quantity']) ? intval($row['quantity']) : 0;
}
return $stockStates;
}
function getProductsForWZ($invoiceIds)
{
$db = $GLOBALS['db'];
$query = "SELECT ip.ecmproduct_id, SUM(ip.quantity) AS quantity, ip.price_netto
FROM ecommerce_invoices_products AS ip
WHERE invoice_id IN ('" . implode('\',\'', $invoiceIds) . "')
AND ip.ecmproduct_id NOT IN ('', '165f364e-9301-25ac-5906-58e38f1de4ca')
GROUP BY ip.ecmproduct_id, ip.price_netto;";
$res = $db->query($query);
$products = array();
while ($row = $db->fetchByAssoc($res)) {
$products[] = array(
'ecmproduct_id' => $row['ecmproduct_id'],
'quantity' => $row['quantity'],
'price_netto' => $row['price_netto'],
);
}
return $products;
}

View File

@@ -0,0 +1,148 @@
<?php
// show all php errors
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set('display_errors', 1);
// read csv file tab separated
$filename = getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/amazonWDT/files/04-2025.csv';
$handle = fopen($filename, 'r');
if ($handle === false) {
die('Cannot open file: ' . $filename);
}
$de = [];
$cz = [];
while (($data = fgetcsv($handle, 0, "\t")) !== false) {
if (count($data) == 1 && $data[0] == null) {
continue;
}
if ($data[1] != 'Przemieszczenie - WDT') {
continue;
}
if ($data[8] == 'PL' && $data[9] == 'DE') {
$de[] = $data;
}
if ($data[8] == 'PL' && $data[9] == 'CZ') {
$cz[] = $data;
}
}
fclose($handle);
//if (count($de) == 0) {
// die('No data for DE');
//}
//createInvoice('DE', $de);
if (count($cz) == 0) {
die('No data for CZ');
}
createInvoice('CZ', $cz, true);
function createInvoice($type, $products, $DO_REAL_SAVE = false)
{
$db = $GLOBALS['db'];
$invoice = new EcmInvoiceOut();
if ($type == 'DE') {
$invoice->parent_id = 'a711623a-0217-41bd-ecf6-664c84553473';
$invoice->parent_name = 'Twinpol DE';
$invoice->parent_nip = '367123377';
}
if ($type == 'CZ') {
$invoice->parent_id = 'bf60a128-0b62-4154-c21f-6797718a6354';
$invoice->parent_name = 'Twinpol CZ';
$invoice->parent_nip = 'CZ687253791';
}
$invoice->parent_address_street = 'Aleja Lipowa 48';
$invoice->parent_address_postalcode = '87-126';
$invoice->parent_address_city = 'Obrowo';
$invoice->type = 'normal';
$date = '30.04.2025';
$invoice->register_date = $date;
$invoice->sell_date = $date;
$invoice->validtill_date = $date;
$invoice->payment_date = $invoice->register_date;
$invoice->payment_date_days = 0;
$invoice->payment_method = '1568e47e-1732-e0ad-0000-67b6377f26df';
$invoice->pdf_type = "K";
$invoice->currency_id = 'PLN';
global $current_user;
$invoice->created_by = $current_user->id;
$invoice->assigned_user_id = $current_user->id;
$invoice->wz_id = '';
$invoice->wz_name = '';
$invoice->position_list = [];
foreach ($products as $p) {
// if last character is dot, remove it
if (substr($p[10], -1) == '.') {
$p[10] = substr($p[10], 0, -1);
}
$fromDb = $db->fetchByAssoc($db->query("SELECT id, name, code, product_category_id, unit_id FROM ecmproducts WHERE code = '$p[10]' OR amazon_code = '$p[10]'"));
if ($fromDb == false) {
var_dump($p);
die('Product not found in DB');
}
$prod = searchProduct($fromDb['id'], $invoice->position_list);
if ($prod) {
$prod['quantity'] += intval($p[11]);
$prod['total_netto'] += floatval(str_replace(",", ".", $p[17]));
$prod['total_brutto'] += floatval(str_replace(",", ".", $p[17]));
$prod['price_start'] = round($prod['total_netto'] / $prod['quantity'], 2);
$prod['price_netto'] = $prod['price_start'];
$prod['price_brutto'] = $prod['price_netto'];
foreach ($invoice->position_list as $key => $value) {
if ($value['product_id'] == $prod['product_id']) {
$invoice->position_list[$key] = $prod;
}
}
} else {
$prod['product_id'] = $fromDb['id'];
$prod['name'] = $fromDb['name'];
$prod['product_code'] = $fromDb['code'];
$prod['quantity'] = intval($p[11]);
$prod['total_netto'] = floatval(str_replace(",", ".", $p[17]));
$prod['total_brutto'] = floatval(str_replace(",", ".", $p[17]));
$prod['total_vat'] = 0;
$prod['price_start'] = round($prod['total_netto'] / $prod['quantity'], 2);
$prod['price_netto'] = $prod['price_start'];
$prod['price_brutto'] = $prod['price_netto'];
$prod['ecmvat_id'] = '9b783d21-5548-6653-e1d6-49610eb3f9dd';
$prod['ecmvat_name'] = '0%';
$prod['ecmvat_value'] = 0;
$prod['ecmproductcategory_id'] = $fromDb['product_category_id'];
$prod['dd_unit_id'] = $fromDb['unit_id'];
global $app_list_strings;
$prod['dd_unit_name'] = $app_list_strings['ecmproducts_unit_dom'][$fromDb['unit_id']];
$invoice->position_list[] = $prod;
}
}
if ($DO_REAL_SAVE) {
$id = $invoice->save();
echo $id;
} else {
var_dump($invoice->position_list);
}
}
function searchProduct($productId, $products)
{
foreach ($products as $p) {
if ($p['product_id'] == $productId) {
return $p;
}
}
return false;
}

View File

@@ -0,0 +1,209 @@
<?php
// show all php errors
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set('display_errors', 1);
// read csv file tab separated
$filename = getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/amazonWDT/files/01-2025.csv';
$handle = fopen($filename, 'r');
if ($handle === false) {
die('Cannot open file: ' . $filename);
}
$de = [];
$cz = [];
while (($data = fgetcsv($handle, 0, "\t")) !== false) {
if (count($data) == 1 && $data[0] == null) {
continue;
}
if ($data[1] != 'Przemieszczenie - WNT') {
continue;
}
if ($data[8] == 'DE' && $data[9] == 'PL') {
$de[] = $data;
}
if ($data[8] == 'CZ' && $data[9] == 'PL') {
$cz[] = $data;
}
}
fclose($handle);
/*
if (count($de) == 0) {
var_dump('No data for DE');
} else {
createFVKOR('DE', $de);
}
if (count($cz) == 0) {
var_dump('No data for CZ');
} else {
createFVKOR('CZ', $cz);
}
*/
//var_dump($cz);
createFVKOR('DE', $de);
die();
function createFVKOR($type, $products, $DO_REAL_SAVE = false)
{
$db = $GLOBALS['db'];
$invoice = new EcmInvoiceOut();
if ($type == 'DE') {
$invoice->parent_id = 'a711623a-0217-41bd-ecf6-664c84553473';
$invoice->parent_name = 'Twinpol DE';
$invoice->parent_nip = '367123377';
}
if ($type == 'CZ') {
$invoice->parent_id = 'bf60a128-0b62-4154-c21f-6797718a6354';
$invoice->parent_name = 'Twinpol CZ';
$invoice->parent_nip = 'CZ687253791';
}
$invoice->parent_address_street = 'Aleja Lipowa 48';
$invoice->parent_address_postalcode = '87-126';
$invoice->parent_address_city = 'Obrowo';
$invoice->type = 'correct';
$date = '31.01.2025';
$invoice->register_date = $date;
$invoice->sell_date = $date;
$invoice->validtill_date = $date;
$invoice->payment_date = $invoice->register_date;
$invoice->payment_date_days = 0;
$invoice->payment_method = '1568e47e-1732-e0ad-0000-67b6377f26df';
$invoice->pdf_type = "K";
$invoice->currency_id = 'PLN';
global $current_user;
$invoice->created_by = $current_user->id;
$invoice->assigned_user_id = $current_user->id;
$invoice->wz_id = '';
$invoice->wz_name = '';
$invoice->position_list = groupProductsAndFindFV($products, $invoice->parent_id, $date);
$invoiceId = $invoice->position_list[0]['old_ecminvoiceout_id'];
foreach ($invoice->position_list as $p) {
if ($p['old_ecminvoiceout_id'] != $invoiceId) {
die('Different ecminvoiceout_id in position_list');
}
}
$invoice->id = $invoice->position_list[0]['old_ecminvoiceout_id'];
var_dump($invoice->position_list);
if ($DO_REAL_SAVE) {
$id = $invoice->save();
echo $id;
}
}
function groupProductsAndFindFV($products, $client, $date)
{
$db = $GLOBALS['db'];
$position_list = [];
foreach ($products as $p) {
// if last character is dot, remove it
if (substr($p[10], -1) == '.') {
$p[10] = substr($p[10], 0, -1);
}
$fromDb = $db->fetchByAssoc($db->query("SELECT id, name, code, product_category_id, unit_id FROM ecmproducts WHERE code = '$p[10]' OR amazon_code = '$p[10]'"));
if ($fromDb == false) {
var_dump($p);
continue;
}
$prod = searchProduct($fromDb['id'], $position_list);
if ($prod) {
$prod['quantity'] += intval($p[11]);
$prod['total_netto'] += floatval(str_replace(",", ".", $p[17]));
foreach ($position_list as $key => $value) {
if ($value['product_id'] == $prod['product_id']) {
$position_list[$key] = $prod;
}
}
} else {
$prod['product_id'] = $fromDb['id'];
$prod['name'] = $fromDb['name'];
$prod['product_code'] = $fromDb['code'];
$prod['quantity'] = intval($p[11]);
$prod['total_netto'] = floatval(str_replace(",", ".", $p[17]));
$prod['total_vat'] = 0;
$prod['ecmvat_id'] = '9b783d21-5548-6653-e1d6-49610eb3f9dd';
$prod['ecmvat_name'] = '0%';
$prod['ecmvat_value'] = 0;
$prod['ecmproductcategory_id'] = $fromDb['product_category_id'];
$prod['dd_unit_id'] = $fromDb['unit_id'];
global $app_list_strings;
$prod['dd_unit_name'] = $app_list_strings['ecmproducts_unit_dom'][$fromDb['unit_id']];
$position_list[] = $prod;
}
}
// find FV
$date = explode(".", $date);
$date = $date[2] . '-' . $date[1] . '-' . $date[0];
$final_list = [];
var_dump($position_list);
foreach ($position_list as $prod) {
$qty = $prod['quantity'];
$prodId = $prod['product_id'];
$query = "
SELECT ii.*, i.id AS invoiceId
FROM ecminvoiceoutitems AS ii
INNER JOIN ecminvoiceouts AS i
ON i.id = ii.ecminvoiceout_id
WHERE ii.quantity >= $qty AND ii.ecmproduct_id = '$prodId'
AND i.type = 'normal' AND i.parent_id = '$client' AND i.register_date < '$date'
ORDER BY i.register_date DESC LIMIT 0,1
";
$fromFV = $db->fetchByAssoc($db->query($query));
if ($fromFV == null) {
die('Nie znaleziono produktu'. $query);
}
$prod['product_itemold_id'] = $fromFV['id'];
$prod['old_total_netto'] = floatval($fromFV['total_netto']);
$prod['old_total_vat'] = floatval($fromFV['total_vat']);
$prod['old_total_brutto'] = floatval($fromFV['total_brutto']);
$prod['old_quantity'] = floatval($fromFV['quantity']);
$prod['total_netto'] = $prod['old_total_netto'] - $prod['total_netto'];
$prod['quantity'] = $prod['old_quantity'] - $prod['quantity'];
$prod['price_netto'] = $prod['total_netto'] / $prod['quantity'];
$prod['price_brutto'] = $prod['price_netto'];
$prod['price_start'] = $prod['price_netto'];
$prod['total_netto'] = $prod['quantity'] * $prod['price_netto'];
$prod['total_brutto'] = $prod['total_netto'];
$prod['old_ecminvoiceout_id'] = $fromFV['invoiceId'];
$final_list[] = $prod;
$fromFV['old_ecminvoiceout_id'] = $fromFV['invoiceId'];
$fromFV['product_corrected'] = 'true';
$final_list[] = $fromFV;
}
return $final_list;
}
function searchProduct($productId, $products)
{
foreach ($products as $p) {
if ($p['product_id'] == $productId) {
return $p;
}
}
return false;
}