sprzadaz analiza AI

This commit is contained in:
zzdrojewskipaw
2025-08-22 15:56:47 +02:00
parent ed05cdeb75
commit d1ff1fa0ac
6 changed files with 484 additions and 63 deletions

View File

@@ -1,66 +1,164 @@
<?php
<!-- KPI -->
<div class="grid">
<div class="card">
<div class="kpi-label">Łączny przychód</div>
<div class="kpi-value">587&nbsp;679,40 PLN</div>
</div>
<div class="card">
<div class="kpi-label">Liczba faktur</div>
<div class="kpi-value">320</div>
</div>
<div class="card">
<div class="kpi-label">Sprzedane jednostki</div>
<div class="kpi-value">182&nbsp;619 szt.</div>
</div>
<div class="card">
<div class="kpi-label">AOV średnia wartość faktury</div>
<div class="kpi-value">1&nbsp;836,50 PLN</div>
<div class="kpi-sub muted">AOV = przychód / liczba faktur</div>
</div>
</div>
$db = $GLOBALS['db'];
<!-- Top produkty -->
<h2>Top produkty wg przychodu</h2>
<div class="card table-card">
<table>
<thead>
<tr>
<th style="width:22%">Kod</th>
<th>Produkt</th>
<th style="width:18%">Przychód [PLN]</th>
</tr>
</thead>
<tbody>
<tr><td>FR00099_250_Wilfa</td><td>WIUCC-250 CLEANING LIQUID COFFEEMAKER, 250 ml</td><td>51&nbsp;217,92</td></tr>
<tr><td>AGDPR01</td><td>Środek do czyszczenia pralek automatycznych</td><td>47&nbsp;500,00</td></tr>
<tr><td>FR00013_1000_Drekker</td><td>Odkamieniacz do automatycznych ekspresów do kawy, 1000 ml</td><td>30&nbsp;600,00</td></tr>
<tr><td>AGDCHRM01</td><td>Płyn do robotów mopujących, 500ml</td><td>22&nbsp;277,70</td></tr>
<tr><td>FR00016_10_2g_amz_de</td><td>Cleaning tablets for coffee machines, 10 x 2g</td><td>19&nbsp;426,00</td></tr>
</tbody>
</table>
</div>
$query = "
SELECT s.document_no, s.register_date, s.parent_name, s.total_netto, si.code, si.name, si.quantity, si.price_netto
FROM ecmsaleitems AS si
INNER JOIN ecmsales AS s ON si.ecmsale_id = s.id
WHERE s.register_date >= NOW() - INTERVAL 7 DAY
ORDER BY s.register_date DESC;
";
<!-- Top klienci -->
<h2>Top klienci wg przychodu</h2>
<div class="card table-card">
<table>
<thead>
<tr>
<th>Klient</th>
<th style="width:20%">Przychód [PLN]</th>
</tr>
</thead>
<tbody>
<tr><td>Euro-net Sp. z o.o.</td><td>138&nbsp;660,08</td></tr>
<tr><td>Wilfa AS</td><td>71&nbsp;616,72</td></tr>
<tr><td>Aqualogis Polska Sp. z o.o.</td><td>58&nbsp;108,20</td></tr>
<tr><td>dm-drogerie markt Sp. z o.o.</td><td>40&nbsp;108,08</td></tr>
<tr><td>MediaRange GmbH</td><td>40&nbsp;064,24</td></tr>
</tbody>
</table>
</div>
$res = $db->query($query);
$results = [];
if ($res) {
$columns = array_keys($db->fetchByAssoc($res));
$results[] = $columns;
mysqli_data_seek($res, 0);
while ($row = $db->fetchByAssoc($res)) {
$results[] = array_values($row);
}
}
$jsonData = json_encode($results);
$apiKey = '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';
$messages = [
[
"role" => "system",
"content" => "Jesteś analitykiem danych. Przygotuj szczegółowy raport sprzedaży na podstawie danych w formacie JSON (jest to lista zamówień z ostatnich 7 dni). Wygeneruj czysty kod HTML wewnątrz jednego <div>, bez <html>, <head> ani <body>. Raport powinien zawierać tabele, nagłówki, podsumowania, wnioski, rekomendacje i listę potencjalnych nieprawidłowości. Dane zachowaj w oryginale (nie tłumacz nazw). Zadbaj o estetyczny i uporządkowany układ raportu. Zwróć tylko kod HTML. Odpowiedz w języku polskim.",
],
[
"role" => "user",
"content" => "Oto dane sprzedaży w formacie JSON:\n\n$jsonData"
]
];
$payload = [
"model" => "gpt-4.1",
"messages" => $messages,
"temperature" => 0.3
];
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Błąd: ' . curl_error($ch);
exit;
}
$data = json_decode($response, true);
$htmlReport = $data['choices'][0]['message']['content'];
echo $htmlReport;
<!-- Mix change alerts -->
<h2>Alerty „mix change” (duże zmiany udziału produktu w przychodzie)</h2>
<div class="card table-card">
<table>
<thead>
<tr>
<th style="width:11%">Data</th>
<th style="width:20%">Kod</th>
<th>Produkt</th>
<th style="width:12%">Przychód [PLN]</th>
<th style="width:10%">Szt.</th>
<th style="width:10%">Udział dnia</th>
<th style="width:14%">Porównanie</th>
<th style="width:13%">Δ udziału</th>
<th style="width:10%">Baseline</th>
</tr>
</thead>
<tbody>
<tr>
<td>2025-07-01</td>
<td>FR00006_250_amz_de</td>
<td>Płyn do czyszczenia pralek automatycznych, 250 ml</td>
<td>1&nbsp;169,60</td>
<td>136</td>
<td><span class="pill ok">23,98%</span></td>
<td>vs mediana miesiąca</td>
<td><span class="pill ok">+19,99 pp</span></td>
<td>3,99%</td>
</tr>
<tr>
<td>2025-07-02</td>
<td>SECO002</td>
<td>ECO Wilgotne Ściereczki do ekranów</td>
<td>1&nbsp;826,52</td>
<td>372</td>
<td><span class="pill ok">24,93%</span></td>
<td>vs mediana miesiąca</td>
<td><span class="pill ok">+12,17 pp</span></td>
<td>12,75%</td>
</tr>
<tr>
<td>2025-07-02</td>
<td>RE00094</td>
<td>Płyn do ekranów TABLET/SMARTFON/LCD/PLASMA, 250 ml</td>
<td>1&nbsp;048,56</td>
<td>204</td>
<td><span class="pill warn">14,31%</span></td>
<td>vs mediana miesiąca</td>
<td><span class="pill warn">+6,55 pp</span></td>
<td>7,76%</td>
</tr>
<tr>
<td>2025-07-09</td>
<td>ICL-6550-INT</td>
<td>Compressed gas duster, 400 ml</td>
<td>3&nbsp;494,40</td>
<td>624</td>
<td><span class="pill warn">10,09%</span></td>
<td>vs mediana miesiąca</td>
<td><span class="pill warn">+6,87 pp</span></td>
<td>3,23%</td>
</tr>
<tr>
<td>2025-07-09</td>
<td>ICL-6575-INT</td>
<td>Compressed gas duster, 600 ml</td>
<td>3&nbsp;463,20</td>
<td>444</td>
<td><span class="pill">10,00%</span></td>
<td>vs mediana miesiąca</td>
<td><span class="pill">+4,39 pp</span></td>
<td>5,61%</td>
</tr>
<tr>
<td>2025-07-10</td>
<td>ICL-6550-INT</td>
<td>Compressed gas duster, 400 ml</td>
<td>1&nbsp;881,60</td>
<td>336</td>
<td><span class="pill">3,05%</span></td>
<td>vs rolling 7 dni</td>
<td><span class="pill bad">7,05 pp</span></td>
<td>10,09%</td>
</tr>
<tr>
<td>2025-07-10</td>
<td>ICL-6575-INT</td>
<td>Compressed gas duster, 600 ml</td>
<td>3&nbsp;463,20</td>
<td>444</td>
<td><span class="pill">5,61%</span></td>
<td>vs rolling 7 dni</td>
<td><span class="pill bad">4,39 pp</span></td>
<td>10,00%</td>
</tr>
<tr>
<td>2025-07-17</td>
<td>FR00006_250_amz_de</td>
<td>Płyn do czyszczenia pralek automatycznych, 250 ml</td>
<td>1&nbsp;080,00</td>
<td>144</td>
<td><span class="pill">3