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.name,
CASE p.group_ks
WHEN 1 THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec'
WHEN 4 THEN 'Usługa'
WHEN '1' THEN 'Towar handlowy'
WHEN '2' THEN 'Wyrób gotowy'
WHEN '3' THEN 'Surowiec'
WHEN '4' THEN 'Usługa'
ELSE 'Nieznane'
END AS group_ks,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
END AS group_ks_name,
GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity,
ii.price_netto,
cur.name AS currency_name
FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id
INNER JOIN currencies AS cur ON cur.id = i.currency_id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci
INNER JOIN ecminvoiceoutitems AS ii
ON i.id = ii.ecminvoiceout_id
AND ii.deleted = 0
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.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2024
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
i.document_no,
i.register_date,
i.parent_name,
p.code,
p.name,
p.group_ks,
ii.quantity,
ii.price_netto
i.id,
ii.id
ORDER BY i.register_date DESC;
",
'filename' => 'invoices_2024.csv',
@@ -302,34 +307,39 @@ ORDER BY i.register_date DESC;
p.code,
p.name,
CASE p.group_ks
WHEN 1 THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec'
WHEN 4 THEN 'Usługa'
WHEN '1' THEN 'Towar handlowy'
WHEN '2' THEN 'Wyrób gotowy'
WHEN '3' THEN 'Surowiec'
WHEN '4' THEN 'Usługa'
ELSE 'Nieznane'
END AS group_ks,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
END AS group_ks_name,
GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity,
ii.price_netto,
cur.name AS currency_name
FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id
INNER JOIN currencies AS cur ON cur.id = i.currency_id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci
INNER JOIN ecminvoiceoutitems AS ii
ON i.id = ii.ecminvoiceout_id
AND ii.deleted = 0
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.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2025
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
i.document_no,
i.register_date,
i.parent_name,
p.code,
p.name,
p.group_ks,
ii.quantity,
ii.price_netto
i.id,
ii.id
ORDER BY i.register_date DESC;
",
'filename' => 'invoices_2025.csv',
@@ -345,39 +355,43 @@ SELECT
p.code,
p.name,
CASE p.group_ks
WHEN 1 THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec'
WHEN 4 THEN 'Usługa'
WHEN '1' THEN 'Towar handlowy'
WHEN '2' THEN 'Wyrób gotowy'
WHEN '3' THEN 'Surowiec'
WHEN '4' THEN 'Usługa'
ELSE 'Nieznane'
END AS group_ks,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity_corrected AS quantity_correced,
END AS group_ks_name,
GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity_corrected AS quantity_corrected,
ii.total_netto_corrected AS total_netto_corrected,
cur.name AS currency_name
FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id
INNER JOIN ecminvoiceouts AS oi ON oi.id = i.ecminvoiceout_id
INNER JOIN currencies AS cur ON cur.id = i.currency_id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci
INNER JOIN ecminvoiceoutitems AS ii
ON i.id = ii.ecminvoiceout_id
AND ii.deleted = 0
INNER JOIN ecmproducts AS p
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.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2024
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
i.document_no,
i.register_date,
oi.document_no,
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
i.id,
ii.id,
oi.id
ORDER BY i.register_date DESC;
",
'filename' => 'correct_invoices_2024.csv',
@@ -393,39 +407,43 @@ SELECT
p.code,
p.name,
CASE p.group_ks
WHEN 1 THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec'
WHEN 4 THEN 'Usługa'
WHEN '1' THEN 'Towar handlowy'
WHEN '2' THEN 'Wyrób gotowy'
WHEN '3' THEN 'Surowiec'
WHEN '4' THEN 'Usługa'
ELSE 'Nieznane'
END AS group_ks,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity_corrected AS quantity_correced,
END AS group_ks_name,
GROUP_CONCAT(DISTINCT c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
ii.quantity_corrected AS quantity_corrected,
ii.total_netto_corrected AS total_netto_corrected,
cur.name AS currency_name
FROM ecminvoiceouts AS i
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id
INNER JOIN ecminvoiceouts AS oi ON oi.id = i.ecminvoiceout_id
INNER JOIN currencies AS cur ON cur.id = i.currency_id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci
INNER JOIN ecminvoiceoutitems AS ii
ON i.id = ii.ecminvoiceout_id
AND ii.deleted = 0
INNER JOIN ecmproducts AS p
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.deleted = 0
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id
WHERE i.type = 'correct' AND YEAR(i.register_date) = 2025
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
i.document_no,
i.register_date,
oi.document_no,
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
i.id,
ii.id,
oi.id
ORDER BY i.register_date DESC;
",
'filename' => 'correct_invoices_2025.csv',
@@ -438,32 +456,40 @@ SELECT
p.code,
p.name,
CASE p.group_ks
WHEN 1 THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec'
WHEN 4 THEN 'Usługa'
WHEN '1' THEN 'Towar handlowy'
WHEN '2' THEN 'Wyrób gotowy'
WHEN '3' THEN 'Surowiec'
WHEN '4' THEN 'Usługa'
ELSE 'Nieznane'
END AS group_ks,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
END AS group_ks_name,
pc.category,
s.name AS stock,
ii.quantity
FROM ecmstockdocinsideouts AS i
INNER JOIN ecmstockdocinsideoutitems AS ii ON i.id = ii.ecmstockdocinsideout_id
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id
INNER JOIN ecmstocks AS s ON i.stock_id = s.id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci
AND cb.bean_name = 'EcmProducts'
INNER JOIN ecmstockdocinsideoutitems AS ii
ON i.id = ii.ecmstockdocinsideout_id
AND ii.deleted = 0
INNER JOIN ecmproducts AS p
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
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id
WHERE YEAR(i.register_date) = 2025
GROUP BY
i.document_no,
i.register_date,
p.code,
p.name,
p.group_ks,
s.name,
ii.quantity
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;
",
'filename' => 'rw_2025.csv',
@@ -476,32 +502,40 @@ SELECT
p.code,
p.name,
CASE p.group_ks
WHEN 1 THEN 'Towar handlowy'
WHEN 2 THEN 'Wyrób gotowy'
WHEN 3 THEN 'Surowiec'
WHEN 4 THEN 'Usługa'
WHEN '1' THEN 'Towar handlowy'
WHEN '2' THEN 'Wyrób gotowy'
WHEN '3' THEN 'Surowiec'
WHEN '4' THEN 'Usługa'
ELSE 'Nieznane'
END AS group_ks,
GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category,
END AS group_ks_name,
pc.category,
s.name AS stock,
ii.quantity
FROM ecmstockdocinsideouts AS i
INNER JOIN ecmstockdocinsideoutitems AS ii ON i.id = ii.ecmstockdocinsideout_id
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id
INNER JOIN ecmstocks AS s ON i.stock_id = s.id
LEFT JOIN ecmproductcategories_bean AS cb ON cb.bean_id COLLATE utf8_general_ci = p.id COLLATE utf8_general_ci
AND cb.bean_name = 'EcmProducts'
INNER JOIN ecmstockdocinsideoutitems AS ii
ON i.id = ii.ecmstockdocinsideout_id
AND ii.deleted = 0
INNER JOIN ecmproducts AS p
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
LEFT JOIN ecmproductcategories AS c ON c.id = cb.ecmproductcategory_id
WHERE YEAR(i.register_date) = 2024
GROUP BY
i.document_no,
i.register_date,
p.code,
p.name,
p.group_ks,
s.name,
ii.quantity
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;
",
'filename' => 'rw_2024.csv',
@@ -520,27 +554,39 @@ SELECT
ELSE 0
END AS ordered_quantity
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 (
SELECT
i.ecmproduct_id,
SUM(i.quantity) AS ordered_quantity
FROM ecmsaleitems AS i
JOIN ecmsales AS es 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'
JOIN ecmsales AS es
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
) AS si ON si.ecmproduct_id = ss.product_id
) AS si
ON si.ecmproduct_id = ss.product_id
LEFT JOIN (
SELECT
poi.ecmproduct_id,
SUM(poi.quantity) AS ordered_quantity
FROM ecmpurchaseorderitems AS poi
JOIN ecmpurchaseorders AS po 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'
JOIN ecmpurchaseorders AS po
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
) 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')
ORDER BY quantity + 0 DESC;",
ORDER BY quantity + 0 DESC;
",
'filename' => 'stocks.csv',
],// stocks
[
@@ -561,10 +607,11 @@ ORDER BY quantity + 0 DESC;",
es.delivery_date,
es.send_date
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')
AND es.deleted = '0'
AND i.deleted = '0'
AND es.deleted = 0
AND i.deleted = 0
AND es.register_date >= '2025-06-01'
ORDER BY es.register_date DESC, es.document_no DESC, i.position;
",
@@ -587,10 +634,11 @@ SELECT
es.register_date,
es.delivery_date
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')
AND es.deleted = '0'
AND i.deleted = '0'
AND es.deleted = 0
AND i.deleted = 0
AND es.register_date >= '2025-06-01'
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']) {
$report[] = "OK → {$result['path']} (wiersze: {$result['rows']})" . PHP_EOL;
} else {
$report[] = "ERR → {$result['path']} ({$result['error']})" . PHP_EOL;;
$report[] = "ERR → {$result['path']} ({$result['error']})" . PHP_EOL;
}
}