src/Controller/CommandeController.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Adresse;
  4. use App\Entity\Document;
  5. use App\Entity\DocumentLigne;
  6. use App\Entity\LdvStatutProjet;
  7. use App\Entity\Projet;
  8. use App\Entity\Societe;
  9. use App\Entity\Souche;
  10. use App\Entity\Tiers;
  11. use App\Repository\DocumentRepository;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use App\Tools\JsonRes;
  18. use Doctrine\Persistence\ManagerRegistry;
  19. use Doctrine\Persistence\ObjectManager;
  20. use function Symfony\Component\Translation\t;
  21. class CommandeController extends AbstractController
  22. {
  23.     private ?ObjectManager $em null;
  24.     private ?string $prefixSociete null;
  25.     /**
  26.      * Stockage des informations de la société pour affichage sur entête document
  27.      * @var object|mixed|null
  28.      */
  29.     private ?object $dataSociete null;
  30.     public function __construct(RequestStack $requestStackManagerRegistry $doctrine)
  31.     {
  32.         $request $requestStack->getCurrentRequest();
  33.         //TODO: page erreur subdomain
  34.         if(null===$request->getSession()->get('societe')) return $this->render('misc/404.html.twig');
  35.         // on récupère le sous-domaine stocké en session pour pointer vers le bon entity manager
  36.         $subdomain $request->getSession()->get('societe')->getBase();
  37.         $this->em $doctrine->getManager($subdomain);
  38.         $this->prefixSociete $request->getSession()->get('societe')->getPrefix();
  39.         if(null!==$this->prefixSociete$this->prefixSociete strtoupper($this->prefixSociete);
  40.         $jsonString $request->getSession()->get('societe')->getData();
  41.         if(null!==$jsonString) {
  42.             $this->dataSociete json_decode($jsonString);
  43.             $this->dataSociete->lib_societe $request->getSession()->get('societe')->getLibSociete();
  44.             $this->dataSociete->logo_path $request->getSession()->get('societe')->getLogoPath();
  45.         }
  46.     }
  47.     /**
  48.      * @Route("/commande/vierge/{id}", methods={"GET"}, name="init_commande_vierge")
  49.      */
  50.     public function getCommandeVierge($id)
  51.     {
  52.         $projetRepository $this->em->getRepository(Projet::class);
  53.         /** @var Projet $projet */
  54.         $projet $projetRepository->find($id);
  55.         if(null===$projet) return $this->render('misc/404.html.twig');
  56.         $commandeVierge = new Document();
  57.         $commandeVierge->initDocVierge($projet'COMMANDE PRESTATAIRE');
  58.         return $this->render('projet/prestataire/commande-form.html.twig', [
  59.             'commande_vierge' => $commandeVierge,
  60.             'label_btn_cancel' => 'Annuler',
  61.         ]);
  62.     }
  63.     /**
  64.      * @Route("/commande/reload-list/{idProjet}", methods={"GET"}, name="commande_reload_list")
  65.      */
  66.     public function commandeReloadList($idProjet)
  67.     {
  68.         $projetRepository $this->em->getRepository(Projet::class);
  69.         /** @var Projet $projet */
  70.         $projet $projetRepository->find($idProjet);
  71.         $docList $projet->getDocuments()->filter(function (Document $document){
  72.             return $document->getTypeDocument() === 'COMMANDE PRESTATAIRE';
  73.         });
  74.         return $this->render('projet/prestataire/document-commande-list.html.twig', [
  75.             'commandeList' => $docList,
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/commande/insert", methods={"POST"}, name="insert_commande")
  80.      */
  81.     public function insertCommande(Request $request)
  82.     {
  83.         //TODO: page erreur entity manager
  84.         if(null===$this->em) return $this->render('misc/404.html.twig');
  85.         // Initialisation du retour Json
  86.         $res = new JsonRes($request->request->all());
  87.         if(!$res->isSuccess()) die($res->returnJson());
  88.         $data $res->getData();
  89.         if(!isset($data['id_projet'])) $res->addErrMsg('champs id_projet inconnu');
  90.         if(!isset($data['id_prestataire'])) $res->addErrMsg('champs id_prestataire inconnu');
  91.         if(!isset($data['reference_commande'])) $res->addErrMsg('champs reference_commande inconnu');
  92.         if(!isset($data['puht'])) $res->addErrMsg('champs puht inconnu');
  93.         if(!isset($data['taux_tva'])) $res->addErrMsg('champs taux_tva inconnu');
  94.         if(!isset($data['qte'])) $res->addErrMsg('champs qte inconnu');
  95.         if(!isset($data['date_commande'])) $res->addErrMsg('champs date_commande inconnu');
  96.         if(!isset($data['statut_commande'])) $res->addErrMsg('champs statut_commande inconnu');
  97.         if(!isset($data['description'])) $res->addErrMsg('champs description inconnu');
  98.         if(!$res->isSuccess()) die($res->returnJson());
  99.         if(''===($data['id_projet'])) $res->addErrMsg('ID Projet manquant');
  100.         if(''===($data['id_prestataire'])) $res->addErrMsg('ID Prestataire manquant');
  101.         if(''===($data['reference_commande'])) $res->addErrMsg('Référence commande obligatoire');
  102.         if(''===($data['puht'])) $res->addErrMsg('Prix unitaire HT obligatoire');
  103.         if(''===($data['taux_tva'])) $res->addErrMsg('Taux TVA obligatoire');
  104.         if(''===($data['qte'])) $res->addErrMsg('Quantité obligatoire');
  105.         if(''===($data['date_commande'])) $res->addErrMsg('Date commande obligatoire');
  106.         if(''===($data['statut_commande'])) $res->addErrMsg('Statut commande obligatoire');
  107.         if(!$res->isSuccess()) die($res->returnJson());
  108.         $dateBdc date_create_from_format('Y-m-d'$data['date_commande']);
  109.         if(!$dateBdc$res->addErrMsg('Le format de la date est incorrect');
  110.         $projetRepository $this->em->getRepository(Projet::class);
  111.         /** @var Projet $projet */
  112.         $projet $projetRepository->find($data['id_projet']);
  113.         if(null==$projet$res->addErrMsg('Projet non identifié');
  114.         $societeRepository $this->em->getRepository(Societe::class);
  115.         /** @var Societe $prestataire */
  116.         $prestataire $societeRepository->find($data['id_prestataire']);
  117.         if(null==$prestataire$res->addErrMsg('Prestataire non identifié');
  118.         if(!$res->isSuccess()) die($res->returnJson());
  119.         /** @var \App\Entity\User $currentUser */
  120.         $currentUser $this->getUser()->getId();
  121.         $data['puht'] = floatval($data['puht']);
  122.         $data['taux_tva'] = floatval($data['taux_tva']);
  123.         $data['qte'] = floatval($data['qte']);
  124.         $montantHT $data['puht'] * $data['qte'];
  125.         $montantTVA $montantHT $data['taux_tva'];
  126.         $commande = new Document();
  127.         $commande->setProjet($projet)
  128.             ->setTiers($prestataire)
  129.             ->setStatut($data['statut_commande'])
  130.             ->setReference($data['reference_commande'])
  131.             ->setTypeDocument('COMMANDE PRESTATAIRE')
  132.             ->setDateBdc($dateBdc)
  133.             ->setDateCrea(new \DateTime())
  134.             ->setUserCrea($currentUser)
  135.             ->setMontantHt($montantHT)
  136.             ->setMontantTva($montantTVA)
  137.             ->setCommentaire($data['description'])
  138.         ;
  139.         $this->em->persist($commande);
  140.         $this->em->flush();
  141.         $numPiece 'BDC-' $commande->getId();
  142.         $commande->setNumPiece($numPiece);
  143.         $this->em->persist($commande);
  144.         $this->em->flush();
  145.         $ligne = new DocumentLigne();
  146.         $ligne->setDocument($commande)
  147.             ->setLibelle($data['reference_commande'])
  148.             ->setMontantHt($montantHT)
  149.             ->setTauxTva($data['taux_tva'])
  150.             ->setMontantTva($montantTVA)
  151.             ->setDateCrea(new \DateTime())
  152.             ->setUserCrea($currentUser)
  153.             ->setQte($data['qte'])
  154.             ->setPuht($data['puht'])
  155.         ;
  156.         $commande->addDocumentLigne($ligne);
  157.         $this->em->persist($commande);
  158.         $this->em->flush();
  159.         $data['id_document'] = $commande->getId();
  160.         $res->setData($data);
  161.         die($res->returnJson());
  162.     }
  163.     /**
  164.      * @Route("/commande/update", methods={"POST"}, name="update_commande")
  165.      */
  166.     public function updateCommande(Request $request)
  167.     {
  168.         //TODO: page erreur entity manager
  169.         if(null===$this->em) return $this->render('misc/404.html.twig');
  170.         // Initialisation du retour Json
  171.         $res = new JsonRes($request->request->all());
  172.         if(!$res->isSuccess()) die($res->returnJson());
  173.         $data $res->getData();
  174.         if(!isset($data['id_commande'])) $res->addErrMsg('champs id_commande inconnu');
  175.         if(!isset($data['reference_commande'])) $res->addErrMsg('champs reference_commande inconnu');
  176.         if(!isset($data['puht'])) $res->addErrMsg('champs puht inconnu');
  177.         if(!isset($data['taux_tva'])) $res->addErrMsg('champs taux_tva inconnu');
  178.         if(!isset($data['qte'])) $res->addErrMsg('champs qte inconnu');
  179.         if(!isset($data['date_commande'])) $res->addErrMsg('champs date_commande inconnu');
  180.         if(!isset($data['statut_commande'])) $res->addErrMsg('champs statut_commande inconnu');
  181.         if(!isset($data['description'])) $res->addErrMsg('champs description inconnu');
  182.         if(!$res->isSuccess()) die($res->returnJson());
  183.         if(''===($data['id_commande'])) $res->addErrMsg('ID Commande manquant');
  184.         if(''===($data['reference_commande'])) $res->addErrMsg('Référence commande obligatoire');
  185.         if(''===($data['puht'])) $res->addErrMsg('Prix unitaire HT obligatoire');
  186.         if(''===($data['taux_tva'])) $res->addErrMsg('Taux TVA obligatoire');
  187.         if(''===($data['qte'])) $res->addErrMsg('Quantité obligatoire');
  188.         if(''===($data['date_commande'])) $res->addErrMsg('Date commande obligatoire');
  189.         if(''===($data['statut_commande'])) $res->addErrMsg('Statut commande obligatoire');
  190.         if(!$res->isSuccess()) die($res->returnJson());
  191.         $dateBdc date_create_from_format('Y-m-d'$data['date_commande']);
  192.         if(!$dateBdc$res->addErrMsg('Le format de la date est incorrect');
  193.         $documentRepository $this->em->getRepository(Document::class);
  194.         /** @var Document $commande */
  195.         $commande $documentRepository->find($data['id_commande']);
  196.         if(null==$commande$res->addErrMsg('Commande non identifiée');
  197.         if(!$res->isSuccess()) die($res->returnJson());
  198.         /** @var \App\Entity\User $currentUser */
  199.         $currentUser $this->getUser()->getId();
  200.         $data['puht'] = floatval($data['puht']);
  201.         $data['taux_tva'] = floatval($data['taux_tva']);
  202.         $data['qte'] = floatval($data['qte']);
  203.         $montantHT $data['puht'] * $data['qte'];
  204.         $montantTVA $montantHT $data['taux_tva'];
  205.         $commande->setStatut($data['statut_commande'])
  206.             ->setReference($data['reference_commande'])
  207.             ->setDateBdc($dateBdc)
  208.             ->setDateModif(new \DateTime())
  209.             ->setUserModif($currentUser)
  210.             ->setMontantHt($montantHT)
  211.             ->setMontantTva($montantTVA)
  212.             ->setCommentaire($data['description'])
  213.         ;
  214.         $this->em->persist($commande);
  215.         $this->em->flush();
  216.         $ligne $commande->getDocumentLignes()->first();
  217.         $ligne->setDocument($commande)
  218.             ->setLibelle($data['reference_commande'])
  219.             ->setMontantHt($montantHT)
  220.             ->setTauxTva($data['taux_tva'])
  221.             ->setMontantTva($montantTVA)
  222.             ->setDateModif(new \DateTime())
  223.             ->setUserModif($currentUser)
  224.             ->setQte($data['qte'])
  225.             ->setPuht($data['puht'])
  226.         ;
  227.         $this->em->persist($ligne);
  228.         $this->em->flush();
  229.         $data['id_document'] = $commande->getId();
  230.         $res->setData($data);
  231.         die($res->returnJson());
  232.     }
  233.     /**
  234.      * @Route("/commande/toggle-statut", methods={"POST"}, name="commande_toggle_statut")
  235.      */
  236.     public function toggleStatutCommande(Request $request)
  237.     {
  238.         //TODO: page erreur entity manager
  239.         if(null===$this->em) return $this->render('misc/404.html.twig');
  240.         // Initialisation du retour Json
  241.         $res = new JsonRes($request->request->all());
  242.         if(!$res->isSuccess()) die($res->returnJson());
  243.         $data $res->getData();
  244.         if(!isset($data['id_commande'])) $res->addErrMsg('champs id_commande inconnu');
  245.         if(!$res->isSuccess()) die($res->returnJson());
  246.         if(''===$data['id_commande']) $res->addErrMsg('id_commande obligatoire');
  247.         if(!$res->isSuccess()) die($res->returnJson());
  248.         if(0===intval($data['id_commande'])) $res->addErrMsg('id_commande non conforme');
  249.         if(!$res->isSuccess()) die($res->returnJson());
  250.         $commandeRepository $this->em->getRepository(Document::class);
  251.         /** @var Document $commande */
  252.         $commande $commandeRepository->find($data['id_commande']);
  253.         if(null===$commande$res->addErrMsg('Commande non identifiée');
  254.         if(!$res->isSuccess()) die($res->returnJson());
  255.         $statut $commande->getStatut();
  256.         if('Non payée'===$statut) {
  257.             $commande->setStatut('Payée');
  258.             $commande->setDatePaiement(new \DateTime());
  259.         }
  260.         if('Payée'===$statut || null===$statut) {
  261.             $commande->setStatut('Non payée');
  262.             $commande->setDatePaiement(null);
  263.         }
  264.         $commande->setDateModif(new \DateTime());
  265.         $this->em->persist($commande);
  266.         $this->em->flush();
  267.         die($res->returnJson());
  268.     }
  269.     /**
  270.      * @Route("/commande/edit/{id}", methods={"GET"}, name="edit_commande")
  271.      */
  272.     public function getCommandeForEdit($id)
  273.     {
  274.         $commandeRepository $this->em->getRepository(Document::class);
  275.         $commande $commandeRepository->find($id);
  276.         return $this->render('projet/prestataire/commande-form.html.twig', [
  277.             'commande' => $commande,
  278.             'label_btn_cancel' => 'Supprimer',
  279.         ]);
  280.     }
  281.     /**
  282.      * @Route("/commande/delete-document", methods={"POST"}, name="delete_commande")
  283.      */
  284.     public function deleteCommande(Request $request): Response
  285.     {
  286.         //TODO: page erreur entity manager
  287.         if(null===$this->em) return $this->render('misc/404.html.twig');
  288.         // Initialisation du retour Json
  289.         $res = new JsonRes($request->request->all());
  290.         if($res->isSuccess()) {
  291.             $data $res->getData();
  292.             if(!isset($data['id_commande'])) $res->addErrMsg('champs id_commande inconnu');
  293.             if($res->isSuccess()) {
  294.                 if(''===$data['id_commande']) $res->addErrMsg('champs id_commande obligatoire');
  295.                 if($res->isSuccess()) {
  296.                     $commandeRepository $this->em->getRepository(Document::class);
  297.                     /** @var Document $commande */
  298.                     $commande $commandeRepository->find($data['id_commande']);
  299.                     if(null===$commande$res->addErrMsg('Commande non identifiée');
  300.                     if($res->isSuccess()) {
  301.                         $docLigneRepository $this->em->getRepository(DocumentLigne::class);
  302.                         foreach ($commande->getDocumentLignes()->toArray() as $documentLigne) {
  303.                             $docLigneRepository->remove($documentLigne);
  304.                         }
  305.                         $commandeRepository->remove($commande);
  306.                         $res->setData($data);
  307.                     }
  308.                 }
  309.             }
  310.         }
  311.         die($res->returnJson());
  312.     }
  313. }