createCSVReport() exportToCSVFile()
This commit is contained in:
@@ -314,25 +314,130 @@ function brecho($msg)
|
|||||||
echo '<br><br>';
|
echo '<br><br>';
|
||||||
}
|
}
|
||||||
// AI analysis
|
// AI analysis
|
||||||
function createCSVReports() {
|
function createCSVReports()
|
||||||
// Zestawienie faktur
|
{
|
||||||
|
{
|
||||||
$db = $GLOBALS['db'];
|
$db = $GLOBALS['db'];
|
||||||
$query = "SELECT ";
|
$exportDir = __DIR__ . "/export";
|
||||||
$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
|
$jobs = [
|
||||||
FROM ecminvoiceouts AS i
|
[
|
||||||
INNER JOIN ecminvoiceoutitems AS ii ON i.id = ii.ecminvoiceout_id
|
'sql' => "
|
||||||
INNER JOIN ecmproducts AS p ON ii.ecmproduct_id = p.id
|
SELECT
|
||||||
WHERE i.type = 'normal' AND YEAR(i.register_date) = 2024
|
i.document_no,
|
||||||
ORDER BY i.register_date DESC;");
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function exportToCSVFile($res, $fullpath, array $headers = null, $delimiter = ';', $withBom = true)
|
||||||
|
{
|
||||||
|
$db = $GLOBALS['db'];
|
||||||
|
|
||||||
|
$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"];
|
||||||
|
}
|
||||||
|
|
||||||
|
$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)) {
|
while ($row = $db->fetchByAssoc($res)) {
|
||||||
var_dump($row);
|
$line = [];
|
||||||
|
foreach ($headers as $h) { $line[] = isset($row[$h]) ? $row[$h] : ''; }
|
||||||
|
fputcsv($fp, $line, $delimiter);
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
// TODO: zapisaś wynik do pliku CSV
|
|
||||||
// zetawienie faktur korygujących
|
|
||||||
|
|
||||||
// zestawienie RW
|
fclose($fp);
|
||||||
|
return ['ok'=>true, 'path'=>$fullpath, 'rows'=>$count, 'error'=>null];
|
||||||
// zestawienie aktualnych stanów magazynowych
|
}
|
||||||
die();
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
print("Hello")
|
|
||||||
Reference in New Issue
Block a user