src/Services/CerfaService.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\Catalogue3en1;
  4. use App\Repository\Catalogue3en1Repository;
  5. use App\Repository\CerfaDataRepository;
  6. use App\Repository\VehicleRepository;
  7. use Doctrine\ORM\EntityManager;
  8. use Doctrine\Bundle\DoctrineBundle\Registry;
  9. use App\Exception\WrongCerfaDataStatusException;
  10. use App\Exception\MissingCerfaDataException;
  11. use App\Entity\Vehicle;
  12. use App\Entity\CerfaData;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. /**
  15. * CerfaService
  16. */
  17. class CerfaService
  18. {
  19.     /**
  20.      * @var CerfaDataRepository $cerfaRepo;
  21.      */
  22.     protected $cerfaRepo;
  23.     /**
  24.      * @var VehicleRepository $vehicleRepo;
  25.      */
  26.     protected $vehicleRepo;
  27.     /**
  28.      * @var Catalogue3en1Repository $catalogueRepo;
  29.      */
  30.     protected $catalogueRepo;
  31.     /**
  32.      * @var EntityManagerInterface $em;
  33.      */
  34.     protected $em;
  35.     protected $parameters;
  36.     function __construct(
  37.         EntityManagerInterface $em
  38.     ) {
  39.         $this->em $em;
  40.         $this->cerfaRepo $em->getRepository(CerfaData::class);
  41.         $this->vehicleRepo $em->getRepository(Vehicle::class);
  42.         $this->catalogueRepo $em->getRepository(Catalogue3en1::class);
  43.     }
  44.     /**
  45.      * Wrapper to be called externally to retrieve a pdf.
  46.      * @param  Vehicle $vehicle [description]
  47.      * @return [type]           [description]
  48.      */
  49.     public function getCerfaForVehicle(Vehicle $vehicle)
  50.     {
  51.         if (!$vehicle->getCerfaData()) {
  52.             throw new MissingCerfaDataException($vehicle);
  53.         }
  54.         return $vehicle->getCerfaData();
  55.     }
  56.     /**
  57.      * Wrapper to be called externally to retrieve a pdf.
  58.      * @param  Vehicle $vehicle [description]
  59.      * @return [type]           [description]
  60.      */
  61.     public function generateCerfaForVehicle(Vehicle $vehicle$preventFlush false)
  62.     {
  63.         if ( $vehicle->getPrintableStatus() != Vehicle::PRINTABLE && $vehicle->getPrintableStatus() != Vehicle::PRINTED ) {
  64.             throw new WrongCerfaDataStatusException($vehicle);
  65.         }
  66.         if (!$this->parameters) {
  67.             $this->parameters $this->catalogueRepo->findAllAssociative();
  68.         }
  69.         $parameters $this->parameters;
  70.         if ($vehicle->getCerfaData()) {
  71.             $cerfaData $vehicle->getCerfaData();
  72.         } else {
  73.             $cerfaData = new CerfaData($vehicle);
  74.             $this->em->persist($cerfaData);
  75.         }
  76.         if($cerfaData->getBlockUpdate() === false || $cerfaData->getBlockUpdate() === null) {
  77.             
  78.             // coalascing different customers (client) to get the correct one 
  79.             $clientForCerfa $vehicle->getClientRegistration() ?? $vehicle->getClientPosition() ?? $vehicle->getClientInvoice();
  80.             $cerfaData->setClientCode($this->optionalField($clientForCerfa->getClientCode()))
  81.                 ->setClientName($this->optionalField($clientForCerfa->getName()))
  82.                 ->setInvoiceNumber($this->optionalField($vehicle->getTechnicalData()->getAccountingDocumentCode()))
  83.                 ->setVan($this->optionalField($vehicle->getVan()))
  84.                 ->setBrand($this->optionalField($vehicle->getOtc()->getBrand()))
  85.                 ->setCnit($this->optionalField($vehicle->getOtc()->getCNIT()))
  86.                 ->setTvv($this->optionalField($vehicle->getTvv()))
  87.                 ->setVin($this->optionalField($vehicle->getVin()))
  88.                 ->setOtcReceptionDate($this->optionalField($vehicle->getOtc()->getDateReception()))
  89.                 ->setOtcReceptionCode($this->optionalField($vehicle->getOtc()->getNumReception()))
  90.                 // PARAMETER Signature
  91.                 ->setSignature($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"signature")))
  92.                 // PARMETER F1
  93.                 ->setMaxTechWeight($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"maxTechWeight")))
  94.                 ->setMaxStateWeight($this->optionalField($vehicle->getOtc()->getMassChargMax()))
  95.                 ->setMaxTotalWeight($this->optionalField($vehicle->getOtc()->getMassCombo()))
  96.                 // PARAMETER G
  97.                 ->setServiceMass($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"serviceMass")))
  98.                 // PARAMETER G1
  99.                 ->setNetNationalMass($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"netNationalMass")))
  100.                 ->setCategory($this->optionalField($vehicle->getOtc()->getCategory()))
  101.                 ->setNationalCategory($this->optionalField($vehicle->getOtc()->getGenre()))
  102.                 ->setBodyCE($this->optionalField($vehicle->getOtc()->getBodyType()))
  103.                 ->setNationalBody($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"nationalBody")))
  104.                 ->setEngineSize($this->optionalField($vehicle->getOtc()->getEngineSize()))
  105.                 ->setMaxNetPower($this->optionalField($vehicle->getOtc()->getPowerNetMax()))
  106.                 ->setFuelType($this->optionalField($vehicle->getOtc()->getEnergy()))
  107.                 ->setNationalAdministrativePower($this->optionalField($vehicle->getOtc()->getPowerAdministrative()))
  108.                 // Always -
  109.                 ->setPowerMassRatio("-")
  110.                 ->setSeats($this->optionalField($vehicle->getSeats()))
  111.                 // ONLY BUS HAVE IT. TRACK IS -
  112.                 ->setInitialSeats('-')
  113.                 ->setSoundLevel($this->optionalField($vehicle->getOtc()->getNivSonorArretMax()))
  114.                 ->setSoundEngineSpeed($this->optionalField($vehicle->getOtc()->getRegimNivSonor()))
  115.                 ->setCo2Emissions($this->optionalField(round(($this->getCOC($vehicle'FO49.04.EF')) ?: $vehicle->getOtc()->getCO2Combo())))
  116.                 ->setEnvironmentalClass($this->optionalField($vehicle->getOtc()->getEnvironmentalClass()))
  117.                 ->setCommercialName($this->optionalField($vehicle->getOtc()->getCommercialName()))
  118.             ;
  119.             //only if Client Requested
  120.             if ($vehicle->getClientInvoice()->getOrderRequested()) {
  121.                 $cerfaData
  122.                     ->setOrderNumber($this->optionalField($vehicle->getTechnicalData()->getPONumber()))
  123.                 ;
  124.             }
  125.             /**
  126.              * Persist
  127.              */
  128.             if ($preventFlush !== true) {
  129.                 $this->em->flush();
  130.             }
  131.         }
  132.         return $cerfaData;
  133.     }
  134.     private function optionalField($value$default '-')
  135.     {
  136.         return ($value) ? $value $default;
  137.     }
  138.     private function getCOC(Vehicle $vehicle$code) {
  139.         $cocArray $vehicle->getCocs();
  140.         foreach ($cocArray as $coc) {
  141.             if ($coc->getCode() == $code) {
  142.                 return ($coc->getValue() != '-' && $coc->getValue() != '--') ? $coc->getValue() : null;
  143.             }
  144.         }
  145.         return null;
  146.     }
  147.     private function parameterCatalogueHandler($parameters$vehicle$field)
  148.     {
  149.         $param $parameters[$field];
  150.         $type $param->getType();
  151.         if ($type == 'constant') {
  152.             return $param->getValue();
  153.         } elseif ($type == 'otc') {
  154.             $otc $vehicle->getOtc();
  155.             if (method_exists($otc'get'.ucfirst($param->getValue()))) {
  156.                 return call_user_func(array($otc'get'.ucfirst($param->getValue())));
  157.             } else {
  158.                 return "ERR - otc param";
  159.             }
  160.         } elseif ($type == 'coc') {
  161.             $cocArray $vehicle->getCocs();
  162.             foreach ($cocArray as $coc) {
  163.                 if ($coc->getCode() == $param->getValue()) {
  164.                     return $coc->getValue();
  165.                 }
  166.             }
  167.             return "ERR - coc param";
  168.         } elseif ($type == 'procedure') {
  169.             try {
  170.                 if (method_exists($this$param->getValue())) {
  171.                     return call_user_func(array($this$param->getValue()), $vehicle$param->getArgs());
  172.                 } else {
  173.                     return "ERR - procedure not found";
  174.                 }
  175.             } catch (\Exception $e) {
  176.                 return "ERR - procedure execution";
  177.             }
  178.         }
  179.         return "ERR - general param";
  180.     }
  181.     /**
  182.      * PROCEDURE for Carr.Nationale (J.3)
  183.      * @param  [type] $vehicle [description]
  184.      * @param  [type] $args    [description]
  185.      * @return [type]          [description]
  186.      */
  187.     private function nationalBodyTypeProcedure($vehicle$args)
  188.     {
  189.         $otc $vehicle->getOtc();
  190.         if ($otc->getIsComplete() === true) {
  191.             if (method_exists($otc'get'.ucfirst($args['otc']))) {
  192.                 return call_user_func(array($otc'get'.ucfirst($args['otc'])));
  193.             } else {
  194.                 return "ERR - procedure - otc search";
  195.             }
  196.         } else {
  197.             return $args['default'];
  198.         }
  199.     }
  200.     /**
  201.      * PROCEDURE for Masse en service (G)
  202.      * @param  [type] $vehicle [description]
  203.      * @param  [type] $args    [description]
  204.      * @return [type]          [description]
  205.      */
  206.     private function serviceMassProcedure($vehicle$args)
  207.     {
  208.         $cocArray $vehicle->getCocs();
  209.         $isComplete false;
  210.         foreach ($cocArray as $coc) {
  211.             if ($coc->getCode() == 'FO38.') {
  212.                 $isComplete true;
  213.                 break;
  214.             }
  215.         }
  216.         if ($isComplete) {
  217.             $searchArray explode('|'$args['coc']);
  218.             $found null;
  219.             foreach ($cocArray as $coc) {
  220.                 if (in_array($coc->getCode(), $searchArray)) {
  221.                     $found $coc->getValue();
  222.                     break;
  223.                 }
  224.             }
  225.             if ($found === null) {
  226.                 return 'ERR - proc args';
  227.             }
  228.             $found floor((float) $found);
  229.             return $found;
  230.         }
  231.         return $args['default'];
  232.     }
  233.     /**
  234.      * PROCEDURE for Pds à vide national (G1)
  235.      * @param  [type] $vehicle [description]
  236.      * @param  [type] $args    [description]
  237.      * @return [type]          [description]
  238.      */
  239.     private function netNationalMassProcedure($vehicle$args)
  240.     {
  241.         $return $this->serviceMassProcedure($vehicle$args);
  242.         if (is_numeric($return)) {
  243.             $return $return $args['addition'];
  244.         }
  245.         return $return;
  246.     }
  247. }