-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistoriqueCommande.php
211 lines (192 loc) · 6.06 KB
/
historiqueCommande.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
//------------------------------------------------
// Page affichant l'historique des commandes du client
// et permettant l'annulation d'une commande si le délais n'est pas dépassé.
//------------------------------------------------
require_once(realpath(__DIR__).'/php/biblio/foncCommunes.php');
$js = array();
$js[] = 'rafraichissement.js';
$css = array();
$css[] = 'historiqueCommande.css';
$titre = 'LogiKek - Historique de commandes';
$description = 'Site de vente de système d\'exploitation';
$motCle = 'OS, Linux, Windows, BSD, Apple, RHEL, Vente, logiciel';
//------------------------------------------------
//Fonction pour afficher le temps restant avant de ne plus
//pouvoir annuler une commande.
//Prend un paramètre une string en format dateTime.
//------------------------------------------------
function tempsRestant($dateAchatString)
{
$maintenant = new DateTime();
$dateAchat = new DateTime($dateAchatString);
//Crée un objet date représentant la date limite pour une annulation.
//P pour période. 2 jours
$dateAnnulationMax = $dateAchat->add(new DateInterval('P2D'));
//Le temps restant.
$interval = $dateAnnulationMax->diff($maintenant);
//Logique de l'affichage du format.
if ($interval->d >= 1)
return $interval->format('%d jour(s) et %h heure(s)');
if ($interval->h >= 1)
return $interval->format('%h heure(s) et %i minute(s)');
if ($interval->i >= 1)
return $interval->format('%i minute(s) et %s seconde(s)');
if ($interval->i < 1)
return $interval->format('%s seconde(s)');
return "erreur";
}
//------------------------------------------------
// Fonction pour s'avoir si la date limite d'annulation est dépassé.
// Retourne vrai ou faux.
//------------------------------------------------
function depaseDateLimite($dateAchatString)
{
$maintenant = new DateTime();
$dateAchat = new DateTime($dateAchatString);
//Crée un objet date représentant la date limite pour une annulation.
//P pour période. 2 jours
$dateAnnulationMax = $dateAchat->add(new DateInterval('P2D'));
return $maintenant > $dateAnnulationMax;
}
if (!isset($_SESSION['authentification']))
{
//Redirection à la page d'authentification.
header("location:./authentification.php?prov=histCommande");
exit();
}
else
{
global $maBD;
$commandes = array();
try
{
//Sélectionne l'historique de commandes en BD.
$resultat = $maBD->selectCommandeDetails($_SESSION['authentification']);
}
catch (Exception $e)
{
exit();
}
//Si il y a au moins une commande dans l'historique.
if (isset($resultat))
{
//Pour chacun des commandes
foreach ($resultat as $value)
{
//Crée un objet Commande et l'enregistre dans le tableau de commandes.
$commandes[] = new Commande($value);
}
//Si le paramètre GET 'annule' est set
if (isset($_GET['annule']))
{
//Récupère la valeur de l'index de la commande que nous voulons annuler.
$numCommande = $_GET['annule'];
//Vérifie le type et les bornes
if (is_numeric($numCommande) && $numCommande >= 0 && $numCommande <= count($commandes) -1)
{
try
{
//Annule la commande.
$resultat = $maBD->annulationCommande($commandes[$numCommande]);
}
catch (Exception $e)
{
exit();
}
//Redirection vers le script pour enlever le paramètre GET et actualiser l'affichage de l'historique.
header("location:./historiqueCommande.php");
exit();
}
}
}
//Pour éviter un bug du validateur HTML5.
//Colspan est à 5 si au moins une commande existe, sinon 1.
//Le bug étant qu'un faux possitif est donnée pour une table avec une colonne d'un colspan d'une longueur
//Plus grande que le nombre de colonnes de la table.
$colspan = count($commandes) >= 1 ? 5 : 1;
require_once("./header.php");
require_once("./sectionGauche.php");
}
?>
<!-- Début section central col-md-9 -->
<div class="col-md-7" id="centre">
<!-- Début de l'historique -->
<div class="row">
<table>
<tr>
<!-- Total du nombres de commandes. -->
<td colspan="<?php echo $colspan ?>">
<label>Vous avez passé un total de <?php echo count($commandes); ?> commandes</label>
</td>
</tr>
<tr>
<!-- Ligne de séparation -->
<td colspan="<?php echo $colspan ?>">
<hr class="noir">
</td>
</tr>
<?php foreach ($commandes as $key => $value): ?>
<!-- Commande -->
<tr>
<td>
<label>Commande : <?php echo $value->getNumCommande(); ?></label>
</td>
<!-- Date et montant de la commande -->
<td class="petit">
<label>
Date : <?php echo $value->getDateCommande(); ?>
<br>
Montant : <?php echo number_format(calculTaxeFrais($value->Total()) , 2); ?>$
</label>
</td>
<?php if (!depaseDateLimite($value->getdateCommande())): //S'il reste encore du temps pour l'annulation?>
<!-- Lien d'annulation de la commande -->
<td>
<a href="./historiqueCommande.php?annule=<?php echo $key ?>">Annuler</a>
</td>
<td> </td>
<!-- Compte à rebours du temps restant pour l'annulation de la commande. -->
<td>
<?php echo 'Encore ', tempsRestant($value->getdateCommande()), ' pour annuler.' ?>
</td>
<?php else: ?>
<td colspan="3"> </td>
<?php endif; ?>
</tr>
<tr>
<td>
<label>Produit</label>
</td>
<td>
<label>Quantité</label>
</td>
<td colspan="3">
<label>Prix</label>
</td>
</tr>
<?php foreach ($value->getTabAchats() as $keyAchat => $valueAchat): ?>
<!-- Achat -->
<tr>
<td><?php echo $valueAchat->getNom(); ?></td>
<td><?php echo $valueAchat->getNombre(); ?></td>
<td colspan="3"><?php echo number_format($valueAchat->getPrix(), 2); ?>$</td>
<?php endforeach; ?>
</tr>
<?php if( count($commandes) - 1 != $key): //Si nous ne sommes pas au dernier Achat de la commande?>
<!-- Ligne de séparation -->
<tr>
<td colspan="5">
<hr class="noir">
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
</div>
</div> <!-- Fin section central col-md-9 -->
<div class="col-md-1"> <!-- Début Section de droite central -->
</div>
<!-- Fin section de droite central -->
</div>
<?php require_once('./footer.php'); ?>