src/Controller/GestionComerciale/PanierController.php line 310

Open in your IDE?
  1. <?php
  2. namespace App\Controller\GestionComerciale;
  3. use App\Entity\Transporteurs\Transporteur;
  4. use App\Entity\Adresses\Adresse;
  5. use App\Entity\Articles\Article;
  6. use App\Entity\Articles\ArticleComplementOption;
  7. use App\Entity\GestionComerciale\Commande;
  8. use App\Entity\GestionComerciale\ArticleCommande;
  9. use App\Entity\GestionComerciale\StatutCommande;
  10. use App\Form\GestionComerciale\CommandeType;
  11. use App\Library\Uploader\Services\FileUploader;
  12. use App\Security\Voter\EntityVoter;
  13. use App\Service\GestionComerciale\EmailService;
  14. use App\Service\GestionComerciale\PanierService;
  15. use App\Service\Articles\ArticleService;
  16. use App\Service\Taxes\TaxeService;
  17. use App\Service\Utilisateur\ColonneTableauService;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use JMS\Serializer\SerializerBuilder;
  20. use Knp\Component\Pager\PaginatorInterface;
  21. use Knp\Snappy\Pdf;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\DependencyInjection\Container;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  29. use Symfony\Component\HttpFoundation\Cookie;
  30. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  31. use Symfony\Component\Validator\Validator\ValidatorInterface;
  32. use Symfony\Contracts\Translation\TranslatorInterface;
  33. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  34. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  35. use App\Model\GestionCommerciale\TypeDocumentCommercial;
  36. /**
  37.  * @Route("/panier")
  38.  */
  39. class PanierController extends AbstractController
  40. {
  41.     /**
  42.      * @Route("/ajouter/{id}", name="ajouter", methods={"POST"})
  43.      */
  44.     public function ajouterint $id,Request $request,EntityManagerInterface $em,PanierService $panierService,ArticleService $articleService): JsonResponse
  45.     {
  46.         $articleRepo $em->getRepository(Article::class);
  47.         $commandeRepo $em->getRepository(Commande::class);
  48.         $user $this->getUser();
  49.         $article $articleRepo->find($id);
  50.         if (!$articleRepo) {
  51.             return $this->json(['success' => false'message' => 'Produit inexistant']);
  52.         }
  53.         $data json_decode($request->getContent(), true);
  54.         $quantite = isset($data['quantite']) && $data['quantite'] > ? (int)$data['quantite'] : 1;
  55.         $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  56.         // Récupérer ou créer le panier
  57.         $panier $commandeRepo->findOneBy([
  58.             'statutCommande' => $statutPanier,
  59.             'utilisateur' => $user,
  60.             'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
  61.         ]);
  62.         if (!$panier) {
  63.             $panier = new Commande();
  64.             if($user$panier->setClient($user->getClient());
  65.             $panier->setStatutCommande($statutPanier);
  66.             $panier->setUtilisateur($user);
  67.             $panier->setTypeDocumentCommercial(TypeDocumentCommercial::PANIER);
  68.             $panier->setTotal(0);
  69.             $panier->setAvecTaxe(0);
  70.             $panier->setTotalTvaFraisPort(0);
  71.             $panier->setTotalTva(0);
  72.             $panier->setTauxTvaFraisPort(0);
  73.             $panier->setTotalFraisPort(0);
  74.             $panier->setTotalTtc(0);
  75.             $panier->setTotalFraisPortTtc(0);
  76.             $panier->setTotalPoids(0);
  77.             $panier->setTotalHtAvecFraisPort(0);
  78.             $em->persist($panier);
  79.         }
  80.         $prixVente $article->getPrixVente();
  81.         $prixRemise $article->getPrixVente();
  82.         $remise 0;
  83.         $remise_supplementaire 0;
  84.         $lignePanier "";
  85.         $articleCommande $panier->getArticleCommande()->filter(fn($a) => $a->getArticle()->getId() === $article->getId())->first();
  86.          if ($articleCommande) {
  87.              if($user) {
  88.                  $remise_calcule $articleService->getRemisesPossibleArticleClient(['article'=>$article],$user->getClient());
  89.                  if($remise_calcule["remiseId"]>0) {
  90.                      $remise $remise_calcule["valeur"];
  91.                      $prixRemise $remise_calcule["prixRemise"];
  92.                  }
  93.              }
  94.              $articleCommande->setRemise($remise);
  95.              $articleCommande->setQuantite($articleCommande->getQuantite() + $quantite);
  96.              $articleCommande->setTotalTtcDevise($prixRemise*($articleCommande->getQuantite()));
  97.              $articleCommande->setTotalHt($prixRemise*($articleCommande->getQuantite()));
  98.              $articleCommande->setTotalWithTax($prixRemise*($articleCommande->getQuantite()));
  99.              $em->persist($articleCommande);
  100.          } else {
  101.              if($user) {
  102.                  $remise_calcule $articleService->getRemisesPossibleArticleClient(['article'=>$article],$user->getClient());
  103.                  if($remise_calcule["remiseId"]>0) {
  104.                      $remise $remise_calcule["valeur"];
  105.                      $prixRemise $remise_calcule["prixRemise"];
  106.                  }
  107.              }
  108.              $articleCommande = new ArticleCommande();
  109.              $articleCommande->setLibelle($article->getLibelle());
  110.              $articleCommande->setLibelleSecondaire($article->getLibelleSecondaire());
  111.              $articleCommande->setReference($article->getReference());
  112.              $articleCommande->setCommande($panier);
  113.              $articleCommande->setArticle($article);
  114.              $articleCommande->setLargeur($article->getLargeur());
  115.              $articleCommande->setProfondeur($article->getProfondeur());
  116.              $articleCommande->setHauteur($article->getHauteur());
  117.              $articleCommande->setPoids($article->getPoids());
  118.              $articleCommande->setQuantite($quantite);
  119.              $articleCommande->setPrixBase($prixVente);
  120.              $articleCommande->setPrixAvecRemise($prixRemise);
  121.              $articleCommande->setPrixUnitaireTtc($prixVente);
  122.              $articleCommande->setTvaVente(0);
  123.              $articleCommande->setTva(0);
  124.              $articleCommande->setRal($quantite);
  125.              $articleCommande->setRemise($remise);
  126.              $articleCommande->setRemiseSupplementaire($remise_supplementaire);
  127.              $articleCommande->setTotalTtcDevise($prixRemise*$quantite);
  128.              $articleCommande->setTotalHt($prixRemise*$quantite);
  129.              $articleCommande->setTotalWithTax($prixRemise*$quantite);
  130.              $em->persist($articleCommande);
  131.              $panier->addArticleCommande($articleCommande);
  132.              $em->flush();
  133.              $lignePanier $this->renderView('GestionComerciale/Panier/ligne_panier_v2.html.twig', ["p"=>$articleCommande]);
  134.          }
  135.         $panierService->majPanier($panier);
  136.         $em->flush();
  137.          $topListeArticlePanier  $this->renderView('GestionComerciale/Panier/liste_article_panier_top.html.twig', []);
  138.         return $this->json([
  139.             'success' => true,
  140.             'articleCommandeId' => $articleCommande->getId(),
  141.             'totalArticles' => count($panier->getArticleCommande()),
  142.             'totalPrix' => $panier->getTotal(),
  143.             'topListeArticlePanier' => $topListeArticlePanier,
  144.             'lignePanier' => $lignePanier,
  145.             'totalPrixArticle' => $prixRemise $articleCommande->getQuantite(),
  146.             'quantite' => $articleCommande->getQuantite(),
  147.         ]);
  148.     }
  149.     /**
  150.      * @Route("/supprimer/{id}", name="dtc_panier_supprimer", methods={"POST"})
  151.      */
  152.     public function supprimerArticleint $id,EntityManagerInterface $emRequest $request,PanierService $panierService): JsonResponse
  153.     {
  154.          $commandeRepo $em->getRepository(Commande::class);
  155.          $articleCommandeRepo $em->getRepository(ArticleCommande::class);
  156.          $user $this->getUser();
  157.         $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  158.         $panier $commandeRepo->findOneBy([
  159.             'statutCommande' => $statutPanier,
  160.             'utilisateur' => $user,
  161.             'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
  162.         ]);
  163.         if (!$panier) {
  164.             return $this->json(['success' => false'message' => 'Panier introuvable']);
  165.         }
  166.         $articleCommande $articleCommandeRepo->find($id);
  167.         if (!$articleCommande) {
  168.             return $this->json(['success' => false'message' => 'Article introuvable dans le panier']);
  169.         }
  170.         $em->remove($articleCommande);
  171.         $em->flush();
  172.         $panierService->majPanier($panier);
  173.         $panierDissocie $panierService->dissocierPanierStock($panier);
  174.         $totalEnStock 0;
  175.         if(count($panierDissocie["stock"]->getArticleCommande())>0) {
  176.             foreach($panierDissocie["stock"]->getArticleCommande() as $ac) {
  177.                 $totalEnStock += $ac->getQuantite();
  178.             }
  179.         }
  180.         $totalArticles 0;
  181.         if(count($panier->getArticleCommande())>0) {
  182.             foreach($panier->getArticleCommande() as $ac) {
  183.                 $totalArticles += $ac->getQuantite();
  184.             }
  185.         }
  186.         return $this->json([
  187.             'success' => true,
  188.             'totalArticles' => $totalArticles,
  189.             'totalPrix' => $panier->getTotal(),
  190.             'totalPoids' => $panier->getTotalPoids(),
  191.             'totalEnStock' => $totalEnStock,
  192.         ]);
  193.     }
  194.     /**
  195.      * @Route("/modifier-quantite/{id}", name="dtc_panier_modifier_quantite", methods={"POST"})
  196.      */
  197.     public function modifierQuantite(int $idRequest $requestEntityManagerInterface $em,PanierService $panierService,ArticleService $articleService): JsonResponse
  198.     {
  199.         $commandeRepo $em->getRepository(Commande::class);
  200.         $articleCommandeRepo $em->getRepository(ArticleCommande::class);
  201.         $user $this->getUser();
  202.         $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  203.         $panier $commandeRepo->findOneBy([
  204.             'statutCommande' => $statutPanier,
  205.             'utilisateur' => $user,
  206.             'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
  207.         ]);
  208.         if (!$panier) {
  209.             return $this->json(['success' => false'message' => 'Panier introuvable']);
  210.         }
  211.         $articleCommande $panier->getArticleCommande()->filter(fn($a) => $a->getId() === $id)->first();
  212.         if (!$articleCommande) {
  213.             return $this->json(['success' => false'message' => 'Article introuvable dans le panier']);
  214.         }
  215.         $data json_decode($request->getContent(), true);
  216.         $quantite = isset($data['quantite']) && $data['quantite'] > ? (int)$data['quantite'] : 1;
  217.         $prixVente $articleCommande->getArticle()->getPrixVente();
  218.         $prixRemise $articleCommande->getArticle()->getPrixVente();
  219.         $remise 0;
  220.         $articleCommande->setQuantite($quantite);
  221.         $remise_calcule $articleService->getRemisesPossibleArticleClient(['article'=>$articleCommande->getArticle()],$user->getClient());
  222.         if($remise_calcule["remiseId"]>0) {
  223.             $remise $remise_calcule["valeur"];
  224.             $prixRemise $remise_calcule["prixRemise"];
  225.         }
  226.         $articleCommande->setRemise($remise);
  227.         $articleCommande->setTotalTtcDevise($prixRemise*($articleCommande->getQuantite()));
  228.         $articleCommande->setTotalHt($prixRemise*($articleCommande->getQuantite()));
  229.         $articleCommande->setTotalWithTax($prixRemise*($articleCommande->getQuantite()));
  230.         $em->persist($articleCommande);
  231.         $panierService->majPanier($panier);
  232.         $panierDissocie $panierService->dissocierPanierStock($panier);
  233.         $totalEnStock 0;
  234.         if(count($panierDissocie["stock"]->getArticleCommande())>0) {
  235.             foreach($panierDissocie["stock"]->getArticleCommande() as $ac) {
  236.                 $totalEnStock += $ac->getQuantite();
  237.             }
  238.         }
  239.         $totalArticles 0;
  240.         if(count($panier->getArticleCommande())>0) {
  241.             foreach($panier->getArticleCommande() as $ac) {
  242.                 $totalArticles += $ac->getQuantite();
  243.             }
  244.         }
  245.         //$articleCommande
  246.         $lignePanier $this->renderView('GestionComerciale/Panier/ligne_panier_v2.html.twig', ["p"=>$articleCommande]);
  247.         return $this->json([
  248.             'ligne' => $lignePanier,
  249.             'success' => true,
  250.             'totalArticles' => $totalArticles,
  251.             'totalPrix' => $panier->getTotal(),
  252.             'totalPrixArticle' => $prixRemise $articleCommande->getQuantite(),
  253.             'articlePrix' => $articleCommande->getPrixBase() * $articleCommande->getQuantite(),
  254.             'totalPoids' => $panier->getTotalPoids(),
  255.             'totalEnStock' => $totalEnStock,
  256.         ]);
  257.     }
  258.     /**
  259.      * @Route("/", name="dtc_panier_afficher")
  260.      */
  261.     public function listerAction(Request $requestEntityManagerInterface $emTranslatorInterface $translator,PanierService $panierService)
  262.     {
  263.         $user $this->getUser();
  264.         if(!$user or !$user->getClient()) return $this->redirectToRoute('dtc_infos_manquantes');
  265.         $param = [];
  266.         $cat_produit_home $this->getParameter('cat_produit_home');
  267.         $commandeRepo $em->getRepository(Commande::class);
  268.         $user $this->getUser();
  269.         $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  270.         $panier $commandeRepo->findOneBy([
  271.             'statutCommande' => $statutPanier,
  272.             'utilisateur' => $user,
  273.             'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
  274.         ]);
  275.         if (!$panier) {
  276.             $panier = new Commande();
  277.             $panier->setClient($user->getClient());
  278.             $panier->setStatutCommande($statutPanier);
  279.             $panier->setUtilisateur($user);
  280.             $panier->setTypeDocumentCommercial(TypeDocumentCommercial::PANIER);
  281.             $panier->setTotal(0);
  282.             $panier->setAvecTaxe(0);
  283.             $panier->setTotalTvaFraisPort(0);
  284.             $panier->setTotalTva(0);
  285.             $panier->setTauxTvaFraisPort(0);
  286.             $panier->setTotalFraisPort(0);
  287.             $panier->setTotalTtc(0);
  288.             $panier->setTotalFraisPortTtc(0);
  289.             $panier->setTotalPoids(0);
  290.             $panier->setTotalHtAvecFraisPort(0);
  291.             $em->persist($panier);
  292.             $em->flush();
  293.         }
  294.         if ($panier->getArticleCommande()->isEmpty()) {
  295.             $this->addFlash('warning''Votre panier est vide.');
  296.         }
  297.        $panierDissocie $panierService->dissocierPanierStock($panier);
  298.         return $this->render(
  299.             'GestionComerciale/Panier/afficher.html.twig',
  300.             [
  301.                 'parametres'=> $param,
  302.                 'panierDissocie' => $panierDissocie,
  303.                 "cat_produit_home"            => $cat_produit_home,
  304.             ]
  305.         );
  306.     }
  307.     /**
  308.      * @Route("/tva/calcul/", name="dtc_panier_calcul_tva")
  309.      */
  310.     public function tvaCalculAction(Request $requestEntityManagerInterface $emTranslatorInterface $translator,ArticleService $articleService,PanierService $panierService)
  311.     {
  312.         $totaux=[];
  313.         $commandeRepo $em->getRepository(Commande::class);
  314.         $transporteurRepo $em->getRepository(Transporteur::class);
  315.         $user $this->getUser();
  316.         $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  317.         $panier $commandeRepo->findOneBy([
  318.             'statutCommande' => $statutPanier,
  319.             'utilisateur' => $user,
  320.             'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
  321.         ]);
  322.         if (!$user->getClient()) {
  323.             return new JsonResponse(['message' => "client introuvable"]);
  324.         }
  325.         else if (!$panier) {
  326.             return new JsonResponse(['message' => "panier introuvable"]);
  327.         }
  328.         else {
  329.             $data json_decode($request->getContent(), true);
  330.             if(empty($data["id_adresse"])) {
  331.                 return new JsonResponse(['message' => "adresse introuvable"]);
  332.             }
  333.             else {
  334.                 $transporteur_obj $transporteurRepo->find($data["id_transporteur"]);
  335.                 $panier->setTransporteur($transporteur_obj);
  336.                 $rep_adresse $em->getRepository(Adresse::class);
  337.                 $adresseFacturation NULL;
  338.                 $adresseLivraison NULL;
  339.                 if(is_object($user->getClient())) $adresseFacturation $rep_adresse->findOneBy(["client" =>$user->getClient(), "facturationDefaut" => "1"] );
  340.                 $adresseLivraison $rep_adresse->find($data["id_adresse"]);
  341.                 $panier->setAdresseLivraison($adresseLivraison);
  342.                 $totalTva 0;
  343.                 foreach($panier->getArticleCommande() as $ac) {
  344.                     $articleService->getTva2($ac);
  345.                     $taxeDefautObj $articleService->getTaxeClientAdresses($ac->getArticle()->getRegleTaxe(), $user->getClient(), $adresseLivraison$adresseFacturation);
  346.                     if($taxeDefautObj) {
  347.                         $ac->setTaxe($taxeDefautObj);
  348.                         $ac->setTva($taxeDefautObj->getTaux());
  349.                         $tvaVente = ($ac->getPrixAvecRemise()*$ac->getQuantite())*$taxeDefautObj->getTaux()/100;
  350.                         $ac->setTvaVente($tvaVente);
  351.                         $ac->setTotalTtcDevise($ac->getTotalHt()+$tvaVente);
  352.                         $ac->setTotalWithTax($ac->getTotalHt()+$tvaVente);
  353.                         $ac->setPrixUnitaireTtc($ac->getPrixAvecRemise()+($ac->getPrixAvecRemise()*$taxeDefautObj->getTaux()/100));
  354.                         $em->persist($ac);
  355.                     }
  356.                 }
  357.                 $panierService->majPanier($panier);
  358.                 $totaux["total"]=$panier->getTotal();
  359.                 $totaux["totalTva"]=$panier->getTotalTva();
  360.                 $totaux["totalFraisPort"]=$panier->getTotalFraisPort();
  361.                 $totaux["totalTvaFraisPort"]=$panier->getTotalTvaFraisPort();
  362.                 $totaux["totalTtc"]=$panier->getTotalTtc();
  363.             }
  364.             return new JsonResponse(['id_transporteur' => $data["id_transporteur"],'totaux'=>$totaux]);
  365.         }
  366.     }
  367. }