Add API call to update invoice. #783
This commit is contained in:
parent
86744afde3
commit
c9057b9617
2 changed files with 184 additions and 0 deletions
|
|
@ -105,3 +105,8 @@ capi_rider_battery_info:
|
|||
path: /rider_api/battery/{serial}
|
||||
controller: App\Controller\CAPI\RiderAppController::getBatteryInfo
|
||||
methods: [GET]
|
||||
|
||||
capi_rider_update_jo:
|
||||
path: /rider_api/job_order/update
|
||||
controller: App\Controller\CAPI\RiderAppController::updateJobOrder
|
||||
methods: [POST]
|
||||
|
|
|
|||
|
|
@ -1174,6 +1174,44 @@ class RiderAppController extends ApiController
|
|||
return new APIResponse(true, 'Battery info found.', $data);
|
||||
}
|
||||
|
||||
public function updateJobOrder(Request $req, EntityManagerInterface $em, InvoiceGeneratorInterface $ic)
|
||||
{
|
||||
$items = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// get job order id
|
||||
if (!isset($items['jo_id']))
|
||||
return new APIResponse(false, 'Missing parameter(s): jo_id');
|
||||
|
||||
$jo_id = $items['jo_id'];
|
||||
if (empty($jo_id) || $jo_id == null)
|
||||
return new APIResponse(false, 'Missing parameter(s): jo_id');
|
||||
|
||||
// validate the trade in items first
|
||||
$ti_items = $items['trade_in_items'];
|
||||
$msg = $this->validateTradeInItems($em, $ti_items);
|
||||
if (!empty($msg))
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user
|
||||
$capi_user = $this->getUser();
|
||||
if ($capi_user == null)
|
||||
return new APIResponse(false, 'User not found.');
|
||||
|
||||
// get rider id from capi user metadata
|
||||
$rider = $this->getRiderFromCAPI($capi_user, $em);
|
||||
if ($rider == null)
|
||||
return new APIResponse(false, 'No rider found.');
|
||||
|
||||
// need to get the existing invoice items using jo id and invoice id
|
||||
$existing_ii = $this->getInvoiceItems($em, $jo_id);
|
||||
|
||||
// $this->generateUpdatedInvoice($em, $jo_id, $existing_ii, $trade_in_items);
|
||||
|
||||
$data = [];
|
||||
|
||||
return new APIResponse(true, 'Job order updated.', $data);
|
||||
}
|
||||
|
||||
public function changeService(Request $req, EntityManagerInterface $em, InvoiceGeneratorInterface $ic)
|
||||
{
|
||||
// $this->debugRequest($req);
|
||||
|
|
@ -1316,6 +1354,147 @@ class RiderAppController extends ApiController
|
|||
return new APIResponse(true, 'Job order service changed.', $data);
|
||||
}
|
||||
|
||||
protected function generateUpdatedInvoice(EntityManagerInterface $em, $jo_id, $existing_ii, $trade_in_items)
|
||||
{
|
||||
// get the job order since we need info in the JO for the invoice criteria
|
||||
$jo = $em->getRepository(JobOrder::class)->find($jo_id);
|
||||
|
||||
// get the service type
|
||||
$stype = $jo->getServiceType();
|
||||
|
||||
// get the source
|
||||
$source = $jo->getSource();
|
||||
|
||||
// get the customer vehicle
|
||||
$cv = $jo->getCustomerVehicle();
|
||||
|
||||
// get the promo id from existing invoice item
|
||||
$promo_id = $existing_ii['promo_id'];
|
||||
if ($promo_id = null)
|
||||
$promo = null;
|
||||
else
|
||||
$promo = $em->getRepository(Promo::class)->find($promo_id);
|
||||
|
||||
// populate Invoice Criteria
|
||||
$icrit = new InvoiceCriteria();
|
||||
$icrit->setServiceType($stype)
|
||||
->setCustomerVehicle($cv)
|
||||
->setSource($source)
|
||||
->setIsTaxable();
|
||||
|
||||
// at this point, all information should be valid
|
||||
// assuming JO information is already valid since this
|
||||
// is in the system already
|
||||
// add promo if any to criteria
|
||||
$icrit->addPromo($promo);
|
||||
|
||||
// get the battery purchased from existing invoice items
|
||||
// add the batteries ordered to criteria
|
||||
$ii_items = $existing_ii['invoice_items'];
|
||||
foreach ($ii_items as $ii_item)
|
||||
{
|
||||
$batt_id = $ii_item['batt_id'];
|
||||
$qty = $ii_item['qty'];
|
||||
|
||||
$battery = $em->getRepository(Battery::class)->find($batt_id);
|
||||
|
||||
$icrit->addEntry($battery, null, $qty);
|
||||
}
|
||||
|
||||
// add the trade in items to the criteria
|
||||
foreach ($trade_in_items as $ti_item)
|
||||
{
|
||||
// TODO: need to discuss how to store since what we have is battery size
|
||||
// and we're supposed to store battery
|
||||
}
|
||||
|
||||
// call generateInvoice
|
||||
$invoice = $ic->generateInvoice($icrit);
|
||||
|
||||
// remove previous invoice
|
||||
$old_invoice = $jo->getInvoice();
|
||||
$em->remove($old_invoice);
|
||||
$em->flush();
|
||||
|
||||
// save new invoice
|
||||
$jo->setInvoice($invoice);
|
||||
$em->persist($invoice);
|
||||
|
||||
// log event?
|
||||
$event = new JOEvent();
|
||||
$event->setDateHappen(new DateTime())
|
||||
->setTypeID(JOEventType::RIDER_EDIT)
|
||||
->setJobOrder($jo)
|
||||
->setRider($jo->getRider());
|
||||
$em->persist($event);
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
protected function getInvoiceItems(EntityManagerInterface $em, $jo_id)
|
||||
{
|
||||
$conn = $em->getConnection();
|
||||
|
||||
// need to get the ordered battery id and quantity from invoice item
|
||||
// and the promo from invoice
|
||||
$query_sql = 'SELECT ii.battery_id AS battery_id, ii.qty AS qty, i.promo_id AS promo_id
|
||||
FROM invoice_item ii, invoice i
|
||||
WHERE ii.invoice_id = i.id
|
||||
AND i.job_order_id = :jo_id
|
||||
AND ii.battery_id IS NOT NULL';
|
||||
|
||||
$query_stmt = $conn->prepare($query_sql);
|
||||
$query_stmt->bindValue('jo_id', $jo_id);
|
||||
|
||||
$results = $query_stmt->executeQuery();
|
||||
|
||||
$promo_id = null;
|
||||
$invoice_items = [];
|
||||
while ($row = $results->fetchAssociative())
|
||||
{
|
||||
$promo_id = $row['promo_id'];
|
||||
$invoice_items[] = [
|
||||
'batt_id' => $row['battery_id'],
|
||||
'qty' => $row['qty'],
|
||||
'trade_in' => ''
|
||||
];
|
||||
}
|
||||
|
||||
$data = [
|
||||
'promo_id' => $promo_id,
|
||||
'invoice_items' => $invoice_items
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function validateTradeInItems(EntityManagerInterface $em, $ti_items)
|
||||
{
|
||||
$msg = '';
|
||||
foreach ($ti_items as $ti_item)
|
||||
{
|
||||
$bs_id = $ti_item['battery_size_id'];
|
||||
$ti_type = $ti_item['trade_in_type'];
|
||||
|
||||
// validate the battery size id
|
||||
$batt_size = $em->getRepository(BatterySize::class)->find($bs_id);
|
||||
if ($batt_size == null)
|
||||
{
|
||||
$msg = 'Invalid battery size for trade in: ' . $bs_id;
|
||||
return $msg;
|
||||
}
|
||||
|
||||
// validate the trade in type
|
||||
if (!TradeInType::validate($ti_type))
|
||||
{
|
||||
$msg = 'Invalid trade in type: ' . $ti_type;
|
||||
return $msg;
|
||||
}
|
||||
}
|
||||
|
||||
return $msg;
|
||||
}
|
||||
|
||||
protected function getCAPIUser($id, EntityManagerInterface $em)
|
||||
{
|
||||
$capi_user = $em->getRepository(APIUser::class)->find($id);
|
||||
|
|
|
|||
Loading…
Reference in a new issue