diff --git a/ensiie-project/src/Model/db_data.php b/ensiie-project/src/Model/db_data.php index bde0493c02e8e5d014c6277e455d57ccaa21007e..f54e17a9895bb5d802c5c43a115c6c63172a88ee 100644 --- a/ensiie-project/src/Model/db_data.php +++ b/ensiie-project/src/Model/db_data.php @@ -257,17 +257,6 @@ function db_getCommandesUtilisateur($idUtilisateur){ $allComands[$res['idCommande']]->updateLastMenu($newMenu); } } - /* - $allComands[$res['idCommande']]->getMenus()[key($lastMenu)]-> - addSpecial( - new Special( - $res['specialItemID'], - $res['nomSpecialType'], - $res['specialTypeID'], - $res['nomSpecialItem'] - ) - ); - */ } return $allComands; @@ -281,14 +270,49 @@ function db_getCommandesUtilisateur($idUtilisateur){ * La variable contenant les informations de l'évenement pour lequel l'utilisateur commande * @param $commande * La variable contenant les informations de la commande - * @param $nom */ -function db_addCommande($utilisateur, $evenement, $commande, $nom){ - $actuel_evenement = db_getActuelEvenement(); - assert($evenement->getIDEvenement() == $actuel_evenement->getIDEvenement()); +function addCommande($utilisateur, $evenement, $commande){ + $actuel_evenement = db_getActuelEvenement(); + assert(strtotime($commande->getDate()) < strtotime($actuel_evenement->getDate())); + $pdo = $GLOBALS['connection']; + $statement = $pdo->prepare( + "INSERT INTO commande (userID, eventID, dateTimeCommande, isPaid) + VALUES (?, ?, ?, ?)" + ); + $date = date_format($commande->getDate(), "Y-m-d H:i:s"); + if(!$statement->execute([$commande->getUtilisateurID(), + $actuel_evenement->getIDEvenement(), + $date, + 0, + ])) + { + throw new Exception("Can't insert Commande !"); + } + $idCommande = $pdo->lastInsertId(); + foreach($commande->getMenus() as $menu){ + $statement_commandeItem = $pdo->prepare( + "INSERT INTO idItemCommande (idCommande, idFood) + VALUES (?, ?)" + ); + if(!$statement_commandeItem->execute([$idCommande, + $menu->getNourriture()->getIDNourriture() + ])){ + throw new Exception("Can't insertcommande_item"); + } + $idCommandeItem = $pdo->lastInsertId(); + $specials = $menu->getSpecials(); + if($specials != NULL && !empty($specials)){ + foreach($specials as $spec){ + $statement_itemSpecial = $pdo->prepare( + "INSERT INTO item_commande_has_special (idItemCommande, idItemSpecial) + VALUES (?, ?)" + ); + $statement_itemSpecial->execute([$idCommandeItem,$spec->getIDSpecial()]); + } + } + } } - function db_addEvenement($evenement) { $pdo->query("INSERT INTO `event` (`eventID`, `typeEvent`, `numeroEvent`, `date_start`, `date_end`) VALUE ($evenement->getIDevenement(), $evenement->getType(), $evenement->getNumero(), $evenement->getDate(), $evenment->getDateFin()");