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.             $cerfaData->setClientCode($this->optionalField($vehicle->getClientInvoice()->getClientCode()))
  78.                 ->setClientName($this->optionalField($vehicle->getClientInvoice()->getName()))
  79.                 ->setInvoiceNumber($this->optionalField($vehicle->getTechnicalData()->getAccountingDocumentCode()))
  80.                 ->setVan($this->optionalField($vehicle->getVan()))
  81.                 ->setBrand($this->optionalField($vehicle->getOtc()->getBrand()))
  82.                 ->setCnit($this->optionalField($vehicle->getOtc()->getCNIT()))
  83.                 ->setTvv($this->optionalField($vehicle->getTvv()))
  84.                 ->setVin($this->optionalField($vehicle->getVin()))
  85.                 ->setOtcReceptionDate($this->optionalField($vehicle->getOtc()->getDateReception()))
  86.                 ->setOtcReceptionCode($this->optionalField($vehicle->getOtc()->getNumReception()))
  87.                 // PARAMETER Signature
  88.                 ->setSignature($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"signature")))
  89.                 // PARMETER F1
  90.                 ->setMaxTechWeight($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"maxTechWeight")))
  91.                 ->setMaxStateWeight($this->optionalField($vehicle->getOtc()->getMassChargMax()))
  92.                 ->setMaxTotalWeight($this->optionalField($vehicle->getOtc()->getMassCombo()))
  93.                 // PARAMETER G
  94.                 ->setServiceMass($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"serviceMass")))
  95.                 // PARAMETER G1
  96.                 ->setNetNationalMass($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"netNationalMass")))
  97.                 ->setCategory($this->optionalField($vehicle->getOtc()->getCategory()))
  98.                 ->setNationalCategory($this->optionalField($vehicle->getOtc()->getGenre()))
  99.                 ->setBodyCE($this->optionalField($vehicle->getOtc()->getBodyType()))
  100.                 ->setNationalBody($this->optionalField($this->parameterCatalogueHandler($parameters$vehicle"nationalBody")))
  101.                 ->setEngineSize($this->optionalField($vehicle->getOtc()->getEngineSize()))
  102.                 ->setMaxNetPower($this->optionalField($vehicle->getOtc()->getPowerNetMax()))
  103.                 ->setFuelType($this->optionalField($vehicle->getOtc()->getEnergy()))
  104.                 ->setNationalAdministrativePower($this->optionalField($vehicle->getOtc()->getPowerAdministrative()))
  105.                 // Always -
  106.                 ->setPowerMassRatio("-")
  107.                 ->setSeats($this->optionalField($vehicle->getSeats()))
  108.                 // ONLY BUS HAVE IT. TRACK IS -
  109.                 ->setInitialSeats('-')
  110.                 ->setSoundLevel($this->optionalField($vehicle->getOtc()->getNivSonorArretMax()))
  111.                 ->setSoundEngineSpeed($this->optionalField($vehicle->getOtc()->getRegimNivSonor()))
  112.                 ->setCo2Emissions($this->optionalField(round(($this->getCOC($vehicle'FO49.04.EF')) ?: $vehicle->getOtc()->getCO2Combo())))
  113.                 ->setEnvironmentalClass($this->optionalField($vehicle->getOtc()->getEnvironmentalClass()))
  114.                 ->setCommercialName($this->optionalField($vehicle->getOtc()->getCommercialName()))
  115.             ;
  116.             //only if Client Requested
  117.             if ($vehicle->getClientInvoice()->getOrderRequested()) {
  118.                 $cerfaData
  119.                     ->setOrderNumber($this->optionalField($vehicle->getTechnicalData()->getPONumber()))
  120.                 ;
  121.             }
  122.             /**
  123.              * Persist
  124.              */
  125.             if ($preventFlush !== true) {
  126.                 $this->em->flush();
  127.             }
  128.         }
  129.         return $cerfaData;
  130.     }
  131.     private function optionalField($value$default '-')
  132.     {
  133.         return ($value) ? $value $default;
  134.     }
  135.     private function getCOC(Vehicle $vehicle$code) {
  136.         $cocArray $vehicle->getCocs();
  137.         foreach ($cocArray as $coc) {
  138.             if ($coc->getCode() == $code) {
  139.                 return ($coc->getValue() != '-' && $coc->getValue() != '--') ? $coc->getValue() : null;
  140.             }
  141.         }
  142.         return null;
  143.     }
  144.     private function parameterCatalogueHandler($parameters$vehicle$field)
  145.     {
  146.         $param $parameters[$field];
  147.         $type $param->getType();
  148.         if ($type == 'constant') {
  149.             return $param->getValue();
  150.         } elseif ($type == 'otc') {
  151.             $otc $vehicle->getOtc();
  152.             if (method_exists($otc'get'.ucfirst($param->getValue()))) {
  153.                 return call_user_func(array($otc'get'.ucfirst($param->getValue())));
  154.             } else {
  155.                 return "ERR - otc param";
  156.             }
  157.         } elseif ($type == 'coc') {
  158.             $cocArray $vehicle->getCocs();
  159.             foreach ($cocArray as $coc) {
  160.                 if ($coc->getCode() == $param->getValue()) {
  161.                     return $coc->getValue();
  162.                 }
  163.             }
  164.             return "ERR - coc param";
  165.         } elseif ($type == 'procedure') {
  166.             try {
  167.                 if (method_exists($this$param->getValue())) {
  168.                     return call_user_func(array($this$param->getValue()), $vehicle$param->getArgs());
  169.                 } else {
  170.                     return "ERR - procedure not found";
  171.                 }
  172.             } catch (\Exception $e) {
  173.                 return "ERR - procedure execution";
  174.             }
  175.         }
  176.         return "ERR - general param";
  177.     }
  178.     /**
  179.      * PROCEDURE for Carr.Nationale (J.3)
  180.      * @param  [type] $vehicle [description]
  181.      * @param  [type] $args    [description]
  182.      * @return [type]          [description]
  183.      */
  184.     private function nationalBodyTypeProcedure($vehicle$args)
  185.     {
  186.         $otc $vehicle->getOtc();
  187.         if ($otc->getIsComplete() === true) {
  188.             if (method_exists($otc'get'.ucfirst($args['otc']))) {
  189.                 return call_user_func(array($otc'get'.ucfirst($args['otc'])));
  190.             } else {
  191.                 return "ERR - procedure - otc search";
  192.             }
  193.         } else {
  194.             return $args['default'];
  195.         }
  196.     }
  197.     /**
  198.      * PROCEDURE for Masse en service (G)
  199.      * @param  [type] $vehicle [description]
  200.      * @param  [type] $args    [description]
  201.      * @return [type]          [description]
  202.      */
  203.     private function serviceMassProcedure($vehicle$args)
  204.     {
  205.         $cocArray $vehicle->getCocs();
  206.         $isComplete false;
  207.         foreach ($cocArray as $coc) {
  208.             if ($coc->getCode() == 'FO38.') {
  209.                 $isComplete true;
  210.                 break;
  211.             }
  212.         }
  213.         if ($isComplete) {
  214.             $searchArray explode('|'$args['coc']);
  215.             $found null;
  216.             foreach ($cocArray as $coc) {
  217.                 if (in_array($coc->getCode(), $searchArray)) {
  218.                     $found $coc->getValue();
  219.                     break;
  220.                 }
  221.             }
  222.             if ($found === null) {
  223.                 return 'ERR - proc args';
  224.             }
  225.             $found floor((float) $found);
  226.             return $found;
  227.         }
  228.         return $args['default'];
  229.     }
  230.     /**
  231.      * PROCEDURE for Pds à vide national (G1)
  232.      * @param  [type] $vehicle [description]
  233.      * @param  [type] $args    [description]
  234.      * @return [type]          [description]
  235.      */
  236.     private function netNationalMassProcedure($vehicle$args)
  237.     {
  238.         $return $this->serviceMassProcedure($vehicle$args);
  239.         if (is_numeric($return)) {
  240.             $return $return $args['addition'];
  241.         }
  242.         return $return;
  243.     }
  244. }