Add batch insert for JO event. #654

This commit is contained in:
Korina Cordero 2022-05-17 12:01:15 +00:00
parent fa02cd1a67
commit a7d1bb6a90

View file

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