204 lines
7.0 KiB
PHP
204 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Export PDF
|
|
* Territory Manager
|
|
* Utilizza FPDF per generare PDF
|
|
*/
|
|
|
|
require_once 'config.php';
|
|
require_once 'functions.php';
|
|
require_once 'db.php';
|
|
|
|
requireLogin();
|
|
|
|
$type = $_GET['type'] ?? '';
|
|
$db = getDB();
|
|
|
|
// Configurazioni
|
|
$warning_days_normal = (int)$db->getConfig('warning_days_normal', 90);
|
|
$warning_days_priority = (int)$db->getConfig('warning_days_priority', 180);
|
|
$warning_days_return = (int)$db->getConfig('warning_days_return', 120);
|
|
|
|
// Semplice classe PDF
|
|
class PDF {
|
|
private $content = '';
|
|
|
|
public function addTitle($title) {
|
|
$this->content .= "<h1 style='color: #2c3e50; margin-bottom: 20px;'>$title</h1>";
|
|
}
|
|
|
|
public function addText($text) {
|
|
$this->content .= "<p style='margin: 10px 0;'>$text</p>";
|
|
}
|
|
|
|
public function addTable($headers, $rows) {
|
|
$this->content .= "<table style='width: 100%; border-collapse: collapse; margin: 20px 0;'>";
|
|
$this->content .= "<thead><tr style='background-color: #34495e; color: white;'>";
|
|
foreach ($headers as $header) {
|
|
$this->content .= "<th style='padding: 10px; border: 1px solid #ddd; text-align: left;'>$header</th>";
|
|
}
|
|
$this->content .= "</tr></thead><tbody>";
|
|
|
|
foreach ($rows as $row) {
|
|
$this->content .= "<tr>";
|
|
foreach ($row as $cell) {
|
|
$this->content .= "<td style='padding: 8px; border: 1px solid #ddd;'>$cell</td>";
|
|
}
|
|
$this->content .= "</tr>";
|
|
}
|
|
|
|
$this->content .= "</tbody></table>";
|
|
}
|
|
|
|
public function output($filename) {
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
header('Content-Disposition: inline; filename="' . $filename . '.html"');
|
|
|
|
echo "<!DOCTYPE html>
|
|
<html lang='it'>
|
|
<head>
|
|
<meta charset='UTF-8'>
|
|
<title>$filename</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; padding: 20px; }
|
|
@media print {
|
|
body { padding: 0; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>";
|
|
echo $this->content;
|
|
echo "<div style='margin-top: 40px; text-align: center; color: #7f8c8d;'>";
|
|
echo "<p>Generato il " . date('d/m/Y H:i') . " - " . APP_NAME . "</p>";
|
|
echo "</div>";
|
|
echo "<script>window.print();</script>";
|
|
echo "</body></html>";
|
|
}
|
|
}
|
|
|
|
$pdf = new PDF();
|
|
|
|
switch ($type) {
|
|
case 'to_assign':
|
|
// Territori da assegnare
|
|
$territories = $db->fetchAll("
|
|
SELECT
|
|
t.id,
|
|
t.numero,
|
|
t.zona,
|
|
t.tipologia,
|
|
MAX(a.returned_date) as last_returned_date,
|
|
DATEDIFF(CURDATE(), MAX(a.returned_date)) as days_in_depot
|
|
FROM territories t
|
|
LEFT JOIN assignments a ON t.id = a.territory_id
|
|
WHERE t.id NOT IN (
|
|
SELECT territory_id FROM assignments WHERE returned_date IS NULL
|
|
)
|
|
GROUP BY t.id, t.numero, t.zona, t.tipologia
|
|
HAVING days_in_depot >= ? OR MAX(a.returned_date) IS NULL
|
|
ORDER BY last_returned_date ASC NULLS FIRST
|
|
", [$warning_days_normal]);
|
|
|
|
$pdf->addTitle('Territori da Assegnare');
|
|
$pdf->addText('Territori in reparto da più di ' . $warning_days_normal . ' giorni');
|
|
$pdf->addText('Totale: ' . count($territories) . ' territori');
|
|
|
|
$headers = ['Numero', 'Zona', 'Tipologia', 'Ultima Restituzione', 'Giorni in Reparto'];
|
|
$rows = [];
|
|
foreach ($territories as $t) {
|
|
$rows[] = [
|
|
htmlspecialchars($t['numero']),
|
|
htmlspecialchars($t['zona']),
|
|
htmlspecialchars($t['tipologia']),
|
|
formatDate($t['last_returned_date']),
|
|
($t['days_in_depot'] ?? 'Mai assegnato')
|
|
];
|
|
}
|
|
|
|
$pdf->addTable($headers, $rows);
|
|
$pdf->output('Territori_da_Assegnare_' . date('Y-m-d'));
|
|
break;
|
|
|
|
case 'priority':
|
|
// Territori prioritari
|
|
$territories = $db->fetchAll("
|
|
SELECT
|
|
t.id,
|
|
t.numero,
|
|
t.zona,
|
|
t.tipologia,
|
|
MAX(a.returned_date) as last_returned_date,
|
|
DATEDIFF(CURDATE(), MAX(a.returned_date)) as days_in_depot
|
|
FROM territories t
|
|
LEFT JOIN assignments a ON t.id = a.territory_id
|
|
WHERE t.id NOT IN (
|
|
SELECT territory_id FROM assignments WHERE returned_date IS NULL
|
|
)
|
|
GROUP BY t.id, t.numero, t.zona, t.tipologia
|
|
HAVING days_in_depot >= ?
|
|
ORDER BY last_returned_date ASC NULLS FIRST
|
|
", [$warning_days_priority]);
|
|
|
|
$pdf->addTitle('Territori Prioritari');
|
|
$pdf->addText('Territori in reparto da più di ' . $warning_days_priority . ' giorni');
|
|
$pdf->addText('Totale: ' . count($territories) . ' territori');
|
|
|
|
$headers = ['Numero', 'Zona', 'Tipologia', 'Ultima Restituzione', 'Giorni in Reparto'];
|
|
$rows = [];
|
|
foreach ($territories as $t) {
|
|
$rows[] = [
|
|
htmlspecialchars($t['numero']),
|
|
htmlspecialchars($t['zona']),
|
|
htmlspecialchars($t['tipologia']),
|
|
formatDate($t['last_returned_date']),
|
|
($t['days_in_depot'] ?? 'Mai assegnato')
|
|
];
|
|
}
|
|
|
|
$pdf->addTable($headers, $rows);
|
|
$pdf->output('Territori_Prioritari_' . date('Y-m-d'));
|
|
break;
|
|
|
|
case 'to_return':
|
|
// Territori da riconsegnare
|
|
$territories = $db->fetchAll("
|
|
SELECT
|
|
t.id,
|
|
t.numero,
|
|
t.zona,
|
|
t.tipologia,
|
|
a.assigned_to,
|
|
a.assigned_date,
|
|
DATEDIFF(CURDATE(), a.assigned_date) as days_assigned
|
|
FROM territories t
|
|
INNER JOIN assignments a ON t.id = a.territory_id
|
|
WHERE a.returned_date IS NULL
|
|
AND DATEDIFF(CURDATE(), a.assigned_date) >= ?
|
|
ORDER BY a.assigned_date ASC
|
|
", [$warning_days_return]);
|
|
|
|
$pdf->addTitle('Territori da Riconsegnare');
|
|
$pdf->addText('Territori assegnati da più di ' . $warning_days_return . ' giorni');
|
|
$pdf->addText('Totale: ' . count($territories) . ' territori');
|
|
|
|
$headers = ['Numero', 'Zona', 'Tipologia', 'Assegnato a', 'Data Assegnazione', 'Giorni'];
|
|
$rows = [];
|
|
foreach ($territories as $t) {
|
|
$rows[] = [
|
|
htmlspecialchars($t['numero']),
|
|
htmlspecialchars($t['zona']),
|
|
htmlspecialchars($t['tipologia']),
|
|
htmlspecialchars($t['assigned_to']),
|
|
formatDate($t['assigned_date']),
|
|
$t['days_assigned'] . ' giorni'
|
|
];
|
|
}
|
|
|
|
$pdf->addTable($headers, $rows);
|
|
$pdf->output('Territori_da_Riconsegnare_' . date('Y-m-d'));
|
|
break;
|
|
|
|
default:
|
|
die('Tipo di export non valido');
|
|
}
|