467 lines
19 KiB
PHP
467 lines
19 KiB
PHP
<?php
|
|
/**
|
|
* Gestione Assegnazioni
|
|
* Territory Manager
|
|
*/
|
|
|
|
require_once 'config.php';
|
|
require_once 'functions.php';
|
|
require_once 'db.php';
|
|
|
|
requireLogin();
|
|
|
|
$page_title = 'Gestione Assegnazioni';
|
|
$db = getDB();
|
|
|
|
$action = $_GET['action'] ?? 'list';
|
|
$territory_id = $_GET['territory_id'] ?? null;
|
|
|
|
// Gestione delle azioni POST
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['action'])) {
|
|
switch ($_POST['action']) {
|
|
case 'assign':
|
|
$territory_id = (int)$_POST['territory_id'];
|
|
$assigned_to = sanitize($_POST['assigned_to']);
|
|
$assigned_date = $_POST['assigned_date'];
|
|
$is_priority = isset($_POST['is_priority']) ? 1 : 0;
|
|
$note = sanitize($_POST['note'] ?? '');
|
|
|
|
// Verifica che il territorio non sia già assegnato
|
|
$existing = $db->fetchOne(
|
|
"SELECT id FROM assignments WHERE territory_id = ? AND returned_date IS NULL",
|
|
[$territory_id]
|
|
);
|
|
|
|
if ($existing) {
|
|
setFlashMessage('Questo territorio è già assegnato', 'error');
|
|
header('Location: assignments.php');
|
|
exit;
|
|
}
|
|
|
|
// Genera link temporaneo
|
|
$link_token = bin2hex(random_bytes(32));
|
|
$link_expiry_days = (int)$db->getConfig('link_expiry_days', 7);
|
|
$link_expires_at = date('Y-m-d H:i:s', strtotime("+$link_expiry_days days"));
|
|
|
|
$result = $db->query(
|
|
"INSERT INTO assignments (territory_id, assigned_to, assigned_date, is_priority, note, link_token, link_expires_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
[$territory_id, $assigned_to, $assigned_date, $is_priority, $note, $link_token, $link_expires_at]
|
|
);
|
|
|
|
if ($result) {
|
|
$assignment_id = $db->getConnection()->lastInsertId();
|
|
$territory = $db->fetchOne("SELECT numero, zona FROM territories WHERE id = ?", [$territory_id]);
|
|
logActivity('assign', "Assegnato territorio {$territory['numero']} - {$territory['zona']} a $assigned_to", 'assignment', $assignment_id);
|
|
setFlashMessage('Territorio assegnato con successo', 'success');
|
|
header("Location: assignments.php?action=view&id=$assignment_id");
|
|
} else {
|
|
setFlashMessage('Errore durante l\'assegnazione', 'error');
|
|
header('Location: assignments.php');
|
|
}
|
|
exit;
|
|
break;
|
|
|
|
case 'return':
|
|
$territory_id = (int)$_POST['territory_id'];
|
|
$returned_date = $_POST['returned_date'];
|
|
|
|
// Recupera info assegnazione
|
|
$assignment = $db->fetchOne(
|
|
"SELECT a.id, a.assigned_to, t.numero, t.zona
|
|
FROM assignments a
|
|
JOIN territories t ON a.territory_id = t.id
|
|
WHERE a.territory_id = ? AND a.returned_date IS NULL",
|
|
[$territory_id]
|
|
);
|
|
|
|
$result = $db->query(
|
|
"UPDATE assignments SET returned_date = ? WHERE territory_id = ? AND returned_date IS NULL",
|
|
[$returned_date, $territory_id]
|
|
);
|
|
|
|
if ($result && $assignment) {
|
|
logActivity('return', "Riconsegnato territorio {$assignment['numero']} - {$assignment['zona']} da {$assignment['assigned_to']}", 'assignment', $assignment['id']);
|
|
setFlashMessage('Territorio riconsegnato con successo', 'success');
|
|
} else {
|
|
setFlashMessage('Errore durante la riconsegna', 'error');
|
|
}
|
|
|
|
header('Location: assignments.php');
|
|
exit;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Lista assegnazioni
|
|
if ($action === 'list') {
|
|
$filter = $_GET['filter'] ?? 'current';
|
|
|
|
if ($filter === 'current') {
|
|
$assignments = $db->fetchAll("
|
|
SELECT
|
|
a.*,
|
|
t.numero,
|
|
t.zona,
|
|
t.tipologia,
|
|
DATEDIFF(CURDATE(), a.assigned_date) as days_assigned
|
|
FROM assignments a
|
|
INNER JOIN territories t ON a.territory_id = t.id
|
|
WHERE a.returned_date IS NULL
|
|
ORDER BY a.assigned_date ASC
|
|
");
|
|
} else {
|
|
$assignments = $db->fetchAll("
|
|
SELECT
|
|
a.*,
|
|
t.numero,
|
|
t.zona,
|
|
t.tipologia,
|
|
DATEDIFF(a.returned_date, a.assigned_date) as days_assigned
|
|
FROM assignments a
|
|
INNER JOIN territories t ON a.territory_id = t.id
|
|
WHERE a.returned_date IS NOT NULL
|
|
ORDER BY a.returned_date DESC
|
|
LIMIT 100
|
|
");
|
|
}
|
|
}
|
|
|
|
// Pagina per nuova assegnazione
|
|
if ($action === 'assign') {
|
|
if ($territory_id) {
|
|
$territory = $db->fetchOne("SELECT * FROM territories WHERE id = ?", [$territory_id]);
|
|
if (!$territory) {
|
|
setFlashMessage('Territorio non trovato', 'error');
|
|
header('Location: assignments.php');
|
|
exit;
|
|
}
|
|
} else {
|
|
// Carica tutti i territori disponibili
|
|
$available_territories = $db->fetchAll("
|
|
SELECT t.*
|
|
FROM territories t
|
|
WHERE t.id NOT IN (
|
|
SELECT territory_id FROM assignments WHERE returned_date IS NULL
|
|
)
|
|
ORDER BY t.numero ASC
|
|
");
|
|
}
|
|
|
|
$is_priority = isset($_GET['priority']) ? 1 : 0;
|
|
}
|
|
|
|
// Pagina per riconsegna
|
|
if ($action === 'return' && $territory_id) {
|
|
$assignment = $db->fetchOne("
|
|
SELECT a.*, t.numero, t.zona, t.tipologia
|
|
FROM assignments a
|
|
INNER JOIN territories t ON a.territory_id = t.id
|
|
WHERE a.territory_id = ? AND a.returned_date IS NULL
|
|
", [$territory_id]);
|
|
|
|
if (!$assignment) {
|
|
setFlashMessage('Assegnazione non trovata', 'error');
|
|
header('Location: assignments.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Visualizzazione dettaglio assegnazione
|
|
if ($action === 'view') {
|
|
$assignment_id = $_GET['id'] ?? null;
|
|
if ($assignment_id) {
|
|
$assignment = $db->fetchOne("
|
|
SELECT a.*, t.numero, t.zona, t.tipologia, t.image_path
|
|
FROM assignments a
|
|
INNER JOIN territories t ON a.territory_id = t.id
|
|
WHERE a.id = ?
|
|
", [$assignment_id]);
|
|
|
|
if (!$assignment) {
|
|
setFlashMessage('Assegnazione non trovata', 'error');
|
|
header('Location: assignments.php');
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<?php if ($action === 'list'): ?>
|
|
<div class="page-header">
|
|
<h1>Gestione Assegnazioni</h1>
|
|
<a href="?action=assign" class="btn btn-primary">+ Nuova Assegnazione</a>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<div class="tabs">
|
|
<a href="?filter=current" class="tab <?php echo $filter === 'current' ? 'active' : ''; ?>">
|
|
Assegnazioni Correnti
|
|
</a>
|
|
<a href="?filter=history" class="tab <?php echo $filter === 'history' ? 'active' : ''; ?>">
|
|
Storico
|
|
</a>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (count($assignments) > 0): ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Territorio</th>
|
|
<th>Zona</th>
|
|
<th>Tipologia</th>
|
|
<th>Assegnato a</th>
|
|
<th>Data Assegnazione</th>
|
|
<?php if ($filter === 'current'): ?>
|
|
<th>Giorni</th>
|
|
<th>Azioni</th>
|
|
<?php else: ?>
|
|
<th>Data Restituzione</th>
|
|
<th>Durata</th>
|
|
<?php endif; ?>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($assignments as $a): ?>
|
|
<tr>
|
|
<td>
|
|
<strong><?php echo htmlspecialchars($a['numero']); ?></strong>
|
|
<?php if ($a['is_priority']): ?>
|
|
<span class="badge badge-danger">Priorità</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><?php echo htmlspecialchars($a['zona']); ?></td>
|
|
<td><?php echo htmlspecialchars($a['tipologia']); ?></td>
|
|
<td><?php echo htmlspecialchars($a['assigned_to']); ?></td>
|
|
<td><?php echo formatDate($a['assigned_date']); ?></td>
|
|
<?php if ($filter === 'current'): ?>
|
|
<td>
|
|
<span class="badge badge-info"><?php echo $a['days_assigned']; ?> giorni</span>
|
|
</td>
|
|
<td class="actions">
|
|
<a href="?action=view&id=<?php echo $a['id']; ?>" class="btn btn-sm btn-secondary">Dettagli</a>
|
|
<a href="?action=return&territory_id=<?php echo $a['territory_id']; ?>" class="btn btn-sm btn-primary">Riconsegna</a>
|
|
</td>
|
|
<?php else: ?>
|
|
<td><?php echo formatDate($a['returned_date']); ?></td>
|
|
<td><?php echo $a['days_assigned']; ?> giorni</td>
|
|
<?php endif; ?>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php else: ?>
|
|
<p class="empty-state">
|
|
<?php echo $filter === 'current' ? 'Nessuna assegnazione corrente' : 'Nessuno storico disponibile'; ?>
|
|
</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php elseif ($action === 'assign'): ?>
|
|
<div class="page-header">
|
|
<h1>Nuova Assegnazione</h1>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<input type="hidden" name="action" value="assign">
|
|
|
|
<div class="form-group">
|
|
<label for="territory_id">Territorio *</label>
|
|
<?php if ($territory_id): ?>
|
|
<input type="hidden" name="territory_id" value="<?php echo $territory_id; ?>">
|
|
<input type="text" class="form-control" readonly
|
|
value="<?php echo htmlspecialchars($territory['numero'] . ' - ' . $territory['zona']); ?>">
|
|
<?php else: ?>
|
|
<select id="territory_id" name="territory_id" required class="form-control">
|
|
<option value="">Seleziona un territorio</option>
|
|
<?php foreach ($available_territories as $t): ?>
|
|
<option value="<?php echo $t['id']; ?>">
|
|
<?php echo htmlspecialchars($t['numero'] . ' - ' . $t['zona'] . ' (' . $t['tipologia'] . ')'); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="assigned_to">Assegnato a *</label>
|
|
<input type="text" id="assigned_to" name="assigned_to" required class="form-control"
|
|
placeholder="Nome della persona">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="assigned_date">Data Assegnazione *</label>
|
|
<input type="date" id="assigned_date" name="assigned_date" required
|
|
value="<?php echo date('Y-m-d'); ?>" class="form-control">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="checkbox-label">
|
|
<input type="checkbox" name="is_priority" value="1" <?php echo $is_priority ? 'checked' : ''; ?>>
|
|
Assegnazione Prioritaria
|
|
</label>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="note">Note</label>
|
|
<textarea id="note" name="note" rows="3" class="form-control"></textarea>
|
|
</div>
|
|
|
|
<div class="form-actions">
|
|
<button type="submit" class="btn btn-primary">Assegna Territorio</button>
|
|
<a href="assignments.php" class="btn btn-secondary">Annulla</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php elseif ($action === 'return'): ?>
|
|
<div class="page-header">
|
|
<h1>Riconsegna Territorio</h1>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<div class="detail-grid">
|
|
<div class="detail-item">
|
|
<strong>Territorio:</strong> <?php echo htmlspecialchars($assignment['numero']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Zona:</strong> <?php echo htmlspecialchars($assignment['zona']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Assegnato a:</strong> <?php echo htmlspecialchars($assignment['assigned_to']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Data Assegnazione:</strong> <?php echo formatDate($assignment['assigned_date']); ?>
|
|
</div>
|
|
</div>
|
|
|
|
<form method="POST" style="margin-top: 20px;">
|
|
<input type="hidden" name="action" value="return">
|
|
<input type="hidden" name="territory_id" value="<?php echo $territory_id; ?>">
|
|
|
|
<div class="form-group">
|
|
<label for="returned_date">Data Restituzione *</label>
|
|
<input type="date" id="returned_date" name="returned_date" required
|
|
value="<?php echo date('Y-m-d'); ?>" class="form-control">
|
|
</div>
|
|
|
|
<div class="form-actions">
|
|
<button type="submit" class="btn btn-primary">Conferma Riconsegna</button>
|
|
<a href="assignments.php" class="btn btn-secondary">Annulla</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php elseif ($action === 'view'): ?>
|
|
<?php
|
|
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
|
|
"://" . $_SERVER['HTTP_NAME'];
|
|
$share_url = $base_url . "/view_territory.php?token=" . $assignment['link_token'];
|
|
$is_link_valid = strtotime($assignment['link_expires_at']) > time();
|
|
?>
|
|
|
|
<div class="page-header">
|
|
<h1>Dettagli Assegnazione</h1>
|
|
<a href="assignments.php" class="btn btn-secondary">Torna alla Lista</a>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Informazioni Territorio</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="detail-grid">
|
|
<div class="detail-item">
|
|
<strong>Numero:</strong> <?php echo htmlspecialchars($assignment['numero']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Zona:</strong> <?php echo htmlspecialchars($assignment['zona']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Tipologia:</strong> <?php echo htmlspecialchars($assignment['tipologia']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Assegnato a:</strong> <?php echo htmlspecialchars($assignment['assigned_to']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Data Assegnazione:</strong> <?php echo formatDate($assignment['assigned_date']); ?>
|
|
</div>
|
|
<div class="detail-item">
|
|
<strong>Priorità:</strong>
|
|
<?php if ($assignment['is_priority']): ?>
|
|
<span class="badge badge-danger">Prioritaria</span>
|
|
<?php else: ?>
|
|
Normale
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($assignment['note']): ?>
|
|
<div class="detail-item" style="margin-top: 15px;">
|
|
<strong>Note:</strong>
|
|
<p><?php echo nl2br(htmlspecialchars($assignment['note'])); ?></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Link Condivisione Territorio</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<p class="help-text">
|
|
Questo link permette di visualizzare il territorio senza accedere al sistema.
|
|
<?php if ($is_link_valid): ?>
|
|
Il link è valido fino al <strong><?php echo formatDateTime($assignment['link_expires_at']); ?></strong>
|
|
<?php else: ?>
|
|
<span class="badge badge-danger">Il link è scaduto</span>
|
|
<?php endif; ?>
|
|
</p>
|
|
|
|
<div class="link-box">
|
|
<input type="text" id="share_link" value="<?php echo htmlspecialchars($share_url); ?>"
|
|
class="form-control" readonly>
|
|
<button onclick="copyLink()" class="btn btn-primary">Copia Link</button>
|
|
</div>
|
|
|
|
<?php if (!$is_link_valid): ?>
|
|
<p class="alert alert-warning" style="margin-top: 15px;">
|
|
Per generare un nuovo link, è necessario creare una nuova assegnazione.
|
|
</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (!$assignment['returned_date']): ?>
|
|
<div class="form-actions">
|
|
<a href="?action=return&territory_id=<?php echo $assignment['territory_id']; ?>"
|
|
class="btn btn-primary">Riconsegna Territorio</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php endif; ?>
|
|
|
|
<script>
|
|
function copyLink() {
|
|
const linkInput = document.getElementById('share_link');
|
|
linkInput.select();
|
|
document.execCommand('copy');
|
|
alert('Link copiato negli appunti!');
|
|
}
|
|
</script>
|
|
|
|
<?php include 'footer.php'; ?>
|