Merge branch '304-create-warranty-when-job-order-is-fulfilled' into 'master'

Resolve "Create warranty when job order is fulfilled"

Closes #304

See merge request jankstudio/resq!341
This commit is contained in:
Kendrick Chan 2020-01-23 03:31:30 +00:00
commit cf4c7b2f28
3 changed files with 135 additions and 3 deletions

View file

@ -32,6 +32,7 @@ use App\Service\MapTools;
use App\Service\HubCounter;
use App\Service\MQTTClient;
use App\Service\APNSClient;
use App\Service\WarrantyHandler;
use Doctrine\ORM\Query;
use Doctrine\DBAL\Connection;
@ -1404,7 +1405,8 @@ class JobOrderController extends Controller
->setWarrantyExpiration($warr_date);
}
public function fulfillmentSubmit(Request $req, ValidatorInterface $validator, MQTTClient $mclient, $id)
public function fulfillmentSubmit(Request $req, ValidatorInterface $validator,
MQTTClient $mclient, $id, WarrantyHandler $wh)
{
$this->denyAccessUnlessGranted('jo_fulfill.list', null, 'No access.');
@ -1480,6 +1482,42 @@ class JobOrderController extends Controller
// save to customer vehicle battery record
$this->updateVehicleBattery($obj);
// create warranty
if ($obj->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW)
{
$serial = null;
$warranty_class = $obj->getWarrantyClass();
$first_name = $obj->getCustomer()->getFirstName();
$last_name = $obj->getCustomer()->getLastName();
$mobile_number = $obj->getCustomer()->getPhoneMobile();
// check if date fulfilled is null
if ($obj->getDateFulfill() == null)
$date_purchase = $obj->getDateCreate();
else
$date_purchase = $obj->getDateFulfill();
$plate_number = $wh->cleanPlateNumber($obj->getCustomerVehicle()->getPlateNumber());
$batt_list = array();
$invoice = $obj->getInvoice();
if (!empty($invoice))
{
// get battery
$invoice_items = $invoice->getItems();
foreach ($invoice_items as $item)
{
$battery = $item->getBattery();
if ($battery != null)
{
$batt_list[] = $item->getBattery();
}
}
}
$wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class);
}
// validated! save the entity
$em->flush();

View file

@ -27,6 +27,7 @@ use App\Ramcar\JOEventType;
use App\Service\InvoiceCreator;
use App\Service\MQTTClient;
use App\Service\WarrantyHandler;
use App\Entity\RiderSession;
use App\Entity\Customer;
@ -590,7 +591,7 @@ class RAPIController extends Controller
return $res->getReturnResponse();
}
public function payment(Request $req, MQTTClient $mclient)
public function payment(Request $req, MQTTClient $mclient, WarrantyHandler $wh)
{
$em = $this->getDoctrine()->getManager();
$required_params = ['jo_id'];
@ -618,6 +619,46 @@ class RAPIController extends Controller
// TODO: tag rider as unavailable
// save to customer vehicle battery record
// TODO: this has to move to JOHandler
$this->updateVehicleBattery($obj);
// create warranty
if ($obj->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW)
{
$serial = null;
$warranty_class = $obj->getWarrantyClass();
$first_name = $obj->getCustomer()->getFirstName();
$last_name = $obj->getCustomer()->getLastName();
$mobile_number = $obj->getCustomer()->getPhoneMobile();
// check if date fulfilled is null
if ($obj->getDateFulfill() == null)
$date_purchase = $obj->getDateCreate();
else
$date_purchase = $obj->getDateFulfill();
$plate_number = $wh->cleanPlateNumber($obj->getCustomerVehicle()->getPlateNumber());
$batt_list = array();
$invoice = $obj->getInvoice();
if (!empty($invoice))
{
// get battery
$invoice_items = $invoice->getItems();
foreach ($invoice_items as $item)
{
$battery = $item->getBattery();
if ($battery != null)
{
$batt_list[] = $item->getBattery();
}
}
}
$wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class);
}
$em->flush();
// send mqtt event (fulfilled)
@ -868,4 +909,56 @@ class RAPIController extends Controller
return $res->getReturnResponse();
}
protected function updateVehicleBattery(JobOrder $jo)
{
// check if new battery
if ($jo->getServiceType() != ServiceType::BATTERY_REPLACEMENT_NEW)
return;
// customer vehicle
$cv = $jo->getCustomerVehicle();
if ($cv == null)
return;
// invoice
$invoice = $jo->getInvoice();
if ($invoice == null)
return;
// invoice items
$items = $invoice->getItems();
if (count($items) <= 0)
return;
// get first battery from invoice
$battery = null;
foreach ($items as $item)
{
$battery = $item->getBattery();
if ($battery != null)
break;
}
// no battery in order
if ($battery == null)
return;
// warranty expiration
$warr = $jo->getWarrantyClass();
if ($warr == WarrantyClass::WTY_PRIVATE)
$warr_months = $battery->getWarrantyPrivate();
else if ($warr == WarrantyClass::WTY_COMMERCIAL)
$warr_months = $battery->getWarrantyCommercial();
else if ($warr == WarrantyClass::WTY_TNV)
$warr_months = 12;
$warr_date = new DateTime();
$warr_date->add(new DateInterval('P' . $warr_months . 'M'));
// update customer vehicle battery
$cv->setCurrentBattery($battery)
->setHasMotoliteBattery(true)
->setWarrantyExpiration($warr_date);
}
}

View file

@ -78,7 +78,8 @@ class WarrantyHandler
->setFirstName($first_name)
->setLastName($last_name)
->setMobileNumber($mobile_number)
->setDatePurchase($date_purchase);
->setDatePurchase($date_purchase)
->setWarrantyClass($warranty_class);
$this->em->persist($warranty);
$this->em->flush();