CSV exports fix

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

View File

@@ -261,34 +261,39 @@ function createCSVReports()
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
ON ii.ecmproduct_id = p.id
AND p.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.bean_name = 'EcmProducts'
AND cb.deleted = 0 AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id LEFT JOIN ecmproductcategories AS c
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2024 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',
@@ -302,34 +307,39 @@ ORDER BY i.register_date DESC;
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
ON ii.ecmproduct_id = p.id
AND p.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.bean_name = 'EcmProducts'
AND cb.deleted = 0 AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id LEFT JOIN ecmproductcategories AS c
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2025 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,39 +355,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 p.deleted = 0
INNER JOIN ecminvoiceouts AS oi
ON oi.id = i.ecminvoiceout_id
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.bean_name = 'EcmProducts'
AND cb.deleted = 0 AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id LEFT JOIN ecmproductcategories AS c
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2024 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,
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_2024.csv', 'filename' => 'correct_invoices_2024.csv',
@@ -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 p.deleted = 0
INNER JOIN ecminvoiceouts AS oi
ON oi.id = i.ecminvoiceout_id
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.bean_name = 'EcmProducts'
AND cb.deleted = 0 AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id LEFT JOIN ecmproductcategories AS c
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2025 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 p.deleted = 0
INNER JOIN ecmstocks AS s
ON i.stock_id = s.id
AND s.deleted = 0
LEFT JOIN (
SELECT
cb.bean_id AS product_id,
GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category
FROM ecmproductcategories_bean AS cb
INNER JOIN ecmproductcategories AS c
ON c.id = cb.ecmproductcategory_id
AND c.deleted = 0
WHERE cb.bean_name = 'EcmProducts'
AND cb.deleted = 0 AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id GROUP BY cb.bean_id
WHERE YEAR(i.register_date) = 2025 ) AS pc
GROUP BY ON pc.product_id = p.id
i.document_no, WHERE i.deleted = 0
i.register_date, AND i.register_date BETWEEN '2025-01-01' AND '2025-12-31'
p.code,
p.name,
p.group_ks,
s.name,
ii.quantity
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 p.deleted = 0
INNER JOIN ecmstocks AS s
ON i.stock_id = s.id
AND s.deleted = 0
LEFT JOIN (
SELECT
cb.bean_id AS product_id,
GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category
FROM ecmproductcategories_bean AS cb
INNER JOIN ecmproductcategories AS c
ON c.id = cb.ecmproductcategory_id
AND c.deleted = 0
WHERE cb.bean_name = 'EcmProducts'
AND cb.deleted = 0 AND cb.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id GROUP BY cb.bean_id
WHERE YEAR(i.register_date) = 2024 ) AS pc
GROUP BY ON pc.product_id = p.id
i.document_no, WHERE i.deleted = 0
i.register_date, AND i.register_date BETWEEN '2024-01-01' AND '2024-12-31'
p.code,
p.name,
p.group_ks,
s.name,
ii.quantity
ORDER BY i.register_date DESC; ORDER BY i.register_date DESC;
", ",
'filename' => 'rw_2024.csv', 'filename' => 'rw_2024.csv',
@@ -520,27 +554,39 @@ 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
ON ss.stock_id = s.id
LEFT JOIN ( 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
ON si.ecmproduct_id = ss.product_id
LEFT JOIN ( 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
ON poi.ecmproduct_id = ss.product_id
WHERE ss.stock_id IN ('c7afd71a-4c3a-bde4-138d-4acaee1644e4', '368479db-22c5-0220-3a14-4bc426b1c709') WHERE ss.stock_id IN ('c7afd71a-4c3a-bde4-138d-4acaee1644e4', '368479db-22c5-0220-3a14-4bc426b1c709')
ORDER BY quantity + 0 DESC;", ORDER BY quantity + 0 DESC;
",
'filename' => 'stocks.csv', 'filename' => 'stocks.csv',
],// stocks ],// stocks
[ [
@@ -561,10 +607,11 @@ ORDER BY quantity + 0 DESC;",
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;
", ",
@@ -587,10 +634,11 @@ SELECT
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;
} }
} }