CSV exports fix

This commit is contained in:
2025-11-27 20:07:59 +01:00
parent fbc3c112a2
commit a7339e01f4

View File

@@ -254,82 +254,92 @@ 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 '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, 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 cur.name 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 INNER 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,
p.code,
p.name,
p.group_ks,
ii.quantity,
ii.price_netto
ORDER BY i.register_date DESC; 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 '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, 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 cur.name 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 INNER 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,
p.code,
p.name,
p.group_ks,
ii.quantity,
ii.price_netto
ORDER BY i.register_date DESC; ORDER BY i.register_date DESC;
", ",
'filename' => 'invoices_2025.csv', 'filename' => 'invoices_2025.csv',
@@ -345,40 +355,44 @@ 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, 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 cur.name 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 ecminvoiceouts AS oi ON oi.id = i.ecminvoiceout_id AND ii.deleted = 0
INNER JOIN currencies AS cur ON cur.id = i.currency_id INNER JOIN ecmproducts AS p
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci ON ii.ecmproduct_id = p.id
AND cb.bean_name = 'EcmProducts' AND p.deleted = 0
AND cb.deleted = 0 INNER JOIN ecminvoiceouts AS oi
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id ON oi.id = i.ecminvoiceout_id
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2024 AND oi.deleted = 0
INNER JOIN currencies AS cur
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 = '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
@@ -393,39 +407,43 @@ 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, 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 cur.name 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 ecminvoiceouts AS oi ON oi.id = i.ecminvoiceout_id AND ii.deleted = 0
INNER JOIN currencies AS cur ON cur.id = i.currency_id INNER JOIN ecmproducts AS p
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci ON ii.ecmproduct_id = p.id
AND cb.bean_name = 'EcmProducts' AND p.deleted = 0
AND cb.deleted = 0 INNER JOIN ecminvoiceouts AS oi
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id ON oi.id = i.ecminvoiceout_id
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2025 AND oi.deleted = 0
INNER JOIN currencies AS cur
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 = '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',
@@ -438,32 +456,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',
@@ -476,32 +502,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',
@@ -520,32 +554,44 @@ SELECT
ELSE 0 ELSE 0
END AS ordered_quantity END AS ordered_quantity
FROM ecmstockstates AS ss FROM ecmstockstates AS ss
JOIN ecmstocks AS s ON ss.stock_id = s.id JOIN ecmstocks AS s
LEFT JOIN ( ON ss.stock_id = s.id
LEFT JOIN (
SELECT SELECT
i.ecmproduct_id, i.ecmproduct_id,
SUM(i.quantity) AS ordered_quantity SUM(i.quantity) AS ordered_quantity
FROM ecmsaleitems AS i FROM ecmsaleitems AS i
JOIN ecmsales AS es ON es.id = i.ecmsale_id JOIN ecmsales AS es
WHERE es.status IN ('s20','s30') AND es.deleted = '0' AND i.deleted = '0' AND es.register_date >= '2025-06-01' ON es.id = i.ecmsale_id
WHERE es.status IN ('s20', 's30')
AND es.deleted = 0
AND i.deleted = 0
AND es.register_date >= '2025-06-01'
GROUP BY i.ecmproduct_id GROUP BY i.ecmproduct_id
) AS si ON si.ecmproduct_id = ss.product_id ) AS si
LEFT JOIN ( ON si.ecmproduct_id = ss.product_id
LEFT JOIN (
SELECT SELECT
poi.ecmproduct_id, poi.ecmproduct_id,
SUM(poi.quantity) AS ordered_quantity SUM(poi.quantity) AS ordered_quantity
FROM ecmpurchaseorderitems AS poi FROM ecmpurchaseorderitems AS poi
JOIN ecmpurchaseorders AS po ON po.id = poi.ecmpurchaseorder_id JOIN ecmpurchaseorders AS po
WHERE po.status IN ('accepted','registered') AND po.deleted = '0' AND poi.deleted = '0' AND po.register_date >= '2025-06-01' ON po.id = poi.ecmpurchaseorder_id
WHERE po.status IN ('accepted', 'registered')
AND po.deleted = 0
AND poi.deleted = 0
AND po.register_date >= '2025-06-01'
GROUP BY poi.ecmproduct_id GROUP BY poi.ecmproduct_id
) AS poi ON poi.ecmproduct_id = ss.product_id ) AS poi
WHERE ss.stock_id IN ('c7afd71a-4c3a-bde4-138d-4acaee1644e4','368479db-22c5-0220-3a14-4bc426b1c709') ON poi.ecmproduct_id = ss.product_id
ORDER BY quantity + 0 DESC;", WHERE ss.stock_id IN ('c7afd71a-4c3a-bde4-138d-4acaee1644e4', '368479db-22c5-0220-3a14-4bc426b1c709')
ORDER BY quantity + 0 DESC;
",
'filename' => 'stocks.csv', 'filename' => 'stocks.csv',
],// stocks ],// stocks
[ [
'sql' => " 'sql' => "
SELECT SELECT
i.code AS product_code, i.code AS product_code,
i.name AS product_name, i.name AS product_name,
i.quantity, i.quantity,
@@ -556,15 +602,16 @@ ORDER BY quantity + 0 DESC;",
WHEN 's20' THEN 'Oczekujący' WHEN 's20' THEN 'Oczekujący'
WHEN 's30' THEN 'Zaakceptowany' WHEN 's30' THEN 'Zaakceptowany'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS status, END AS status,
es.register_date, es.register_date,
es.delivery_date, es.delivery_date,
es.send_date es.send_date
FROM ecmsaleitems AS i FROM ecmsaleitems AS i
JOIN ecmsales AS es ON es.id = i.ecmsale_id JOIN ecmsales AS es
ON es.id = i.ecmsale_id
WHERE es.status IN ('s20','s30') WHERE es.status IN ('s20','s30')
AND es.deleted = '0' AND es.deleted = 0
AND i.deleted = '0' AND i.deleted = 0
AND es.register_date >= '2025-06-01' AND es.register_date >= '2025-06-01'
ORDER BY es.register_date DESC, es.document_no DESC, i.position; ORDER BY es.register_date DESC, es.document_no DESC, i.position;
", ",
@@ -583,14 +630,15 @@ SELECT
WHEN 'accepted' THEN 'Zaakceptowany' WHEN 'accepted' THEN 'Zaakceptowany'
WHEN 'registered' THEN 'Zarejestrowany' WHEN 'registered' THEN 'Zarejestrowany'
ELSE 'Nieznane' ELSE 'Nieznane'
END AS status, END AS status,
es.register_date, es.register_date,
es.delivery_date es.delivery_date
FROM ecmpurchaseorderitems AS i FROM ecmpurchaseorderitems AS i
JOIN ecmpurchaseorders AS es ON es.id = i.ecmpurchaseorder_id JOIN ecmpurchaseorders AS es
ON es.id = i.ecmpurchaseorder_id
WHERE es.status IN ('accepted','registered') WHERE es.status IN ('accepted','registered')
AND es.deleted = '0' AND es.deleted = 0
AND i.deleted = '0' AND i.deleted = 0
AND es.register_date >= '2025-06-01' AND es.register_date >= '2025-06-01'
ORDER BY es.register_date DESC, es.document_no DESC, i.position; ORDER BY es.register_date DESC, es.document_no DESC, i.position;
", ",
@@ -612,7 +660,7 @@ ORDER BY es.register_date DESC, es.document_no DESC, i.position;
if ($result['ok']) { if ($result['ok']) {
$report[] = "OK → {$result['path']} (wiersze: {$result['rows']})" . PHP_EOL; $report[] = "OK → {$result['path']} (wiersze: {$result['rows']})" . PHP_EOL;
} else { } else {
$report[] = "ERR → {$result['path']} ({$result['error']})" . PHP_EOL;; $report[] = "ERR → {$result['path']} ({$result['error']})" . PHP_EOL;
} }
} }