resq/src/Command/GenerateWarrantyFromJobOrderCommand.php

125 lines
5.1 KiB
PHP

<?php
namespace App\Command;
use DateTime;
use DateInterval;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\JobOrder;
use App\Entity\Warranty;
use App\Entity\SAPBattery;
use App\Ramcar\ServiceType;
class GenerateWarrantyFromJobOrderCommand extends Command
{
protected $em;
public function __construct(ObjectManager $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this->setName('warranty:generate')
->setDescription('Generates warranty from job order and inserts into database')
->setHelp('Generate warranty from job order');
}
protected function computeDateExpire($date_create, $warranty_period)
{
$expire_date = clone $date_create;
$expire_date->add(new DateInterval('P'.$warranty_period.'M'));
return $expire_date;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->em;
$qb = $this->em->getRepository(JobOrder::class)->createQueryBuilder('j');
// get all job orders with service_type='battery_replacement_new'
$qb->select('j')
->where('j.service_type = :service_type')
->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW);
$query = $qb->getQuery();
$result = $query->getResult();
foreach ($result as $jo)
{
$invoice_items = [];
// For now, one invoice == one battery
$invoice_items = $jo->getInvoice()->getItems();
$invoice_item = $invoice_items[0];
if ($invoice_item != null)
{
if($invoice_item->getBattery() != null)
{
// manually retrieve the SAPBattery using the SAPCode
$battery_sap_code = $invoice_item->getBattery()->getSAPCode();
$sap_battery = $this->em->getRepository(SAPBattery::class)->find($battery_sap_code);
// get warranty period for battery
$warranty_period = 0;
if ($invoice_item->getBattery()->getWarrantyPrivate() != null)
{
$warranty_period = $invoice_item->getBattery()->getWarrantyPrivate();
}
if ($invoice_item->getBattery()->getWarrantyCommercial() != null)
{
$warranty_period = $invoice_item->getBattery()->getWarrantyCommercial();
}
if ($invoice_item->getBattery()->getWarrantyTnv() != null)
{
$warranty_period = $invoice_item->getBattery()->getWarrantyTnv();
}
if ($jo->getInvoice()->getDateCreate() != null)
{
// check if plate number is "clean". If not, do not insert into warranty
if (!(Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber())))
{
continue;
}
// check if warranty already exists
$cleaned_plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber());
$expiry_date = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period);
$found_warranty = $this->em->createQuery("SELECT count(w) FROM App\Entity\Warranty w
WHERE w.plate_number = :plate_number AND w.date_expire = :date_expire")
->setParameter('plate_number', $cleaned_plate_number)
->setParameter('date_expire', $expiry_date->format('Y-m-d'))
->getSingleScalarResult();
if ($found_warranty == 0)
{
$warranty = new Warranty();
$warranty->setWarrantyClass($jo->getWarrantyClass())
->setFirstName($jo->getCustomer()->getFirstName())
->setLastName($jo->getCustomer()->getLastName())
->setMobileNumber($jo->getCustomer()->getPhoneMobile())
->setPlateNumber($cleaned_plate_number)
->setDatePurchase($jo->getInvoice()->getDateCreate())
->setDateExpire($expiry_date)
->setBatteryModel($invoice_item->getBattery()->getModel())
->setBatterySize($invoice_item->getBattery()->getSize())
->setSAPBattery($sap_battery);
$em->persist($warranty);
}
$em->flush();
}
}
}
}
}
}