createCSVReport() exportToCSVFile()
This commit is contained in:
@@ -314,25 +314,130 @@ function brecho($msg)
|
||||
echo '<br><br>';
|
||||
}
|
||||
// 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']}) <br/>";
|
||||
} else {
|
||||
$report[] = "ERR → {$result['path']} ({$result['error']}) <br/>";
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
$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];
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
print("Hello")
|
||||
Reference in New Issue
Block a user