Compare commits

..

6 Commits

8 changed files with 331 additions and 266 deletions

View File

@@ -30,7 +30,7 @@ function bimai_exportEcommerceDate($since)
AND YEAR(i.register_date) = 2024 AND YEAR(i.register_date) = 2024
AND i.register_date > '$sinceDate' AND i.register_date > '$sinceDate'
ORDER BY i.register_date ORDER BY i.register_date
LIMIT 0,10; LIMIT 0,10000;
"; ";
$db = $GLOBALS['db']; $db = $GLOBALS['db'];

View File

@@ -323,85 +323,97 @@ function createCSVReports()
$jobs = [ $jobs = [
[ [
'sql' => " 'sql' => "
SELECT SELECT
i.document_no, i.document_no,
i.register_date, i.register_date,
i.parent_name, i.parent_name,
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Usługi' WHEN '3' THEN 'Usługi'
WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały'
WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity, ii.quantity,
ii.price_netto, ii.price_netto,
cur.name AS currency_name COALESCE(cur.name, 'PLN') AS currency_name
FROM ecminvoiceouts AS i FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id INNER JOIN ecminvoiceoutitems AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.ecminvoiceout_id
INNER JOIN currencies AS cur ON cur.id = i.currency_id AND ii.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci INNER JOIN ecmproducts AS p
AND cb.bean_name = 'EcmProducts' ON ii.ecmproduct_id = p.id
AND cb.deleted = 0 AND p.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id LEFT JOIN currencies AS cur
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2024 ON cur.id = i.currency_id
AND cur.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb
ON cb.bean_id = p.id
AND cb.bean_name = 'EcmProducts'
AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c
ON c.id = cb.ecmproductcategory_id
AND c.deleted = 0
WHERE i.type = 'normal'
AND i.register_date BETWEEN '2024-01-01' AND '2024-12-31'
AND i.deleted = 0
GROUP BY GROUP BY
i.document_no, i.id,
i.register_date, ii.id
i.parent_name, ORDER BY
p.code, i.register_date DESC;
p.name,
p.group_ks,
ii.quantity,
ii.price_netto
ORDER BY i.register_date DESC;
", ",
'filename' => 'invoices_2024.csv', 'filename' => 'invoices_2024.csv',
], // invoices 2024 ], // invoices 2024
[ [
'sql' => " 'sql' => "
SELECT SELECT
i.document_no, i.document_no,
i.register_date, i.register_date,
i.parent_name, i.parent_name,
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Usługi' WHEN '3' THEN 'Usługi'
WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały'
WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity, ii.quantity,
ii.price_netto, ii.price_netto,
cur.name AS currency_name COALESCE(cur.name, 'PLN') AS currency_name
FROM ecminvoiceouts AS i FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id INNER JOIN ecminvoiceoutitems AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.ecminvoiceout_id
INNER JOIN currencies AS cur ON cur.id = i.currency_id AND ii.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci INNER JOIN ecmproducts AS p
AND cb.bean_name = 'EcmProducts' ON ii.ecmproduct_id = p.id
AND cb.deleted = 0 AND p.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id LEFT JOIN currencies AS cur
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2025 ON cur.id = i.currency_id
AND cur.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb
ON cb.bean_id = p.id
AND cb.bean_name = 'EcmProducts'
AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c
ON c.id = cb.ecmproductcategory_id
AND c.deleted = 0
WHERE i.type = 'normal'
AND i.register_date BETWEEN '2025-01-01' AND '2025-12-31'
AND i.deleted = 0
GROUP BY GROUP BY
i.document_no, i.id,
i.register_date, ii.id
i.parent_name, ORDER BY
p.code, i.register_date DESC;
p.name,
p.group_ks,
ii.quantity,
ii.price_netto
ORDER BY i.register_date DESC;
", ",
'filename' => 'invoices_2025.csv', 'filename' => 'invoices_2025.csv',
], // invoices 2025 ], // invoices 2025
@@ -416,41 +428,38 @@ SELECT
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Usługi' WHEN '3' THEN 'Usługi'
WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały'
WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity_corrected AS quantity_correced, ii.quantity_corrected AS quantity_corrected,
ii.total_netto_corrected AS total_netto_corrected, ii.total_netto_corrected AS total_netto_corrected,
cur.name AS currency_name COALESCE(cur.name, 'PLN') AS currency_name
FROM ecminvoiceouts AS i FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id INNER JOIN ecminvoiceoutitems AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.ecminvoiceout_id AND ii.deleted = 0
INNER JOIN ecminvoiceouts AS oi ON oi.id = i.ecminvoiceout_id INNER JOIN ecmproducts AS p
INNER JOIN currencies AS cur ON cur.id = i.currency_id ON ii.ecmproduct_id = p.id AND p.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci LEFT JOIN ecminvoiceouts AS oi
AND cb.bean_name = 'EcmProducts' ON oi.id = i.ecminvoiceout_id AND oi.deleted = 0
AND cb.deleted = 0 LEFT JOIN currencies AS cur
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id ON cur.id = i.currency_id AND cur.deleted = 0
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2024 LEFT JOIN ecmproductcategories_bean AS cb
ON cb.bean_id = p.id
AND cb.bean_name = 'EcmProducts'
AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c
ON c.id = cb.ecmproductcategory_id AND c.deleted = 0
WHERE i.type = 'correct'
AND i.register_date BETWEEN '2024-01-01' AND '2024-12-31'
AND i.deleted = 0
GROUP BY GROUP BY
i.document_no, i.id,
i.register_date, ii.id,
oi.document_no, oi.id
oi.register_date, ORDER BY i.register_date DESC;
i.parent_name,
p.code,
p.name,
p.group_ks,
ii.quantity,
ii.price_netto,
ii.quantity_corrected,
ii.total_netto_corrected
ORDER BY i.register_date DESC;
", ",
'filename' => 'correct_invoices_2024.csv', 'filename' => 'correct_invoices_2024.csv',
], // correct invoices 2024 ], // correct invoices 2024
@@ -465,40 +474,37 @@ SELECT
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Usługi' WHEN '3' THEN 'Usługi'
WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały'
WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity_corrected AS quantity_correced, ii.quantity_corrected AS quantity_corrected,
ii.total_netto_corrected AS total_netto_corrected, ii.total_netto_corrected AS total_netto_corrected,
cur.name AS currency_name COALESCE(cur.name, 'PLN') AS currency_name
FROM ecminvoiceouts AS i FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id INNER JOIN ecminvoiceoutitems AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.ecminvoiceout_id AND ii.deleted = 0
INNER JOIN ecminvoiceouts AS oi ON oi.id = i.ecminvoiceout_id INNER JOIN ecmproducts AS p
INNER JOIN currencies AS cur ON cur.id = i.currency_id ON ii.ecmproduct_id = p.id AND p.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci LEFT JOIN ecminvoiceouts AS oi
AND cb.bean_name = 'EcmProducts' ON oi.id = i.ecminvoiceout_id AND oi.deleted = 0
AND cb.deleted = 0 LEFT JOIN currencies AS cur
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id ON cur.id = i.currency_id AND cur.deleted = 0
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2025 LEFT JOIN ecmproductcategories_bean AS cb
ON cb.bean_id = p.id
AND cb.bean_name = 'EcmProducts'
AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c
ON c.id = cb.ecmproductcategory_id AND c.deleted = 0
WHERE i.type = 'correct'
AND i.register_date BETWEEN '2025-01-01' AND '2025-12-31'
AND i.deleted = 0
GROUP BY GROUP BY
i.document_no, i.id,
i.register_date, ii.id,
oi.document_no, oi.id
oi.register_date,
i.parent_name,
p.code,
p.name,
p.group_ks,
ii.quantity,
ii.price_netto,
ii.quantity_corrected,
ii.total_netto_corrected
ORDER BY i.register_date DESC; ORDER BY i.register_date DESC;
", ",
'filename' => 'correct_invoices_2025.csv', 'filename' => 'correct_invoices_2025.csv',
@@ -512,33 +518,37 @@ SELECT
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Usługi' WHEN '3' THEN 'Usługi'
WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały'
WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, pc.category,
ii.quantity, ii.quantity,
ii.price_netto ii.price_netto
FROM ecommerce_invoices AS i FROM ecommerce_invoices AS i
INNER JOIN ecommerce_invoices_products AS ii ON i.id = ii.invoice_id INNER JOIN ecommerce_invoices_products AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.invoice_id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci INNER JOIN ecmproducts AS p
AND cb.bean_name = 'EcmProducts' ON ii.ecmproduct_id = p.id
AND cb.deleted = 0 LEFT JOIN (
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id SELECT
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2024 cb.bean_id AS product_id,
GROUP BY GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category
i.document_no, FROM ecmproductcategories_bean AS cb
i.register_date, INNER JOIN ecmproductcategories AS c
i.parent_name, ON c.id = cb.ecmproductcategory_id
p.code, AND c.deleted = 0
p.name, WHERE cb.bean_name = 'EcmProducts'
p.group_ks, AND cb.deleted = 0
ii.quantity, GROUP BY cb.bean_id
ii.price_netto ) AS pc
ON pc.product_id = p.id
WHERE i.type = 'normal'
AND i.register_date >= '2024-01-01'
AND i.register_date < '2025-01-01'
ORDER BY i.register_date DESC; ORDER BY i.register_date DESC;
", ",
'filename' => 'ecommerce_invoices_2024.csv', 'filename' => 'ecommerce_invoices_2024.csv',
@@ -552,33 +562,37 @@ SELECT
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Usługi' WHEN '3' THEN 'Usługi'
WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały'
WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, pc.category,
ii.quantity, ii.quantity,
ii.price_netto ii.price_netto
FROM ecommerce_invoices AS i FROM ecommerce_invoices AS i
INNER JOIN ecommerce_invoices_products AS ii ON i.id = ii.invoice_id INNER JOIN ecommerce_invoices_products AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.invoice_id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci INNER JOIN ecmproducts AS p
AND cb.bean_name = 'EcmProducts' ON ii.ecmproduct_id = p.id
AND cb.deleted = 0 LEFT JOIN (
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id SELECT
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2025 cb.bean_id AS product_id,
GROUP BY GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category
i.document_no, FROM ecmproductcategories_bean AS cb
i.register_date, INNER JOIN ecmproductcategories AS c
i.parent_name, ON c.id = cb.ecmproductcategory_id
p.code, AND c.deleted = 0
p.name, WHERE cb.bean_name = 'EcmProducts'
p.group_ks, AND cb.deleted = 0
ii.quantity, GROUP BY cb.bean_id
ii.price_netto ) AS pc
ON pc.product_id = p.id
WHERE i.type = 'normal'
AND i.register_date >= '2025-01-01'
AND i.register_date < '2026-01-01'
ORDER BY i.register_date DESC; ORDER BY i.register_date DESC;
", ",
'filename' => 'ecommerce_invoices_2025.csv', 'filename' => 'ecommerce_invoices_2025.csv',
@@ -603,32 +617,40 @@ SELECT
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec' WHEN '3' THEN 'Surowiec'
WHEN 4 THEN 'Usługa' WHEN '4' THEN 'Usługa'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, pc.category,
s.name AS stock, s.name AS stock,
ii.quantity ii.quantity
FROM ecmstockdocinsideouts AS i FROM ecmstockdocinsideouts AS i
INNER JOIN ecmstockdocinsideoutitems AS ii ON i.id = ii.ecmstockdocinsideout_id INNER JOIN ecmstockdocinsideoutitems AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.ecmstockdocinsideout_id
INNER JOIN ecmstocks AS s ON i.stock_id = s.id AND ii.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci INNER JOIN ecmproducts AS p
AND cb.bean_name = 'EcmProducts' ON ii.ecmproduct_id = p.id
AND cb.deleted = 0 AND p.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id INNER JOIN ecmstocks AS s
WHERE YEAR(i.register_date) = 2025 ON i.stock_id = s.id
GROUP BY AND s.deleted = 0
i.document_no, LEFT JOIN (
i.register_date, SELECT
p.code, cb.bean_id AS product_id,
p.name, GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category
p.group_ks, FROM ecmproductcategories_bean AS cb
s.name, INNER JOIN ecmproductcategories AS c
ii.quantity ON c.id = cb.ecmproductcategory_id
AND c.deleted = 0
WHERE cb.bean_name = 'EcmProducts'
AND cb.deleted = 0
GROUP BY cb.bean_id
) AS pc
ON pc.product_id = p.id
WHERE i.deleted = 0
AND i.register_date BETWEEN '2025-01-01' AND '2025-12-31'
ORDER BY i.register_date DESC; ORDER BY i.register_date DESC;
", ",
'filename' => 'rw_2025.csv', 'filename' => 'rw_2025.csv',
@@ -641,32 +663,40 @@ SELECT
p.code, p.code,
p.name, p.name,
CASE p.group_ks CASE p.group_ks
WHEN 1 THEN 'Towar handlowy' WHEN '1' THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy' WHEN '2' THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec' WHEN '3' THEN 'Surowiec'
WHEN 4 THEN 'Usługa' WHEN '4' THEN 'Usługa'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS group_ks, END AS group_ks_name,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, pc.category,
s.name AS stock, s.name AS stock,
ii.quantity ii.quantity
FROM ecmstockdocinsideouts AS i FROM ecmstockdocinsideouts AS i
INNER JOIN ecmstockdocinsideoutitems AS ii ON i.id = ii.ecmstockdocinsideout_id INNER JOIN ecmstockdocinsideoutitems AS ii
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id ON i.id = ii.ecmstockdocinsideout_id
INNER JOIN ecmstocks AS s ON i.stock_id = s.id AND ii.deleted = 0
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci INNER JOIN ecmproducts AS p
AND cb.bean_name = 'EcmProducts' ON ii.ecmproduct_id = p.id
AND cb.deleted = 0 AND p.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id INNER JOIN ecmstocks AS s
WHERE YEAR(i.register_date) = 2024 ON i.stock_id = s.id
GROUP BY AND s.deleted = 0
i.document_no, LEFT JOIN (
i.register_date, SELECT
p.code, cb.bean_id AS product_id,
p.name, GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category
p.group_ks, FROM ecmproductcategories_bean AS cb
s.name, INNER JOIN ecmproductcategories AS c
ii.quantity ON c.id = cb.ecmproductcategory_id
AND c.deleted = 0
WHERE cb.bean_name = 'EcmProducts'
AND cb.deleted = 0
GROUP BY cb.bean_id
) AS pc
ON pc.product_id = p.id
WHERE i.deleted = 0
AND i.register_date BETWEEN '2024-01-01' AND '2024-12-31'
ORDER BY i.register_date DESC; ORDER BY i.register_date DESC;
", ",
'filename' => 'rw_2024.csv', 'filename' => 'rw_2024.csv',

View File

@@ -1,4 +1,5 @@
<?php <?php
// die('Przerwa techniczna. Michał Zieliński');
if(!defined('sugarEntry'))define('sugarEntry', true); if(!defined('sugarEntry'))define('sugarEntry', true);
$SHOW_ERRORS = false; // set to true to show errors, false to hide them $SHOW_ERRORS = false; // set to true to show errors, false to hide them

View File

@@ -0,0 +1,80 @@
<?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'];
}

View File

@@ -3,27 +3,27 @@
//error_reporting(LC_ALL); //error_reporting(LC_ALL);
//ini_set('display_errors', 1); //ini_set('display_errors', 1);
// ?XDEBUG_SESSION_START=PHPSTORM // ?XDEBUG_SESSION_START=PHPSTORM
//apilo_importInvoices();
function apilo_importInvoices() function apilo_importInvoices()
{ {
$apilo_config = apilo_loadConfiguration(); $apilo_config = apilo_loadConfiguration();
$db = $GLOBALS['db']; $db = $GLOBALS['db'];
$dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'"); $dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'");
$offset = intval($db->fetchByAssoc($dbRes)['last_id']); $offset = intval($db->fetchByAssoc($dbRes)['last_id']);
$invoices = apilo_loadInvoices($apilo_config['token'], $offset); $invoices = apilo_loadInvoices($apilo_config['token'], $offset);
brecho($apilo_config); if (isset($invoices->error)) {
if (isset($invoices->errors)) {
if (apilo_refreshToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) { if (apilo_refreshToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
//$apilo_config = apilo_loadConfiguration(); $apilo_config = apilo_loadConfiguration();
//$invoices = apilo_loadInvoices($apilo_config['token'], $offset); $invoices = apilo_loadInvoices($apilo_config['token'], $offset);
} else { } else {
return false; return false;
} }
} }
brecho($invoices); brecho(count($invoices->documents));
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, documents count', count($invoices->documents)); $GLOBALS['log']->bimit('----- Importing invoices from Apilo, documents count', count($invoices->documents));
@@ -56,7 +56,7 @@ function apilo_loadInvoices($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' => 50, 'limit' => 100,
'offset' => $offset 'offset' => $offset
]; ];
$url .= '?' . http_build_query($params); $url .= '?' . http_build_query($params);
@@ -105,19 +105,19 @@ function apilo_refreshToken($refreshToken, $clientId, $clientSecret)
$err = curl_error($curl); $err = curl_error($curl);
curl_close($curl); curl_close($curl);
brecho($response);
brecho($httpCode); if ($httpCode !== 201) {
if ($httpCode !== 200) {
return false; return false;
} }
$tokenData = json_decode($response, true); $tokenData = json_decode($response, true);
brecho($tokenData); var_dump($tokenData);
if (isset($tokenData['accessToken'])) { if (isset($tokenData['accessToken'])) {
global $db; global $db;
$db->query("UPDATE config SET value='" . $tokenData['access_token'] . "' WHERE category='apilo' AND name='access_token'"); $db->query("UPDATE config SET value='" . $tokenData['accessToken'] . "' WHERE category='apilo' AND name='token'");
if (isset($tokenData['refresh_token'])) { if (isset($tokenData['refreshToken'])) {
$db->query("UPDATE config SET value='" . $tokenData['refresh_token'] . "' WHERE category='apilo' AND name='refresh_token'"); $db->query("UPDATE config SET value='" . $tokenData['refreshToken'] . "' WHERE category='apilo' AND name='refreshToken'");
} }
return true; return true;
} }

View File

@@ -1,108 +1,60 @@
<?php <?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/***************************************************************************** /*****************************************************************************
* The contents of this file are subject to the RECIPROCAL PUBLIC LICENSE * The contents of this file are subject to the RECIPROCAL PUBLIC LICENSE
* Version 1.1 ("License"); You may not use this file except in compliance * Version 1.1 ("License"); You may not use this file except in compliance
* with the License. You may obtain a copy of the License at * with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/rpl.php. Software distributed under the * http://opensource.org/licenses/rpl.php. Software distributed under the
* License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
* either express or implied. * either express or implied.
* *
* You may: * You may:
* a) Use and distribute this code exactly as you received without payment or * a) Use and distribute this code exactly as you received without payment or
* a royalty or other fee. * a royalty or other fee.
* b) Create extensions for this code, provided that you make the extensions * b) Create extensions for this code, provided that you make the extensions
* publicly available and document your modifications clearly. * publicly available and document your modifications clearly.
* c) Charge for a fee for warranty or support or for accepting liability * c) Charge for a fee for warranty or support or for accepting liability
* obligations for your customers. * obligations for your customers.
* *
* You may NOT: * You may NOT:
* a) Charge for the use of the original code or extensions, including in * a) Charge for the use of the original code or extensions, including in
* electronic distribution models, such as ASP (Application Service * electronic distribution models, such as ASP (Application Service
* Provider). * Provider).
* b) Charge for the original source code or your extensions other than a * b) Charge for the original source code or your extensions other than a
* nominal fee to cover distribution costs where such distribution * nominal fee to cover distribution costs where such distribution
* involves PHYSICAL media. * involves PHYSICAL media.
* c) Modify or delete any pre-existing copyright notices, change notices, * c) Modify or delete any pre-existing copyright notices, change notices,
* or License text in the Licensed Software * or License text in the Licensed Software
* d) Assert any patent claims against the Licensor or Contributors, or * d) Assert any patent claims against the Licensor or Contributors, or
* which would in any way restrict the ability of any third party to use the * which would in any way restrict the ability of any third party to use the
* Licensed Software. * Licensed Software.
* *
* You must: * You must:
* a) Document any modifications you make to this code including the nature of * a) Document any modifications you make to this code including the nature of
* the change, the authors of the change, and the date of the change. * the change, the authors of the change, and the date of the change.
* b) Make the source code for any extensions you deploy available via an * b) Make the source code for any extensions you deploy available via an
* Electronic Distribution Mechanism such as FTP or HTTP download. * Electronic Distribution Mechanism such as FTP or HTTP download.
* c) Notify the licensor of the availability of source code to your extensions * c) Notify the licensor of the availability of source code to your extensions
* and include instructions on how to acquire the source code and updates. * and include instructions on how to acquire the source code and updates.
* d) Grant Licensor a world-wide, non-exclusive, royalty-free license to use, * d) Grant Licensor a world-wide, non-exclusive, royalty-free license to use,
* reproduce, perform, modify, sublicense, and distribute your extensions. * reproduce, perform, modify, sublicense, and distribute your extensions.
* *
* The Original Code is: CommuniCore * The Original Code is: CommuniCore
* Olavo Farias * Olavo Farias
* 2006-04-7 olavo.farias@gmail.com * 2006-04-7 olavo.farias@gmail.com
* *
* The Initial Developer of the Original Code is CommuniCore. * The Initial Developer of the Original Code is CommuniCore.
* Portions created by CommuniCore are Copyright (C) 2005 CommuniCore Ltda * Portions created by CommuniCore are Copyright (C) 2005 CommuniCore Ltda
* All Rights Reserved. * All Rights Reserved.
********************************************************************************/ ********************************************************************************/
global $mod_strings, $current_user; global $mod_strings, $current_user;
if(ACLController::checkAccess('EcmInvoiceOuts', "edit", true)) $module_menu [] = Array("index.php?module=".'EcmInvoiceOuts'."&action=EditView&return_module=".'EcmInvoiceOuts'."&return_action=DetailView", translate('LNK_NEW_'.'ECMQUOTE', 'EcmInvoiceOuts'),"CreateEcmInvoiceOuts", 'EcmInvoiceOuts'); if (ACLController::checkAccess('EcmInvoiceOuts', "edit", true)) $module_menu [] = array("index.php?module=" . 'EcmInvoiceOuts' . "&action=EditView&return_module=" . 'EcmInvoiceOuts' . "&return_action=DetailView", translate('LNK_NEW_' . 'ECMQUOTE', 'EcmInvoiceOuts'), "CreateEcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=index&return_module=EcmInvoiceOuts&return_action=DetailView", translate('LNK_ECMQUOTES_LIST','EcmInvoiceOuts'),"EcmInvoiceOuts", 'EcmInvoiceOuts'); if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=index&return_module=EcmInvoiceOuts&return_action=DetailView", translate('LNK_ECMQUOTES_LIST', 'EcmInvoiceOuts'), "EcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=Report_INTRASTAT", "Raport INTRASTAT","EcmInvoiceOuts", 'EcmInvoiceOuts'); if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=Report_INTRASTAT", "Raport INTRASTAT", "EcmInvoiceOuts", 'EcmInvoiceOuts');
if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=ecommerce", "Faktury E-Commerce", "EcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=ecommerce", "Faktury E-Commerce","EcmInvoiceOuts", 'EcmInvoiceOuts'); if (ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = array("index.php?module=EcmInvoiceOuts&action=bimit_invoiceSummary", "Analiza faktur", "EcmInvoiceOuts", 'EcmInvoiceOuts');
if(ACLController::checkAccess('EcmInvoiceOuts', "list", true)) $module_menu [] = Array("index.php?module=EcmInvoiceOuts&action=bimit_invoiceSummary", "Analiza faktur","EcmInvoiceOuts", 'EcmInvoiceOuts');

View File

@@ -27,6 +27,8 @@ if (isset($_REQUEST['import_baselinker'])) {
include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/baselinkerInvoiceDetails.php'); include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/baselinkerInvoiceDetails.php');
} else if (isset($_REQUEST['apilo_products'])) { } else if (isset($_REQUEST['apilo_products'])) {
include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/checkApiloProducts.php'); include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/checkApiloProducts.php');
} else if (isset($_REQUEST['temu'])) {
include_once(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/TEMU/temuInvoicesListView.php');
} else { } else {
include_once(getcwd().'/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php'); include_once(getcwd().'/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php');
} }

View File

@@ -114,7 +114,7 @@ GROUP BY ip.ecmproduct_id, ip.price_netto;
foreach($order->products as $product) { foreach($order->products as $product) {
$prod = $db->fetchByAssoc($db->query("SELECT id, name, code, unit_id FROM ecmproducts WHERE code = '".$product->extCode."' AND deleted = 0")); $prod = $db->fetchByAssoc($db->query("SELECT id, name, code, unit_id FROM ecmproducts WHERE code = '".$product->extCode."' AND deleted = 0"));
if (!isset($prod)) { if (!isset($prod)) {
echo 'Brak produktu, nie wystawiaj dokumentu! '.$product->extCode.'<br>'; echo 'Brak produktu, nie wystawiaj dokumentu! '.$product->extCode.' (Zamówienie: '.$order->orderNumber.')<br>';
} else { } else {
$products[] = array( $products[] = array(
'product_id' => $prod['id'], 'product_id' => $prod['id'],