227 lines
11 KiB
PHP

<?php
include('include/headscript.php');
if (!isset($iduserlogin)) die("Errore: utente non loggato.");
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// Recupera la scuola
$stmt = $pdo->prepare("SELECT id, name FROM schools WHERE owner_id = ?");
$stmt->execute([$iduserlogin]);
$school = $stmt->fetch();
if (!$school) die("Scuola non trovata.");
$school_id = $school['id'];
// Recupera tutti gli ordini con tutti i dati necessari
$stmt = $pdo->prepare("
SELECT
o.id,
o.order_number,
o.created_at,
o.price,
o.status,
o.payment_method,
o.total_entries,
o.available_entries,
o.expiration_date,
u.first_name,
u.last_name,
u.email,
p.name AS product_name,
pv.name AS variation_name,
c.name AS class_name,
ct.level,
ct.day_of_week
FROM orders o
JOIN auth_users u ON o.user_id = u.id
JOIN products p ON o.product_id = p.id
LEFT JOIN product_variations pv ON o.variation_id = pv.id
LEFT JOIN classes c ON o.class_id = c.id
LEFT JOIN class_types ct ON o.class_type_id = ct.id
WHERE o.school_id = ?
ORDER BY o.created_at DESC
");
$stmt->execute([$school_id]);
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ordini - <?php echo htmlspecialchars($school['name']); ?></title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<!-- DataTables CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.7/css/dataTables.bootstrap5.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.5.0/css/responsive.bootstrap5.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.4.2/css/buttons.bootstrap5.min.css">
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="container-fluid px-1">
<div class="card radius-15 shadow-lg mb-4">
<div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
<h4 class="mb-0">
Gestione Ordini
<span class="badge bg-light text-dark ms-2"><?php echo count($orders); ?></span>
</h4>
<div>
<button class="btn btn-light btn-sm" onclick="table.buttons().export()">
Esporta
</button>
</div>
</div>
</div>
<div class="card radius-15 shadow">
<div class="card-body p-0">
<div class="table-responsive">
<table id="ordersTable" class="table table-striped table-hover" style="width:100%">
<thead class="table-dark">
<tr>
<th>Data</th>
<th>Ordine #</th>
<th>Cliente</th>
<th>Email</th>
<th>Prodotto</th>
<th>Variazione</th>
<th>Prezzo</th>
<th>Ingressi</th>
<th>Scadenza</th>
<th>Stato</th>
<th>Pagamento</th>
<th>Lezione</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $o): ?>
<tr>
<td data-order="<?php echo $o['created_at']; ?>">
<strong><?php echo date('d/m/Y', strtotime($o['created_at'])); ?></strong><br>
<small class="text-muted"><?php echo date('H:i', strtotime($o['created_at'])); ?></small>
</td>
<td><strong>#<?php echo $o['order_number']; ?></strong></td>
<td><?php echo htmlspecialchars($o['first_name'] . ' ' . $o['last_name']); ?></td>
<td>
<a href="mailto:<?php echo htmlspecialchars($o['email']); ?>">
<?php echo htmlspecialchars($o['email']); ?>
</a>
</td>
<td><?php echo htmlspecialchars($o['product_name']); ?></td>
<td><?php echo htmlspecialchars($o['variation_name'] ?: '-'); ?></td>
<td><strong>€<?php echo number_format($o['price'], 2); ?></strong></td>
<td>
<?php echo $o['total_entries'] ? $o['available_entries'] . '/' . $o['total_entries'] : 'Illimitati'; ?>
</td>
<td>
<?php if ($o['expiration_date']): ?>
<span class="text-danger fw-bold"><?php echo date('d/m/Y', strtotime($o['expiration_date'])); ?></span>
<?php else: ?>
<em>Nessuna</em>
<?php endif; ?>
</td>
<td>
<span class="badge <?php echo $o['status'] == 'completed' ? 'bg-success' : ($o['status'] == 'pending' ? 'bg-warning' : 'bg-secondary'); ?>">
<?php echo ucfirst($o['status']); ?>
</span>
</td>
<td>
<span class="badge bg-info"><?php echo strtoupper($o['payment_method']); ?></span>
</td>
<td>
<?php if ($o['class_name']): ?>
<small>
<strong><?php echo htmlspecialchars($o['class_name']); ?></strong><br>
<?php echo ucfirst($o['level'] ?? ''); ?>
(<?php echo ucfirst($o['day_of_week'] ?? 'qualsiasi'); ?>)
</small>
<?php else: ?>
<em>Tutte le classi</em>
<?php endif; ?>
</td>
<td>
<div class="btn-group" role="group">
<button type="button" class="btn btn-sm btn-outline-primary">Dettaglio</button>
<button type="button" class="btn btn-sm btn-outline-success">Prenotazioni</button>
<button type="button" class="btn btn-sm btn-outline-warning">Email</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<!-- DataTables + Plugin -->
<script src="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.7/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.5.0/js/responsive.bootstrap5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.bootstrap5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.html5.min.js"></script>
<script>
$(document).ready(function() {
$('#ordersTable').DataTable({
language: {
url: "//cdn.datatables.net/plug-ins/1.10.25/i18n/Italian.json"
},
pageLength: 25,
lengthMenu: [10, 25, 50, 100],
order: [
[0, 'desc']
],
responsive: true,
dom: 'Bfrtip',
buttons: [{
extend: 'excelHtml5',
text: 'Esporta Excel',
className: 'btn btn-success btn-sm'
},
{
extend: 'csvHtml5',
text: 'Esporta CSV',
className: 'btn btn-info btn-sm'
}
],
columnDefs: [{
targets: '_all',
className: 'text-center'
},
{
targets: 12,
orderable: false
}
]
});
});
</script>
</body>
</html>