From 0dda309a76f81dde2594b4af7f251ec061a64d08 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 May 2022 04:08:28 +0000 Subject: [PATCH] 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) {