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, + ]; + } + } }