From 754af5e1e42afe7e5cadf90b4db003e8b7090642 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Tue, 28 May 2019 01:20:09 +0800 Subject: [PATCH] Fix memory issue for warranty generation #213 --- .../GenerateWarrantyFromJobOrderCommand.php | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Command/GenerateWarrantyFromJobOrderCommand.php b/src/Command/GenerateWarrantyFromJobOrderCommand.php index 57bf1be4..86653d97 100644 --- a/src/Command/GenerateWarrantyFromJobOrderCommand.php +++ b/src/Command/GenerateWarrantyFromJobOrderCommand.php @@ -66,15 +66,22 @@ class GenerateWarrantyFromJobOrderCommand extends Command { $this->warranties_hash = []; + /* $warranties = $this->em->getRepository(Warranty::class)->findAll(); foreach($warranties as $warranty) { $plate_number = $warranty->getPlateNumber(); $date_expire = $warranty->getDateExpire(); + + // skip null date expire + if ($date_expire == null) + continue; + $expiry_date = $date_expire->format('Y-m-d'); $this->warranties_hash[$plate_number][$expiry_date] = $warranty->getID(); } + */ } protected function findSAPBattery($batt_id) @@ -102,19 +109,32 @@ class GenerateWarrantyFromJobOrderCommand extends Command { $em = $this->em; + // to save on joins, go with invoice item first + $query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type'); + $query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW); + /* $query = $em->createQuery('SELECT jo FROM App\Entity\JobOrder jo WHERE jo.service_type = :service_type'); $query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW); + */ $result = $query->iterate(); foreach ($result as $row) { - $jo = $row[0]; + $invoice_item = $row[0]; + $invoice = $invoice_item->getInvoice(); + $jo = $invoice->getJobOrder(); + $cv = $jo->getCustomerVehicle(); + $customer = $jo->getCustomer(); + /* $invoice_items = []; - // For now, one invoice == one battery + // 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) @@ -129,6 +149,7 @@ class GenerateWarrantyFromJobOrderCommand extends Command } // get warranty period for battery + // TODO: base it on the battery type $warranty_period = 0; if ($invoice_item->getBattery()->getWarrantyPrivate() != null) { @@ -143,15 +164,17 @@ class GenerateWarrantyFromJobOrderCommand extends Command $warranty_period = $invoice_item->getBattery()->getWarrantyTnv(); } - if ($jo->getInvoice()->getDateCreate() != null) + if ($invoice->getDateCreate() != null) { // check if plate number is "clean". If not, do not insert into warranty - if (!(Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()))) + if (!(Warranty::cleanPlateNumber($cv->getPlateNumber()))) { continue; } + // check if warranty already exists - $cleaned_plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + $cleaned_plate_number = Warranty::cleanPlateNumber($cv->getPlateNumber()); + $expiry_date = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period); $found_warranty = $this->findWarranty($cleaned_plate_number, $expiry_date); if (!$found_warranty) @@ -167,9 +190,9 @@ class GenerateWarrantyFromJobOrderCommand extends Command $date_expire = $expiry_date->format('Y-m-d'); - $first_name = addslashes(trim($jo->getCustomer()->getFirstName())); - $last_name = addslashes(trim($jo->getCustomer()->getLastName())); - $mobile_number = addslashes(trim($jo->getCustomer()->getPhoneMobile())); + $first_name = addslashes(trim($customer->getFirstName())); + $last_name = addslashes(trim($customer->getLastName())); + $mobile_number = addslashes(trim($customer->getPhoneMobile())); $values = '(' . $bty_model_id . ',' . $bty_size_id . ',NULL,\'' . $warranty_class . '\',\'' . $cleaned_plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $date_purchase @@ -184,6 +207,7 @@ class GenerateWarrantyFromJobOrderCommand extends Command } } } + $em->detach($row[0]); $em->clear(); }