src/EventSubscriber/Boutique/CommandeSubscriber.php line 485

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Boutique;
  3. use App\Entity\Boutique\AvoirHistorique;
  4. use App\Entity\Boutique\Facture;
  5. use App\Entity\Boutique\ProduitStockBalance;
  6. use App\Entity\User\CagnotteHistorique;
  7. use App\Entity\User\PromotionHistorique;
  8. use App\Manager\Boutique\ProduitStockBalanceManager;
  9. use App\Manager\Boutique\WebserviceManager;
  10. use App\Utils\Toolbox;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use App\Utils\Emails\CommandeEmail;
  13. use App\Entity\Boutique\Produit;
  14. use App\Entity\Boutique\ProduitDeclinaison;
  15. use App\Entity\Boutique\Commande;
  16. use App\Entity\Boutique\CommandeHistorique;
  17. use App\Event\CommandeEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Routing\RouterInterface;
  20. class CommandeSubscriber implements EventSubscriberInterface
  21. {
  22.     private $em;
  23.     private $commandeEmail;
  24.     private $router;
  25.     private $webserviceManager;
  26.     /**
  27.      * @var ProduitStockBalanceManager
  28.      */
  29.     private $produitStockBalanceManager;
  30.     public function __construct(
  31.         EntityManagerInterface $entityManager,
  32.         CommandeEmail $commandeEmail,
  33.         RouterInterface $router,
  34.         WebserviceManager $webserviceManager,
  35.         ProduitStockBalanceManager $produitStockBalanceManager
  36.     ) {
  37.         $this->em                   $entityManager;
  38.         $this->commandeEmail        $commandeEmail;
  39.         $this->router               $router;
  40.         $this->webserviceManager    $webserviceManager;
  41.         $this->produitStockBalanceManager $produitStockBalanceManager;
  42.     }
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return array(
  46.             CommandeEvent::COMMANDE_ENREGISTRER_SUCCESS          => array('onCommandeEnregistrerSuccess', -10),
  47.             CommandeEvent::COMMANDE_ENREGISTRER_STATUT           => array('onCommandeEnregistrerStatut', -10),
  48.             CommandeEvent::COMMANDE_ENREGISTRER_MODEPAIEMENT     => array('onCommandeEnregistrerModepaiement', -10),
  49.         );
  50.     }
  51.     public function onCommandeEnregistrerSuccess(CommandeEvent $event)
  52.     {
  53.     }
  54.     public function onCommandeEnregistrerStatut(CommandeEvent $event)
  55.     {
  56.         $commande   $event->getCommande();
  57.         $eventUser  $event->getUser();
  58.         // Création Facture
  59.         if (
  60.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  61.             $commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE &&
  62.             is_null($commande->getFacture())
  63.         ) {
  64.             $numero strtoupper(Toolbox::generateRandomString(10));
  65.             $facture = new Facture();
  66.             $facture->setNumero($numero);
  67.             $this->em->persist($facture);
  68.             $this->em->flush();
  69.             $commande->setFacture($facture);
  70.             $this->em->persist($commande);
  71.             $this->em->flush();
  72.         }
  73.         // Création Facture avoir
  74.         if (
  75.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  76.             ($commande->getStatut() == Commande::STATUT_ANNULEE || $commande->getStatut() == Commande::STATUT_REMBOURSE) &&
  77.             !is_null($commande->getFacture()) &&
  78.             is_null($commande->getFactureAvoir())
  79.         ) {
  80.             $numero strtoupper(Toolbox::generateRandomString(10));
  81.             $facture = new Facture();
  82.             $facture->setNumero($numero);
  83.             $facture->setIsAvoir(true);
  84.             $this->em->persist($facture);
  85.             $this->em->flush();
  86.             $commande->setFactureAvoir($facture);
  87.             $this->em->persist($commande);
  88.             $this->em->flush();
  89.         }
  90.         // MAJ des ebillets gere par le site
  91.         if (
  92.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  93.             $commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE &&
  94.             false === $commande->getCommandeAyantDroits()->isEmpty()
  95.         ) {
  96.             $ebilletsEnRupture = [];
  97.             foreach ($commande->getCommandeAyantDroits() as $commandeAyantDroit) {
  98.                 $commandeLigne $commandeAyantDroit->getCommandeLigne();
  99.                 $entite $commandeLigne->getProduit();
  100.                 if (!is_null($commandeLigne->getProduitDeclinaison())) {
  101.                     $entite $commandeLigne->getProduitDeclinaison();
  102.                 }
  103.                 $eanDisponibles $entite->getProduitEansDisponibles();
  104.                 // Si on a des ebillets dispo en stock
  105.                 if (count($eanDisponibles)) {
  106.                     $produitEan $eanDisponibles[0];
  107.                     $produitEan->setCommandeAyantDroit($commandeAyantDroit);
  108.                     $produitEan->setUtilise(true);
  109.                     $produitEan->setNom($commandeAyantDroit->getNom());
  110.                     $produitEan->setPrenom($commandeAyantDroit->getPrenom());
  111.                     $this->em->persist($produitEan);
  112.                     $this->em->flush();
  113.                 }
  114.                 else {
  115.                     $commandeAyantDroit->setEbilletManquant(true);
  116.                     $this->em->persist($commandeAyantDroit);
  117.                     $this->em->flush();
  118.                 }
  119.                 // Envoi mails notification pour ebillet (produit)
  120.                 if (
  121.                     is_null($commandeLigne->getProduitDeclinaison()) &&
  122.                     !is_null($commandeLigne->getProduit()->getSeuilAlerteBillet()) &&
  123.                     count($commandeLigne->getProduit()->getProduitEansDisponibles()) <= $commandeLigne->getProduit()->getSeuilAlerteBillet()
  124.                 ) {
  125.                     $ebilletsEnRupture[$commandeLigne->getProduit()->getId().'_0'] = [
  126.                         'produit'       => $commandeLigne->getProduit(),
  127.                         'declinaison'   => null
  128.                     ];
  129.                 }
  130.                 // Envoi mails notification pour ebillet (declinaison)
  131.                 if (
  132.                     !is_null($commandeLigne->getProduitDeclinaison()) &&
  133.                     !is_null($commandeLigne->getProduitDeclinaison()->getSeuilAlerteBillet()) &&
  134.                     count($commandeLigne->getProduitDeclinaison()->getProduitEansDisponibles()) <= $commandeLigne->getProduitDeclinaison()->getSeuilAlerteBillet()
  135.                 ) {
  136.                     $ebilletsEnRupture[$commandeLigne->getProduit()->getId().'_'.$commandeLigne->getProduitDeclinaison()->getId()] = [
  137.                         'produit'       => $commandeLigne->getProduit(),
  138.                         'declinaison'   => $commandeLigne->getProduitDeclinaison(),
  139.                     ];
  140.                 }
  141.             }
  142.             foreach ($ebilletsEnRupture as $ebilletEnRupture) {
  143.                 $this->commandeEmail->alerteStockEbillet($ebilletEnRupture['produit'], $ebilletEnRupture['declinaison']);
  144.             }
  145.         }
  146.         // MAJ de la passation ReducCE
  147.         if (
  148.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  149.             $commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE &&
  150.             !empty($commande->getNumeroReducCe()) &&
  151.             false === $commande->getPassation()
  152.         ) {
  153.             $response $this->webserviceManager->passationCommande([
  154.                 'CE_ID'             => getenv('APP_PARTENAIRE_ID'),
  155.                 'commandes_numero'  => $commande->getNumeroReducCe()
  156.             ]);
  157.             $xml simplexml_load_string($response->PASSATION_COMMANDEResult->any);
  158.             if (isset($xml->NewDataSet->Commande->statut)) {
  159.                 $statut             = (string) $xml->NewDataSet->Commande->statut;
  160.                 $error              = (string) $xml->NewDataSet->Commande->message_erreur;
  161.                 $commandes_numero   = (string) $xml->NewDataSet->Commande->commandes_numero;
  162.                 if ("true" == $statut) {
  163.                     $commande->setPassation(true);
  164.                     $this->em->persist($commande);
  165.                     $this->em->flush();
  166.                 }
  167.                 else {
  168.                     $this->webserviceManager->addLog(
  169.                         'PASSATION_COMMANDEResult',
  170.                         $commande->getId(),
  171.                         $error
  172.                     );
  173.                 }
  174.             }
  175.         }
  176.         // Envoi mail de notification si la commande n'a pas deja ete dans ce statut et que le statut declenche un mail
  177.         if (
  178.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  179.             in_array($commande->getStatut(), $commande->getStatutEmail())
  180.         ) {
  181.             if ($commande->getStatut() == Commande::STATUT_ATTENTE_CHEQUE)
  182.                 $this->commandeEmail->alerteCommandeAttentePaiementCheque($commande);
  183.             if ($commande->getStatut() == Commande::STATUT_ATTENTE_VIREMENT)
  184.                 $this->commandeEmail->alerteCommandeAttentePaiementVirement($commande);
  185.             if ($commande->getStatut() == Commande::STATUT_ATTENTE_REAPPRO)
  186.                 $this->commandeEmail->alerteCommandeAttenteReapprovisionnement($commande);
  187.             if ($commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE)
  188.                 $this->commandeEmail->alerteCommandePaiementAccepte($commande);
  189.             if ($commande->getStatut() == Commande::STATUT_PAIEMENT_ERREUR)
  190.                 $this->commandeEmail->alerteCommandePaiementErreur($commande);
  191.             if ($commande->getStatut() == Commande::STATUT_ENCOURS_PREPA)
  192.                 $this->commandeEmail->alerteCommandeEnCoursPreparation($commande);
  193.             if (
  194.                 $commande->getStatut() == Commande::STATUT_ENCOURS_LIVRAISON ||
  195.                 $commande->getStatut() == Commande::STATUT_EXPEDIE_ENTREPRISE ||
  196.                 $commande->getStatut() == Commande::STATUT_EXPEDIE_DOMICILE ||
  197.                 $commande->getStatut() == Commande::STATUT_EXPEDIE_PARTIELLE_ENTREPRISE ||
  198.                 $commande->getStatut() == Commande::STATUT_EXPEDIE_PARTIELLE_DOMICILE
  199.             ) {
  200.                 $this->commandeEmail->alerteCommandeEnCoursLivraison($commande);
  201.             }
  202.             if (
  203.                 $commande->getStatut() == Commande::STATUT_LIVRAISON_ENTREPRISE ||
  204.                 $commande->getStatut() == Commande::STATUT_LIVRAISON_PARTIELLE_ENTREPRISE
  205.             ) {
  206.                 $this->commandeEmail->alerteCommandeLivre($commande);
  207.             }
  208.             if (
  209.                 $commande->getStatut() == Commande::STATUT_RETRAIT_AGENCE ||
  210.                 $commande->getStatut() == Commande::STATUT_RETRAIT_AGENCE_PARTIEL
  211.             ) {
  212.                 $this->commandeEmail->alerteCommandeRetraitAgence($commande);
  213.             }
  214.             if ($commande->getStatut() == Commande::STATUT_ANNULEE)
  215.                 $this->commandeEmail->alerteCommandeAnnulee($commande);
  216.             if ($commande->getStatut() == Commande::STATUT_REMBOURSE)
  217.                 $this->commandeEmail->alerteCommandeRemboursement($commande);
  218.             if ($commande->getStatut() == Commande::STATUT_REMBOURSE_PARTIEL)
  219.                 $this->commandeEmail->alerteCommandeRemboursementPartiel($commande);
  220.             if ($commande->getStatut() == Commande::STATUT_CREDIT_PARTIEL)
  221.                 $this->commandeEmail->alerteCommandeRemboursementPartiel($commande);
  222.             if ($commande->getStatut() == Commande::STATUT_LIVRE)
  223.                 $this->commandeEmail->alerteCommandeLivre($commande);
  224.             if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_BIENVENUE)
  225.                 $this->commandeEmail->alerteCommercialBienvenue($commande);
  226.             if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_PARRAINAGE)
  227.                 $this->commandeEmail->alerteCommercialParrainage($commande);
  228.             if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_EVALUATION)
  229.                 $this->commandeEmail->alerteCommercialEvaluation($commande);
  230.             if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_PROMOTION)
  231.                 $this->commandeEmail->alerteCommercialPromotion($commande);
  232.             if ($commande->getStatut() == Commande::STATUT_PAIEMENT_PARTIEL_ACCEPTE)
  233.                 $this->commandeEmail->alerteCommandePaiementPartielAccepte($commande);
  234.             if ($commande->getStatut() == Commande::STATUT_COMMANDE_ADMIN) {
  235.                 $adminToken $commande->generateAdminToken();
  236.                 $commande->setAdminToken($adminToken);
  237.                 $this->em->persist($commande);
  238.                 $this->em->flush();
  239.                 $this->commandeEmail->alerteCommandeAdmin($commande);
  240.             }
  241.         }
  242.         // MAJ des avoirs
  243.         if (
  244.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  245.             in_array($commande->getStatut(), $commande->getDebitAvoir()) &&
  246.             false === $commande->checkStatutPaiementAttente() &&
  247.             true === $commande->getUtiliserAvoir() &&
  248.             !is_null($commande->getMontantAvoir())
  249.         ) {
  250.             if ($commande->getStatut() == Commande::STATUT_ANNULEE || $commande->getStatut() == Commande::STATUT_REMBOURSE) {
  251.                 $avoirHistoriques $commande->getAvoirHistoriques();
  252.                 foreach ($avoirHistoriques as $avoirHistorique) {
  253.                     $avoir $avoirHistorique->getAvoir();
  254.                     $nouveauSolde = ($avoir->getReste() + $avoirHistorique->getMontant());
  255.                     $avoir->setReste($nouveauSolde);
  256.                     $this->em->persist($avoir);
  257.                     $newAvoirHistorique = clone ($avoirHistorique);
  258.                     $newAvoirHistorique->setOperation(AvoirHistorique::OPERATION_CREDIT);
  259.                     $this->em->persist($newAvoirHistorique);
  260.                 }
  261.                 $this->em->flush();
  262.             }
  263.             else {
  264.                 $avoirADebiter $commande->getMontantAvoir();
  265.                 foreach ($commande->getAvoirUtilises() as $avoir) {
  266.                     if ($avoir->getReste() > 0) {
  267.                         $avoirHistorique = new AvoirHistorique();
  268.                         $avoirHistorique->setOperateur($commande->getUser());
  269.                         $avoirHistorique->setAvoir($avoir);
  270.                         $avoirHistorique->setCommande($commande);
  271.                         $avoirHistorique->setOperation(AvoirHistorique::OPERATION_DEBIT);
  272.                         $nouveauSolde = ($avoir->getReste() - $avoirADebiter);
  273.                         if ($nouveauSolde >= 0) {
  274.                             $avoirHistorique->setMontant($avoirADebiter);
  275.                             $avoirADebiter 0;
  276.                             $avoir->setReste($nouveauSolde);
  277.                         }
  278.                         else {
  279.                             $avoirHistorique->setMontant($avoir->getReste());
  280.                             $avoirADebiter -= $avoir->getReste();
  281.                             $avoir->setReste(0);
  282.                         }
  283.                         $avoir->addAvoirHistorique($avoirHistorique);
  284.                         $this->em->persist($avoir);
  285.                         $this->em->flush();
  286.                         if (== $avoirADebiter) {
  287.                             break;
  288.                         }
  289.                     }
  290.                 }
  291.             }
  292.         }
  293.         // MAJ de la cagnotte CSE
  294.         if (
  295.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  296.             in_array($commande->getStatut(), $commande->getDebitCagnotte()) &&
  297.             false === $commande->checkStatutPaiementAttente() &&
  298.             true === $commande->getUtiliserCagnotte() &&
  299.             !is_null($commande->getMontantCagnotte())
  300.         ) {
  301.             $user $commande->getUser();
  302.             if ($commande->getStatut() == Commande::STATUT_ANNULEE || $commande->getStatut() == Commande::STATUT_REMBOURSE) {
  303.                 $nouveauMontantCagnotte = ($user->getCagnotte() + $commande->getMontantCagnotte());
  304.                 $user->setCagnotte($nouveauMontantCagnotte);
  305.                 $operation CagnotteHistorique::OPERATION_CREDIT;
  306.             } else {
  307.                 $nouveauMontantCagnotte = ($user->getCagnotte() - $commande->getMontantCagnotte());
  308.                 $user->setCagnotte($nouveauMontantCagnotte);
  309.                 $operation CagnotteHistorique::OPERATION_DEBIT;
  310.             }
  311.             $cagnotteHistorique = new CagnotteHistorique();
  312.             $cagnotteHistorique->setOperation($operation);
  313.             $cagnotteHistorique->setUser($user);
  314.             $cagnotteHistorique->setMontant($commande->getMontantCagnotte());
  315.             $cagnotteHistorique->setOperateur($user);
  316.             $cagnotteHistorique->setCommande($commande);
  317.             if (false === $user->getGroups()->isEmpty()) {
  318.                 $cagnotteHistorique->setGroup($user->getGroups()->first());
  319.             }
  320.             if (!is_null($commande->getPromotion())) {
  321.                 $cagnotteHistorique->setPromotion($commande->getPromotion());
  322.             }
  323.             $user->addCagnotteHistorique($cagnotteHistorique);
  324.             $this->em->persist($user);
  325.             $this->em->flush();
  326.         }
  327.         // MAJ subventions
  328.         if (
  329.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  330.             in_array($commande->getStatut(), $commande->getDebitSubvention()) &&
  331.             false === $commande->checkStatutPaiementAttente()
  332.         ) {
  333.             if (
  334.                 $commande->getStatut() == Commande::STATUT_ANNULEE ||
  335.                 $commande->getStatut() == Commande::STATUT_REMBOURSE ||
  336.                 $commande->getStatut() == Commande::STATUT_REMBOURSE_PARTIEL ||
  337.                 $commande->getStatut() == Commande::STATUT_CREDIT_PARTIEL
  338.             ) {
  339.                 $operation PromotionHistorique::OPERATION_CREDIT;
  340.             }
  341.             else {
  342.                 $operation PromotionHistorique::OPERATION_DEBIT;
  343.             }
  344.             $user $commande->getUser();
  345.             foreach ($commande->getLignes() as $ligne) {
  346.                 if (!is_null($ligne->getPromotion())) {
  347.                     $dateUtilisationAt $ligne->getCreatedAt();
  348.                     if ($operation == PromotionHistorique::OPERATION_CREDIT) {
  349.                         $dateUtilisationAt = new \DateTime();
  350.                     }
  351.                     $promotionHistorique = new PromotionHistorique();
  352.                     $promotionHistorique->setUser($user);
  353.                     $promotionHistorique->setOperateur($user);
  354.                     $promotionHistorique->setCommande($commande);
  355.                     $promotionHistorique->setCommandeLigne($ligne);
  356.                     $promotionHistorique->setDateUtilisationAt($dateUtilisationAt);
  357.                     $promotionHistorique->setPromotion($ligne->getPromotion());
  358.                     $promotionHistorique->setOperation($operation);
  359.                     if (false === $user->getGroups()->isEmpty()) {
  360.                         $promotionHistorique->setGroup($user->getGroups()->first());
  361.                     }
  362.                     if (
  363.                         ($commande->getStatut() == Commande::STATUT_REMBOURSE_PARTIEL || $commande->getStatut() == Commande::STATUT_CREDIT_PARTIEL) &&
  364.                         (!is_null($ligne->getQuantiteRetourne()) || !is_null($ligne->getMontantRembourse()))
  365.                     ) {
  366.                         if (!is_null($ligne->getMontantRembourse())) {
  367.                             $promotionHistorique->setMontant($ligne->getMontantRembourse());
  368.                         }
  369.                         if (!is_null($ligne->getQuantiteRetourne())) {
  370.                             $promotionHistorique->setQuantite($ligne->getQuantiteRetourne());
  371.                         }
  372.                         $commande->addPromotionHistorique($promotionHistorique);
  373.                     }
  374.                     if (
  375.                         $commande->getStatut() != Commande::STATUT_REMBOURSE_PARTIEL &&
  376.                         $commande->getStatut() != Commande::STATUT_CREDIT_PARTIEL
  377.                     ) {
  378.                         $promotionHistorique->setMontant($ligne->getMontantRemise() * $ligne->getQuantite());
  379.                         $promotionHistorique->setQuantite($ligne->getQuantite());
  380.                         $commande->addPromotionHistorique($promotionHistorique);
  381.                     }
  382.                 }
  383.             }
  384.             $this->em->persist($commande);
  385.             $this->em->flush();
  386.         }
  387.         // MAJ du stock si la commande n'a pas deja ete dans ce statut et que le statut declenche une MAJ du stock
  388.         if (
  389.             false === $commande->checkStatutHistorique($commande->getStatut()) &&
  390. //            false === $commande->checkStatutPaiementAttente() &&
  391.             in_array($commande->getStatut(), $commande->getStatutMajStock())
  392.         ) {
  393.             $this->produitStockBalanceManager->updateStockByCommande($commande);
  394.         }
  395.         // Ajout dans table historique
  396.         $commandeHistorique = new CommandeHistorique();
  397.         $commandeHistorique->setCommande($commande);
  398.         $commandeHistorique->setUser($eventUser);
  399.         $commandeHistorique->setStatut($commande->getStatut());
  400.         $commande->addCommandeHistorique($commandeHistorique);
  401.         $this->em->persist($commande);
  402.         $this->em->flush();
  403.     }
  404.     public function onCommandeEnregistrerModepaiement(CommandeEvent $event)
  405.     {
  406.     }
  407. }