From 19949ce95ab908caaf70cf415ca9e0b4db5d5694 Mon Sep 17 00:00:00 2001 From: zzdrojewskipaw Date: Mon, 25 Aug 2025 00:30:24 +0200 Subject: [PATCH] createCSVReport() exportToCSVFile() --- REST/functions.php | 143 +++++++++++++++++++--- modules/EcmInvoiceOuts/invoice_summary.py | 2 - 2 files changed, 124 insertions(+), 21 deletions(-) delete mode 100644 modules/EcmInvoiceOuts/invoice_summary.py diff --git a/REST/functions.php b/REST/functions.php index 30d234e7..d2b00023 100644 --- a/REST/functions.php +++ b/REST/functions.php @@ -314,25 +314,130 @@ function brecho($msg) echo '

'; } // AI analysis - function createCSVReports() { - // Zestawienie faktur - $db = $GLOBALS['db']; - $query = "SELECT "; - $res = $db->query(" - 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;"); - while ($row = $db->fetchByAssoc($res)) { - var_dump($row); +function createCSVReports() +{ + { + $db = $GLOBALS['db']; + $exportDir = __DIR__ . "/export"; + + $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 + ", + 'filename' => 'invoices_2024_' . date('Ymd_His') . '.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 + ", + 'filename' => 'top_products_' . date('Ymd_His') . '.csv', + ], + [ + 'sql' =>"SELECT COUNT(*) FROM ecminvoiceouts WHERE YEAR(register_date)=2025", + 'filename' => 'ecminvoiceouts_2025_' . date('Ymd_His') . '.csv', + ], + // ... dopisz kolejne zestawienia ... + ]; + + $report = []; + foreach ($jobs as $job) { + $sql = $job['sql']; + $filename = $job['filename']; + $headers = isset($job['headers']) ? $job['headers'] : null; + + $res = $db->query($sql); + $fullpath = rtrim($exportDir, "/") . "/" . $filename; + + $result = exportToCSVFile($res, $fullpath, $headers, ';', true); + + if ($result['ok']) { + $report[] = "OK → {$result['path']} (wiersze: {$result['rows']})
"; + } else { + $report[] = "ERR → {$result['path']} ({$result['error']})
"; + } + } + + echo implode("\n", $report); + exit; } - // TODO: zapisaś wynik do pliku CSV - // zetawienie faktur korygujących +} +function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';', $withBom = true) +{ + $db = $GLOBALS['db']; - // zestawienie RW + $dir = dirname($fullpath); + if (!is_dir($dir)) { + if (!@mkdir($dir, 0775, true)) { + return ['ok'=>false, 'path'=>$fullpath, 'rows'=>0, 'error'=>"Nie mogę utworzyć katalogu: $dir"]; + } + } + if (!is_writable($dir)) { + return ['ok'=>false, 'path'=>$fullpath, 'rows'=>0, 'error'=>"Katalog nie jest zapisywalny: $dir"]; + } - // zestawienie aktualnych stanów magazynowych - die(); - } \ No newline at end of file + $fp = @fopen($fullpath, 'w'); + if ($fp === false) { + return ['ok'=>false, 'path'=>$fullpath, 'rows'=>0, 'error'=>"Nie mogę otworzyć pliku do zapisu: $fullpath"]; + } + + // BOM dla Excel PL + if ($withBom) { + fwrite($fp, "\xEF\xBB\xBF"); + } + + // pobierz pierwszy wiersz, by ewentualnie zbudować nagłówki + $first = $db->fetchByAssoc($res); + + // brak danych → pusty plik z ewentualnym nagłówkiem (jeśli podany ręcznie) + if (!$first) { + if ($headers !== null) { + fputcsv($fp, $headers, $delimiter); + } + fclose($fp); + return ['ok'=>true, 'path'=>$fullpath, 'rows'=>0, 'error'=>null]; + } + + // dynamiczne nagłówki, jeśli nie podano + if ($headers === null) { + $headers = array_keys($first); + } + + // wypisz nagłówki + fputcsv($fp, $headers, $delimiter); + + // zapisz pierwszy wiersz w kolejności nagłówków + $line = []; + foreach ($headers as $h) { $line[] = isset($first[$h]) ? $first[$h] : ''; } + fputcsv($fp, $line, $delimiter); + $count = 1; + + // pozostałe wiersze + while ($row = $db->fetchByAssoc($res)) { + $line = []; + foreach ($headers as $h) { $line[] = isset($row[$h]) ? $row[$h] : ''; } + fputcsv($fp, $line, $delimiter); + $count++; + } + + fclose($fp); + return ['ok'=>true, 'path'=>$fullpath, 'rows'=>$count, 'error'=>null]; +} \ No newline at end of file diff --git a/modules/EcmInvoiceOuts/invoice_summary.py b/modules/EcmInvoiceOuts/invoice_summary.py deleted file mode 100644 index 62d6f1fb..00000000 --- a/modules/EcmInvoiceOuts/invoice_summary.py +++ /dev/null @@ -1,2 +0,0 @@ - -print("Hello")