src/EventSubscriber/VehicleEventSubscriber.php line 339

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\App;
  4. use App\Controller\AdminFDPGammaController;
  5. use App\Entity\CO2Criteria;
  6. use App\Entity\FDPGamma;
  7. use App\Entity\OTCData;
  8. use App\Entity\Vehicle;
  9. use App\Entity\Worklog;
  10. use App\Event\IssueEvent;
  11. use App\Event\OTCEvent;
  12. use App\Event\WorklogEvent;
  13. use App\Repository\CO2CriteriaRepository;
  14. use App\Repository\FDPGammaRepository;
  15. use App\Repository\OTCDataRepository;
  16. use App\Repository\VehicleRepository;
  17. use App\Services\CerfaService;
  18. use App\Services\EmailService;
  19. use DateTime;
  20. use Doctrine\Bundle\DoctrineBundle\Registry;
  21. use Doctrine\ORM\EntityManager;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use Doctrine\ORM\OptimisticLockException;
  24. use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
  25. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use App\Event\VehicleEvent;
  28. use App\Event\COCEvent;
  29. use App\Event\SAPEvent;
  30. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  31. use Symfony\Component\Routing\Router;
  32. use Symfony\Component\Routing\RouterInterface;
  33. use Symfony\Component\Validator\Constraints\Date;
  34. class VehicleEventSubscriber implements EventSubscriberInterface
  35. {
  36.     /** @var EntityManager $doctrine */
  37.     protected $em;
  38.     /** @var TraceableEventDispatcher $dispatcher */
  39.     private $dispatcher;
  40.     /** @var CerfaService $cerfaService */
  41.     private $cerfaService;
  42.     /** @var VehicleRepository $vehicleRepository */
  43.     private $vehicleRepository;
  44.     /** @var OTCDataRepository $otcDataRepository */
  45.     private $otcDataRepository;
  46.     /** @var CO2CriteriaRepository $co2CriteriaRepository */
  47.     private $co2CriteriaRepository;
  48.     /**
  49.      * @var AdminUrlGenerator $router
  50.      */
  51.     private $router;
  52.     /**
  53.      * @var FDPGammaRepository
  54.      */
  55.     private $FDPGammaRepository;
  56.     private $baseUrl 'http://localhost';
  57.     public function __construct(
  58.         EventDispatcherInterface $dispatcher,
  59.         EntityManagerInterface $em,
  60.         AdminUrlGenerator $router,
  61.         CerfaService $cerfaService
  62.     ) {
  63.         $this->dispatcher $dispatcher;
  64.         $this->em $em;
  65.         $this->router $router;
  66.         $this->cerfaService $cerfaService;
  67.         $this->vehicleRepository $em->getRepository(Vehicle::class);
  68.         $this->otcDataRepository $em->getRepository(OTCData::class);
  69.         $this->co2CriteriaRepository $em->getRepository(CO2Criteria::class);
  70.         $this->FDPGammaRepository $em->getRepository(FDPGamma::class);
  71.     }
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             'iris.vehicle.sap' => [['handleSap'], ['handleStatus'], ['handleCerfaData']],
  76.             'iris.vehicle.cdfs' => [['handleCdfs'], ['handleStatus'], ['handleCerfaData']],
  77.             'iris.vehicle.coc' => [['handleCocFluxForOtc'], ['handleCO2Eligibility'], ['handleStatus'], ['handleCerfaData']],
  78.             //region
  79.             // 'iris.vehicle.ecoc' => [],
  80.             //endregion
  81.             'iris.vehicle.otc' => [['handleOtc']],
  82.             'iris.vehicle.force' => [['handleStatus']],
  83.             'iris.vehicle.cerfa' => [['handleCerfaData']],
  84.         ];
  85.     }
  86.     public static function checkVehicleAAASend(Vehicle $vehicle)
  87.     {
  88.         if ($vehicle->getAAAStatus() != null && $vehicle->getRegisterStatus() == Vehicle::REGISTRABLE) {
  89.             if ($vehicle->getCerfaData() != null && $vehicle->getCerfaData()->getBlockUpdate() === true) {
  90.                 return false;
  91.             }
  92.             $vehicle->setAAAStatus(Vehicle::AAA_READY);
  93.             return true;
  94.         }
  95.         return false;
  96.     }
  97.     /**
  98.      * @param SAPEvent $event
  99.      */
  100.     public function handleSap(SAPEvent $event)
  101.     {
  102.         /** @var Vehicle $vehicle */
  103.         $vehicle      $event->getVehicle();
  104.         $isNewVehicle $event->getIsNewVehicle();
  105.         $oldClient    $event->getOldClient();
  106.         if (!$isNewVehicle && $oldClient != null && $vehicle->getPositionRunNumber() == null) { // Se era un veicolo già presente e aveva già un clientInvoice e non aveva un positionRunNumber
  107.             if ($oldClient->getId() != $vehicle->getClientInvoice()->getId()) { // Se è cambiato il clientInvoice
  108.                 if (
  109.                     $vehicle->getClientInvoice()->getIsGrandCompte() || (
  110.                         // DOP
  111.                         $vehicle->getTechnicalData()->getAdministrativeSalesChannel() == 'Own Dealers'
  112.                         && $vehicle->getTechnicalData()->getAccountingDocumentType()  == 'TZ'
  113.                         && $vehicle->getTechnicalData()->getAccountingDocumentDate()  >= new DateTime('2018-07-01')
  114.                     )
  115.                 ) {
  116.                     $vehicle->setPositioned(true);
  117.                     $vehicle->setClientPosition($vehicle->getClientInvoice());
  118.                     if ($vehicle->getRegisterStatus() != Vehicle::NON_REGISTRABLE) {
  119.                         if ($vehicle->getClientPosition()->getSivPartner()) {
  120.                             $vehicle->setClientRegistration($vehicle->getClientPosition()->getSivPartner());
  121.                         } else {
  122.                             $vehicle->setClientRegistration($vehicle->getClientPosition());
  123.                         }
  124.                     }
  125.                 } else {
  126.                     $vehicle->setPositioned(false);
  127.                     $vehicle->setClientPosition(null);
  128.                     if ($vehicle->getRegisterStatus() != Vehicle::NON_REGISTRABLE) {
  129.                         $vehicle->setClientRegistration(null);
  130.                         self::checkVehicleAAASend($vehicle);
  131.                         $vehicle->setRegisterStatus(Vehicle::NON_REGISTRABLE);
  132.                     }
  133.                 }
  134.                 // Force recalculation of 3en1
  135.                 if ($vehicle->getPrintableStatus() == Vehicle::PRINTED) {
  136.                     $vehicle->setPrintableStatus(Vehicle::PRINTABLE);
  137.                 }
  138.                 self::checkVehicleAAASend($vehicle);
  139.             }
  140.         } elseif ($isNewVehicle && $vehicle->getClientInvoice()->getIsGrandCompte()) { // Se il veicolo è nuovo e il clientInvoice è un grandCompte (il veicolo ha sempre un clientInvoice)
  141.             $vehicle->setPositioned(true);
  142.             $vehicle->setClientPosition($vehicle->getClientInvoice());
  143.         }
  144.         /** @var FDPGamma $FDPGamma */
  145.         $FDPGamma $this->FDPGammaRepository->findByVehicle($vehicle);
  146.         if (!$FDPGamma) {
  147.             $newFDPGamma = new FDPGamma();
  148.             $newFDPGamma->setFdp($vehicle->getTechnicalData()->getFDP());
  149.             $newFDPGamma->setGamma($vehicle->getTechnicalData()->getCGM());
  150.             $this->em->persist($newFDPGamma);
  151.         } else {
  152.             $vehicle->setInternalGamma($FDPGamma->getInternalGamma());
  153.         }
  154.         $this->em->flush();
  155.         $this->em->detach(isset($newFDPGamma) ? $newFDPGamma $FDPGamma);
  156.         if (isset($newFDPGamma)) {
  157.             $url $this->router->setController(AdminFDPGammaController::class)
  158.                 ->setAction('edit')
  159.                 ->setEntityId($newFDPGamma->getId())
  160.                 ->generateUrl();
  161.             if (strpos($url$this->baseUrl) === 0) {
  162.                 $relativeUrl substr($urlstrlen($this->baseUrl));
  163.             }
  164.             $event = new IssueEvent('Creating new FDP Gamma, please add manually the Internal Gamma'IssueEvent::SAP$relativeUrl);
  165.             $this->dispatcher->dispatch($event'iris.issue');
  166.         }
  167.     }
  168.     public function handleStatus(VehicleEvent $event$preventFlush false)
  169.     {
  170.         $vehicle $event->getVehicle();
  171.         // Regression on workflow
  172.         if (
  173.             $vehicle->getRegisterStatus() != Vehicle::REGISTERED &&
  174.             ($vehicle->getPrintableStatus() == Vehicle::PRINTABLE || $vehicle->getPrintableStatus() == Vehicle::PRINTED) &&
  175.             (!$vehicle->getDateCOC() || !$vehicle->getDateOTC() || !$vehicle->getDateTechData())
  176.         ) {
  177.             $vehicle->setPrintableStatus(Vehicle::INCOMPLETE);
  178.             $vehicle->setDatePrint3en1(null);
  179.             if ($vehicle->getRegisterStatus() == Vehicle::REGISTRABLE) {
  180.                 $vehicle->setRegisterStatus(Vehicle::NON_REGISTRABLE);
  181.                 $vehicle->setAAAStatus(Vehicle::AAA_READY);
  182.             }
  183.         }
  184.         if ((!$vehicle->getPrintableStatus() || $vehicle->getPrintableStatus() == Vehicle::INCOMPLETE) &&
  185.             ($vehicle->getDateCOC() && $vehicle->getDateOTC() && $vehicle->getDateTechData())
  186.         ) {
  187.             $vehicle->setPrintableStatus(Vehicle::PRINTABLE);
  188.         }
  189.         if (!$vehicle->getRegisterStatus() || $vehicle->getRegisterStatus() == Vehicle::NON_REGISTRABLE) {
  190.             if (($vehicle->getPrintableStatus() == Vehicle::PRINTABLE || $vehicle->getPrintableStatus() == Vehicle::PRINTED) && $vehicle->getPositioned()) {
  191.                 $vehicle->setRegisterStatus(Vehicle::REGISTRABLE);
  192.                 $vehicle->setAAAStatus(Vehicle::AAA_READY);
  193.             }
  194.         }
  195.         if (!$vehicle->getClientPosition() && $vehicle->getPositioned() && ($vehicle->getPrintableStatus() == Vehicle::PRINTABLE || $vehicle->getPrintableStatus() == Vehicle::PRINTED)) {
  196.             $vehicle->setClientPosition($vehicle->getClientInvoice());
  197.         }
  198.         if (
  199.             !$vehicle->getClientRegistration()
  200.             && $vehicle->getClientPosition()
  201.             && $vehicle->getRegisterStatus() == Vehicle::REGISTRABLE
  202.         ) {
  203.             if ($vehicle->getClientPosition()->getSivPartner()) {
  204.                 $vehicle->setClientRegistration($vehicle->getClientPosition()->getSivPartner());
  205.             } else {
  206.                 $vehicle->setClientRegistration($vehicle->getClientPosition());
  207.             }
  208.         }
  209.         if ($preventFlush !== true) {
  210.             $this->em->flush();
  211.         }
  212.         $event->setVehicle($vehicle);
  213.     }
  214.     public function handleCdfs(VehicleEvent $event)
  215.     {
  216.         /** @var Vehicle $vehicle */
  217.         $vehicle $event->getVehicle();
  218.         if ($vehicle->getClientPosition() && $vehicle->getPositioned() && $vehicle->getClientRegistration() != null) {
  219.             if ($vehicle->getClientPosition()->getSivPartner()) {
  220.                 if ($vehicle->getClientRegistration()->getId() != $vehicle->getClientPosition()->getSivPartner()->getId()) {
  221.                     $vehicle->setClientRegistration($vehicle->getClientPosition()->getSivPartner());
  222.                     self::checkVehicleAAASend($vehicle);
  223.                 }
  224.             } else {
  225.                 if ($vehicle->getClientRegistration()->getId() != $vehicle->getClientPosition()->getId()) {
  226.                     $vehicle->setClientRegistration($vehicle->getClientPosition());
  227.                     self::checkVehicleAAASend($vehicle);
  228.                 }
  229.             }
  230.         }
  231.     }
  232.     public function handleCocFluxForOtc(COCEvent $event)
  233.     {
  234.         $vehicle $event->getVehicle();
  235.         $user $event->getUser();
  236.         $numReception $event->getNumReception();
  237.         $otc $this->otcDataRepository->getOTCForNumReception($vehicle$numReception);
  238.         if ($otc) {
  239.             $oldOtc $vehicle->getOtc();
  240.             if ($oldOtc != null && $oldOtc->getId() != $otc->getId()) {
  241.                 if ($vehicle->getPrintableStatus() == Vehicle::PRINTED) {
  242.                     $vehicle->setPrintableStatus(Vehicle::PRINTABLE);
  243.                 }
  244.                 self::checkVehicleAAASend($vehicle);
  245.             }
  246.             $vehicle->setDateOtc($otc->getDateReception());
  247.             $vehicle->setOtc($otc);
  248.             $eventWorklog = new WorklogEvent($vehicle'otc'$user);
  249.             $this->dispatcher->dispatch($eventWorklog'iris.worklog');
  250.         } else {
  251.             $vehicle->setDateOtc(null);
  252.             $vehicle->setOtc(null);
  253.             if ($cerfa $vehicle->getCerfaData()) {
  254.                 $vehicle->setCerfaData(null);
  255.                 $this->em->remove($cerfa);
  256.             }
  257.         }
  258.         $this->em->flush();
  259.         $event->setVehicle($vehicle);
  260.     }
  261.     public function handleCO2Eligibility(VehicleEvent $event)
  262.     {
  263.         $vehicle $event->getVehicle();
  264.         if ($otc $vehicle->getOtc()) {
  265.             if (($environmentalClass $otc->getEnvironmentalClass()) && ($category $otc->getCategory())) {
  266.                 $CO2Eligibility = (bool)$this->co2CriteriaRepository->findOneBy(array('environmentalClass' => $environmentalClass'vehicleCategory' => $category));
  267.                 $vehicle->setCO2Eligibility($CO2Eligibility);
  268.             }
  269.         }
  270.         $event->setVehicle($vehicle);
  271.     }
  272.     public function handleOtc(OTCEvent $otcEvent)
  273.     {
  274.         /** @var OTCData $otc */
  275.         $otc $otcEvent->getOtc();
  276.         $otc $this->em->merge($otc);
  277.         /** @var VehicleRepository $vehicleRepository */
  278.         $vehicles $this->vehicleRepository->findForOtc($otc->getTvv(), $otc->getNumReception());
  279.         foreach ($vehicles as $vehicle) {
  280.             $vehicle->setDateOtc($otc->getDateReception());
  281.             $vehicle->setOtc($otc);
  282.             $user $otcEvent->getUser();
  283.             $eventWorklog = new WorklogEvent($vehicle'otc'$user);
  284.             $this->dispatcher->dispatch($eventWorklog'iris.worklog');
  285.             if ($vehicle->getPrintableStatus() == Vehicle::PRINTED) {
  286.                 $vehicle->setPrintableStatus(Vehicle::PRINTABLE);
  287.             }
  288.             self::checkVehicleAAASend($vehicle);
  289.         }
  290.         foreach ($vehicles as $vehicle) {
  291.             $vehicleEvent = new VehicleEvent($vehicle);
  292.             $this->handleCO2Eligibility($vehicleEvent);
  293.             $this->handleStatus($vehicleEventtrue);
  294.             $this->handleCerfaData($vehicleEventtrue);
  295.         }
  296.         $this->em->flush();
  297.     }
  298.     public static $counter 0;
  299.     public function handleCerfaData(VehicleEvent $event$preventFlush false)
  300.     {
  301.         $vehicle $event->getVehicle();
  302.         if ($vehicle->getPrintableStatus() == Vehicle::PRINTABLE) {
  303.             $flush self::$counter++ % 500 == 0;
  304.             $this->cerfaService->generateCerfaForVehicle($vehicle, !$flush);
  305.         }
  306.     }
  307. }