Files
crm.twinpol.com/modules/EcmInvoiceOuts/report_form.php
2025-09-29 00:19:15 +02:00

178 lines
6.9 KiB
PHP

<?php
/**
* report_form.php — formularz + uruchomienie analysisAI.py z parametrami
* ZGODNE z PHP 5.6 i Sugar 6 (wyciszone E_STRICT/E_DEPRECATED/NOTICE).
*/
// --- wycisz „hałas” Sugar CRM ---
error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED & ~E_NOTICE);
ini_set('display_errors', '0');
// (opcjonalnie) loguj do pliku
// ini_set('log_errors', '1');
// ini_set('error_log', '/var/log/php_form_errors.log');
// --- ŚCIEŻKI (dostosuj do swojej instalacji) ---
$python = '/usr/local/bin/python3';
$script = '/var/www/html/modules/EcmInvoiceOuts/ai/analysisAI.py';
$baseDir = dirname($script);
// --- domyślne wartości pól ---
$defaultDateTo = date('Y-m-d');
$defaultDateFrom = date('Y-m-d', strtotime('-7 days'));
// --- zbieranie POST (PHP 5.6 friendly) ---
$submitted = (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST');
$post_date_from = isset($_POST['date_from']) ? $_POST['date_from'] : $defaultDateFrom;
$post_date_to = isset($_POST['date_to']) ? $_POST['date_to'] : $defaultDateTo;
$post_preaggs = (isset($_POST['preaggs']) && is_array($_POST['preaggs'])) ? $_POST['preaggs'] : array();
$post_with_ai = !empty($_POST['with_ai']);
function h($v) { return htmlspecialchars($v, ENT_QUOTES, 'UTF-8'); }
function is_valid_date_yyyy_mm_dd($d) {
return (bool)preg_match('/^\d{4}-\d{2}-\d{2}$/', $d);
}
// --- wykonanie skryptu Pythona, jeśli formularz został wysłany ---
$ran = false;
$ok = false;
$rc = 0;
$out = '';
$err = '';
if ($submitted) {
// prosta walidacja dat
if (!is_valid_date_yyyy_mm_dd($post_date_from) || !is_valid_date_yyyy_mm_dd($post_date_to)) {
$err = "Nieprawidłowy format daty. Użyj YYYY-MM-DD.";
$ran = true;
} else {
// zbuduj argumenty
$args = array(
'--date-from', $post_date_from,
'--date-to', $post_date_to,
'--ai', ($post_with_ai ? 'true' : 'false')
);
if (!empty($post_preaggs)) {
// CSV z zaznaczonych preagregatów
$args[] = '--metrics';
$args[] = implode(',', $post_preaggs);
}
// komenda: przejdź do katalogu skryptu, uruchom pythona; zbierz stdout+stderr
$cmd = 'cd ' . escapeshellarg($baseDir) . ' && ' .
escapeshellcmd($python) . ' ' . escapeshellarg($script);
foreach ($args as $a) {
$cmd .= ' ' . escapeshellarg($a);
}
$output = array();
$returnVar = 0;
exec($cmd . ' 2>&1', $output, $returnVar);
$ran = true;
$rc = $returnVar;
$out = implode("\n", $output);
$ok = ($returnVar === 0);
if (!$ok && $err === '') {
$err = "Błąd uruchamiania skryptu Python (kod: " . $rc . "):\n" . $out;
}
}
}
?>
<!doctype html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>Generator raportu sprzedaży</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font: 14px/1.4 system-ui, Arial, sans-serif; padding: 20px; }
fieldset { margin-bottom: 16px; padding: 12px; border-radius: 6px; border:1px solid #e5e5e5; }
.row { display: flex; gap: 16px; flex-wrap: wrap; }
.col { min-width: 220px; flex: 1; }
label { display:block; margin: 6px 0; }
input[type="date"], button { padding: 6px 10px; font-size:14px; }
button { margin-top: 10px; cursor: pointer; border:1px solid #0a66c2; background:#0a66c2; color:#fff; border-radius:8px; }
.pill { display:inline-block; padding:2px 8px; border-radius:999px; background:#eee; margin:4px 6px 0 0; }
.out { white-space: normal; background: #fff; border:1px solid #ddd; padding:12px; border-radius:6px; }
.error { white-space: pre-wrap; background: #fff3f3; border:1px solid #f3c2c2; padding:12px; border-radius:6px; color:#b00020; }
.muted { color:#666; }
</style>
</head>
<body>
<h1>Raport sprzedaży — parametry</h1>
<form method="post">
<!-- zakres dat -->
<fieldset>
<legend>Zakres dat</legend>
<div class="row">
<div class="col">
<label>Data od:
<input type="date" name="date_from" value="<?php echo h($post_date_from); ?>" required>
</label>
</div>
<div class="col">
<label>Data do:
<input type="date" name="date_to" value="<?php echo h($post_date_to); ?>" required>
</label>
</div>
</div>
</fieldset>
<!-- preagregaty -->
<fieldset>
<legend>Preagregaty do analizy</legend>
<label><input type="checkbox" name="preaggs[]" value="daily_sales" <?php echo in_array('daily_sales', $post_preaggs, true) ? 'checked' : ''; ?>> Dzienne sprzedaże</label>
<label><input type="checkbox" name="preaggs[]" value="product_summary" <?php echo in_array('product_summary', $post_preaggs, true) ? 'checked' : ''; ?>> Podsumowanie produktów</label>
<label><input type="checkbox" name="preaggs[]" value="customer_summary" <?php echo in_array('customer_summary', $post_preaggs, true) ? 'checked' : ''; ?>> Podsumowanie klientów</label>
<label><input type="checkbox" name="preaggs[]" value="product_daily" <?php echo in_array('product_daily', $post_preaggs, true) ? 'checked' : ''; ?>> Sprzedaż produktu dziennie</label>
<label><input type="checkbox" name="preaggs[]" value="top10_products_by_sales" <?php echo in_array('top10_products_by_sales', $post_preaggs, true) ? 'checked' : ''; ?>> Top10 produktów</label>
<label><input type="checkbox" name="preaggs[]" value="top10_customers_by_sales"<?php echo in_array('top10_customers_by_sales', $post_preaggs, true) ? 'checked' : ''; ?>> Top10 klientów</label>
</fieldset>
<!-- AI -->
<fieldset>
<legend>Analiza AI</legend>
<label>
<input type="checkbox" name="with_ai" <?php echo $post_with_ai ? 'checked' : ''; ?>> Dołącz analizę AI
</label>
</fieldset>
<button type="submit">Generuj</button>
</form>
<?php if ($submitted): ?>
<hr>
<h2>Użyte parametry</h2>
<p>
<span class="pill">Od: <?php echo h($post_date_from); ?></span>
<span class="pill">Do: <?php echo h($post_date_to); ?></span>
<span class="pill">AI: <?php echo $post_with_ai ? 'tak' : 'nie'; ?></span>
</p>
<p>Preagregaty:
<?php
if (!empty($post_preaggs)) {
foreach ($post_preaggs as $p) {
echo '<span class="pill">'.h($p).'</span>';
}
} else {
echo '<span class="muted">brak</span>';
}
?>
</p>
<h2>Wynik analizy</h2>
<?php if (!$ok): ?>
<div class="error"><?php echo h($err); ?></div>
<?php else: ?>
<!-- Zakładamy, że Python zwraca gotowy HTML -->
<div class="out"><?php echo $out; ?></div>
<?php endif; ?>
<?php endif; ?>
</body>
</html>