Optimize sql call to get job order information. Fix creation issue in warranty handler. #689
This commit is contained in:
parent
0ea02ae04d
commit
0dc972b906
2 changed files with 301 additions and 37 deletions
|
|
@ -1884,8 +1884,10 @@ class APIController extends Controller implements LoggedController
|
|||
}
|
||||
|
||||
// get job order data
|
||||
$jo = $em->getRepository(JobOrder::class)->find($id);
|
||||
if ($jo == null)
|
||||
// $jo = $em->getRepository(JobOrder::class)->find($id);
|
||||
// find just the job order and selected fields
|
||||
$jo_data = $this->findJobOrder($id, $em);
|
||||
if (empty($jo_data))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No job order information found');
|
||||
|
|
@ -1893,16 +1895,21 @@ class APIController extends Controller implements LoggedController
|
|||
}
|
||||
|
||||
// check if job order belongs to customer / user
|
||||
if ($cust->getID() != $jo->getCustomer()->getID())
|
||||
if ($cust->getID() != $jo_data['customer_id'])
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No job order information found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// put into job order data array
|
||||
$jo_data = $this->generateJobOrderData($req, $jo, $rt);
|
||||
// we already have customer from the mobile session
|
||||
// get the rest of the needed information from invoice and rider rating and customer vehicle
|
||||
$other_data = $this->findOtherData($jo_data, $id, $em);
|
||||
|
||||
// put into job order data array
|
||||
// TODO: might need a new function to generate the JO data
|
||||
// $jo_data = $this->generateJobOrderData($req, $jo, $rt);
|
||||
$jo_data = $this->createJobOrderData($req, $jo_data, $other_data, $cust, $rt);
|
||||
|
||||
$data = [
|
||||
'job_order' => $jo_data
|
||||
|
|
@ -4696,6 +4703,280 @@ class APIController extends Controller implements LoggedController
|
|||
return $jo_data;
|
||||
}
|
||||
|
||||
protected function createJobOrderData($req, $jo_data, $other_data, $cust, $rt)
|
||||
{
|
||||
$status = $jo_data['status'];
|
||||
|
||||
// need to convert the date_create to DateTime then string with the correct format
|
||||
$date_create = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_create']);
|
||||
$str_date_create = $date_create->format('M d, Y');
|
||||
|
||||
$data = [
|
||||
'id' => $jo_data['id'],
|
||||
'date_create' => $str_date_create,
|
||||
'service_type' => $jo_data['service_type'],
|
||||
'destination' => [
|
||||
'long' => $jo_data['longitude'],
|
||||
'lat' => $jo_data['latitude'],
|
||||
],
|
||||
'delivery_address' => $jo_data['delivery_address'],
|
||||
'delivery_instructions' => $jo_data['delivery_instructions'],
|
||||
'jo_status' => $status,
|
||||
'status' => $this->generateAPIRiderStatus($status),
|
||||
];
|
||||
|
||||
// get customer vehicle and warranty
|
||||
$cv_data = $other_data['cv_data'];
|
||||
$cv_id = $cv_data['id'];
|
||||
$plate_number = $cv_data['plate_number'];
|
||||
|
||||
$warranty = $this->findWarranty($plate_number);
|
||||
|
||||
$data['customer_vehicle'] = [
|
||||
'id' => $cv_id,
|
||||
'plate_number' => $plate_number,
|
||||
'warranty' => $warranty,
|
||||
];
|
||||
|
||||
// customer information
|
||||
$data['customer'] = [
|
||||
'first_name' => $cust->getFirstName(),
|
||||
'last_name' => $cust->getLastName(),
|
||||
'mobile_number' => $cust->getPhoneMobile(),
|
||||
];
|
||||
|
||||
// rider
|
||||
$rider_data = $other_data['rider_data'];
|
||||
if (empty($rider_data))
|
||||
$data['rider'] = null;
|
||||
else
|
||||
{
|
||||
// default image url
|
||||
$url_prefix = $req->getSchemeAndHttpHost();
|
||||
$image_url = $url_prefix . '/assets/images/user.gif';
|
||||
if ($rider_data['image_file'] != null)
|
||||
$image_url = $url_prefix . '/uploads/' . $rider_data['image_file'];
|
||||
|
||||
$coord = $rt->getRiderLocation($rider_data['id']);
|
||||
|
||||
$data['rider'] = [
|
||||
'id' => $rider_data['id'],
|
||||
'name' => $rider_data['name'],
|
||||
'plate_num' => $rider_data['plate_num'],
|
||||
'contact_num' => $rider_data['contact_num'],
|
||||
'image_url' => $image_url,
|
||||
'location' => [
|
||||
'long' => $coord->getLongitude(),
|
||||
'lat' => $coord->getLatitude()
|
||||
]
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
// invoice items
|
||||
$items = [];
|
||||
$invoice_items = $other_data['invoice_items'];
|
||||
foreach ($invoice_items as $item)
|
||||
{
|
||||
$items[] = [
|
||||
'id' => $item['id'],
|
||||
'title' => $item['title'],
|
||||
'qty' => $item['qty'],
|
||||
'price' => $item['price'],
|
||||
];
|
||||
}
|
||||
$data['items'] = $items;
|
||||
|
||||
// dates depending on status
|
||||
// need to convert the date_fulfill and date_cancel to DateTime then string with the correct format
|
||||
$str_date_fulfill = '';
|
||||
$str_date_cancel = '';
|
||||
if ($jo_data['date_fulfill'] != null)
|
||||
{
|
||||
$date_fulfill = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_fulfill']);
|
||||
$str_date_fulfill = $date_fulfill->format('M d, Y');
|
||||
}
|
||||
if ($jo_data['date_cancel'] != null)
|
||||
{
|
||||
$date_cancel = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_cancel']);
|
||||
$str_date_cancel = $date_cancel->format('M d, Y');
|
||||
}
|
||||
|
||||
switch ($status)
|
||||
{
|
||||
case JOStatus::FULFILLED:
|
||||
$data['date_fulfilled'] = $str_date_fulfill;
|
||||
break;
|
||||
case JOStatus::CANCELLED:
|
||||
if (empty($str_date_cancel))
|
||||
{
|
||||
$date_cancel = new DateTime();
|
||||
$data['date_cancelled'] = $date_cancel->format('M d, Y');
|
||||
}
|
||||
else
|
||||
$data['date_cancelled'] = $str_date_cancel;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function findJobOrder($id, EntityManagerInterface $em)
|
||||
{
|
||||
$found_jo = [];
|
||||
|
||||
$conn = $em->getConnection();
|
||||
|
||||
$jo_sql = 'SELECT jo.date_create, jo.service_type, ST_x(jo.coordinates), ST_y(jo.coordinates),
|
||||
jo.delivery_address, jo.delivery_instructions, jo.status, jo.rider_id, jo.customer_id, jo.cvehicle_id,
|
||||
jo.date_fulfill, jo.date_cancel
|
||||
FROM job_order jo WHERE jo.id = :id';
|
||||
$stmt = $conn->prepare($jo_sql);
|
||||
$stmt->execute(['id' => $id]);
|
||||
|
||||
$jo_results = $stmt->fetch();
|
||||
|
||||
if (!empty($jo_results))
|
||||
{
|
||||
$found_jo = [
|
||||
'id' => $id,
|
||||
'date_create' => $jo_results['date_create'],
|
||||
'date_fulfill' => $jo_results['date_fulfill'],
|
||||
'date_cancel' => $jo_results['date_cancel'],
|
||||
'service_type' => $jo_results['service_type'],
|
||||
'longitude' => $jo_results['ST_x(jo.coordinates)'],
|
||||
'latitude' => $jo_results['ST_y(jo.coordinates)'],
|
||||
'delivery_address' => $jo_results['delivery_address'],
|
||||
'delivery_instructions' => $jo_results['delivery_instructions'],
|
||||
'status' => $jo_results['status'],
|
||||
'rider_id' => $jo_results['rider_id'],
|
||||
'customer_id' => $jo_results['customer_id'],
|
||||
'cv_id' => $jo_results['cvehicle_id'],
|
||||
];
|
||||
}
|
||||
|
||||
return $found_jo;
|
||||
}
|
||||
|
||||
protected function findOtherData($jo_info, $id, EntityManagerInterface $em)
|
||||
{
|
||||
$other_data = [];
|
||||
|
||||
// need to find invoice items via invoice. so find invoice using id aka jo_id
|
||||
$ii_data = $this->findInvoiceItems($id, $em);
|
||||
|
||||
// need to find rider if any
|
||||
$rider_id = $jo_info['rider_id'];
|
||||
$r_data = [];
|
||||
if ($rider_id != null)
|
||||
{
|
||||
$r_data = $this->findRider($rider_id, $em);
|
||||
}
|
||||
|
||||
// need to find customer vehicle using cvehicle_id from jo_info
|
||||
$cv_id = $jo_info['cv_id'];
|
||||
$cv_data = $this->findCustomerVehicleById($cv_id, $em);
|
||||
|
||||
$other_data = [
|
||||
'invoice_items' => $ii_data,
|
||||
'rider_data' => $r_data,
|
||||
'cv_data' => $cv_data,
|
||||
];
|
||||
|
||||
return $other_data;
|
||||
}
|
||||
|
||||
protected function findInvoiceItems($id, EntityManagerInterface $em)
|
||||
{
|
||||
$invoice_items = [];
|
||||
|
||||
$conn = $em->getConnection();
|
||||
|
||||
// need to find invoice items via invoice. so find invoice using id aka jo_id
|
||||
$inv_sql = 'SELECT i.id FROM invoice i WHERE i.job_order_id = :jo_id';
|
||||
$stmt = $conn->prepare($inv_sql);
|
||||
$stmt->execute(array('jo_id' => $id));
|
||||
|
||||
$inv_results = $stmt->fetch();
|
||||
|
||||
if (empty($inv_results))
|
||||
return $invoice_items;
|
||||
|
||||
$invoice_id = $inv_results['id'];
|
||||
|
||||
// find invoice items using invoice id
|
||||
$ii_sql = 'SELECT ii.id, ii.qty, ii.title, ii.price from invoice_item ii
|
||||
WHERE ii.invoice_id = :invoice_id';
|
||||
$stmt = $conn->prepare($ii_sql);
|
||||
$stmt->execute(array('invoice_id' => $invoice_id));
|
||||
|
||||
$ii_results = $stmt->fetchAll();
|
||||
|
||||
foreach ($ii_results as $ii_result)
|
||||
{
|
||||
$invoice_items[] = [
|
||||
'id' => $ii_result['id'],
|
||||
'qty' => $ii_result['qty'],
|
||||
'title' => $ii_result['title'],
|
||||
'price' => $ii_result['price'],
|
||||
];
|
||||
}
|
||||
|
||||
return $invoice_items;
|
||||
}
|
||||
|
||||
protected function findRider($id, EntityManagerInterface $em)
|
||||
{
|
||||
$rider_data = [];
|
||||
|
||||
$conn = $em->getConnection();
|
||||
|
||||
$rider_sql = 'SELECT r.first_name, r.last_name, r.contact_num, r.image_file, r.plate_number
|
||||
FROM rider r WHERE r.id = :id';
|
||||
$stmt = $conn->prepare($rider_sql);
|
||||
$stmt->execute(['id' => $id]);
|
||||
|
||||
$rider_result = $stmt->fetch();
|
||||
|
||||
if (!empty($rider_result))
|
||||
{
|
||||
$rider_data = [
|
||||
'id' => $id,
|
||||
'name' => $rider_result['first_name'] . ' ' . $rider_result['last_name'],
|
||||
'plate_num' => $rider_result['plate_number'],
|
||||
'contact_num' => $rider_result['contact_num'],
|
||||
'image_file' => $rider_result['image_file'],
|
||||
];
|
||||
}
|
||||
|
||||
return $rider_data;
|
||||
}
|
||||
|
||||
protected function findCustomerVehicleById($id, EntityManagerInterface $em)
|
||||
{
|
||||
$cv_data = [];
|
||||
|
||||
$conn = $em->getConnection();
|
||||
|
||||
$cv_sql = 'SELECT cv.plate_number FROM customer_vehicle cv WHERE cv.id = :id';
|
||||
$stmt = $conn->prepare($cv_sql);
|
||||
$stmt->execute(['id' => $id]);
|
||||
|
||||
$cv_result = $stmt->fetch();
|
||||
|
||||
if (!empty($cv_result))
|
||||
{
|
||||
$cv_data = [
|
||||
'id' => $id,
|
||||
'plate_number' => $cv_result['plate_number'],
|
||||
];
|
||||
}
|
||||
|
||||
return $cv_data;
|
||||
}
|
||||
|
||||
protected function normalizeString($string)
|
||||
{
|
||||
return trim(strtolower($string));
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use App\Entity\CustomerVehicle;
|
|||
use App\Service\WarrantyAPILogger;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
use App\Ramcar\WarrantyStatus;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
|
|
@ -38,7 +39,7 @@ class WarrantyHandler
|
|||
$bmodel_name = '';
|
||||
$bsize_name ='';
|
||||
$sap_batt_id = '';
|
||||
$w_serial = null;
|
||||
$w_serial = 'NULL';
|
||||
|
||||
foreach ($batt_list as $battery)
|
||||
{
|
||||
|
|
@ -80,49 +81,31 @@ class WarrantyHandler
|
|||
|
||||
// compute expiry date
|
||||
$date_expire = null;
|
||||
$str_date_expire = 'NULL';
|
||||
if ((!empty($warranty_class)) &&
|
||||
(count($batt_list) != 0))
|
||||
{
|
||||
$period = $this->getWarrantyPeriod($batt_list, $warranty_class);
|
||||
$date_expire = $this->computeDateExpire($date_purchase, $period);
|
||||
$str_date_expire = $date_expire->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
// set and save values
|
||||
if (trim($serial) != '')
|
||||
$w_serial = $serial;
|
||||
|
||||
$str_date_purchase = $date_purchase->format('Y-m-d H:i:s');
|
||||
$date_create = new DateTime();
|
||||
$str_date_create = $date_create->format('Y-m-d H:i:s');
|
||||
|
||||
// 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();
|
||||
$sql_values = '(' . $bmodel_id . ',' . $bsize_id . ',\'' . $sap_batt_id . '\',\'' . $w_serial . '\',\'' . $warranty_class . '\',\''
|
||||
. $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $str_date_create . '\',\'' . $str_date_purchase
|
||||
. '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $cust_vehicle->getID() . ',' . $customer->getID() . ',\'' . $source . '\')';
|
||||
$w_sql = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,serial,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";
|
||||
$conn = $this->em->getConnection();
|
||||
$w_stmt = $conn->prepare($w_sql);
|
||||
$w_stmt->execute();
|
||||
|
||||
// log warranty creation
|
||||
$action = 'create';
|
||||
|
|
|
|||
Loading…
Reference in a new issue