-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemprunts.php
executable file
·168 lines (152 loc) · 6.46 KB
/
emprunts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
require_once 'includes/db.php';
define('DUREE_EMPRUNT', 14);
if(isset($_POST['ajouter_emprunt'])) {
$id_livre = $_POST['id_livre'];
$id_abonne = $_POST['id_abonne'];
$date_emprunt = date('Y-m-d');
$date_retour_prevue = date('Y-m-d', strtotime("+".DUREE_EMPRUNT." days"));
try {
$pdo->beginTransaction();
$stmt = $pdo->prepare("SELECT disponible FROM livres WHERE id = ?");
$stmt->execute([$id_livre]);
$livre = $stmt->fetch();
if(!$livre['disponible']) {
throw new Exception("Ce livre n'est pas disponible");
}
$sql = "INSERT INTO emprunts (id_livre, id_abonne, date_emprunt, date_retour_prevue)
VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id_livre, $id_abonne, $date_emprunt, $date_retour_prevue]);
$pdo->commit();
$message = "Emprunt enregistré avec succès";
} catch(Exception $e) {
$pdo->rollBack();
$error = $e->getMessage();
}
}
// Traitement du retour d'un livre
if(isset($_POST['retourner_livre'])) {
$id_emprunt = $_POST['id_emprunt'];
$date_retour = date('Y-m-d');
$sql = "UPDATE emprunts SET date_retour = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$date_retour, $id_emprunt]);
}
$query = "SELECT e.*,
l.titre as livre_titre,
CONCAT(a.nom, ' ', a.prenom) as abonne_nom,
DATEDIFF(e.date_retour_prevue, CURRENT_DATE) as jours_restants
FROM emprunts e
JOIN livres l ON e.id_livre = l.id
JOIN abonnes a ON e.id_abonne = a.id
WHERE e.date_retour IS NULL
ORDER BY e.date_retour_prevue ASC";
$emprunts = $pdo->query($query)->fetchAll();
$livres_dispo = $pdo->query("SELECT * FROM livres WHERE disponible = 1")->fetchAll();
$abonnes = $pdo->query("SELECT * FROM abonnes")->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Gestion des Emprunts</title>
<link rel="stylesheet" href="styles/cyberpunk.css">
<link rel="stylesheet" href="styles/emprunt.css">
</head>
<body>
<a href="index.php" class="cyber-button"> 🏠 Retour à l'accueil</a>
<?php include 'includes/nav.php'; ?>
<div class="container">
<?php if(isset($message)): ?>
<div class="cyber-card success-message">
<?= htmlspecialchars($message) ?>
</div>
<?php endif; ?>
<?php if(isset($error)): ?>
<div class="cyber-card error-message">
<?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<div class="cyber-card">
<h2>Nouvel Emprunt</h2>
<form method="POST" class="cyber-form">
<div class="form-group">
<select name="id_livre" class="cyber-input" required>
<option value="">Sélectionner un livre</option>
<?php foreach($livres_dispo as $livre): ?>
<option value="<?= $livre['id'] ?>">
<?= htmlspecialchars($livre['titre']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<select name="id_abonne" class="cyber-input" required>
<option value="">Sélectionner un abonné</option>
<?php foreach($abonnes as $abonne): ?>
<option value="<?= $abonne['id'] ?>">
<?= htmlspecialchars($abonne['nom'] . ' ' . $abonne['prenom']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" name="ajouter_emprunt" class="cyber-btn">Enregistrer l'emprunt</button>
</form>
</div>
<div class="cyber-card">
<h2>Emprunts en cours</h2>
<table class="cyber-table">
<thead>
<tr>
<th>Livre</th>
<th>Abonné</th>
<th>Date d'emprunt</th>
<th>Date de retour prévue</th>
<th>Jours restants</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($emprunts as $emprunt): ?>
<tr class="<?= $emprunt['jours_restants'] < 0 ? 'retard' : '' ?>">
<td><?= htmlspecialchars($emprunt['livre_titre']) ?></td>
<td><?= htmlspecialchars($emprunt['abonne_nom']) ?></td>
<td><?= htmlspecialchars($emprunt['date_emprunt']) ?></td>
<td><?= htmlspecialchars($emprunt['date_retour_prevue']) ?></td>
<td>
<?php if($emprunt['jours_restants'] < 0): ?>
<span class="retard-badge">
Retard de <?= abs($emprunt['jours_restants']) ?> jours
</span>
<?php else: ?>
<?= $emprunt['jours_restants'] ?> jours
<?php endif; ?>
</td>
<td>
<form method="POST" style="display: inline;">
<input type="hidden" name="id_emprunt" value="<?= $emprunt['id'] ?>">
<button type="submit" name="retourner_livre" class="cyber-btn">
Retourner
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const messages = document.querySelectorAll('.success-message, .error-message');
messages.forEach(msg => {
setTimeout(() => {
msg.style.opacity = '0';
setTimeout(() => msg.remove(), 500);
}, 3000);
});
});
</script>
</body>
</html>