299 lines
12 KiB
PHP
299 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Pagina Dashboard (Home)
|
|
* Territory Manager
|
|
*/
|
|
|
|
require_once 'config.php';
|
|
require_once 'functions.php';
|
|
require_once 'db.php';
|
|
|
|
requireLogin();
|
|
|
|
$page_title = 'Dashboard';
|
|
$db = getDB();
|
|
|
|
// Carica 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);
|
|
|
|
// Ottieni il limite (default 10, se expanded = all)
|
|
$limit_normal = isset($_GET['expand_normal']) ? '' : 'LIMIT 10';
|
|
$limit_priority = isset($_GET['expand_priority']) ? '' : 'LIMIT 10';
|
|
$limit_return = isset($_GET['expand_return']) ? '' : 'LIMIT 10';
|
|
|
|
// Territori da assegnare (in reparto da più di X giorni)
|
|
$territories_to_assign = $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
|
|
$limit_normal
|
|
", [$warning_days_normal]);
|
|
|
|
// Territori prioritari (in reparto da più di X giorni)
|
|
$territories_priority = $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
|
|
$limit_priority
|
|
", [$warning_days_priority]);
|
|
|
|
// Territori da riconsegnare (assegnati da più di X giorni)
|
|
$territories_to_return = $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
|
|
$limit_return
|
|
", [$warning_days_return]);
|
|
|
|
// Conteggi totali
|
|
$count_to_assign = $db->fetchOne("
|
|
SELECT COUNT(*) as total
|
|
FROM (
|
|
SELECT t.id
|
|
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
|
|
HAVING DATEDIFF(CURDATE(), MAX(a.returned_date)) >= ? OR MAX(a.returned_date) IS NULL
|
|
) as subquery
|
|
", [$warning_days_normal])['total'];
|
|
|
|
$count_priority = $db->fetchOne("
|
|
SELECT COUNT(*) as total
|
|
FROM (
|
|
SELECT t.id
|
|
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
|
|
HAVING DATEDIFF(CURDATE(), MAX(a.returned_date)) >= ?
|
|
) as subquery
|
|
", [$warning_days_priority])['total'];
|
|
|
|
$count_to_return = $db->fetchOne("
|
|
SELECT COUNT(*) as total
|
|
FROM assignments
|
|
WHERE returned_date IS NULL
|
|
AND DATEDIFF(CURDATE(), assigned_date) >= ?
|
|
", [$warning_days_return])['total'];
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<div class="dashboard-header">
|
|
<h1>Dashboard</h1>
|
|
<div class="dashboard-actions">
|
|
<a href="territories.php?action=add" class="btn btn-primary">+ Nuovo Territorio</a>
|
|
<a href="assignments.php?action=assign" class="btn btn-success">+ Nuova Assegnazione</a>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Territori da Assegnare -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Territori da Assegnare (<?php echo $count_to_assign; ?>)</h2>
|
|
<div class="card-actions">
|
|
<?php if (count($territories_to_assign) > 0): ?>
|
|
<a href="export_pdf.php?type=to_assign" class="btn btn-sm btn-secondary" target="_blank">📄 Export PDF</a>
|
|
<?php if (!isset($_GET['expand_normal'])): ?>
|
|
<a href="?expand_normal=1" class="btn btn-sm btn-primary">Mostra Tutti</a>
|
|
<?php else: ?>
|
|
<a href="index.php" class="btn btn-sm btn-secondary">Mostra Meno</a>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<p class="help-text">Territori in reparto da più di <?php echo $warning_days_normal; ?> giorni</p>
|
|
<?php if (count($territories_to_assign) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Numero</th>
|
|
<th>Zona</th>
|
|
<th>Tipologia</th>
|
|
<th>Ultima Restituzione</th>
|
|
<th>Giorni in Reparto</th>
|
|
<th>Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($territories_to_assign as $territory): ?>
|
|
<tr>
|
|
<td><strong><?php echo htmlspecialchars($territory['numero']); ?></strong></td>
|
|
<td><?php echo htmlspecialchars($territory['zona']); ?></td>
|
|
<td><?php echo htmlspecialchars($territory['tipologia']); ?></td>
|
|
<td><?php echo formatDate($territory['last_returned_date']); ?></td>
|
|
<td>
|
|
<span class="badge badge-warning">
|
|
<?php echo $territory['days_in_depot'] ?? 'Mai assegnato'; ?> giorni
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<a href="assignments.php?action=assign&territory_id=<?php echo $territory['id']; ?>"
|
|
class="btn btn-sm btn-success">Assegna</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun territorio da assegnare</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Territori Prioritari -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Territori Prioritari (<?php echo $count_priority; ?>)</h2>
|
|
<div class="card-actions">
|
|
<?php if (count($territories_priority) > 0): ?>
|
|
<a href="export_pdf.php?type=priority" class="btn btn-sm btn-secondary" target="_blank">📄 Export PDF</a>
|
|
<?php if (!isset($_GET['expand_priority'])): ?>
|
|
<a href="?expand_priority=1" class="btn btn-sm btn-primary">Mostra Tutti</a>
|
|
<?php else: ?>
|
|
<a href="index.php" class="btn btn-sm btn-secondary">Mostra Meno</a>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<p class="help-text">Territori in reparto da più di <?php echo $warning_days_priority; ?> giorni</p>
|
|
<?php if (count($territories_priority) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Numero</th>
|
|
<th>Zona</th>
|
|
<th>Tipologia</th>
|
|
<th>Ultima Restituzione</th>
|
|
<th>Giorni in Reparto</th>
|
|
<th>Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($territories_priority as $territory): ?>
|
|
<tr class="priority-row">
|
|
<td><strong><?php echo htmlspecialchars($territory['numero']); ?></strong></td>
|
|
<td><?php echo htmlspecialchars($territory['zona']); ?></td>
|
|
<td><?php echo htmlspecialchars($territory['tipologia']); ?></td>
|
|
<td><?php echo formatDate($territory['last_returned_date']); ?></td>
|
|
<td>
|
|
<span class="badge badge-danger">
|
|
<?php echo $territory['days_in_depot'] ?? 'Mai assegnato'; ?> giorni
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<a href="assignments.php?action=assign&territory_id=<?php echo $territory['id']; ?>&priority=1"
|
|
class="btn btn-sm btn-success">Assegna</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun territorio prioritario</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Territori da Riconsegnare -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Territori da Riconsegnare (<?php echo $count_to_return; ?>)</h2>
|
|
<div class="card-actions">
|
|
<?php if (count($territories_to_return) > 0): ?>
|
|
<a href="export_pdf.php?type=to_return" class="btn btn-sm btn-secondary" target="_blank">📄 Export PDF</a>
|
|
<?php if (!isset($_GET['expand_return'])): ?>
|
|
<a href="?expand_return=1" class="btn btn-sm btn-primary">Mostra Tutti</a>
|
|
<?php else: ?>
|
|
<a href="index.php" class="btn btn-sm btn-secondary">Mostra Meno</a>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<p class="help-text">Territori assegnati da più di <?php echo $warning_days_return; ?> giorni</p>
|
|
<?php if (count($territories_to_return) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Numero</th>
|
|
<th>Zona</th>
|
|
<th>Tipologia</th>
|
|
<th>Assegnato a</th>
|
|
<th>Data Assegnazione</th>
|
|
<th>Giorni Assegnato</th>
|
|
<th>Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($territories_to_return as $territory): ?>
|
|
<tr>
|
|
<td><strong><?php echo htmlspecialchars($territory['numero']); ?></strong></td>
|
|
<td><?php echo htmlspecialchars($territory['zona']); ?></td>
|
|
<td><?php echo htmlspecialchars($territory['tipologia']); ?></td>
|
|
<td><?php echo htmlspecialchars($territory['assigned_to']); ?></td>
|
|
<td><?php echo formatDate($territory['assigned_date']); ?></td>
|
|
<td>
|
|
<span class="badge badge-warning">
|
|
<?php echo $territory['days_assigned']; ?> giorni
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<a href="assignments.php?action=return&territory_id=<?php echo $territory['id']; ?>"
|
|
class="btn btn-sm btn-primary">Riconsegna</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun territorio da riconsegnare</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'footer.php'; ?>
|