src/Controller/GestionComerciale/DevisController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller\GestionComerciale;
  3. use App\Entity\Articles\Article;
  4. use App\Entity\GestionComerciale\ArticleCommande;
  5. use App\Entity\GestionComerciale\Commande;
  6. use App\Entity\GestionComerciale\StatutCommande;
  7. use App\Model\GestionCommerciale\TypeDocumentCommercial;
  8. use App\Form\GestionComerciale\CommandeType;
  9. use App\Library\Datatable\Util\Datatable;
  10. use App\Library\Uploader\Services\FileUploader;
  11. use App\Security\Voter\EntityVoter;
  12. use App\Service\Articles\ArticleService;
  13. use App\Service\GestionComerciale\EmailService;
  14. use App\Service\GestionComerciale\PanierService;
  15. use App\Service\GestionComerciale\CommandeService;
  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\Form\Devis\DevisVersPanierType;
  36. class DevisController extends AbstractController
  37. {
  38.     /**
  39.      * @Route("/compte/devis", name="dtc_devis_liste")
  40.      */
  41.     public function listerAction(Request $requestEntityManagerInterface $em,CommandeService $commandeServiceDatatable $datatableTranslatorInterface $translatorColonneTableauService $serviceColonneTableau)
  42.     {
  43.         $user $this->getUser();
  44.         //if(is_object($user->getClient())) $commandeService->importDocumentsErp($user->getClient()->getId(),["devis"],["log"=>false]);
  45.         $tableau_class_cellule = array();
  46.         $tableau_class_cellule[] = ["className" => "visible_export colonne_id text-center""targets" => [0], "visible" => false"orderable" => true];
  47.         $tableau_class_cellule[] = ["className" => "visible_export colonne_id text-center""targets" => [1],"title"=>'<i style="font-size:1.5em;" class="fa fa-clipboard">'"visible" => true"orderable" => false];
  48.         $tableau_class_cellule[] = ["className" => "visible_export colonne_id text-left""targets" => [2], "visible" => true"orderable" => false];
  49.         $tableau_class_cellule[] = ["className" => "visible_export colonne_id text-right""targets" => [3],"title"=>'<i style="font-size:1.5em;" class="fa fa-calendar"> <b>/</b> <i style="" class="fa fa-euro">&nbsp;&nbsp; '"visible" => true"orderable" => false];
  50.         $tableau_class_cellule[] = ["className" => "visible_export colonne_id text-center""targets" => [4],"title"=>'<i style="font-size:1.5em;" class="fa fa-gear">'"visible" => true"orderable" => false];
  51.         // $this->commandeService->importDocumentsErp($client_tab["id"],["devis"]);
  52.         $response = new Response();
  53.         $this->datatable($request$em$datatable$translator$response);
  54.         $param $request->query->all();
  55.         return $this->render('GestionComerciale/Devis/lister.html.twig',
  56.             [
  57.                 'tableauClassColonne' => $tableau_class_cellule,
  58.                 'parametres'=> $param,
  59.             ]
  60.             ,$response);
  61.     }
  62.     /**
  63.      * set datatable configs
  64.      *
  65.      * @return \App\Library\Datatable\Util\Datatable
  66.      */
  67.     private function datatable(Request $requestEntityManagerInterface $emDatatable $datatableTranslatorInterface $translator)
  68.     {
  69.         $user $this->getUser();
  70.         $param $request->query->all();
  71.         $datatable
  72.             ->setDatatableId('liste_commande')
  73.             ->setEntity(Commande::class, "x")
  74.             ->setFields(
  75.                 [
  76.                     $translator->trans("ID")              => 'x.id',
  77.                     'date' => 'x.date',
  78.                     $translator->trans("RÉFÉRENCE")             => 'x.reference',
  79.                     $translator->trans("Total HT")             => 'x.total',
  80.                     $translator->trans("Actions")      => 'x.id',
  81.                     "_identifier_"                     => 'x.id',
  82.                 ]
  83.             )
  84.             //->addJoin('x.transporteur', 't', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
  85.             ->addJoin('x.utilisateur''u'\Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
  86.             ->setRenderers(
  87.                 [
  88.                     => [
  89.                         'view' => 'FO/DataTable/type_document_vente.html.twig',
  90.                     ],
  91.                     => [
  92.                         'view' => 'FO/DataTable/reference_document_commercail.html.twig',
  93.                     ],
  94.                     /*
  95.                     2 => [
  96.                         'view'   => 'FO/DataTable/avec_lien_edit_route.html.twig',
  97.                         'params' => [
  98.                             'edit_route' => 'dtc_commande_liste',
  99.                         ],
  100.                     ],
  101.                     */
  102.                     => [
  103.                         'view' => 'FO/DataTable/prix_commande_heure.html.twig',
  104.                     ],
  105.                     => [
  106.                         'view'   => 'FO/DataTable/actions.html.twig',
  107.                         'params' => [
  108.                             'edit_route'                  => 'dtc_devis_liste',
  109.                             'objet'                       => Commande::class,
  110.                             'deplier'                     => false,
  111.                             'table'                       => "liste_commande",
  112.                         ],
  113.                     ],
  114.                 ]
  115.             )
  116.             ->setMultiple([])
  117.             ->setOrder("x.date""desc")
  118.             ->setSearch(true)
  119.             ->setSearchFields([]);
  120.         $client_id 10000000000000;
  121.         if($user->getClient())  $client_id $user->getClient()->getId();
  122.         $where "x.typeDocumentCommercial = :typeDocumentCommercial and x.client = :client and x.visible = :visible";
  123.         $parameters = ['typeDocumentCommercial' => TypeDocumentCommercial::DEVIS,'client'=>$client_id,'visible'=>1];
  124.         if (array_key_exists('parametres'$param)) {
  125.             $response = new Response();
  126.             if (array_key_exists('client'$param["parametres"]) and $param["parametres"]["client"] > 0) {
  127.                 //$datatable->addJoin('x.client', 'client', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN);
  128.                 $parameters["client"] = $param["parametres"]["client"];
  129.                 if ($where != "") {
  130.                     $where .= " AND ";
  131.                 }
  132.                 $where .= "c.id = :client";
  133.             }
  134.             if (array_key_exists('date_debut'$param["parametres"]) and $param["parametres"]["date_debut"] != "") {
  135.                 $param["parametres"]["date_debut"] = \DateTime::createFromFormat('d/m/Y'$param["parametres"]["date_debut"]);
  136.                 $param["parametres"]["date_debut"]->setTime(000000);
  137.                 $parameters["date_debut"] = $param["parametres"]["date_debut"];
  138.                 if ($where != "") {
  139.                     $where .= " AND ";
  140.                 }
  141.                 $where .= "x.date >= :date_debut";
  142.             }
  143.             if (array_key_exists('transporteur'$param["parametres"]) and $param["parametres"]["transporteur"] > 0) {
  144.                 //$datatable->addJoin('x.client', 'client', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN);
  145.                 $parameters["transporteur"] = $param["parametres"]["transporteur"];
  146.                 if ($where != "") {
  147.                     $where .= " AND ";
  148.                 }
  149.                 $where .= "t.id = :transporteur";
  150.             }
  151.             if (array_key_exists('date_fin'$param["parametres"]) and $param["parametres"]["date_fin"] != "") {
  152.                 $param["parametres"]["date_fin"] = \DateTime::createFromFormat('d/m/Y'$param["parametres"]["date_fin"]);
  153.                 $param["parametres"]["date_fin"]->setTime(235959);
  154.                 $parameters["date_fin"] = $param["parametres"]["date_fin"];
  155.                 if ($where != "") {
  156.                     $where .= " AND ";
  157.                 }
  158.                 $where .= "x.date <= :date_fin";
  159.             }
  160.         }
  161.         if ($where != '') {
  162.             $datatable->setWhere($where$parameters);
  163.             $qb $datatable->getQueryBuilder()->getDoctrineQueryBuilder();
  164.         }
  165.         return $datatable;
  166.     }
  167.     /**
  168.      * @Route("/compte/devis/grid", name="dtc_devis_liste_grid")
  169.      */
  170.     public function gridAction(Request $requestEntityManagerInterface $emDatatable $datatableTranslatorInterface $translator)
  171.     {
  172.         return $this->datatable($request$em$datatable$translator)->execute();
  173.     }
  174.     /**
  175.      * @Route("/compte/devis/transformer/{id}", name="dtc_devis_transformer_panier", requirements={"id"="\d+"})
  176.      */
  177.     public function tranformerEnPanierAction(Request $request,Commande $devisEntityManagerInterface $em,PanierService $panierService,ArticleService $articleService)
  178.     {
  179.         $action $request->query->get('action');
  180.         $titre_modal "Passer votre devis en commande";
  181.         if($action=="editer"$titre_modal "Éditer votre devis";
  182.         if($action=="dupliquer"$titre_modal "Commander à nouveau";
  183.         $commandeRepo $em->getRepository(Commande::class);
  184.         $user $this->getUser();
  185.         $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  186.         $panier $commandeRepo->findOneBy(['statutCommande' => $statutPanier,'utilisateur' => $user,'typeDocumentCommercial' => TypeDocumentCommercial::PANIER]);
  187.         $errors = [];
  188.         $form $this->createForm(DevisVersPanierType::class);
  189.         $form->handleRequest($request);
  190.         if ($form->isSubmitted()) {
  191.             if($form->isValid()) {
  192.                 $articleRepo $em->getRepository(Article::class);
  193.                 $data $form->getData();
  194.                 $actionPanier $data['action_panier'];
  195.                 // Votre logique métier ici
  196.                 if ($actionPanier === 'vider') {
  197.                     $panierService->viderPanier($user);
  198.                 }
  199.                 if (!$panier) {
  200.                     $panier = new Commande();
  201.                     $panier->setClient($user->getClient());
  202.                     $panier->setStatutCommande($statutPanier);
  203.                     $panier->setUtilisateur($user);
  204.                     $panier->setTypeDocumentCommercial(TypeDocumentCommercial::PANIER);
  205.                     $panier->setTotal(0);
  206.                     $panier->setAvecTaxe(0);
  207.                     $panier->setTotalTvaFraisPort(0);
  208.                     $panier->setTotalTva(0);
  209.                     $panier->setTauxTvaFraisPort(0);
  210.                     $panier->setTotalFraisPort(0);
  211.                     $panier->setTotalTtc(0);
  212.                     $panier->setTotalFraisPortTtc(0);
  213.                     $panier->setTotalPoids(0);
  214.                     $panier->setTotalHtAvecFraisPort(0);
  215.                     $em->persist($panier);
  216.                 }
  217.                 $panier->setReferenceClient($devis->getReferenceClient());
  218.                 foreach($devis->getArticleCommande() as $ac)
  219.                 {
  220.                     $article $ac->getArticle();
  221.                     $quantite $ac->getQuantite();
  222.                     $prixVente $article->getPrixVente();
  223.                     $prixRemise $article->getPrixVente();
  224.                     $remise 0;
  225.                     $remise_supplementaire 0;
  226.                     $articleCommande $panier->getArticleCommande()->filter(fn($a) => $a->getArticle()->getId() === $article->getId())->first();
  227.                     if ($articleCommande) {
  228.                         $remise_calcule $articleService->getRemisesPossibleArticleClient(['article'=>$article],$user->getClient());
  229.                         if($remise_calcule["remiseId"]>0) {
  230.                             $remise $remise_calcule["valeur"];
  231.                             $prixRemise $remise_calcule["prixRemise"];
  232.                         }
  233.                         $articleCommande->setRemise($remise);
  234.                         $articleCommande->setQuantite($articleCommande->getQuantite() + $quantite);
  235.                         $articleCommande->setTotalTtcDevise($prixRemise*($articleCommande->getQuantite()));
  236.                         $articleCommande->setTotalHt($prixRemise*($articleCommande->getQuantite()));
  237.                         $articleCommande->setTotalWithTax($prixRemise*($articleCommande->getQuantite()));
  238.                         $em->persist($articleCommande);
  239.                     }
  240.                     else
  241.                     {
  242.                         $remise_calcule $articleService->getRemisesPossibleArticleClient(['article'=>$article],$user->getClient());
  243.                         if($remise_calcule["remiseId"]>0) {
  244.                             $remise $remise_calcule["valeur"];
  245.                             $prixRemise $remise_calcule["prixRemise"];
  246.                         }
  247.                         $articleCommande = new ArticleCommande();
  248.                         $articleCommande->setLibelle($article->getLibelle());
  249.                         $articleCommande->setLibelleSecondaire($article->getLibelleSecondaire());
  250.                         $articleCommande->setReference($article->getReference());
  251.                         $articleCommande->setCommande($panier);
  252.                         $articleCommande->setArticle($article);
  253.                         $articleCommande->setLargeur($article->getLargeur());
  254.                         $articleCommande->setProfondeur($article->getProfondeur());
  255.                         $articleCommande->setHauteur($article->getHauteur());
  256.                         $articleCommande->setPoids($article->getPoids());
  257.                         $articleCommande->setQuantite($quantite);
  258.                         $articleCommande->setPrixBase($prixVente);
  259.                         $articleCommande->setPrixAvecRemise($prixRemise);
  260.                         $articleCommande->setPrixUnitaireTtc($prixVente);
  261.                         $articleCommande->setTvaVente(0);
  262.                         $articleCommande->setTva(0);
  263.                         $articleCommande->setRal($quantite);
  264.                         $articleCommande->setRemise($remise);
  265.                         $articleCommande->setRemiseSupplementaire($remise_supplementaire);
  266.                         $articleCommande->setTotalTtcDevise($prixRemise*$quantite);
  267.                         $articleCommande->setTotalHt($prixRemise*$quantite);
  268.                         $articleCommande->setTotalWithTax($prixRemise*$quantite);
  269.                         $em->persist($articleCommande);
  270.                         $panier->addArticleCommande($articleCommande);
  271.                         $em->flush();
  272.                     }
  273.                 }
  274.                 $panierService->majPanier($panier);
  275.                 $em->flush();
  276.                 $url $this->generateUrl('dtc_checkout_v2',['cible'=>'commande']);
  277.                 if($action=="editer")  $url $this->generateUrl('dtc_checkout_v2',['cible'=>'devis']);
  278.                 return new Response(json_encode(['rendu' => '''valide' => '1''url' => $url'type' => '']),200,['Content-Type' => 'application/json']);
  279.             }
  280.             else {
  281.                 foreach ($form->getErrors(true) as $error) {
  282.                     $fieldName $error->getOrigin() ? $error->getOrigin()->getName() : 'form';
  283.                     $errors[] = [
  284.                         'field' => $fieldName,
  285.                         'message' => $error->getMessage()
  286.                     ];
  287.                 }
  288.             }
  289.         }
  290.         $rendu    $this->renderView('GestionComerciale/Devis/devis_vers_panier_modal.html.twig',['form' => $form->createView(),'errors' => $errors,'panier'=>$panier,'devis'=>$devis,'action'=>$action]);
  291.         return new JsonResponse(['rendu' => $rendu'valide' => '0''url' => '''titre' => $titre_modal]);
  292.     }
  293. }