Fix memory error when querying the database. #213
This commit is contained in:
parent
46bc686372
commit
8adea20f59
1 changed files with 96 additions and 30 deletions
|
|
@ -17,15 +17,21 @@ use App\Entity\Warranty;
|
||||||
use App\Entity\SAPBattery;
|
use App\Entity\SAPBattery;
|
||||||
|
|
||||||
use App\Ramcar\ServiceType;
|
use App\Ramcar\ServiceType;
|
||||||
|
use App\Ramcar\WarrantyStatus;
|
||||||
|
|
||||||
class GenerateWarrantyFromJobOrderCommand extends Command
|
class GenerateWarrantyFromJobOrderCommand extends Command
|
||||||
{
|
{
|
||||||
protected $em;
|
protected $em;
|
||||||
|
protected $sapbatt_hash;
|
||||||
|
protected $warranties_hash;
|
||||||
|
|
||||||
public function __construct(ObjectManager $em)
|
public function __construct(ObjectManager $em)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
|
|
||||||
|
$this->loadSAPBatteries();
|
||||||
|
$this->loadWarranties();
|
||||||
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,22 +49,68 @@ class GenerateWarrantyFromJobOrderCommand extends Command
|
||||||
return $expire_date;
|
return $expire_date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function loadSAPBatteries()
|
||||||
|
{
|
||||||
|
$this->sapbatt_hash = [];
|
||||||
|
|
||||||
|
$sap_batteries = $this->em->getRepository(SAPBattery::class)->findAll();
|
||||||
|
foreach($sap_batteries as $sap_batt)
|
||||||
|
{
|
||||||
|
$id = $sap_batt->getID();
|
||||||
|
$brand_size = $sap_batt->getBrand()->getID() . " " . $sap_batt->getSize()->getID();
|
||||||
|
$this->sapbatt_hash[$id] = $brand_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function loadWarranties()
|
||||||
|
{
|
||||||
|
$this->warranties_hash = [];
|
||||||
|
|
||||||
|
$warranties = $this->em->getRepository(Warranty::class)->findAll();
|
||||||
|
foreach($warranties as $warranty)
|
||||||
|
{
|
||||||
|
$plate_number = $warranty->getPlateNumber();
|
||||||
|
$date_expire = $warranty->getDateExpire();
|
||||||
|
$expiry_date = $date_expire->format('Y-m-d');
|
||||||
|
|
||||||
|
$this->warranties_hash[$plate_number][$expiry_date] = $warranty->getID();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function findSAPBattery($batt_id)
|
||||||
|
{
|
||||||
|
if (!isset($this->sapbatt_hash[$batt_id]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function findWarranty($plate_number, $expiry_date)
|
||||||
|
{
|
||||||
|
$date_expire = $expiry_date->format('Y-m-d');
|
||||||
|
if (!isset($this->warranties_hash[$plate_number][$date_expire]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$em = $this->em;
|
$em = $this->em;
|
||||||
|
|
||||||
$qb = $this->em->getRepository(JobOrder::class)->createQueryBuilder('j');
|
$query = $em->createQuery('SELECT jo FROM App\Entity\JobOrder jo
|
||||||
|
WHERE jo.service_type = :service_type');
|
||||||
|
$query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW);
|
||||||
|
|
||||||
// get all job orders with service_type='battery_replacement_new'
|
$result = $query->iterate();
|
||||||
$qb->select('j')
|
|
||||||
->where('j.service_type = :service_type')
|
|
||||||
->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW);
|
|
||||||
|
|
||||||
$query = $qb->getQuery();
|
foreach ($result as $row)
|
||||||
$result = $query->getResult();
|
|
||||||
|
|
||||||
foreach ($result as $jo)
|
|
||||||
{
|
{
|
||||||
|
$jo = $row[0];
|
||||||
$invoice_items = [];
|
$invoice_items = [];
|
||||||
// For now, one invoice == one battery
|
// For now, one invoice == one battery
|
||||||
$invoice_items = $jo->getInvoice()->getItems();
|
$invoice_items = $jo->getInvoice()->getItems();
|
||||||
|
|
@ -69,7 +121,12 @@ class GenerateWarrantyFromJobOrderCommand extends Command
|
||||||
{
|
{
|
||||||
// manually retrieve the SAPBattery using the SAPCode
|
// manually retrieve the SAPBattery using the SAPCode
|
||||||
$battery_sap_code = $invoice_item->getBattery()->getSAPCode();
|
$battery_sap_code = $invoice_item->getBattery()->getSAPCode();
|
||||||
$sap_battery = $this->em->getRepository(SAPBattery::class)->find($battery_sap_code);
|
$found_battery = $this->findSAPBattery($battery_sap_code);
|
||||||
|
$sap_code = '\'' . $battery_sap_code . '\'';
|
||||||
|
if (!$found_battery)
|
||||||
|
{
|
||||||
|
$sap_code = 'NULL';
|
||||||
|
}
|
||||||
|
|
||||||
// get warranty period for battery
|
// get warranty period for battery
|
||||||
$warranty_period = 0;
|
$warranty_period = 0;
|
||||||
|
|
@ -96,30 +153,39 @@ class GenerateWarrantyFromJobOrderCommand extends Command
|
||||||
// check if warranty already exists
|
// check if warranty already exists
|
||||||
$cleaned_plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber());
|
$cleaned_plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber());
|
||||||
$expiry_date = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period);
|
$expiry_date = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period);
|
||||||
$found_warranty = $this->em->createQuery("SELECT count(w) FROM App\Entity\Warranty w
|
$found_warranty = $this->findWarranty($cleaned_plate_number, $expiry_date);
|
||||||
WHERE w.plate_number = :plate_number AND w.date_expire = :date_expire")
|
if (!$found_warranty)
|
||||||
->setParameter('plate_number', $cleaned_plate_number)
|
|
||||||
->setParameter('date_expire', $expiry_date->format('Y-m-d'))
|
|
||||||
->getSingleScalarResult();
|
|
||||||
if ($found_warranty == 0)
|
|
||||||
{
|
{
|
||||||
$warranty = new Warranty();
|
$bty_model_id = $invoice_item->getBattery()->getModel()->getID();
|
||||||
$warranty->setWarrantyClass($jo->getWarrantyClass())
|
$bty_size_id = $invoice_item->getBattery()->getSize()->getID();
|
||||||
->setFirstName($jo->getCustomer()->getFirstName())
|
$warranty_class = $jo->getWarrantyClass();
|
||||||
->setLastName($jo->getCustomer()->getLastName())
|
|
||||||
->setMobileNumber($jo->getCustomer()->getPhoneMobile())
|
$date = $jo->getInvoice()->getDateCreate();
|
||||||
->setPlateNumber($cleaned_plate_number)
|
$date_purchase = $date->format('Y-m-d');
|
||||||
->setDatePurchase($jo->getInvoice()->getDateCreate())
|
|
||||||
->setDateExpire($expiry_date)
|
$date_create = date('Y-m-d H:i:s');
|
||||||
->setBatteryModel($invoice_item->getBattery()->getModel())
|
|
||||||
->setBatterySize($invoice_item->getBattery()->getSize())
|
$date_expire = $expiry_date->format('Y-m-d');
|
||||||
->setSAPBattery($sap_battery);
|
|
||||||
$em->persist($warranty);
|
$first_name = addslashes(trim($jo->getCustomer()->getFirstName()));
|
||||||
}
|
$last_name = addslashes(trim($jo->getCustomer()->getLastName()));
|
||||||
$em->flush();
|
$mobile_number = addslashes(trim($jo->getCustomer()->getPhoneMobile()));
|
||||||
|
|
||||||
|
$values = '(' . $bty_model_id . ',' . $bty_size_id . ',NULL,\'' . $warranty_class . '\',\''
|
||||||
|
. $cleaned_plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $date_purchase
|
||||||
|
. '\',\'' . $date_expire . '\',NULL,'
|
||||||
|
. $sap_code . ',NULL,\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\');';
|
||||||
|
|
||||||
|
$sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,serial,warranty_class,plate_number,status,date_create,date_purchase,date_expire,date_claim,sap_bty_id,claim_id,first_name,last_name,mobile_number) VALUES ' . $values . "\n";
|
||||||
|
|
||||||
|
echo $sql_statement;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$em->detach($row[0]);
|
||||||
|
$em->clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue