diff --git a/src/Command/SetJobOrderCustNewCommand.php b/src/Command/SetJobOrderCustNewCommand.php new file mode 100644 index 00000000..43558072 --- /dev/null +++ b/src/Command/SetJobOrderCustNewCommand.php @@ -0,0 +1,74 @@ +em = $em; + $this->jo_manager = $jo_manager; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('jobOrder:setcustomernew') + ->setDescription('Set job order\'s customer new flag for existing job orders.') + ->setHelp('Set job order\'s customer new flag for existing job orders'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + + // pdo connection + $db = $em->getConnection(); + + // get all the ids for all job orders + $all_query_sql = 'SELECT id AS jo_id, customer_id AS cust_id FROM job_order ORDER BY id'; + + $all_query_stmt = $db->prepare($all_query_sql); + $all_query_stmt->execute(); + + $all_jo_results = $all_query_stmt->fetchAll(); + + foreach ($all_jo_results as $jo_row) + { + // for each jo id, get the customer id + $jo_id = $jo_row['jo_id']; + $cust_id = $jo_row['cust_id']; + + // check how many JOs have that customer id + $jo_count = $this->jo_manager->getCustomerJobOrderCount(); + + // if one or less, set flag_cust_new to true + if ($jo_count <= 1) + $this->updateCustNew($db, $jo_id); + } + + return 0; + } + + protected function updateCustNew($db, $jo_id) + { + $update_jo_sql = 'UPDATE job_order SET flag_cust_new = :flag_cust_new WHERE id = :jo_id'; + + $update_jo_stmt = $db->prepare($update_jo_sql); + $update_jo_stmt->execute([ + 'flag_cust_new' => true, + 'jo_id' => $jo_id + ]); + } +} diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 821fa490..16a9cdf5 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -931,13 +931,13 @@ class APIController extends Controller implements LoggedController } // check if customer has more than one job order already - $flag_new_cust = false; - $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust); + $flag_cust_new = false; + $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust->getID()); if ($cust_jo_count <= 1) - $flag_new_cust = true; + $flag_cust_new = true; $jo->setCustomer($cust); - $jo->setCustNew($flag_new_cust); + $jo->setCustNew($flag_cust_new); // validate service type $stype = $req->request->get('service_type'); @@ -2868,14 +2868,13 @@ class APIController extends Controller implements LoggedController // } // check if customer has more than one job order already - $flag_new_cust = false; - $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust); - error_log('cust jo count for customer id ' .$cust->getID() . ' ' . $cust_jo_count); + $flag_cust_new = false; + $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust->getID()); if ($cust_jo_count <= 1) - $flag_new_cust = true; + $flag_cust_new = true; $jo->setCustomer($cust); - $jo->setCustNew($flag_new_cust); + $jo->setCustNew($flag_cust_new); // validate service type $stype = $req->request->get('service_type'); diff --git a/src/Controller/CustomerAppAPI/JobOrderController.php b/src/Controller/CustomerAppAPI/JobOrderController.php index 3bffbfc6..bd91e9fb 100644 --- a/src/Controller/CustomerAppAPI/JobOrderController.php +++ b/src/Controller/CustomerAppAPI/JobOrderController.php @@ -598,14 +598,14 @@ class JobOrderController extends ApiController // } // check if customer has more than one job order already - $flag_new_cust = false; + $flag_cust_new = false; - $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust); + $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust->getID()); if ($cust_jo_count <= 1) - $flag_new_cust = true; + $flag_cust_new = true; $jo->setCustomer($cust); - $jo->setCustNew($flag_new_cust); + $jo->setCustNew($flag_cust_new); // validate service type $stype = $req->request->get('service_type'); @@ -1025,14 +1025,14 @@ class JobOrderController extends ApiController } // check if customer has more than one job order already - $flag_new_cust = false; + $flag_cust_new = false; - $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust); + $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust->getID()); if ($cust_jo_count <= 1) - $flag_new_cust = true; + $flag_cust_new = true; $jo->setCustomer($cust); - $jo->setCustNew($flag_new_cust); + $jo->setCustNew($flag_cust_new); // validate service type $stype = $req->request->get('service_type'); diff --git a/src/Controller/TAPI/JobOrderController.php b/src/Controller/TAPI/JobOrderController.php index 023543be..f7541b50 100644 --- a/src/Controller/TAPI/JobOrderController.php +++ b/src/Controller/TAPI/JobOrderController.php @@ -138,7 +138,7 @@ class JobOrderController extends ApiController ->setAdvanceOrder($data['is_advance_order']) ->setStatusAutoAssign(AutoAssignStatus::NOT_ASSIGNED) ->setLandmark($data['landmark']) - ->setCustNew($data['flag_new_cust']); + ->setCustNew($data['flag_cust_new']); $jo->setCustomer($data['customer']); $jo->setCustomerVehicle($data['customer_vehicle']); @@ -1390,7 +1390,7 @@ class JobOrderController extends ApiController 'customer_vehicle' => $cust_data['customer_vehicle'], 'source' => TransactionOrigin::THIRD_PARTY, 'warranty_class' => $warranty_class, - 'flag_new_cust' => $cust_data['flag_new_cust'], + 'flag_cust_new' => $cust_data['flag_cust_new'], ]; return null; @@ -1564,7 +1564,7 @@ class JobOrderController extends ApiController // find customer + customer vehicle combo $cust_vehicle = $this->findCustomerAndCustomerVehicle($data, $em); - $flag_new_cust = false; + $flag_cust_new = false; if ($cust_vehicle == null) { // find customer given phone number @@ -1601,7 +1601,7 @@ class JobOrderController extends ApiController // add customer vehicle $cust_vehicle = $this->createCustomerVehicle($em, $cust, $data); - $flag_new_cust = true; + $flag_cust_new = true; } else { @@ -1615,15 +1615,15 @@ class JobOrderController extends ApiController // check if customer has more than one job order already if ($cust != null) { - $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust); + $cust_jo_count = $jo_manager->getCustomerJobOrderCount($cust->getID()); if ($cust_jo_count <= 1) - $flag_new_cust = true; + $flag_cust_new = true; } $c_data = [ 'customer' => $cust_vehicle->getCustomer(), 'customer_vehicle' => $cust_vehicle, - 'flag_new_cust' => $flag_new_cust, + 'flag_cust_new' => $flag_cust_new, ]; return $c_data; diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index 8be6bd4c..23a77289 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -363,7 +363,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface // find customer $cust_id = $req->request->get('cid'); $customer = $em->getRepository(Customer::class)->find($cust_id); - $flag_new_cust = false; + $flag_cust_new = false; if (empty($customer)) { $error_array['customer_vehicle'] = 'Invalid customer specified.'; @@ -394,9 +394,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface } // check if customer has more than one job order already - $cust_jo_count = $this->jo_manager->getCustomerJobOrderCount($customer); + $cust_jo_count = $this->jo_manager->getCustomerJobOrderCount($customer->getID()); if ($cust_jo_count <= 1) - $flag_new_cust = true; + $flag_cust_new = true; // check if lat and lng are provided if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) { @@ -530,7 +530,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface ->setEmergencyType($etype) ->setOwnershipType($owner_type) ->setCustomerLocation($cust_location) - ->setCustNew($flag_new_cust); + ->setCustNew($flag_cust_new); // check if user is null, meaning call to create came from API if ($user != null) diff --git a/src/Service/JobOrderManager.php b/src/Service/JobOrderManager.php index 7f097267..e592b6ca 100644 --- a/src/Service/JobOrderManager.php +++ b/src/Service/JobOrderManager.php @@ -39,16 +39,14 @@ class JobOrderManager return false; } - public function getCustomerJobOrderCount($customer) + public function getCustomerJobOrderCount($customer_id) { $db = $this->em->getConnection(); - $cust_id = $customer->getID(); - $query_sql = 'SELECT COUNT(*) AS jo_count FROM job_order WHERE customer_id = :cust_id AND status != :status_cancelled'; $query_stmt = $db->prepare($query_sql); - $query_stmt->bindValue('cust_id', $cust_id); + $query_stmt->bindValue('cust_id', $customer_id); $query_stmt->bindValue('status_cancelled', JOStatus::CANCELLED); $jo_results = $query_stmt->executeQuery();