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
|
<?php
require_once('includes/header.php'); //on inclus 1 fois le dossier concerner
//Creation du panier
if (! isset($_SESSION['bag'])) {
$_SESSION['bag'] = array();
}
//Donnees externes utilisee par le panier
$id = (isset($_POST['id'])?$_POST['id']:(isset($_GET['id'])?$_GET['id']:null));
$l = (isset($_POST['l']) ?$_POST['l'] :(isset($_GET['l'])?$_GET['l'] :null));
$q = (isset($_POST['q']) ?$_POST['q'] :(isset($_GET['q'])?$_GET['q'] :null));
$p = (isset($_POST['p']) ?$_POST['p'] :(isset($_GET['p'])?$_GET['p'] :null));
//Traitements du panier
if($id != null){
// Ajouter un nouvel article
if ( isset ( $_GET['action'] ) ){
$_SESSION['bag'][$id]['id'] = $id;
$_SESSION['bag'][$id]['l'] = $l;
$_SESSION['bag'][$id]['q'] = $q;
$_SESSION['bag'][$id]['p'] = $p;
//vider le panier
}elseif ( isset ( $_GET['vider'] ) ){
unset($_SESSION['bag']);
//Supprimer un article du panier
}elseif ( isset ( $_GET['delete'] ) ){
if(isset($_GET['id'])){
$id = $_GET['id']; //reference du produit a retirer
unset($_SESSION['bag'][$id]);
/*echo "<pre>";
print_r( $_SESSION['bag'] );
echo "</pre>";
die();*/
}
//Modifier la quantité achetée + TRANSACTION
}elseif ( isset ( $_GET['q'] ) && isset ( $_GET['modify'] ) ){
$q = $_GET['q'];
$_SESSION['bag'][$_GET['id']]['q'] = $q;
// traitement SQL dans un bloc try catch
try {
// démarrer la transaction
$conn->beginTransaction();
// mettre à jour stock
$update = $conn->prepare("UPDATE detail SET det_quantite = ".$q." WHERE art_id = ".$id." && com_id = '1' ");
$update->execute();
// mettre à jour stock
$update2 = $conn->prepare("UPDATE article SET art_stock = art_stock - ".$q." WHERE art_id = ".$id." ");
$update2->execute();
/*echo "<pre>";
print_r($update);
echo "</pre>";
//die("here");*/
// vérifier le stock
$squery = "SELECT art_stock from article WHERE art_id = " .$id;
$result = $conn->query($squery);
$row = $result->fetch();
if ($row['art_stock'] < 0){
// stock insuffisant : rollBack (ajout commande et mise à jour du stock)
$conn->rollBack();
$conn = null;
// vider panier
//unset($_SESSION['bag']);
die("<h1 style='color:red'; align='center'>Commande annulée : stock insuffisant !!!</h1>");
}
//ici, tout est en ordre donc commit
$conn->commit();
} catch(PDOException $ex){
// erreur catchée : rollBack
$conn->rollBack();
die("ATTENTION Commande NON enregistrée : " . $ex->getMessage());
}
}
}
?>
<body style="background-color:#F0F0F0 ;margin-top: 20px">
<div style="background-color:#FFFFFF;width:960px; margin:0 auto;padding: 10px">
<table >
<ul align="right"; class='panier'>
<li ><a href="?vider&id=$id">Vider le panier</a></li>
</ul>
<h3 align="center">VOTRE PANIER</h3>
<table align='center' border='1px' style='width:50%'>
<tr >
<th width="50" >#</th>
<th width="100">Libellé</th>
<th width="50" >Prix</th>
<th width="150">Quantité</th>
<th width="150" >Action</th>
</tr>
<tr align="center">
<?php
if (!isset($_SESSION['client']['cli_id'])){
die("<h2 style='color:red';>Vous devez d'abord vous connecter !!!</h2>");
}
if(isset($_SESSION['bag'])){
$total_panier = 0;
foreach ($_SESSION['bag'] as $key){
$i = 0;
if($key != 0){
echo "<tr>";
echo "<td align='center'>".$key['id']."</td>";
echo "<td align='center'>".$key['l']."</td>";
echo "<td align='center'>".$key['p']."</td>";
?>
<form >
<a href="?modify&id=<?php echo $key['id']?>&q=<?php echo $key['q']; ?>"></a>
<td align='center'>
<input type="text" name="q" size="5" value="<?php echo $key['q'] ?>"/>
<input type="hidden" name="id" value="<?php echo $key['id'] ?>"/>
<input type="submit" name="modify" value="Modifier"/>
</td>
</form>
<td align='center'; size="5"><a href="?delete&id=<?php echo $key['id'] ?>">Delete</a></td>
<?php
echo "</tr>";
// Calcule le prix total du panier
$total_panier += $key['p'] * $key['q'];
$i++;
// Calcule le prix TTC du panier
$select = $conn->query("SELECT art_tva FROM article");
$s = $select->fetch(PDO::FETCH_OBJ);
$tva = $s->art_tva;
$ttc = $total_panier + ($total_panier * $tva / 100);
}
echo "</tr>";
}
?>
</table>
</table><br/>
<table align='center' border='1px' style='width:50%'>
<tr >
<?php
// Affiche le prix total du panier
echo "<th >Total:<br/>";
echo "$total_panier Euros</th>";
// Affiche le total TTC du panier
echo "<th >TTC:<br/>";
echo "$ttc Euros</th>";
}else{
// Message si le panier est vide
die ('Votre panier est vide');
}
?>
</tr>
</table>
<ul align="right"; class='panier'>
<li ><a href="my_Account.php">Acheter</a></li>
<?php
if(isset($_SESSION['bag'])){
foreach ($_SESSION['bag'] as $idart){
if($idart != 0){
/*echo "<br/><br/>";
echo $idart['id']."<br/>";
echo $idart['l']."<br/>";
echo $idart['p']."<br/>";
/*echo "<pre>";
print_r($idart);
echo "</pre>"; */
}
}
// créer la commande
$insert = $conn->prepare("INSERT INTO detail (art_id,com_id,det_prix,det_quantite) VALUES(".$idart['id'].", '2', ".$total_panier.", ".$idart['q'].")");
$insert->execute();
/*echo "<pre>";
print_r($insert);
echo "</pre>";
die("here");*/
}
?>
</ul>
</div>
</body> |
Partager