From d1ed3b92a55f16e8659b6de43ec4839c6b9dc84c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Mon, 25 Aug 2025 20:39:46 +0200 Subject: [PATCH] AI reports --- REST/functions.php | 187 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 162 insertions(+), 25 deletions(-) diff --git a/REST/functions.php b/REST/functions.php index d2b00023..e31ed8d6 100644 --- a/REST/functions.php +++ b/REST/functions.php @@ -323,39 +323,176 @@ function createCSVReports() $jobs = [ [ 'sql' => " - SELECT - i.document_no, - i.register_date, - i.parent_name, - p.code, - p.name, - p.group_ks, - ii.quantity, - ii.price_netto - 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 - WHERE i.type = 'normal' AND YEAR(i.register_date) = 2024 - ORDER BY i.register_date DESC + SELECT + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + CASE p.group_ks + WHEN 1 THEN 'Towar handlowy' + WHEN 2 THEN 'Wyrób gotowy' + WHEN 3 THEN 'Usługi' + WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' + WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' + ELSE 'Nieznane' + END AS group_ks, + GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, + ii.quantity, + ii.price_netto +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 +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' + 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 +GROUP BY + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + p.group_ks, + ii.quantity, + ii.price_netto +ORDER BY i.register_date DESC; ", - 'filename' => 'invoices_2024_' . date('Ymd_His') . '.csv', + 'filename' => 'invoices_2024.csv', ], [ 'sql' => " - SELECT code, name, SUM(ii.quantity) AS units, SUM(ii.price_netto*ii.quantity) AS revenue - FROM ecminvoiceoutitems ii - JOIN ecmproducts p ON p.id = ii.ecmproduct_id - GROUP BY code, name - ORDER BY revenue DESC - LIMIT 100 + SELECT + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + CASE p.group_ks + WHEN 1 THEN 'Towar handlowy' + WHEN 2 THEN 'Wyrób gotowy' + WHEN 3 THEN 'Usługi' + WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' + WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' + ELSE 'Nieznane' + END AS group_ks, + GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, + ii.quantity, + ii.price_netto +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 +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' + 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 +GROUP BY + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + p.group_ks, + ii.quantity, + ii.price_netto +ORDER BY i.register_date DESC; ", - 'filename' => 'top_products_' . date('Ymd_His') . '.csv', + 'filename' => 'invoices_2025.csv', ], [ - 'sql' =>"SELECT COUNT(*) FROM ecminvoiceouts WHERE YEAR(register_date)=2025", - 'filename' => 'ecminvoiceouts_2025_' . date('Ymd_His') . '.csv', + 'sql' => " +SELECT + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + CASE p.group_ks + WHEN 1 THEN 'Towar handlowy' + WHEN 2 THEN 'Wyrób gotowy' + WHEN 3 THEN 'Usługi' + WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' + WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' + ELSE 'Nieznane' + END AS group_ks, + GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, + ii.quantity, + ii.price_netto +FROM ecommerce_invoices AS i +INNER JOIN ecommerce_invoices_products AS ii ON i.id = ii.invoice_id +INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.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' + 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 +GROUP BY + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + p.group_ks, + ii.quantity, + ii.price_netto +ORDER BY i.register_date DESC; + ", + 'filename' => 'ecommerce_invoices_2024.csv', + ], + [ + 'sql' => " +SELECT + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + CASE p.group_ks + WHEN 1 THEN 'Towar handlowy' + WHEN 2 THEN 'Wyrób gotowy' + WHEN 3 THEN 'Usługi' + WHEN '530547ef-2dea-7622-843b-59d745b14c64' THEN 'Materiały' + WHEN '8451dded-710f-51c2-7ed1-60a377eaa7b7' THEN 'Surowce' + ELSE 'Nieznane' + END AS group_ks, + GROUP_CONCAT(c.name ORDER BY cb.position SEPARATOR ' | ') AS category, + ii.quantity, + ii.price_netto +FROM ecommerce_invoices AS i +INNER JOIN ecommerce_invoices_products AS ii ON i.id = ii.invoice_id +INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.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' + 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 +GROUP BY + i.document_no, + i.register_date, + i.parent_name, + p.code, + p.name, + p.group_ks, + ii.quantity, + ii.price_netto +ORDER BY i.register_date DESC; + ", + 'filename' => 'ecommerce_invoices_2025.csv', + ], + [ + 'sql' => " + SELECT + ss.product_code, + ss.product_name, + COALESCE(NULLIF(ss.quantity, ''), 0) AS quantity, + s.name + FROM ecmstockstates AS ss + JOIN ecmstocks AS s ON ss.stock_id = s.id + ORDER BY quantity + 0 DESC;", + 'filename' => 'stocks.csv', ], - // ... dopisz kolejne zestawienia ... ]; $report = [];