src/Controller/Controller_Frontoffice/Panier/PanierController.php line 35
<?php
namespace App\Controller\Controller_Frontoffice\Panier;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Commande;
use App\Entity\Membres;
use App\Entity\Newsletter;
use App\Entity\Panier;
use App\Entity\Token;
use App\Entity\Articles;
use App\Entity\CodePromo;
use App\Entity\HistoriquePrixArticle;
use App\Entity\Images;
use App\Entity\Slider;
use App\Entity\SousCategories;
use App\Entity\Entreprise;
use App\Entity\Categories;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Serializer\SerializerInterface;
class PanierController extends AbstractController
{
#[Route('/panier', name: 'panier')]
public function panierAction(EntityManagerInterface $entityManager,Request $request): Response
{
$session = new Session();
$typeCompte = "PARTICULIER";
if($session->get("connectU") == "OK" && $session->get("userU") != null){
if($session->get("userU")->getCodeType() == 2){
$typeCompte = "PRO";
}
}
$session = new Session();
$panier = array();
$petitpan = array();
if($session->get("panier")){
$panier = $session->get("panier");
}
if($session->get("petitpan")){
$petitpan = $session->get("petitpan");
}
$n = count($panier);
$article = array();
$total = 0;
$i=0;
$totalTva = 0;
$livraison = 0;
foreach ($panier as $p){
$repository = $entityManager->getRepository(Articles::class);
$pr = $repository->findOneById($p->getIdArticle());
if($pr){
$prixU = 0;
if($typeCompte == "PARTICULIER"){
$prixU = $pr->getPrixTTC();
if($pr->getPrixPromo() > 0 && $pr->getPrixPromo() < $pr->getPrixTTC()){
$prixU = $pr->getPrixPromo();
}
}else{
$prixU = $pr->getPrixProTTC();
if($pr->getPrixPromo() > 0 && $pr->getPrixPromo() < $pr->getPrixProTTC()){
$prixU = $pr->getPrixPromo();
}
}
$prixTotal = $p->getQte()*$prixU;
$article[$i]["obj"] = $pr;
$p->setPrixU($prixU);
$p->setPrixTotal($prixTotal);
$article[$i]["pan"] = $p;
$total = $total + $prixTotal;
//$totalTva = $totalTva + ((($article[$i]["obj"]->getPrixHT()*$article[$i]["obj"]->getTva())/100)*$article[$i]["qte"]);
$i++;
}
}
$repository = $entityManager->getRepository(Entreprise :: class);
$entreprise = $repository->findOneById(1);
if($total <= $entreprise->getPrixMinCmd() && $total > 0){
$livraison = $entreprise->getFraisLivraison();
}
if($total > $entreprise->getPrixMinCmd2()){
$livraison = $entreprise->getFraisLivraison2();
}
$connect ="";
if($session->get("connectU")){
$connect = $session->get("connectU");
}
$totalPromo = 0;
$response = "";
$codePromo = "";
if($request->getMethod() == "POST"){
$formName = $request->get("formName");
if ($formName == "updateQte") {
$ids = $request->get("ids");
for ($i = 0; $i < count($ids); $i++) {
$qte = $_POST["quant-".$ids[$i]];
$prixTotal = $qte * $panier[$ids[$i]]->getPrixU();
$panier[$ids[$i]]->setQte($qte);
$panier[$ids[$i]]->setPrixTotal($prixTotal);
//----- update database ------//
if($session->get("connectU") && $session->get("connectU") == "OK"){
$idCommande = $session->get("commande");
$repository = $entityManager->getRepository(Panier::class);
$panierDB = $repository->findOneBy(array("idCommande"=>$idCommande,"idArticle"=>$panier[$ids[$i]]->getIdArticle()));
if($panierDB){
$panierDB->setQte($qte);
$panierDB->setPrixTotal($prixTotal);
$entityManager->flush();
}
}
}
return $this->redirect("/panier/");
}
elseif($formName == "codePromo"){
if($total > 0){
$codePromo = $request->get("codePromo");
$repository = $entityManager->getRepository(CodePromo::class);
$promo = $repository->findOneByCode($codePromo);
if(!$promo){
$response = "Code promo invalide !";
}
else{
$expired = false;
if($promo->getIfDateFin() == 1){
$now = new \DateTime();
if($now->format("Y-m-d H:i") > $promo->getDateFin()->format("Y-m-d H:i")){
$expired = true;
}
}
if($expired == true){
$totalTTC = $total;
$totalPromo = ($totalTTC*$promo->getPromo())/100;
$response = "OK";
}
}
}
}
}
//----------- update commande --------//
if($session->get("connectU") && $session->get("connectU") == "OK"){
$idCommande = $session->get("commande");
$repository = $entityManager->getRepository(Commande::class);
$commande = $repository->findOneById($idCommande);
if($commande){
$commande->setTotal($total);
//----- livraison ----//
$commande->setLivraison($livraison);
//----- promo ----//
$commande->setCodePromo($codePromo);
$commande->setPromo($totalPromo);
$entityManager->flush();
}
}
return $this->render('frontoffice_bundle/panier/panier.html.twig',
array('controller_name' => 'PanierController',"npanier"=>$n,"articles"=>$article,"total"=>$total,"connect"=>$connect,"totalTva"=>$totalTva,
"totalPromo"=>$totalPromo,"response"=>$response,"codePromo"=>$codePromo,"livraison"=>$livraison));
}
#[Route('/deleteFromPanier', name: 'deleteFromPanier')]
public function deletecPanierAction(EntityManagerInterface $entityManager,Request $request): Response
{
$session = new Session();
$panier = array();
$petitpan = array();
if($session->get("panier")){
$panier = $session->get("panier");
//----- delete where user is connected -----//
if($session->get("connectU") && $session->get("connectU") == "OK"){
$idCommande = $session->get("commande");
$repository = $entityManager->getRepository(Panier::class);
$panierDB = $repository->findOneBy(array("idCommande"=>$idCommande,"idArticle"=>$panier[$_GET['id']]->getIdArticle()));
if($panierDB){
$repository = $entityManager->getRepository(Commande::class);
$commande = $repository->findOneById($idCommande);
$commande->setTotal($commande->getTotal()-$panierDB->getPrixTotal());
$total = $commande->getTotal();
$livraison = 0;
$repository = $entityManager->getRepository(Entreprise::class);
$entreprise = $repository->findOneById(1);
if($total <= $entreprise->getPrixMinCmd() && $total > 0){
$livraison = $entreprise->getFraisLivraison();
}
if($total > $entreprise->getPrixMinCmd2()){
$livraison = $entreprise->getFraisLivraison2();
}
//----- update code Promo -----//
$commande->setLivraison($livraison);
$entityManager->remove($panierDB);
$entityManager->flush();
}
}
unset($panier[$_GET['id']]);
$session->set("panier",$panier);
//----petit panier
if($session->get("petitpan")){
$petitpan = $session->get("petitpan");
unset($petitpan[$_GET['id']]);
$session->set("petitpan",$petitpan);
}
}
$response = "OK";
return new Response($response);
}
//----- ajouter dans le panier
#[Route('/addpanier', name: 'addpanier')]
public function addPanierAction(EntityManagerInterface $entityManager,Request $request): Response
{
$session = new Session();
$typeCompte = "PARTICULIER";
if($session->get("connectU") == "OK" && $session->get("userU") != null){
if($session->get("userU")->getCodeType() == 2){
$typeCompte = "PRO";
}
}
$session = new Session();
$panier = array();
$petitpan= array();
if($session->get("panier")){
$panier = $session->get("panier");
}
//------petit panier
if($session->get("petitpan")){
$petitpan = $session->get("petitpan");
}
$idArticle = $request->get("idArticle");
$quantite = $request->get("quantite");
if (array_key_exists($idArticle, $panier)) {
$response = "EXIST";
}
else{
$repository = $entityManager->getRepository(Articles::class);
$t = $repository->findOneById($idArticle);
if($quantite < $t->getMinQte()){
$response = "QUANTITY";
}
else{
$repository = $entityManager->getRepository(SousCategories::class);
$sc = $repository->findOneById($t->getIdSousCategorie());
$designation = "";
if($sc){
$designation = $sc->getTitre();
}
$infoArticle = $t->getTitre();
if(!empty($infoArticle)){
$designation = $designation." (".$infoArticle.")";
}
$prixU = 0;
if($typeCompte == "PARTICULIER"){
$prixU = $t->getPrixTTC();
if($t->getPrixPromo() > 0 && $t->getPrixPromo() < $t->getPrixTTC()){
$prixU = $t->getPrixPromo();
}
}else{
$prixU = $t->getPrixProTTC();
if($t->getPrixPromo() > 0 && $t->getPrixPromo() < $t->getPrixProTTC()){
$prixU = $t->getPrixPromo();
}
}
$prixTotal = $quantite*$prixU;
$p = new Panier();
$p->setIdArticle($idArticle);
$p->setDesignation($designation);
$p->setPrixU($prixU);
$p->setPrixTotal($prixTotal);
$p->setQte($quantite);
$p->setUnite($t->getUnite());
$panier[$idArticle] = $p;
$session->set("panier",$panier);
//-----mettre l'article dans la session pour le petit panier
$petitpan[$idArticle]['produit'] = $t;
$petitpan[$idArticle]['panier'] = $p;
$session->set("petitpan",$petitpan);
//-----IF USER CONNECTED - SAVE DATA IN DATA BASE.
if($session->get("connectU") && $session->get("connectU") == "OK"){
$idCommande = $session->get("commande");
$p->setIdCommande($idCommande);
$entityManager->persist($p);
$entityManager->flush();
$repository = $entityManager->getRepository(Commande::class);
$commande = $repository->findOneById($idCommande);
$totalC = $commande->getTotal();
$t = $totalC + $prixTotal;
$commande->setTotal($t);
//----- livraison ----//
$livraison = 0;
$repository = $entityManager->getRepository(Entreprise::class);
$entreprise = $repository->findOneById(1);
if($t <= $entreprise->getPrixMinCmd() && $t > 0){
$livraison = $entreprise->getFraisLivraison();
}
if($t > $entreprise->getPrixMinCmd2()){
$livraison = $entreprise->getFraisLivraison2();
}
$commande->setLivraison($livraison);
$entityManager->flush();
}
$response = "OK";
}
}
return new Response($response);
}
}