<?php
namespace App\Services;
use App\Entity\Catalogue3en1;
use App\Repository\Catalogue3en1Repository;
use App\Repository\CerfaDataRepository;
use App\Repository\VehicleRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\Bundle\DoctrineBundle\Registry;
use App\Exception\WrongCerfaDataStatusException;
use App\Exception\MissingCerfaDataException;
use App\Entity\Vehicle;
use App\Entity\CerfaData;
use Doctrine\ORM\EntityManagerInterface;
/**
* CerfaService
*/
class CerfaService
{
/**
* @var CerfaDataRepository $cerfaRepo;
*/
protected $cerfaRepo;
/**
* @var VehicleRepository $vehicleRepo;
*/
protected $vehicleRepo;
/**
* @var Catalogue3en1Repository $catalogueRepo;
*/
protected $catalogueRepo;
/**
* @var EntityManagerInterface $em;
*/
protected $em;
protected $parameters;
function __construct(
EntityManagerInterface $em
) {
$this->em = $em;
$this->cerfaRepo = $em->getRepository(CerfaData::class);
$this->vehicleRepo = $em->getRepository(Vehicle::class);
$this->catalogueRepo = $em->getRepository(Catalogue3en1::class);
}
/**
* Wrapper to be called externally to retrieve a pdf.
* @param Vehicle $vehicle [description]
* @return [type] [description]
*/
public function getCerfaForVehicle(Vehicle $vehicle)
{
if (!$vehicle->getCerfaData()) {
throw new MissingCerfaDataException($vehicle);
}
return $vehicle->getCerfaData();
}
/**
* Wrapper to be called externally to retrieve a pdf.
* @param Vehicle $vehicle [description]
* @return [type] [description]
*/
public function generateCerfaForVehicle(Vehicle $vehicle, $preventFlush = false)
{
if ( $vehicle->getPrintableStatus() != Vehicle::PRINTABLE && $vehicle->getPrintableStatus() != Vehicle::PRINTED ) {
throw new WrongCerfaDataStatusException($vehicle);
}
if (!$this->parameters) {
$this->parameters = $this->catalogueRepo->findAllAssociative();
}
$parameters = $this->parameters;
if ($vehicle->getCerfaData()) {
$cerfaData = $vehicle->getCerfaData();
} else {
$cerfaData = new CerfaData($vehicle);
$this->em->persist($cerfaData);
}
if($cerfaData->getBlockUpdate() === false || $cerfaData->getBlockUpdate() === null) {
$cerfaData->setClientCode($this->optionalField($vehicle->getClientInvoice()->getClientCode()))
->setClientName($this->optionalField($vehicle->getClientInvoice()->getName()))
->setInvoiceNumber($this->optionalField($vehicle->getTechnicalData()->getAccountingDocumentCode()))
->setVan($this->optionalField($vehicle->getVan()))
->setBrand($this->optionalField($vehicle->getOtc()->getBrand()))
->setCnit($this->optionalField($vehicle->getOtc()->getCNIT()))
->setTvv($this->optionalField($vehicle->getTvv()))
->setVin($this->optionalField($vehicle->getVin()))
->setOtcReceptionDate($this->optionalField($vehicle->getOtc()->getDateReception()))
->setOtcReceptionCode($this->optionalField($vehicle->getOtc()->getNumReception()))
// PARAMETER Signature
->setSignature($this->optionalField($this->parameterCatalogueHandler($parameters, $vehicle, "signature")))
// PARMETER F1
->setMaxTechWeight($this->optionalField($this->parameterCatalogueHandler($parameters, $vehicle, "maxTechWeight")))
->setMaxStateWeight($this->optionalField($vehicle->getOtc()->getMassChargMax()))
->setMaxTotalWeight($this->optionalField($vehicle->getOtc()->getMassCombo()))
// PARAMETER G
->setServiceMass($this->optionalField($this->parameterCatalogueHandler($parameters, $vehicle, "serviceMass")))
// PARAMETER G1
->setNetNationalMass($this->optionalField($this->parameterCatalogueHandler($parameters, $vehicle, "netNationalMass")))
->setCategory($this->optionalField($vehicle->getOtc()->getCategory()))
->setNationalCategory($this->optionalField($vehicle->getOtc()->getGenre()))
->setBodyCE($this->optionalField($vehicle->getOtc()->getBodyType()))
->setNationalBody($this->optionalField($this->parameterCatalogueHandler($parameters, $vehicle, "nationalBody")))
->setEngineSize($this->optionalField($vehicle->getOtc()->getEngineSize()))
->setMaxNetPower($this->optionalField($vehicle->getOtc()->getPowerNetMax()))
->setFuelType($this->optionalField($vehicle->getOtc()->getEnergy()))
->setNationalAdministrativePower($this->optionalField($vehicle->getOtc()->getPowerAdministrative()))
// Always -
->setPowerMassRatio("-")
->setSeats($this->optionalField($vehicle->getSeats()))
// ONLY BUS HAVE IT. TRACK IS -
->setInitialSeats('-')
->setSoundLevel($this->optionalField($vehicle->getOtc()->getNivSonorArretMax()))
->setSoundEngineSpeed($this->optionalField($vehicle->getOtc()->getRegimNivSonor()))
->setCo2Emissions($this->optionalField(round(($this->getCOC($vehicle, 'FO49.04.EF')) ?: $vehicle->getOtc()->getCO2Combo())))
->setEnvironmentalClass($this->optionalField($vehicle->getOtc()->getEnvironmentalClass()))
->setCommercialName($this->optionalField($vehicle->getOtc()->getCommercialName()))
;
//only if Client Requested
if ($vehicle->getClientInvoice()->getOrderRequested()) {
$cerfaData
->setOrderNumber($this->optionalField($vehicle->getTechnicalData()->getPONumber()))
;
}
/**
* Persist
*/
if ($preventFlush !== true) {
$this->em->flush();
}
}
return $cerfaData;
}
private function optionalField($value, $default = '-')
{
return ($value) ? $value : $default;
}
private function getCOC(Vehicle $vehicle, $code) {
$cocArray = $vehicle->getCocs();
foreach ($cocArray as $coc) {
if ($coc->getCode() == $code) {
return ($coc->getValue() != '-' && $coc->getValue() != '--') ? $coc->getValue() : null;
}
}
return null;
}
private function parameterCatalogueHandler($parameters, $vehicle, $field)
{
$param = $parameters[$field];
$type = $param->getType();
if ($type == 'constant') {
return $param->getValue();
} elseif ($type == 'otc') {
$otc = $vehicle->getOtc();
if (method_exists($otc, 'get'.ucfirst($param->getValue()))) {
return call_user_func(array($otc, 'get'.ucfirst($param->getValue())));
} else {
return "ERR - otc param";
}
} elseif ($type == 'coc') {
$cocArray = $vehicle->getCocs();
foreach ($cocArray as $coc) {
if ($coc->getCode() == $param->getValue()) {
return $coc->getValue();
}
}
return "ERR - coc param";
} elseif ($type == 'procedure') {
try {
if (method_exists($this, $param->getValue())) {
return call_user_func(array($this, $param->getValue()), $vehicle, $param->getArgs());
} else {
return "ERR - procedure not found";
}
} catch (\Exception $e) {
return "ERR - procedure execution";
}
}
return "ERR - general param";
}
/**
* PROCEDURE for Carr.Nationale (J.3)
* @param [type] $vehicle [description]
* @param [type] $args [description]
* @return [type] [description]
*/
private function nationalBodyTypeProcedure($vehicle, $args)
{
$otc = $vehicle->getOtc();
if ($otc->getIsComplete() === true) {
if (method_exists($otc, 'get'.ucfirst($args['otc']))) {
return call_user_func(array($otc, 'get'.ucfirst($args['otc'])));
} else {
return "ERR - procedure - otc search";
}
} else {
return $args['default'];
}
}
/**
* PROCEDURE for Masse en service (G)
* @param [type] $vehicle [description]
* @param [type] $args [description]
* @return [type] [description]
*/
private function serviceMassProcedure($vehicle, $args)
{
$cocArray = $vehicle->getCocs();
$isComplete = false;
foreach ($cocArray as $coc) {
if ($coc->getCode() == 'FO38.') {
$isComplete = true;
break;
}
}
if ($isComplete) {
$searchArray = explode('|', $args['coc']);
$found = null;
foreach ($cocArray as $coc) {
if (in_array($coc->getCode(), $searchArray)) {
$found = $coc->getValue();
break;
}
}
if ($found === null) {
return 'ERR - proc args';
}
$found = floor((float) $found);
return $found;
}
return $args['default'];
}
/**
* PROCEDURE for Pds à vide national (G1)
* @param [type] $vehicle [description]
* @param [type] $args [description]
* @return [type] [description]
*/
private function netNationalMassProcedure($vehicle, $args)
{
$return = $this->serviceMassProcedure($vehicle, $args);
if (is_numeric($return)) {
$return = $return + $args['addition'];
}
return $return;
}
}