From 789c919179b5ea6de1a30f1462b587a8d538a5ec Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 5 Apr 2022 10:30:49 +0000 Subject: [PATCH 01/14] Redo command to include open job orders. --- ...and.php => FulfillOpenJobOrderCommand.php} | 90 +++++-------------- src/Service/WarrantyHandler.php | 12 +-- 2 files changed, 31 insertions(+), 71 deletions(-) rename src/Command/{FulfillAssignedJobOrderCommand.php => FulfillOpenJobOrderCommand.php} (65%) diff --git a/src/Command/FulfillAssignedJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php similarity index 65% rename from src/Command/FulfillAssignedJobOrderCommand.php rename to src/Command/FulfillOpenJobOrderCommand.php index a26067e2..818b89ef 100644 --- a/src/Command/FulfillAssignedJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -26,7 +26,7 @@ use App\Service\WarrantyHandler; use DateTime; use DateInterval; -class FulfillAssignedJobOrderCommand extends Command +class FulfillOpenJobOrderCommand extends Command { protected $em; protected $wh; @@ -41,9 +41,9 @@ class FulfillAssignedJobOrderCommand extends Command protected function configure() { - $this->setName('joborder:fulfillassignednosms') - ->setDescription('Fulfill assigned job orders without sending an SMS message.') - ->setHelp('Mark assigned job orders as fulfilled and should not send a SMS message. Date format: YYYY-MM-DD') + $this->setName('joborder:fulfillopenjosnosms') + ->setDescription('Fulfill open job orders without sending an SMS message.') + ->setHelp('Mark open job orders as fulfilled and should not send a SMS message. Date format: YYYY-MM-DD') ->addArgument('end_date', InputArgument::REQUIRED, 'End date. Format: YYYY-MM-DD'); } @@ -52,15 +52,29 @@ class FulfillAssignedJobOrderCommand extends Command // get the input date $str_date_end = $input->getArgument('end_date'); - // retrieve job orders that have status assigned and has not been fulfilled starting from input date and before. + // append the 23:59:59 to end date + $str_date_end = $str_date_end . ' ' . '23:59:59'; + + // retrieve job orders that are open and have not been fulfilled starting from input date and before. // starting time to count is date schedule $date_end = new DateTime($str_date_end); - $query = $this->em->createQuery('SELECT jo FROM App\Entity\JobOrder jo WHERE jo.status = :status + error_log($date_end->format('Y-m-d H:i:s')); + + $query = $this->em->createQuery('SELECT jo FROM App\Entity\JobOrder jo + WHERE (jo.status = :pending_status + OR jo.status = :rider_assign_status + OR jo.status = :assigned_status + OR jo.status = :in_progress_status + OR jo.status = :in_transit_status) AND jo.date_schedule <= :date_end'); $jo_results = $query->setParameters([ - 'status' => JOStatus::ASSIGNED, + 'pending_status' => JOStatus::PENDING, + 'rider_assign_status' => JOStatus::RIDER_ASSIGN, + 'assigned_status' => JOStatus::ASSIGNED, + 'in_progress_status' => JOStatus::IN_PROGRESS, + 'in_transit_status' => JOStatus::IN_TRANSIT, 'date_end' => $date_end, ]) ->getResult(); @@ -88,75 +102,19 @@ class FulfillAssignedJobOrderCommand extends Command $this->em->persist($event); - // update the customer vehicle battery record - $this->updateVehicleBattery($jo); - - $this->em->flush(); - // check if new battery // if yes, create warranty if ($this->checkIfNewBattery($jo)) { $this->createWarranty($jo, $user); } - } + } + + $this->em->flush(); return 0; } - protected function updateVehicleBattery(JobOrder $jo) - { - // check if new battery - if (!($this->checkIfNewBattery($jo))) - return; - - // customer vehicle - $cv = $jo->getCustomerVehicle(); - if ($cv == null) - return; - - // invoice - $invoice = $jo->getInvoice(); - if ($invoice == null) - return; - - // invoice items - $items = $invoice->getItems(); - if (count($items) <= 0) - return; - - // get first battery from invoice - $battery = null; - foreach ($items as $item) - { - $battery = $item->getBattery(); - if ($battery != null) - break; - } - - // no battery in order - if ($battery == null) - return; - - // warranty expiration - $warr = $jo->getWarrantyClass(); - $warr_months = 0; - if ($warr == WarrantyClass::WTY_PRIVATE) - $warr_months = $battery->getWarrantyPrivate(); - else if ($warr == WarrantyClass::WTY_COMMERCIAL) - $warr_months = $battery->getWarrantyCommercial(); - else if ($warr == WarrantyClass::WTY_TNV) - $warr_months = 12; - - $warr_date = new DateTime(); - $warr_date->add(new DateInterval('P' . $warr_months . 'M')); - - // update customer vehicle battery - $cv->setCurrentBattery($battery) - ->setHasMotoliteBattery(true) - ->setWarrantyExpiration($warr_date); - } - protected function checkIfNewBattery(JobOrder $jo) { if ($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index a465de6e..24499b78 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -43,11 +43,11 @@ class WarrantyHandler foreach ($batt_list as $battery) { // get the battery model and battery size - $model_id = $battery->getModel()->getID(); - $size_id = $battery->getSize()->getID(); + $bty_model = $battery->getModel(); + $bty_size = $battery->getSize(); - $bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); + //$bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); + //$bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); if ($bty_model != null) { @@ -149,12 +149,14 @@ class WarrantyHandler $q = $this->em->createQuery('update App\Entity\CustomerVehicle cv set cv.curr_battery = :batt_id, cv.warranty_code = :serial, - cv.warranty_expiration = :expiry_date + cv.warranty_expiration = :expiry_date, + cv.flag_motolite_battery = :flag_motolite_battery where cv.plate_number = :plate_number') ->setParameters([ 'batt_id' => $battery_id, 'serial' => $serial, 'expiry_date' => $date_expire, + 'flag_motolite_battery' => true, 'plate_number' => $plate_number]); $q->execute(); } From b831deacfb4dcb7011063ba6c29a3cecbb2bec4a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 6 Apr 2022 03:09:46 +0000 Subject: [PATCH 02/14] Add fixes. --- composer.json | 7 ++++++- src/Command/FulfillOpenJobOrderCommand.php | 16 ++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 90a490d6..5ade4a34 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,12 @@ "preferred-install": { "*": "dist" }, - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "composer/package-versions-deprecated": true, + "symfony/flex": true, + "symfony/thanks": true + } }, "autoload": { "psr-4": { diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index 818b89ef..3dd37b89 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -88,19 +88,19 @@ class FulfillOpenJobOrderCommand extends Command { error_log('Fulfilling job order ' . $jo->getID()); - $jo->fulfill(); + //$jo->fulfill(); // set delivery status - $jo->setDeliveryStatus(DeliveryStatus::FULFILLED); + //$jo->setDeliveryStatus(DeliveryStatus::FULFILLED); // create JO event - $event = new JOEvent(); - $event->setDateHappen(new DateTime()) - ->setTypeID(JOEventType::FULFILL) - ->setJobOrder($jo) - ->setUser($user); + //$event = new JOEvent(); + //$event->setDateHappen(new DateTime()) + // ->setTypeID(JOEventType::FULFILL) + // ->setJobOrder($jo) + // ->setUser($user); - $this->em->persist($event); + //$this->em->persist($event); // check if new battery // if yes, create warranty From a50d59116069aa3af5bdd2cc80ee76063c05254a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 7 Apr 2022 09:32:23 +0000 Subject: [PATCH 03/14] Redo warranty flow. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 33 +++++++-- src/Service/WarrantyHandler.php | 86 ++++++++++++++-------- 2 files changed, 82 insertions(+), 37 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index 3dd37b89..2fefc0d6 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -13,6 +13,7 @@ use App\Entity\JobOrder; use App\Entity\JOEvent; use App\Entity\User; use App\Entity\Warranty; +use App\Entity\SAPBattery; use App\Ramcar\JOStatus; use App\Ramcar\JOEventType; @@ -49,6 +50,23 @@ class FulfillOpenJobOrderCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { + /* + $sap_code = 'WSLMF9BR-RV100-L'; + + error_log($sap_code); + + $conn = $this->em->getConnection(); + $sql = 'SELECT sap.id FROM sap_battery sap WHERE sap.id = :id'; + $stmt = $conn->prepare($sql); + $stmt->execute(array('id' => $sap_code)); + + $query_results = $stmt->fetchColumn(); + + error_log($query_results); + + return 0; + */ + // get the input date $str_date_end = $input->getArgument('end_date'); @@ -59,25 +77,29 @@ class FulfillOpenJobOrderCommand extends Command // starting time to count is date schedule $date_end = new DateTime($str_date_end); - error_log($date_end->format('Y-m-d H:i:s')); + error_log('mogol ' . $date_end->format('Y-m-d H:i:s')); + /* $query = $this->em->createQuery('SELECT jo FROM App\Entity\JobOrder jo WHERE (jo.status = :pending_status OR jo.status = :rider_assign_status OR jo.status = :assigned_status OR jo.status = :in_progress_status OR jo.status = :in_transit_status) - AND jo.date_schedule <= :date_end'); + AND jo.date_schedule <= :date_end + AND jo.service_type = :stype'); - $jo_results = $query->setParameters([ + $query->setParameters([ 'pending_status' => JOStatus::PENDING, 'rider_assign_status' => JOStatus::RIDER_ASSIGN, 'assigned_status' => JOStatus::ASSIGNED, 'in_progress_status' => JOStatus::IN_PROGRESS, 'in_transit_status' => JOStatus::IN_TRANSIT, 'date_end' => $date_end, - ]) - ->getResult(); + 'stype' => ServiceType::BATTERY_REPLACEMENT_NEW, + ])->setMaxResults(1); + + $jo_results = $query->getResult(); error_log('Found job orders ' . count($jo_results)); @@ -111,6 +133,7 @@ class FulfillOpenJobOrderCommand extends Command } $this->em->flush(); + */ return 0; } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 24499b78..b59083a2 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -33,12 +33,12 @@ class WarrantyHandler $batt_list, DateTime $date_purchase, $warranty_class, $user_id, $source, $customer, $cust_vehicle) { - // new warranty - $warranty = new Warranty(); - + $bmodel_id = ''; + $bsize_id = ''; $bmodel_name = ''; $bsize_name =''; $sap_batt_id = ''; + $w_serial = null; foreach ($batt_list as $battery) { @@ -46,30 +46,35 @@ class WarrantyHandler $bty_model = $battery->getModel(); $bty_size = $battery->getSize(); - //$bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); - //$bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); - if ($bty_model != null) { - $warranty->setBatteryModel($bty_model); + $bmodel_id = $bty_model->getID(); $bmodel_name = $bty_model->getName(); } if ($bty_size != null) { - $warranty->setBatterySize($bty_size); + $bsize_id = $bty_size->getID(); $bsize_name = $bty_size->getName(); - } + } $sap_code = $battery->getSAPCode(); if (!empty($sap_code)) { // find sap battery - $sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code); - if ($sap_battery != null) + /* + $conn = $this->em->getConnection(); + $sql = 'SELECT sap.id FROM sap_battery sap WHERE sap.id = :id'; + $stmt = $conn->prepare($sql); + $stmt->execute(array('id' => $sap_code)); + + $query_results = $stmt->fetchAll(); + + foreach($query_results as $row) { - $warranty->setSAPBattery($sap_battery); - $sap_batt_id = $sap_battery->getID(); + $sap_batt_id = $row['id']; } + */ + $sap_batt_id = $sap_code; } } @@ -80,27 +85,44 @@ class WarrantyHandler { $period = $this->getWarrantyPeriod($batt_list, $warranty_class); $date_expire = $this->computeDateExpire($date_purchase, $period); - - $warranty->setDateExpire($date_expire); } // set and save values - if (trim($serial) == '') - $warranty->setSerial(null); - else - $warranty->setSerial($serial); - $warranty->setPlateNumber($plate_number) - ->setFirstName($first_name) - ->setLastName($last_name) - ->setMobileNumber($mobile_number) - ->setDatePurchase($date_purchase) - ->setWarrantyClass($warranty_class) - ->setCreateSource($source) - ->setCustomer($customer) - ->setVehicle($cust_vehicle); + if (trim($serial) != '') + $w_serial = $serial; - $this->em->persist($warranty); - $this->em->flush(); + // insert warranty + $q = $this->em->createQuery('INSERT App\Entity\Warranty w + SET w.serial = :serial, + w.plate_number = :plate_number, + w.first_name = :first_name, + w.last_name = :last_name, + w.mobile_number = :mobile_number, + w.date_purchase = :date_purchase, + w.warranty_class = :warranty_class, + w.create_source = :create_source, + w.customer_id = :customer_id, + w.vehicle_id = :vehicle_id, + w.bty_model_id = :bmodel_id, + w.bty_size_id = :bsize_id, + w.sap_bty_id = :sap_batt_id, + w.date_expire = :date_expire') + ->setParameters([ + 'serial' => $serial, + 'plate_number' => $plate_number, + 'first_name' => $first_name, + 'last_name' => $last_name, + 'mobile_number' => $mobile_number, + 'date_purchase' => $date_purchase, + 'warranty_class' => $warranty_class, + 'create_source' => $source, + 'customer_id' => $customer->getID(), + 'vehicle_id' => $cust_vehicle->getID(), + 'bmodel_id' => $bmodel_id, + 'bsize_id' => $bsize_id, + 'sap_batt_id' => $sap_batt_id, + 'date_expire' => $date_expire]); + $q->execute(); // log warranty creation $action = 'create'; @@ -121,10 +143,10 @@ class WarrantyHandler 'sap_battery' => $sap_batt_id, 'plate_number' => $plate_number, ]; - $this->logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); + //$this->logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); // update customer vehicle with warranty info - $this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire); + //$this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire); } public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire) From d9de5cd519b6a0d11b170ade0d1fe6a84691512a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 May 2022 10:08:41 +0000 Subject: [PATCH 04/14] Redo the command to fulfill all open job orders. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 318 ++++++++++++++------- 1 file changed, 209 insertions(+), 109 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index 2fefc0d6..e18b5d2d 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -22,20 +22,16 @@ use App\Ramcar\WarrantyClass; use App\Ramcar\ServiceType; use App\Ramcar\WarrantySource; -use App\Service\WarrantyHandler; - use DateTime; use DateInterval; class FulfillOpenJobOrderCommand extends Command { protected $em; - protected $wh; - public function __construct(EntityManagerInterface $em, WarrantyHandler $wh) + public function __construct(EntityManagerInterface $em) { $this->em = $em; - $this->wh = $wh; parent::__construct(); } @@ -50,140 +46,244 @@ class FulfillOpenJobOrderCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { - /* - $sap_code = 'WSLMF9BR-RV100-L'; - - error_log($sap_code); - - $conn = $this->em->getConnection(); - $sql = 'SELECT sap.id FROM sap_battery sap WHERE sap.id = :id'; - $stmt = $conn->prepare($sql); - $stmt->execute(array('id' => $sap_code)); - - $query_results = $stmt->fetchColumn(); - - error_log($query_results); - - return 0; - */ - // get the input date $str_date_end = $input->getArgument('end_date'); // append the 23:59:59 to end date $str_date_end = $str_date_end . ' ' . '23:59:59'; - // retrieve job orders that are open and have not been fulfilled starting from input date and before. // starting time to count is date schedule $date_end = new DateTime($str_date_end); - error_log('mogol ' . $date_end->format('Y-m-d H:i:s')); + // get current date and convert to string + $current_date = new DateTime(); + $str_current_date = $current_date->format('Y-m-d H:i:s'); - /* - $query = $this->em->createQuery('SELECT jo FROM App\Entity\JobOrder jo - WHERE (jo.status = :pending_status - OR jo.status = :rider_assign_status - OR jo.status = :assigned_status - OR jo.status = :in_progress_status - OR jo.status = :in_transit_status) - AND jo.date_schedule <= :date_end - AND jo.service_type = :stype'); + // find all open job orders starting from input date and before + // need to get customer id, customer vehicle id, service type, warranty class + $conn = $this->em->getConnection(); - $query->setParameters([ - 'pending_status' => JOStatus::PENDING, - 'rider_assign_status' => JOStatus::RIDER_ASSIGN, - 'assigned_status' => JOStatus::ASSIGNED, - 'in_progress_status' => JOStatus::IN_PROGRESS, - 'in_transit_status' => JOStatus::IN_TRANSIT, - 'date_end' => $date_end, - 'stype' => ServiceType::BATTERY_REPLACEMENT_NEW, - ])->setMaxResults(1); + $jo_sql = 'SELECT jo.id AS jo_id, c.id AS c_id, cv.id AS cv_id, i + jo.service_type, jo.warranty_class, jo.rider_id, jo.date_schedule + FROM job_order jo, customer c, customer_vehicle cv + WHERE jo.customer_id = c.id AND jo.cvehicle_id = cv.id + AND (jo.status != :cancelled + OR jo.status != :fulfilled) + AND jo.date_schedule <= :date_end'; + $stmt = $conn->prepare($jo_sql); + $stmt->execute(['cancelled' => JOStatus::CANCELLED, + 'fulfilled' => JOStatus::FULFILLED, + 'date_end' => $str_date_end]); - $jo_results = $query->getResult(); + $jo_results = $stmt->fetchAll(); - error_log('Found job orders ' . count($jo_results)); + error_log('JOs found ' . count($jo_results)); - // get user. Use the admin user. - $user = $this->em->getRepository(User::class)->find(1); - - foreach ($jo_results as $jo) + foreach ($jo_results as $jo_row) { - error_log('Fulfilling job order ' . $jo->getID()); + // get the data first + $jo_id = $jo_row['jo_id']; + $cust_id = $jo_row['c_id']; + $cv_id = $jo_row['cv_id']; + $service_type = $jo_row['service_type']; + $warranty_class = $jo_row['warranty_class']; + $rider_id = $jo_row['rider_id']; + $date_schedule = $jo_row['date_schedule']; - //$jo->fulfill(); + // fulfill JO, create job order event + $this->processJOAndJOEvent($conn, $jo_id, $str_current_date, $rider_id); - // set delivery status - //$jo->setDeliveryStatus(DeliveryStatus::FULFILLED); + // check service type + if ($service_type == ServiceType::BATTERY_REPLACEMENT_NEW) + { + // new battery so we need to create warranty so we need to get battery information + $batt_info = $this->getBatteryInformation($jo_id); - // create JO event - //$event = new JOEvent(); - //$event->setDateHappen(new DateTime()) - // ->setTypeID(JOEventType::FULFILL) - // ->setJobOrder($jo) - // ->setUser($user); - - //$this->em->persist($event); - - // check if new battery - // if yes, create warranty - if ($this->checkIfNewBattery($jo)) - { - $this->createWarranty($jo, $user); + $this->createWarrantyForJO($str_current_date, $date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info); } - } - - $this->em->flush(); - */ + } return 0; + } - protected function checkIfNewBattery(JobOrder $jo) + protected function processJOAndJOEvent($conn, $jo_id, $str_current_date, $rider_id) { - if ($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) - return true; + $fulfill_jo_sql = 'UPDATE job_order SET status = :fulfilled WHERE id = :jo_id'; + $fulfill_jo_stmt = $conn->prepare($fulfill_jo_sql); + // TODO: don't forget to uncomment this when testing the whole thing + //$fulfill_jo_stmt->execute([ + // 'fulfilled' => JOStatus::FULFILLED, + // 'jo_id' => $jo_id, + //]); - return false; + // create jo event + // set user to admin that has id of 1 + $user_id = 1; + $jo_event_values = '(' . $user_id . ',' . $jo_id . ',\'' . $str_current_date . '\',\'' . $str_current_date . '\',\'' . JOEventType::FULFILL . '\',' . $rider_id . ')'; + + $create_jo_event_sql = 'INSERT into `jo_event` (create_user_id, job_order_id, date_create, date_happen, type_id, rider_id) VALUES ' . $jo_event_values . ';' . "\n"; + + // error_log($create_jo_event_sql); + + $create_jo_event_stmt = $conn->prepare($create_jo_event_sql); + // TODO: don't forget to uncomment this when testing the whole thing + // $create_jo_event_stmt->execute(); } - protected function createWarranty(JobOrder $jo, User $user) + protected function getBatteryInformation($jo_id) { - $serial = null; - $warranty_class = $jo->getWarrantyClass(); - $first_name = $jo->getCustomer()->getFirstName(); - $last_name = $jo->getCustomer()->getLastName(); - $mobile_number = $jo->getCustomer()->getPhoneMobile(); + $batt_info = []; - // use date_schedule for warranty expiration computation - $date_purchase = $jo->getDateSchedule(); + // get the battery id from invoice item + $ii_sql = 'SELECT ii.id, ii.battery_id FROM invoice i, invoice_item ii + WHERE i.job_order_id = :jo_id + AND ii.invoice_id = i.id + AND ii.battery_id IS NOT NULL'; + $ii_stmt = $conn->prepare($ii_sql); + $ii_stmt->execute([ + 'jo_id' => $jo_id, + ]); - // validate plate number - $plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); - if ($plate_number != false) - { - $batt_list = array(); - $invoice = $jo->getInvoice(); - if (!empty($invoice)) - { - // get battery - $invoice_items = $invoice->getItems(); - foreach ($invoice_items as $item) - { - $battery = $item->getBattery(); - if ($battery != null) - { - $batt_list[] = $item->getBattery(); - } - } - } + $ii_result = $ii_stmt->fetch(); - $user_id = $user->getUsername(); - $source = WarrantySource::ADMIN_PANEL; - $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class, $user_id, $source, $jo->getCustomer(), $jo->getCustomerVehicle()->getVehicle()); - } - else - { - error_log('Invalid plate number for warranty. Plate number = ' . $jo->getCustomerVehicle()->getPlateNumber()); - } + $batt_id = $ii_result['battery_id']; + + // for battery, we need model id, size id, sap_code, warr_private, warr_commercial, warr_tnv + $batt_sql = 'SELECT b.model_id, b.size_id, b.sap_code, b.warr_private, b.warr_commercial, b.warr_tnv + FROM battery b + WHERE b.id = :batt_id'; + $batt_stmt = $conn->prepare($batt_sql); + $batt_stmt->execute([ + 'batt_id' => $batt_id, + ]); + + $batt_result = $batt_stmt->fetch(); + + $batt_info[] = [ + 'model_id' => $batt_result['model_id'], + 'size_id' => $batt_result['size_id'], + 'sap_code' => $batt_result['sap_code'], + 'warr_private' => $batt_result['warr_private'], + 'warr_commercial' => $batt_result['warr_commercial'], + 'warr_tnv' => $batt_result['warr_tnv'], + ]; + + return $batt_info; } + + protected function createWarrantyForJO($str_current_date, $date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info) + { + $conn = $this->em->getConnection(); + + // prepare warranty info + // convert date schedule to string since we use this for date_purchase + $str_date_purchase = $date_schedule->format('Y-m-d H:i:s'); + + // get the warranty period based on warranty class from batt_info + $warranty_period = $this->getWarrantyPeriod($batt_info, $warranty_class); + + // compute date expiry + $date_expire = $this->computeDateExpire($date_schedule, $warranty_period); + + // convert to string the expiry date + $str_date_expire = $date_expire->format('Y-m-d H:i:s'); + + // get customer + $cust_info = $this->getCustomerInfo($conn, $cust_id); + + // get customer vehicle + $cv_info = $this->getCustomerVehicleInfo($conn, $cv_id); + + // TODO: prep sql_values for the insert warranty statement + + $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $sql_values . ';' . "\n"; + + + } + + protected function getCustomerInfo($conn, $id) + { + $cust_info = []; + + $cust_sql = 'SELECT c.first_name, c.last_name, c.phone_mobile + FROM customer c + WHERE c.id = :id'; + $cust_stmt = $conn->prepare($cust_sql); + $cust_stmt->execute([ + 'id' => $id, + ]); + + $cust_result = $cust_stmt->fetch(); + + $cust_info[] = [ + 'first_name' => $cust_result['first_name'], + 'last_name' => $cust_result['last_name'], + 'mobile' => $cust_result['phone_mobile'], + ]; + + return $cust_info; + } + + protected function getCustomerVehicleInfo($conn, $id) + { + $cv_info = []; + + $cv_sql = 'SELECT cv.plate_number + FROM customer_vechicle cv + WHERE cv.id = :id'; + $cv_stmt = $conn->prepare($cv_sql); + $cv_stmt->execute([ + 'id' => $id, + ]); + + $cv_result = $cv_stmt->fetch(); + + $plate_number = $cv_result['plate_number']; + + $clean_plate = $this->cleanPlateNumber($plate_number); + + $cv_info[] = [ + 'plate_number' => $clean_plate, + ]; + + return $cv_info; + } + + protected function getWarrantyPeriod($batt_info, $warranty_class) + { + // set default period to that of private + $period = $batt_info['warr_private'] ; + + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + { + $period = $batt_info['warr_private']; + return $period; + } + if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) + { + $period = $batt_info['warr_commercial']; + return $period; + } + if ($warranty_class == WarrantyClass::WTY_TNV) + { + $period = $batt_info['warr_tnv']; + return $period; + } + + return $period; + } + + protected function computeDateExpire($purchase_date, $warranty_period) + { + $expire_date = clone $purchase_date; + $expire_date->add(new DateInterval('P'.$warranty_period.'M')); + return $expire_date; + } + + protected function cleanPlateNumber($plate) + { + return strtoupper(str_replace(' ', '', $plate)); + } + } From 0dda309a76f81dde2594b4af7f251ec061a64d08 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 May 2022 04:08:28 +0000 Subject: [PATCH 05/14] Add insert command for warranty creation. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 99 +++++++++++++++------- 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index e18b5d2d..263afa29 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -21,6 +21,7 @@ use App\Ramcar\DeliveryStatus; use App\Ramcar\WarrantyClass; use App\Ramcar\ServiceType; use App\Ramcar\WarrantySource; +use App\Ramcar\WarrantyStatus; use DateTime; use DateInterval; @@ -63,16 +64,14 @@ class FulfillOpenJobOrderCommand extends Command // need to get customer id, customer vehicle id, service type, warranty class $conn = $this->em->getConnection(); - $jo_sql = 'SELECT jo.id AS jo_id, c.id AS c_id, cv.id AS cv_id, i + $jo_sql = 'SELECT jo.id AS jo_id, c.id AS c_id, cv.id AS cv_id, jo.service_type, jo.warranty_class, jo.rider_id, jo.date_schedule FROM job_order jo, customer c, customer_vehicle cv WHERE jo.customer_id = c.id AND jo.cvehicle_id = cv.id - AND (jo.status != :cancelled - OR jo.status != :fulfilled) + AND jo.status IN (\'pending\', \'rider_assign\', \'assigned\', \'in_transit\', \'in_progress\') AND jo.date_schedule <= :date_end'; $stmt = $conn->prepare($jo_sql); - $stmt->execute(['cancelled' => JOStatus::CANCELLED, - 'fulfilled' => JOStatus::FULFILLED, + $stmt->execute([ 'date_end' => $str_date_end]); $jo_results = $stmt->fetchAll(); @@ -88,18 +87,21 @@ class FulfillOpenJobOrderCommand extends Command $service_type = $jo_row['service_type']; $warranty_class = $jo_row['warranty_class']; $rider_id = $jo_row['rider_id']; - $date_schedule = $jo_row['date_schedule']; + $str_date_schedule = $jo_row['date_schedule']; // fulfill JO, create job order event $this->processJOAndJOEvent($conn, $jo_id, $str_current_date, $rider_id); + error_log('JO id and service type ' . $jo_id . ' ' . $service_type); + // check service type if ($service_type == ServiceType::BATTERY_REPLACEMENT_NEW) { // new battery so we need to create warranty so we need to get battery information - $batt_info = $this->getBatteryInformation($jo_id); + $batt_info = $this->getBatteryInformation($conn, $jo_id); - $this->createWarrantyForJO($str_current_date, $date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info); + if (!empty($batt_info)) + $this->createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info); } } @@ -109,13 +111,13 @@ class FulfillOpenJobOrderCommand extends Command protected function processJOAndJOEvent($conn, $jo_id, $str_current_date, $rider_id) { - $fulfill_jo_sql = 'UPDATE job_order SET status = :fulfilled WHERE id = :jo_id'; + $fulfill_jo_sql = 'UPDATE job_order SET status = :fulfilled, delivery_status = :del_fulfilled WHERE id = :jo_id'; $fulfill_jo_stmt = $conn->prepare($fulfill_jo_sql); - // TODO: don't forget to uncomment this when testing the whole thing - //$fulfill_jo_stmt->execute([ - // 'fulfilled' => JOStatus::FULFILLED, - // 'jo_id' => $jo_id, - //]); + $fulfill_jo_stmt->execute([ + 'fulfilled' => JOStatus::FULFILLED, + 'del_fulfilled' => DeliveryStatus::FULFILLED + 'jo_id' => $jo_id, + ]); // create jo event // set user to admin that has id of 1 @@ -127,12 +129,13 @@ class FulfillOpenJobOrderCommand extends Command // error_log($create_jo_event_sql); $create_jo_event_stmt = $conn->prepare($create_jo_event_sql); - // TODO: don't forget to uncomment this when testing the whole thing - // $create_jo_event_stmt->execute(); + $create_jo_event_stmt->execute(); } - protected function getBatteryInformation($jo_id) + protected function getBatteryInformation($conn, $jo_id) { + error_log('JO ID ' . $jo_id); + $batt_info = []; // get the battery id from invoice item @@ -147,6 +150,10 @@ class FulfillOpenJobOrderCommand extends Command $ii_result = $ii_stmt->fetch(); + // checking for result + if (empty($ii_result)) + return $batt_info; + $batt_id = $ii_result['battery_id']; // for battery, we need model id, size id, sap_code, warr_private, warr_commercial, warr_tnv @@ -160,7 +167,7 @@ class FulfillOpenJobOrderCommand extends Command $batt_result = $batt_stmt->fetch(); - $batt_info[] = [ + $batt_info = [ 'model_id' => $batt_result['model_id'], 'size_id' => $batt_result['size_id'], 'sap_code' => $batt_result['sap_code'], @@ -169,37 +176,64 @@ class FulfillOpenJobOrderCommand extends Command 'warr_tnv' => $batt_result['warr_tnv'], ]; + // error_log(print_r($batt_info)); + return $batt_info; } - protected function createWarrantyForJO($str_current_date, $date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info) + protected function createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info) { - $conn = $this->em->getConnection(); - - // prepare warranty info - // convert date schedule to string since we use this for date_purchase - $str_date_purchase = $date_schedule->format('Y-m-d H:i:s'); + // convert current date to string since we use this for date_create + $str_current_date = $current_date->format('Y-m-d H:i:s'); // get the warranty period based on warranty class from batt_info $warranty_period = $this->getWarrantyPeriod($batt_info, $warranty_class); // compute date expiry + // convert to DateTime date schedule + $date_schedule = DateTime::createFromFormat('Y-m-d H:i:s', $str_date_schedule); $date_expire = $this->computeDateExpire($date_schedule, $warranty_period); // convert to string the expiry date $str_date_expire = $date_expire->format('Y-m-d H:i:s'); + // check if date_expire is after or equal to the current date + // if so, set warranty status to active + $warranty_status = WarrantyStatus::EXPIRED; + if ($date_expire >= $current_date) + $warranty_status = WarrantyStatus::ACTIVE; + // get customer $cust_info = $this->getCustomerInfo($conn, $cust_id); // get customer vehicle $cv_info = $this->getCustomerVehicleInfo($conn, $cv_id); - // TODO: prep sql_values for the insert warranty statement + // customer info + $first_name = addslashes($cust_info['first_name']); + $last_name = addslashes($cust_info['last_name']); + $mobile = addslashes($cust_info['mobile']); - $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $sql_values . ';' . "\n"; + // customer vehicle info + $plate_number = $cv_info['plate_number']; + $vehicle_id = $cv_info['vehicle_id']; - + // battery info + $model_id = $batt_info['model_id']; + $size_id = $batt_info['size_id']; + $sap_code = $batt_info['sap_code']; + + // populate the values string for the values to be inserted into warranty + $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $warranty_class . '\',\'' + . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule + . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; + + $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; + + error_log($sql_statement); + + $stmt = $conn->prepare($sql_statement); + $stmt->execute(); } protected function getCustomerInfo($conn, $id) @@ -216,7 +250,7 @@ class FulfillOpenJobOrderCommand extends Command $cust_result = $cust_stmt->fetch(); - $cust_info[] = [ + $cust_info = [ 'first_name' => $cust_result['first_name'], 'last_name' => $cust_result['last_name'], 'mobile' => $cust_result['phone_mobile'], @@ -229,8 +263,8 @@ class FulfillOpenJobOrderCommand extends Command { $cv_info = []; - $cv_sql = 'SELECT cv.plate_number - FROM customer_vechicle cv + $cv_sql = 'SELECT cv.plate_number, cv.vehicle_id + FROM customer_vehicle cv WHERE cv.id = :id'; $cv_stmt = $conn->prepare($cv_sql); $cv_stmt->execute([ @@ -243,8 +277,9 @@ class FulfillOpenJobOrderCommand extends Command $clean_plate = $this->cleanPlateNumber($plate_number); - $cv_info[] = [ + $cv_info = [ 'plate_number' => $clean_plate, + 'vehicle_id' => $cv_result['vehicle_id'], ]; return $cv_info; @@ -253,7 +288,7 @@ class FulfillOpenJobOrderCommand extends Command protected function getWarrantyPeriod($batt_info, $warranty_class) { // set default period to that of private - $period = $batt_info['warr_private'] ; + $period = $batt_info['warr_private']; if ($warranty_class == WarrantyClass::WTY_PRIVATE) { From 559a61bb13d0ad145eaf3f896143de7941d38970 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 May 2022 07:04:52 +0000 Subject: [PATCH 06/14] Add checking for null rider ids. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index 263afa29..f18c5baf 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -115,14 +115,21 @@ class FulfillOpenJobOrderCommand extends Command $fulfill_jo_stmt = $conn->prepare($fulfill_jo_sql); $fulfill_jo_stmt->execute([ 'fulfilled' => JOStatus::FULFILLED, - 'del_fulfilled' => DeliveryStatus::FULFILLED + 'del_fulfilled' => DeliveryStatus::FULFILLED, 'jo_id' => $jo_id, ]); // create jo event // set user to admin that has id of 1 $user_id = 1; - $jo_event_values = '(' . $user_id . ',' . $jo_id . ',\'' . $str_current_date . '\',\'' . $str_current_date . '\',\'' . JOEventType::FULFILL . '\',' . $rider_id . ')'; + + $r_id = 'NULL'; + // check if rider is null + if ($rider_id != NULL) + $r_id = $rider_id; + + // need to check rider id if null since that will change the jo_event_values + $jo_event_values = '(' . $user_id . ',' . $jo_id . ',\'' . $str_current_date . '\',\'' . $str_current_date . '\',\'' . JOEventType::FULFILL . '\',' . $r_id . ')'; $create_jo_event_sql = 'INSERT into `jo_event` (create_user_id, job_order_id, date_create, date_happen, type_id, rider_id) VALUES ' . $jo_event_values . ';' . "\n"; @@ -224,6 +231,8 @@ class FulfillOpenJobOrderCommand extends Command $sap_code = $batt_info['sap_code']; // populate the values string for the values to be inserted into warranty + // TODO: add checking for sap code. we need another value string and sql statement if sap_code is blank + // where sap_code is not inserted $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $warranty_class . '\',\'' . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; From f89e8548a9fa6de0adb0cd25146e77203b944f1f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 13 May 2022 07:22:10 +0000 Subject: [PATCH 07/14] Add checking for sap code for warranty creation. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 30 +++++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index f18c5baf..37cd6c07 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -231,13 +231,23 @@ class FulfillOpenJobOrderCommand extends Command $sap_code = $batt_info['sap_code']; // populate the values string for the values to be inserted into warranty - // TODO: add checking for sap code. we need another value string and sql statement if sap_code is blank - // where sap_code is not inserted - $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $warranty_class . '\',\'' - . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule - . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; + // check for sap_code. Not all batteries have sap_code + if (empty($sap_code)) + { + $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $warranty_class . '\',\'' + . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule + . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; - $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; + $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; + } + else + { + $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $warranty_class . '\',\'' + . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule + . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; + + $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; + } error_log($sql_statement); @@ -327,7 +337,13 @@ class FulfillOpenJobOrderCommand extends Command protected function cleanPlateNumber($plate) { - return strtoupper(str_replace(' ', '', $plate)); + // trim plate number down to 20 characters + $trim_plate = str_replace(' ','', $plate); + + // truncate plate number down to 20 (max length) + $trunc_plate = substr($trim_plate, 0, 20); + + return strtoupper($trunc_plate); } } From c099ffd5fd1ecd47dd3f9fda3a90025653169159 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 13 May 2022 09:24:20 +0000 Subject: [PATCH 08/14] Optimize sql selects. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 166 ++++++++++++++------- 1 file changed, 109 insertions(+), 57 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index 37cd6c07..ec2c4533 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -29,6 +29,7 @@ use DateInterval; class FulfillOpenJobOrderCommand extends Command { protected $em; + protected $batt_hash; public function __construct(EntityManagerInterface $em) { @@ -47,6 +48,9 @@ class FulfillOpenJobOrderCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { + // load batteries into hash + $this->populateBatteryIndex(); + // get the input date $str_date_end = $input->getArgument('end_date'); @@ -78,6 +82,7 @@ class FulfillOpenJobOrderCommand extends Command error_log('JOs found ' . count($jo_results)); + $jo_ctr = 1; foreach ($jo_results as $jo_row) { // get the data first @@ -89,36 +94,59 @@ class FulfillOpenJobOrderCommand extends Command $rider_id = $jo_row['rider_id']; $str_date_schedule = $jo_row['date_schedule']; - // fulfill JO, create job order event - $this->processJOAndJOEvent($conn, $jo_id, $str_current_date, $rider_id); + // fulfill JO + $this->fulfillJO($conn, $jo_id, $jo_ctr); - error_log('JO id and service type ' . $jo_id . ' ' . $service_type); + // create JO event + $this->createJOEvent($conn, $jo_id, $str_current_date, $rider_id); + + error_log($jo_ctr . ' Processing JO ' . $jo_id); // check service type if ($service_type == ServiceType::BATTERY_REPLACEMENT_NEW) { - // new battery so we need to create warranty so we need to get battery information - $batt_info = $this->getBatteryInformation($conn, $jo_id); + // new battery so we need to create warranty so we need to get battery id from invoice + $batt_id = $this->getBatteryInformation($conn, $jo_id); - if (!empty($batt_info)) - $this->createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info); + if (($batt_id != null) && (isset($this->batt_hash[$batt_id]))) + $this->createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_id); } + + $jo_ctr++; } return 0; } - protected function processJOAndJOEvent($conn, $jo_id, $str_current_date, $rider_id) + protected function fulfillJO($conn, $jo_id, $jo_ctr) { - $fulfill_jo_sql = 'UPDATE job_order SET status = :fulfilled, delivery_status = :del_fulfilled WHERE id = :jo_id'; - $fulfill_jo_stmt = $conn->prepare($fulfill_jo_sql); - $fulfill_jo_stmt->execute([ - 'fulfilled' => JOStatus::FULFILLED, - 'del_fulfilled' => DeliveryStatus::FULFILLED, - 'jo_id' => $jo_id, - ]); + // TODO: fix this since previous JO ids are not saved. Might have to put update_wheres as a class variable + $update_wheres[] = 'id = ' . $jo_id; + if ($jo_ctr == 100) + { + $update_where_string = implode(' OR ' , $update_wheres); + + // update job order + $fulfill_jo_sql = 'UPDATE job_order SET status = :fulfilled, delivery_status = :del_fulfilled WHERE ' . $update_where_string; + + error_log($fulfill_jo_sql); + + $fulfill_jo_stmt = $conn->prepare($fulfill_jo_sql); + $fulfill_jo_stmt->execute([ + 'fulfilled' => JOStatus::FULFILLED, + 'del_fulfilled' => DeliveryStatus::FULFILLED, + ]); + + // reset the wheres string + $update_wheres = []; + } + + } + + protected function createJOEvent($conn, $jo_id, $str_current_date, $rider_id) + { // create jo event // set user to admin that has id of 1 $user_id = 1; @@ -141,60 +169,50 @@ class FulfillOpenJobOrderCommand extends Command protected function getBatteryInformation($conn, $jo_id) { - error_log('JO ID ' . $jo_id); + // break this down into two sql calls + // get the invoice for job order + $i_sql = 'SELECT i.id FROM invoice i + WHERE i.job_order_id = :jo_id'; + $i_stmt = $conn->prepare($i_sql); + $i_stmt->execute([ + 'jo_id' => $jo_id, + ]); - $batt_info = []; + $i_result = $i_stmt->fetch(); + + // check if invoice exists + if (empty($i_result)) + return null; + + $invoice_id = $i_result['id']; // get the battery id from invoice item - $ii_sql = 'SELECT ii.id, ii.battery_id FROM invoice i, invoice_item ii - WHERE i.job_order_id = :jo_id - AND ii.invoice_id = i.id + $ii_sql = 'SELECT ii.battery_id FROM invoice_item ii + WHERE ii.invoice_id = :invoice_id AND ii.battery_id IS NOT NULL'; $ii_stmt = $conn->prepare($ii_sql); $ii_stmt->execute([ - 'jo_id' => $jo_id, + 'invoice_id' => $invoice_id, ]); $ii_result = $ii_stmt->fetch(); // checking for result if (empty($ii_result)) - return $batt_info; + return null; $batt_id = $ii_result['battery_id']; - // for battery, we need model id, size id, sap_code, warr_private, warr_commercial, warr_tnv - $batt_sql = 'SELECT b.model_id, b.size_id, b.sap_code, b.warr_private, b.warr_commercial, b.warr_tnv - FROM battery b - WHERE b.id = :batt_id'; - $batt_stmt = $conn->prepare($batt_sql); - $batt_stmt->execute([ - 'batt_id' => $batt_id, - ]); - - $batt_result = $batt_stmt->fetch(); - - $batt_info = [ - 'model_id' => $batt_result['model_id'], - 'size_id' => $batt_result['size_id'], - 'sap_code' => $batt_result['sap_code'], - 'warr_private' => $batt_result['warr_private'], - 'warr_commercial' => $batt_result['warr_commercial'], - 'warr_tnv' => $batt_result['warr_tnv'], - ]; - - // error_log(print_r($batt_info)); - - return $batt_info; + return $batt_id; } - protected function createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_info) + protected function createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_id) { // convert current date to string since we use this for date_create $str_current_date = $current_date->format('Y-m-d H:i:s'); - // get the warranty period based on warranty class from batt_info - $warranty_period = $this->getWarrantyPeriod($batt_info, $warranty_class); + // get the warranty period based on warranty class from battery hash + $warranty_period = $this->getWarrantyPeriod($batt_id, $warranty_class); // compute date expiry // convert to DateTime date schedule @@ -226,9 +244,9 @@ class FulfillOpenJobOrderCommand extends Command $vehicle_id = $cv_info['vehicle_id']; // battery info - $model_id = $batt_info['model_id']; - $size_id = $batt_info['size_id']; - $sap_code = $batt_info['sap_code']; + $model_id = $this->batt_hash[$batt_id]['model_id']; + $size_id = $this->batt_hash[$batt_id]['size_id']; + $sap_code = $this->batt_hash[$batt_id]['sap_code']; // populate the values string for the values to be inserted into warranty // check for sap_code. Not all batteries have sap_code @@ -249,7 +267,7 @@ class FulfillOpenJobOrderCommand extends Command $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; } - error_log($sql_statement); + // error_log($sql_statement); $stmt = $conn->prepare($sql_statement); $stmt->execute(); @@ -304,24 +322,24 @@ class FulfillOpenJobOrderCommand extends Command return $cv_info; } - protected function getWarrantyPeriod($batt_info, $warranty_class) + protected function getWarrantyPeriod($batt_id, $warranty_class) { // set default period to that of private - $period = $batt_info['warr_private']; + $period = $this->batt_hash[$batt_id]['warr_private']; if ($warranty_class == WarrantyClass::WTY_PRIVATE) { - $period = $batt_info['warr_private']; + $period = $this->batt_hash[$batt_id]['warr_private']; return $period; } if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) { - $period = $batt_info['warr_commercial']; + $period = $this->batt_hash[$batt_id]['warr_commercial']; return $period; } if ($warranty_class == WarrantyClass::WTY_TNV) { - $period = $batt_info['warr_tnv']; + $period = $this->batt_hash[$batt_id]['warr_tnv']; return $period; } @@ -346,4 +364,38 @@ class FulfillOpenJobOrderCommand extends Command return strtoupper($trunc_plate); } + protected function populateBatteryIndex() + { + $conn = $this->em->getConnection(); + + // get all the batteries + $sql = 'SELECT b.id, b.model_id, b.size_id, b.sap_code, b.warr_private, b.warr_commercial, b.warr_tnv + FROM battery b'; + $stmt = $conn->prepare($sql); + $stmt->execute(); + + $results = $stmt->fetchAll(); + + // go through the rows + foreach ($results as $row) + { + // breaking this down for clarity + $battery_id = $row['id']; + $model_id = $row['model_id']; + $size_id = $row['size_id']; + $sap_code = trim($row['sap_code']); + $warr_private = $row['warr_private']; + $warr_commercial = $row['warr_commercial']; + $warr_tnv = $row['warr_tnv']; + + $this->batt_hash[$battery_id] = [ + 'sap_code' => $sap_code, + 'model_id' => $model_id, + 'size_id' => $size_id, + 'warr_private' => $warr_private, + 'warr_commercial' => $warr_commercial, + 'warr_tnv' => $warr_tnv, + ]; + } + } } From a7d1bb6a90a191518408fd374d92cf4f795e37d9 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 17 May 2022 12:01:15 +0000 Subject: [PATCH 09/14] Add batch insert for JO event. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 134 +++++++++++++++++---- 1 file changed, 109 insertions(+), 25 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index ec2c4533..1b216845 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -28,8 +28,13 @@ use DateInterval; class FulfillOpenJobOrderCommand extends Command { + const DEFAULT_SAP_WARRANTY = 12; + const JO_BATCH_CTR = 500; + const JO_EVENT_BATCH_CTR = 500; + protected $em; protected $batt_hash; + protected $sap_batt_hash; public function __construct(EntityManagerInterface $em) { @@ -51,6 +56,9 @@ class FulfillOpenJobOrderCommand extends Command // load batteries into hash $this->populateBatteryIndex(); + // load sap batteries into hash + $this->populateSAPBatteryIndex(); + // get the input date $str_date_end = $input->getArgument('end_date'); @@ -82,7 +90,15 @@ class FulfillOpenJobOrderCommand extends Command error_log('JOs found ' . count($jo_results)); - $jo_ctr = 1; + $total_jos = count($jo_results); + + $running_jo_total = 0; + $running_jo_event_total = 0; + $update_jo_ctr = 0; + $update_wheres = []; + $create_jo_event_ctr = 0; + $jo_event_values = ''; + foreach ($jo_results as $jo_row) { // get the data first @@ -95,12 +111,12 @@ class FulfillOpenJobOrderCommand extends Command $str_date_schedule = $jo_row['date_schedule']; // fulfill JO - $this->fulfillJO($conn, $jo_id, $jo_ctr); + $this->fulfillJO($conn, $jo_id, $update_jo_ctr, $update_wheres, $running_jo_total, $total_jos); // create JO event - $this->createJOEvent($conn, $jo_id, $str_current_date, $rider_id); + $this->createJOEvent($conn, $jo_id, $str_current_date, $rider_id, $create_jo_event_ctr, $jo_event_values, $jo_event_total, $total_jos); - error_log($jo_ctr . ' Processing JO ' . $jo_id); + // error_log($jo_ctr . ' Processing JO ' . $jo_id); // check service type if ($service_type == ServiceType::BATTERY_REPLACEMENT_NEW) @@ -111,27 +127,32 @@ class FulfillOpenJobOrderCommand extends Command if (($batt_id != null) && (isset($this->batt_hash[$batt_id]))) $this->createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_id); } - - $jo_ctr++; } return 0; } - protected function fulfillJO($conn, $jo_id, $jo_ctr) + protected function fulfillJO($conn, $jo_id, &$update_jo_ctr, &$update_wheres, &$running_jo_total, $total_jos) { - // TODO: fix this since previous JO ids are not saved. Might have to put update_wheres as a class variable $update_wheres[] = 'id = ' . $jo_id; + $unprocessed_jos = $total_jos - $running_jo_total; - if ($jo_ctr == 100) + error_log('Unprocessed JOs ' . $unprocessed_jos); + error_log('Update JO counter ' . $update_jo_ctr); + + // TODO: check why this is not working for unprocessed jos. this is for the case when we have the remainder. + // solution to this will also need to be applied to jo event creation + // update db when we reach max # of JOs or when remaining JOs are less than max # + if (($update_jo_ctr == self::JO_BATCH_CTR) || + ($update_jo_ctr == $unprocessed_jos)) { $update_where_string = implode(' OR ' , $update_wheres); // update job order $fulfill_jo_sql = 'UPDATE job_order SET status = :fulfilled, delivery_status = :del_fulfilled WHERE ' . $update_where_string; - error_log($fulfill_jo_sql); + // error_log($fulfill_jo_sql); $fulfill_jo_stmt = $conn->prepare($fulfill_jo_sql); $fulfill_jo_stmt->execute([ @@ -139,19 +160,32 @@ class FulfillOpenJobOrderCommand extends Command 'del_fulfilled' => DeliveryStatus::FULFILLED, ]); + error_log('Processed ' . $update_jo_ctr . ' job orders. '); + + // update the running total + $running_jo_total = $running_jo_total + $update_jo_ctr; + // reset the wheres string $update_wheres = []; - } + // reset the update jo counter + $update_jo_ctr = 0; + } + else + $update_jo_ctr++; } - protected function createJOEvent($conn, $jo_id, $str_current_date, $rider_id) + protected function createJOEvent($conn, $jo_id, $str_current_date, $rider_id, &$create_jo_event_ctr, &$jo_event_value_string, + &$running_jo_event_total, $total_jos) { + $unprocessed_events = $total_jos - $running_jo_event_total; + // create jo event // set user to admin that has id of 1 $user_id = 1; $r_id = 'NULL'; + // need to check rider id if null since that will change the jo_event_values // check if rider is null if ($rider_id != NULL) $r_id = $rider_id; @@ -159,12 +193,37 @@ class FulfillOpenJobOrderCommand extends Command // need to check rider id if null since that will change the jo_event_values $jo_event_values = '(' . $user_id . ',' . $jo_id . ',\'' . $str_current_date . '\',\'' . $str_current_date . '\',\'' . JOEventType::FULFILL . '\',' . $r_id . ')'; - $create_jo_event_sql = 'INSERT into `jo_event` (create_user_id, job_order_id, date_create, date_happen, type_id, rider_id) VALUES ' . $jo_event_values . ';' . "\n"; - - // error_log($create_jo_event_sql); + if (strlen($jo_event_value_string) == 0) + { + // first entry to insert, no comma before + $jo_event_value_string = $jo_event_values; + } + else + { + // need to insert a comma after the existing string + $jo_event_value_string = $jo_event_value_string . ',' . $jo_event_values; + } - $create_jo_event_stmt = $conn->prepare($create_jo_event_sql); - $create_jo_event_stmt->execute(); + // insert to db when we reach max # of JOs or when remaining JOs are less than max # + if (($create_jo_event_ctr == self::JO_EVENT_BATCH_CTR) || + ($create_jo_event_ctr == $unprocessed_events)) + { + $create_jo_event_sql = 'INSERT into `jo_event` (create_user_id, job_order_id, date_create, date_happen, type_id, rider_id) VALUES ' . $jo_event_value_string . ';' . "\n"; + + // error_log($create_jo_event_sql); + + $create_jo_event_stmt = $conn->prepare($create_jo_event_sql); + $create_jo_event_stmt->execute(); + + // update the running jo event total + $running_jo_event_total = $running_jo_event_total + $create_jo_event_ctr; + + // reset the counter and value string + $jo_event_value_string = ''; + $create_jo_event_ctr = 0; + } + else + $create_jo_event_ctr++; } protected function getBatteryInformation($conn, $jo_id) @@ -214,7 +273,7 @@ class FulfillOpenJobOrderCommand extends Command // get the warranty period based on warranty class from battery hash $warranty_period = $this->getWarrantyPeriod($batt_id, $warranty_class); - // compute date expiry + // compute date expiry. // convert to DateTime date schedule $date_schedule = DateTime::createFromFormat('Y-m-d H:i:s', $str_date_schedule); $date_expire = $this->computeDateExpire($date_schedule, $warranty_period); @@ -246,27 +305,29 @@ class FulfillOpenJobOrderCommand extends Command // battery info $model_id = $this->batt_hash[$batt_id]['model_id']; $size_id = $this->batt_hash[$batt_id]['size_id']; - $sap_code = $this->batt_hash[$batt_id]['sap_code']; + // need to confirm that sap_code exists in sap_battery + if (isset($this->sap_batt_hash['sap_code'])) + $sap_code = $this->batt_hash[$batt_id]['sap_code']; + else + $sap_code = 'NULL'; // populate the values string for the values to be inserted into warranty // check for sap_code. Not all batteries have sap_code - if (empty($sap_code)) + if ($sap_code == 'NULL') { - $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $warranty_class . '\',\'' + $value_string = '(' . $model_id . ',' . $size_id . ',' . $sap_code . ',\'' . $warranty_class . '\',\'' . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; - - $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; } else { $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $warranty_class . '\',\'' . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; - - $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; } + $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; + // error_log($sql_statement); $stmt = $conn->prepare($sql_statement); @@ -398,4 +459,27 @@ class FulfillOpenJobOrderCommand extends Command ]; } } + + protected function populateSAPBatteryIndex() + { + $conn = $this->em->getConnection(); + + // get all the sap batteries + $sql = 'SELECT sap.id, sap.brand_id, sap.size_id FROM sap_battery sap'; + $stmt = $conn->prepare($sql); + $stmt->execute(); + + $results = $stmt->fetchAll(); + + // go through the rows + foreach ($results as $row) + { + // set warranty period to default warranty period for SAP batteries + $this->sap_batt_hash[$row['id']] = [ + 'sap_brand' => $row['brand_id'], + 'sap_size' => $row['size_id'], + 'warranty' => self::DEFAULT_SAP_WARRANTY, + ]; + } + } } From 1778af3f9f205378e9d46162e15402dfc70c3167 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 18 May 2022 05:53:38 +0000 Subject: [PATCH 10/14] Fix issue on batch updates and inserts. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 58 +++++++++++++--------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index 1b216845..bf3849f4 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -92,6 +92,7 @@ class FulfillOpenJobOrderCommand extends Command $total_jos = count($jo_results); + $jo_ctr = 0; $running_jo_total = 0; $running_jo_event_total = 0; $update_jo_ctr = 0; @@ -110,11 +111,13 @@ class FulfillOpenJobOrderCommand extends Command $rider_id = $jo_row['rider_id']; $str_date_schedule = $jo_row['date_schedule']; + $jo_ctr++; + // fulfill JO - $this->fulfillJO($conn, $jo_id, $update_jo_ctr, $update_wheres, $running_jo_total, $total_jos); + $this->fulfillJO($conn, $jo_id, $update_jo_ctr, $update_wheres, $jo_ctr, $total_jos); // create JO event - $this->createJOEvent($conn, $jo_id, $str_current_date, $rider_id, $create_jo_event_ctr, $jo_event_values, $jo_event_total, $total_jos); + $this->createJOEvent($conn, $jo_id, $str_current_date, $rider_id, $create_jo_event_ctr, $jo_event_values, $jo_ctr, $total_jos); // error_log($jo_ctr . ' Processing JO ' . $jo_id); @@ -133,19 +136,13 @@ class FulfillOpenJobOrderCommand extends Command } - protected function fulfillJO($conn, $jo_id, &$update_jo_ctr, &$update_wheres, &$running_jo_total, $total_jos) + protected function fulfillJO($conn, $jo_id, &$update_jo_ctr, &$update_wheres, $jo_ctr, $total_jos) { $update_wheres[] = 'id = ' . $jo_id; - $unprocessed_jos = $total_jos - $running_jo_total; - error_log('Unprocessed JOs ' . $unprocessed_jos); - error_log('Update JO counter ' . $update_jo_ctr); - - // TODO: check why this is not working for unprocessed jos. this is for the case when we have the remainder. - // solution to this will also need to be applied to jo event creation - // update db when we reach max # of JOs or when remaining JOs are less than max # + // update db when we reach max # of JOs or when we reach total number of jos if (($update_jo_ctr == self::JO_BATCH_CTR) || - ($update_jo_ctr == $unprocessed_jos)) + ($jo_ctr == $total_jos)) { $update_where_string = implode(' OR ' , $update_wheres); @@ -160,11 +157,6 @@ class FulfillOpenJobOrderCommand extends Command 'del_fulfilled' => DeliveryStatus::FULFILLED, ]); - error_log('Processed ' . $update_jo_ctr . ' job orders. '); - - // update the running total - $running_jo_total = $running_jo_total + $update_jo_ctr; - // reset the wheres string $update_wheres = []; @@ -176,10 +168,8 @@ class FulfillOpenJobOrderCommand extends Command } protected function createJOEvent($conn, $jo_id, $str_current_date, $rider_id, &$create_jo_event_ctr, &$jo_event_value_string, - &$running_jo_event_total, $total_jos) + $jo_ctr, $total_jos) { - $unprocessed_events = $total_jos - $running_jo_event_total; - // create jo event // set user to admin that has id of 1 $user_id = 1; @@ -204,9 +194,9 @@ class FulfillOpenJobOrderCommand extends Command $jo_event_value_string = $jo_event_value_string . ',' . $jo_event_values; } - // insert to db when we reach max # of JOs or when remaining JOs are less than max # + // insert to db when we reach max # of JOs or when we reach total number of jos if (($create_jo_event_ctr == self::JO_EVENT_BATCH_CTR) || - ($create_jo_event_ctr == $unprocessed_events)) + ($jo_ctr == $total_jos)) { $create_jo_event_sql = 'INSERT into `jo_event` (create_user_id, job_order_id, date_create, date_happen, type_id, rider_id) VALUES ' . $jo_event_value_string . ';' . "\n"; @@ -215,9 +205,6 @@ class FulfillOpenJobOrderCommand extends Command $create_jo_event_stmt = $conn->prepare($create_jo_event_sql); $create_jo_event_stmt->execute(); - // update the running jo event total - $running_jo_event_total = $running_jo_event_total + $create_jo_event_ctr; - // reset the counter and value string $jo_event_value_string = ''; $create_jo_event_ctr = 0; @@ -311,6 +298,27 @@ class FulfillOpenJobOrderCommand extends Command else $sap_code = 'NULL'; + /* + // create array for the infile + $warranty_data = [ + $model_id, + $size_id, + $sap_code, + $warranty_class, + $plate_number, + $warranty_status, + $str_current_date, + $str_date_schedule, + $str_date_expire, + $first_name, + $last_name, + $mobile, + $vehicle_id, + $cust_id, + $WarrantySource::ADMIN_PANEL, + ]; + */ + // populate the values string for the values to be inserted into warranty // check for sap_code. Not all batteries have sap_code if ($sap_code == 'NULL') @@ -332,6 +340,8 @@ class FulfillOpenJobOrderCommand extends Command $stmt = $conn->prepare($sql_statement); $stmt->execute(); + + // $this->createLoadDataFileForWarranty($warranty_data); } protected function getCustomerInfo($conn, $id) From 31e2088b6ad2b0e5ebe9323162c96f953f23ed33 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 18 May 2022 05:55:46 +0000 Subject: [PATCH 11/14] Add log for processing. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index bf3849f4..b6512853 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -144,6 +144,8 @@ class FulfillOpenJobOrderCommand extends Command if (($update_jo_ctr == self::JO_BATCH_CTR) || ($jo_ctr == $total_jos)) { + error_log('Processing ' . $update_jo_ctr . ' job orders...'); + $update_where_string = implode(' OR ' , $update_wheres); // update job order From c8208df0f3dc054f630bd8c46c169776f5675b68 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 18 May 2022 08:46:13 +0000 Subject: [PATCH 12/14] Modify warranty creation to be done by load data infile. #654 --- config/packages/doctrine.yaml | 2 + src/Command/FulfillOpenJobOrderCommand.php | 61 ++++++++++++++++++---- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index c154fbc2..8dfccd95 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -11,6 +11,8 @@ doctrine: driver: 'pdo_mysql' server_version: '5.7' charset: utf8mb4 + options: + !php/const PDO::MYSQL_ATTR_LOCAL_INFILE: true # With Symfony 3.3, remove the `resolve:` prefix url: '%env(resolve:DATABASE_URL)%' diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index b6512853..a582d349 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -93,12 +93,11 @@ class FulfillOpenJobOrderCommand extends Command $total_jos = count($jo_results); $jo_ctr = 0; - $running_jo_total = 0; - $running_jo_event_total = 0; $update_jo_ctr = 0; $update_wheres = []; $create_jo_event_ctr = 0; $jo_event_values = ''; + $w_data = []; foreach ($jo_results as $jo_row) { @@ -128,10 +127,14 @@ class FulfillOpenJobOrderCommand extends Command $batt_id = $this->getBatteryInformation($conn, $jo_id); if (($batt_id != null) && (isset($this->batt_hash[$batt_id]))) - $this->createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_id); + $w_data[] = $this->createWarrantyForJO($conn, $current_date, $str_date_schedule, $cust_id, $cv_id, $warranty_class, $batt_id); } } + // TODO: make data file too for JO event creation + + $this->createLoadDataFileForWarranty($w_data); + return 0; } @@ -268,7 +271,10 @@ class FulfillOpenJobOrderCommand extends Command $date_expire = $this->computeDateExpire($date_schedule, $warranty_period); // convert to string the expiry date - $str_date_expire = $date_expire->format('Y-m-d H:i:s'); + $str_date_expire = $date_expire->format('Y-m-d'); + + // convert date schedule to just date + $str_date_purchase = $date_schedule->format('Y-m-d'); // check if date_expire is after or equal to the current date // if so, set warranty status to active @@ -298,11 +304,10 @@ class FulfillOpenJobOrderCommand extends Command if (isset($this->sap_batt_hash['sap_code'])) $sap_code = $this->batt_hash[$batt_id]['sap_code']; else - $sap_code = 'NULL'; + $sap_code = '\N'; - /* // create array for the infile - $warranty_data = [ + $warranty_data = [ $model_id, $size_id, $sap_code, @@ -310,17 +315,18 @@ class FulfillOpenJobOrderCommand extends Command $plate_number, $warranty_status, $str_current_date, - $str_date_schedule, + $str_date_purchase, $str_date_expire, $first_name, $last_name, $mobile, + 1, $vehicle_id, $cust_id, - $WarrantySource::ADMIN_PANEL, + WarrantySource::ADMIN_PANEL, ]; - */ + /* // populate the values string for the values to be inserted into warranty // check for sap_code. Not all batteries have sap_code if ($sap_code == 'NULL') @@ -342,8 +348,41 @@ class FulfillOpenJobOrderCommand extends Command $stmt = $conn->prepare($sql_statement); $stmt->execute(); + */ - // $this->createLoadDataFileForWarranty($warranty_data); + return $warranty_data; + } + + protected function createLoadDataFileForWarranty($warranty_data) + { + // cache directory + $cache_dir = __DIR__ . '/../../var/cache'; + + $file = $cache_dir . '/warranty_data.tab'; + error_log('opening file for warranty - ' . $file); + + $fp = fopen($file, 'w'); + if ($fp === false) + { + error_log('could not open file for load data infile - ' . $file); + } + else + { + foreach ($warranty_data as $key => $data) + { + $line = implode('|', $data) . "\r\n"; + fwrite($fp, $line); + } + } + + fclose($fp); + + error_log('Loading warranty data'); + $conn = $this->em->getConnection(); + $stmt = $conn->prepare('LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE warranty FIELDS TERMINATED BY \'|\' LINES TERMINATED BY \'\\r\\n\' (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source)'); + $result = $stmt->execute(); + if (!$result) + error_log('Failed loading data.'); } protected function getCustomerInfo($conn, $id) From 941f59ab6e05432a115082b76d6bd1bc36ef982a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 18 May 2022 09:34:15 +0000 Subject: [PATCH 13/14] Change jo event creation. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 118 ++++++++++----------- 1 file changed, 54 insertions(+), 64 deletions(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index a582d349..d6821a15 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -29,8 +29,7 @@ use DateInterval; class FulfillOpenJobOrderCommand extends Command { const DEFAULT_SAP_WARRANTY = 12; - const JO_BATCH_CTR = 500; - const JO_EVENT_BATCH_CTR = 500; + const JO_BATCH_CTR = 200; protected $em; protected $batt_hash; @@ -95,9 +94,8 @@ class FulfillOpenJobOrderCommand extends Command $jo_ctr = 0; $update_jo_ctr = 0; $update_wheres = []; - $create_jo_event_ctr = 0; - $jo_event_values = ''; $w_data = []; + $jo_evt_data = []; foreach ($jo_results as $jo_row) { @@ -116,7 +114,7 @@ class FulfillOpenJobOrderCommand extends Command $this->fulfillJO($conn, $jo_id, $update_jo_ctr, $update_wheres, $jo_ctr, $total_jos); // create JO event - $this->createJOEvent($conn, $jo_id, $str_current_date, $rider_id, $create_jo_event_ctr, $jo_event_values, $jo_ctr, $total_jos); + $jo_evt_data[] = $this->createJOEvent($conn, $jo_id, $str_current_date, $rider_id); // error_log($jo_ctr . ' Processing JO ' . $jo_id); @@ -131,8 +129,10 @@ class FulfillOpenJobOrderCommand extends Command } } - // TODO: make data file too for JO event creation + // load data file for jo event + $this->createLoadDataFileForJOEvent($jo_evt_data); + // load data file for warranty $this->createLoadDataFileForWarranty($w_data); return 0; @@ -172,50 +172,28 @@ class FulfillOpenJobOrderCommand extends Command $update_jo_ctr++; } - protected function createJOEvent($conn, $jo_id, $str_current_date, $rider_id, &$create_jo_event_ctr, &$jo_event_value_string, - $jo_ctr, $total_jos) + protected function createJOEvent($conn, $jo_id, $str_current_date, $rider_id) { // create jo event // set user to admin that has id of 1 $user_id = 1; - $r_id = 'NULL'; - // need to check rider id if null since that will change the jo_event_values + $r_id = '\N'; // check if rider is null if ($rider_id != NULL) $r_id = $rider_id; - // need to check rider id if null since that will change the jo_event_values - $jo_event_values = '(' . $user_id . ',' . $jo_id . ',\'' . $str_current_date . '\',\'' . $str_current_date . '\',\'' . JOEventType::FULFILL . '\',' . $r_id . ')'; + // create array for the jo event + $data = [ + $user_id, + $jo_id, + $str_current_date, + $str_current_date, + JOEventType::FULFILL, + $r_id, + ]; - if (strlen($jo_event_value_string) == 0) - { - // first entry to insert, no comma before - $jo_event_value_string = $jo_event_values; - } - else - { - // need to insert a comma after the existing string - $jo_event_value_string = $jo_event_value_string . ',' . $jo_event_values; - } - - // insert to db when we reach max # of JOs or when we reach total number of jos - if (($create_jo_event_ctr == self::JO_EVENT_BATCH_CTR) || - ($jo_ctr == $total_jos)) - { - $create_jo_event_sql = 'INSERT into `jo_event` (create_user_id, job_order_id, date_create, date_happen, type_id, rider_id) VALUES ' . $jo_event_value_string . ';' . "\n"; - - // error_log($create_jo_event_sql); - - $create_jo_event_stmt = $conn->prepare($create_jo_event_sql); - $create_jo_event_stmt->execute(); - - // reset the counter and value string - $jo_event_value_string = ''; - $create_jo_event_ctr = 0; - } - else - $create_jo_event_ctr++; + return $data; } protected function getBatteryInformation($conn, $jo_id) @@ -326,30 +304,6 @@ class FulfillOpenJobOrderCommand extends Command WarrantySource::ADMIN_PANEL, ]; - /* - // populate the values string for the values to be inserted into warranty - // check for sap_code. Not all batteries have sap_code - if ($sap_code == 'NULL') - { - $value_string = '(' . $model_id . ',' . $size_id . ',' . $sap_code . ',\'' . $warranty_class . '\',\'' - . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule - . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; - } - else - { - $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $warranty_class . '\',\'' - . $plate_number . '\',\'' . $warranty_status . '\',\'' . $str_current_date . '\',\'' . $str_date_schedule - . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::ADMIN_PANEL . '\')'; - } - - $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $value_string . ';' . "\n"; - - // error_log($sql_statement); - - $stmt = $conn->prepare($sql_statement); - $stmt->execute(); - */ - return $warranty_data; } @@ -383,6 +337,42 @@ class FulfillOpenJobOrderCommand extends Command $result = $stmt->execute(); if (!$result) error_log('Failed loading data.'); + + // TODO: delete file? + } + + protected function createLoadDataFileForJOEvent($jo_evt_data) + { + // cache directory + $cache_dir = __DIR__ . '/../../var/cache'; + + $file = $cache_dir . '/jo_event_data.tab'; + error_log('opening file for jo_event - ' . $file); + + $fp = fopen($file, 'w'); + if ($fp === false) + { + error_log('could not open file for load data infile - ' . $file); + } + else + { + foreach ($jo_evt_data as $key => $data) + { + $line = implode('|', $data) . "\r\n"; + fwrite($fp, $line); + } + } + + fclose($fp); + + error_log('Loading jo event data'); + $conn = $this->em->getConnection(); + $stmt = $conn->prepare('LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE jo_event FIELDS TERMINATED BY \'|\' LINES TERMINATED BY \'\\r\\n\' (create_user_id, job_order_id, date_create, date_happen, type_id, rider_id)'); + $result = $stmt->execute(); + if (!$result) + error_log('Failed loading data.'); + + // TODO: delete file? } protected function getCustomerInfo($conn, $id) From ba87fe61c59d0d00d5e09ae8bd1c49c14a4cb10f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 19 May 2022 05:44:23 +0000 Subject: [PATCH 14/14] Set flag_activated to false. #654 --- src/Command/FulfillOpenJobOrderCommand.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Command/FulfillOpenJobOrderCommand.php b/src/Command/FulfillOpenJobOrderCommand.php index d6821a15..2e3f3b9e 100644 --- a/src/Command/FulfillOpenJobOrderCommand.php +++ b/src/Command/FulfillOpenJobOrderCommand.php @@ -284,6 +284,9 @@ class FulfillOpenJobOrderCommand extends Command else $sap_code = '\N'; + // set flag_activated to false since that's the default in Warranty's constructor + $flag_activated = false; + // create array for the infile $warranty_data = [ $model_id, @@ -298,7 +301,7 @@ class FulfillOpenJobOrderCommand extends Command $first_name, $last_name, $mobile, - 1, + $flag_activated, $vehicle_id, $cust_id, WarrantySource::ADMIN_PANEL,