415 lines
14 KiB
PHP
415 lines
14 KiB
PHP
<?php
|
|
/**
|
|
* Statistiche
|
|
* Territory Manager
|
|
*/
|
|
|
|
require_once 'config.php';
|
|
require_once 'functions.php';
|
|
require_once 'db.php';
|
|
|
|
requireLogin();
|
|
|
|
$page_title = 'Statistiche';
|
|
$db = getDB();
|
|
|
|
// Anno corrente per default
|
|
$selected_year = $_GET['year'] ?? date('Y');
|
|
$selected_month = $_GET['month'] ?? null;
|
|
|
|
// Statistiche generali
|
|
$total_territories = $db->fetchOne("SELECT COUNT(*) as total FROM territories")['total'];
|
|
$assigned_territories = $db->fetchOne("
|
|
SELECT COUNT(DISTINCT territory_id) as total
|
|
FROM assignments
|
|
WHERE returned_date IS NULL
|
|
")['total'];
|
|
$available_territories = $total_territories - $assigned_territories;
|
|
|
|
// Media percorrenza mensile (anno corrente)
|
|
$monthly_stats = $db->fetchAll("
|
|
SELECT
|
|
MONTH(assigned_date) as month,
|
|
COUNT(*) as total_assignments,
|
|
AVG(DATEDIFF(COALESCE(returned_date, CURDATE()), assigned_date)) as avg_duration
|
|
FROM assignments
|
|
WHERE YEAR(assigned_date) = ?
|
|
GROUP BY MONTH(assigned_date)
|
|
ORDER BY month
|
|
", [$selected_year]);
|
|
|
|
// Media percorrenza annuale
|
|
$yearly_stats = $db->fetchAll("
|
|
SELECT
|
|
YEAR(assigned_date) as year,
|
|
COUNT(*) as total_assignments,
|
|
AVG(DATEDIFF(COALESCE(returned_date, CURDATE()), assigned_date)) as avg_duration
|
|
FROM assignments
|
|
GROUP BY YEAR(assigned_date)
|
|
ORDER BY year DESC
|
|
");
|
|
|
|
// Territori mai assegnati
|
|
$never_assigned = $db->fetchAll("
|
|
SELECT t.*
|
|
FROM territories t
|
|
LEFT JOIN assignments a ON t.id = a.territory_id
|
|
WHERE a.id IS NULL
|
|
ORDER BY t.numero
|
|
");
|
|
|
|
// Top 10 territori più assegnati
|
|
$most_assigned = $db->fetchAll("
|
|
SELECT
|
|
t.numero,
|
|
t.zona,
|
|
t.tipologia,
|
|
COUNT(a.id) as assignment_count,
|
|
AVG(DATEDIFF(COALESCE(a.returned_date, CURDATE()), a.assigned_date)) as avg_duration
|
|
FROM territories t
|
|
INNER JOIN assignments a ON t.id = a.territory_id
|
|
GROUP BY t.id, t.numero, t.zona, t.tipologia
|
|
ORDER BY assignment_count DESC
|
|
LIMIT 10
|
|
");
|
|
|
|
// Statistiche per persona (chi ha avuto più territori)
|
|
$person_stats = $db->fetchAll("
|
|
SELECT
|
|
assigned_to,
|
|
COUNT(*) as total_assignments,
|
|
COUNT(CASE WHEN returned_date IS NULL THEN 1 END) as current_assignments,
|
|
AVG(DATEDIFF(COALESCE(returned_date, CURDATE()), assigned_date)) as avg_duration
|
|
FROM assignments
|
|
GROUP BY assigned_to
|
|
ORDER BY total_assignments DESC
|
|
LIMIT 10
|
|
");
|
|
|
|
// Anni disponibili per filtro
|
|
$available_years = $db->fetchAll("
|
|
SELECT DISTINCT YEAR(assigned_date) as year
|
|
FROM assignments
|
|
ORDER BY year DESC
|
|
");
|
|
|
|
// Statistiche per zona
|
|
$zone_stats = $db->fetchAll("
|
|
SELECT
|
|
t.zona,
|
|
COUNT(DISTINCT t.id) as total_territories,
|
|
COUNT(a.id) as total_assignments,
|
|
AVG(DATEDIFF(COALESCE(a.returned_date, CURDATE()), a.assigned_date)) as avg_duration
|
|
FROM territories t
|
|
LEFT JOIN assignments a ON t.id = a.territory_id
|
|
GROUP BY t.zona
|
|
ORDER BY total_territories DESC
|
|
");
|
|
|
|
// Statistiche tempo reale (ultimi 7 giorni)
|
|
$recent_activity = $db->fetchAll("
|
|
SELECT
|
|
DATE(assigned_date) as date,
|
|
COUNT(*) as assignments
|
|
FROM assignments
|
|
WHERE assigned_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
|
|
GROUP BY DATE(assigned_date)
|
|
ORDER BY date DESC
|
|
");
|
|
|
|
$recent_returns = $db->fetchAll("
|
|
SELECT
|
|
DATE(returned_date) as date,
|
|
COUNT(*) as returns
|
|
FROM assignments
|
|
WHERE returned_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
|
|
GROUP BY DATE(returned_date)
|
|
ORDER BY date DESC
|
|
");
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<div class="page-header">
|
|
<h1>Statistiche</h1>
|
|
<div class="header-actions">
|
|
<form method="GET" style="display: inline-flex; gap: 10px;">
|
|
<select name="year" class="form-control" onchange="this.form.submit()">
|
|
<?php foreach ($available_years as $y): ?>
|
|
<option value="<?php echo $y['year']; ?>" <?php echo $selected_year == $y['year'] ? 'selected' : ''; ?>>
|
|
<?php echo $y['year']; ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Statistiche Generali -->
|
|
<div class="stats-grid">
|
|
<div class="stat-card">
|
|
<div class="stat-value"><?php echo $total_territories; ?></div>
|
|
<div class="stat-label">Totale Territori</div>
|
|
</div>
|
|
<div class="stat-card">
|
|
<div class="stat-value"><?php echo $assigned_territories; ?></div>
|
|
<div class="stat-label">Territori Assegnati</div>
|
|
</div>
|
|
<div class="stat-card">
|
|
<div class="stat-value"><?php echo $available_territories; ?></div>
|
|
<div class="stat-label">Territori Disponibili</div>
|
|
</div>
|
|
<div class="stat-card">
|
|
<div class="stat-value"><?php echo count($never_assigned); ?></div>
|
|
<div class="stat-label">Mai Assegnati</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Attività Recente (7 giorni) -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Attività Ultimi 7 Giorni</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="activity-grid">
|
|
<div>
|
|
<h3>Assegnazioni</h3>
|
|
<?php if (count($recent_activity) > 0): ?>
|
|
<table class="table table-sm">
|
|
<tbody>
|
|
<?php foreach ($recent_activity as $activity): ?>
|
|
<tr>
|
|
<td><?php echo formatDate($activity['date']); ?></td>
|
|
<td><strong><?php echo $activity['assignments']; ?></strong> assegnazioni</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessuna assegnazione recente</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div>
|
|
<h3>Riconsegne</h3>
|
|
<?php if (count($recent_returns) > 0): ?>
|
|
<table class="table table-sm">
|
|
<tbody>
|
|
<?php foreach ($recent_returns as $returns): ?>
|
|
<tr>
|
|
<td><?php echo formatDate($returns['date']); ?></td>
|
|
<td><strong><?php echo $returns['returns']; ?></strong> riconsegne</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessuna riconsegna recente</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Media Percorrenza Mensile -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Media Percorrenza Mensile - <?php echo $selected_year; ?></h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (count($monthly_stats) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Mese</th>
|
|
<th>Totale Assegnazioni</th>
|
|
<th>Durata Media</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
$month_names = ['', 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno',
|
|
'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
|
|
foreach ($monthly_stats as $stat):
|
|
?>
|
|
<tr>
|
|
<td><?php echo $month_names[$stat['month']]; ?></td>
|
|
<td><?php echo $stat['total_assignments']; ?></td>
|
|
<td><?php echo round($stat['avg_duration']); ?> giorni</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun dato per l'anno selezionato</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Media Percorrenza Annuale -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Media Percorrenza Annuale</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (count($yearly_stats) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Anno</th>
|
|
<th>Totale Assegnazioni</th>
|
|
<th>Durata Media</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($yearly_stats as $stat): ?>
|
|
<tr>
|
|
<td><strong><?php echo $stat['year']; ?></strong></td>
|
|
<td><?php echo $stat['total_assignments']; ?></td>
|
|
<td><?php echo round($stat['avg_duration']); ?> giorni</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun dato disponibile</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Statistiche per Zona -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Statistiche per Zona</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (count($zone_stats) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Zona</th>
|
|
<th>Totale Territori</th>
|
|
<th>Totale Assegnazioni</th>
|
|
<th>Durata Media</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($zone_stats as $stat): ?>
|
|
<tr>
|
|
<td><strong><?php echo htmlspecialchars($stat['zona']); ?></strong></td>
|
|
<td><?php echo $stat['total_territories']; ?></td>
|
|
<td><?php echo $stat['total_assignments']; ?></td>
|
|
<td><?php echo $stat['avg_duration'] ? round($stat['avg_duration']) . ' giorni' : '-'; ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun dato disponibile</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Top 10 Territori Più Assegnati -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Top 10 Territori Più Assegnati</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (count($most_assigned) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Territorio</th>
|
|
<th>Zona</th>
|
|
<th>Tipologia</th>
|
|
<th>N° Assegnazioni</th>
|
|
<th>Durata Media</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($most_assigned 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 $territory['assignment_count']; ?></td>
|
|
<td><?php echo round($territory['avg_duration']); ?> giorni</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun dato disponibile</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Statistiche per Persona -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Top 10 Persone per Assegnazioni</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (count($person_stats) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Nome</th>
|
|
<th>Totale Assegnazioni</th>
|
|
<th>Assegnazioni Correnti</th>
|
|
<th>Durata Media</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($person_stats as $person): ?>
|
|
<tr>
|
|
<td><strong><?php echo htmlspecialchars($person['assigned_to']); ?></strong></td>
|
|
<td><?php echo $person['total_assignments']; ?></td>
|
|
<td><?php echo $person['current_assignments']; ?></td>
|
|
<td><?php echo round($person['avg_duration']); ?> giorni</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">Nessun dato disponibile</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Territori Mai Assegnati -->
|
|
<?php if (count($never_assigned) > 0): ?>
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Territori Mai Assegnati (<?php echo count($never_assigned); ?>)</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Numero</th>
|
|
<th>Zona</th>
|
|
<th>Tipologia</th>
|
|
<th>Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($never_assigned 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>
|
|
<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>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php include 'footer.php'; ?>
|