diff --git a/REST/functions.php b/REST/functions.php index f5286d70..23d5daa1 100644 --- a/REST/functions.php +++ b/REST/functions.php @@ -323,85 +323,97 @@ function createCSVReports() $jobs = [ [ 'sql' => " - SELECT +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 '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, + 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 + COALESCE(cur.name, 'PLN') 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 - 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 +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 +LEFT 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 = '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 -ORDER BY i.register_date DESC; + i.id, + ii.id +ORDER BY + i.register_date DESC; ", 'filename' => 'invoices_2024.csv', ], // invoices 2024 [ 'sql' => " - SELECT +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 '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, + 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 + COALESCE(cur.name, 'PLN') 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 - 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 +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 +LEFT 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 = '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 -ORDER BY i.register_date DESC; + i.id, + ii.id +ORDER BY + i.register_date DESC; ", 'filename' => 'invoices_2025.csv', ], // invoices 2025 @@ -416,41 +428,38 @@ SELECT 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' + WHEN '1' THEN 'Towar handlowy' + WHEN '2' THEN 'Wyrób gotowy' + WHEN '3' THEN 'Usługi' 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 + COALESCE(cur.name, 'PLN') 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 - 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 +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 +LEFT JOIN ecminvoiceouts AS oi + ON oi.id = i.ecminvoiceout_id AND oi.deleted = 0 +LEFT 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 - 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 -ORDER BY i.register_date DESC; + i.id, + ii.id, + oi.id +ORDER BY i.register_date DESC; ", 'filename' => 'correct_invoices_2024.csv', ], // correct invoices 2024 @@ -465,40 +474,37 @@ SELECT 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' + WHEN '1' THEN 'Towar handlowy' + WHEN '2' THEN 'Wyrób gotowy' + WHEN '3' THEN 'Usługi' 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 + COALESCE(cur.name, 'PLN') 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 - 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 +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 +LEFT JOIN ecminvoiceouts AS oi + ON oi.id = i.ecminvoiceout_id AND oi.deleted = 0 +LEFT 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 - 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', @@ -512,33 +518,37 @@ SELECT 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 '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, + END AS group_ks_name, + pc.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 +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 ( + 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 + GROUP BY cb.bean_id +) 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; ", 'filename' => 'ecommerce_invoices_2024.csv', @@ -552,33 +562,37 @@ SELECT 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 '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, + END AS group_ks_name, + pc.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 +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 ( + 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 + GROUP BY cb.bean_id +) 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; ", 'filename' => 'ecommerce_invoices_2025.csv', @@ -603,32 +617,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' - 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 +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 + 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', @@ -641,32 +663,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' - 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 +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 + 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',