Add changeService, getPromos, getBatteries. #617
This commit is contained in:
parent
bbee3c4b96
commit
27c8cd1f2d
1 changed files with 239 additions and 1 deletions
|
|
@ -1008,14 +1008,246 @@ class RiderController extends APIController
|
|||
|
||||
public function getPromos(Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [];
|
||||
$missing = $this->checkMissingParameters($req, $required_params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$params = implode(', ', $missing);
|
||||
return new APIResponse(false, 'Missing parameter(s): ' . $params);
|
||||
}
|
||||
|
||||
// get capi user to link to rider api user
|
||||
$capi_user_id = $this->getUser()->getID();
|
||||
|
||||
// check if capi user already has a rider api user
|
||||
$rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]);
|
||||
|
||||
// are we logged in?
|
||||
if (!$rapi_session->hasRider())
|
||||
return new APIResponse(false, 'No logged in rider.');
|
||||
|
||||
$promos = $em->getRepository(Promo::class)->findAll();
|
||||
|
||||
$promo_data = [];
|
||||
foreach ($promos as $promo)
|
||||
{
|
||||
$promo_data[] = [
|
||||
'id' => $promo->getID(),
|
||||
'name' => $promo->getName(),
|
||||
'code' => $promo->getCode(),
|
||||
];
|
||||
}
|
||||
|
||||
$data = [
|
||||
'promos' => $promo_data,
|
||||
];
|
||||
|
||||
return new APIResponse(true, 'Promos found.', $data);
|
||||
}
|
||||
|
||||
public function getBatteries(Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [];
|
||||
$missing = $this->checkMissingParameters($req, $required_params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$params = implode(', ', $missing);
|
||||
return new APIResponse(false, 'Missing parameter(s): ' . $params);
|
||||
}
|
||||
|
||||
// get capi user to link to rider api user
|
||||
$capi_user_id = $this->getUser()->getID();
|
||||
|
||||
// check if capi user already has a rider api user
|
||||
$rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]);
|
||||
|
||||
// are we logged in?
|
||||
if (!$rapi_session->hasRider())
|
||||
return new APIResponse(false, 'No logged in rider.');
|
||||
|
||||
$batts = $em->getRepository(Battery::class)->findAll();
|
||||
$models = $em->getRepository(BatteryModel::class)->findAll();
|
||||
$sizes = $em->getRepository(BatterySize::class)->findAll();
|
||||
|
||||
$batt_data = [];
|
||||
foreach ($batts as $batt)
|
||||
{
|
||||
$batt_data[] = [
|
||||
'id' => $batt->getID(),
|
||||
'model_id' => $batt->getModel()->getID(),
|
||||
'size_id' => $batt->getSize()->getID(),
|
||||
'sell_price' => $batt->getSellingPrice(),
|
||||
];
|
||||
}
|
||||
|
||||
$model_data = [];
|
||||
foreach ($models as $model)
|
||||
{
|
||||
$model_data[] = [
|
||||
'id' => $model->getID(),
|
||||
'name' => $model->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
$size_data = [];
|
||||
foreach ($sizes as $size)
|
||||
{
|
||||
$size_data[] = [
|
||||
'id' => $size->getID(),
|
||||
'name' => $size->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
$data = [
|
||||
'batteries' => $batt_data,
|
||||
'models' => $model_data,
|
||||
'sizes' => $size_data,
|
||||
];
|
||||
|
||||
return new APIResponse(true, 'Batteries found.', $data);
|
||||
}
|
||||
|
||||
public function changeService(Request $req, EntityManagerInterface $em)
|
||||
public function changeService(Request $req, EntityManagerInterface $em, InvoiceGeneratorInterface $ic)
|
||||
{
|
||||
$this->debugRequest($req);
|
||||
|
||||
// allow rider to change service, promo, battery and trade-in options
|
||||
$required_params = ['jo_id', 'stype_id', 'promo_id'];
|
||||
|
||||
// get capi user to link to rider api user
|
||||
$capi_user_id = $this->getUser()->getID();
|
||||
|
||||
// check if capi user already has a rider api user
|
||||
$rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]);
|
||||
|
||||
// are we logged in?
|
||||
if (!$rapi_session->hasRider())
|
||||
return new APIResponse(false, 'No logged in rider.');
|
||||
|
||||
$rider = $rapi_session->getRider();
|
||||
|
||||
$msg = $this->checkJO($req, $required_params, $jo, $rider);
|
||||
if (!empty($msg))
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// check service type
|
||||
$stype_id = $req->request->get('stype_id');
|
||||
if (!ServiceType::validate($stype_id))
|
||||
{
|
||||
$data = [
|
||||
'error' => 'Invalid service type - ' . $stype_id
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
// check promo id
|
||||
$promo_id = $req->request->get('promo_id');
|
||||
// no promo
|
||||
if ($promo_id == 0)
|
||||
$promo = null;
|
||||
else
|
||||
{
|
||||
$promo = $em->getRepository(Promo::class)->find($promo_id);
|
||||
if ($promo == null)
|
||||
{
|
||||
$data = [
|
||||
'error' => 'Invalid promo id - ' . $promo_id
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// check or number
|
||||
$or_num = $req->request->get('or_num');
|
||||
if ($or_num != null)
|
||||
$jo->setORNum($or_num);
|
||||
|
||||
// coolant
|
||||
$flag_coolant = $req->request->get('flag_coolant', 'false');
|
||||
if ($flag_coolant == 'true')
|
||||
$jo->setHasCoolant(true);
|
||||
else
|
||||
$jo->setHasCoolant(false);
|
||||
|
||||
// has motolite battery
|
||||
$cv = $jo->getCustomerVehicle();
|
||||
$has_motolite = $req->request->get('has_motolite', 'false');
|
||||
if ($has_motolite == 'true')
|
||||
$cv->setHasMotoliteBattery(true);
|
||||
else
|
||||
$cv->setHasMotoliteBattery(false);
|
||||
$em->persist($cv);
|
||||
|
||||
// check battery id
|
||||
$batt_id = $req->request->get('batt_id', null);
|
||||
// no battery
|
||||
if ($batt_id == 0 || $batt_id == null)
|
||||
$battery = null;
|
||||
else
|
||||
{
|
||||
$battery = $em->getRepository(Battery::class)->find($batt_id);
|
||||
if ($battery == null)
|
||||
{
|
||||
$data = [
|
||||
'error' => 'Invalid battery id - ' . $batt_id
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// check trade in
|
||||
$trade_in = $req->request->get('trade_in');
|
||||
if (!TradeInType::validate($trade_in))
|
||||
$trade_in = null;
|
||||
|
||||
// check mode of payment
|
||||
$mode = $req->request->get('mode_of_payment');
|
||||
if (!ModeOfPayment::validate($mode))
|
||||
$mode = ModeOfPayment::CASH;
|
||||
$jo->setModeOfPayment($mode);
|
||||
|
||||
// generate new invoice
|
||||
$crit = new InvoiceCriteria();
|
||||
$crit->setServiceType($stype_id);
|
||||
$crit->setCustomerVehicle($cv);
|
||||
$crit->setHasCoolant($jo->hasCoolant());
|
||||
|
||||
if ($promo != null)
|
||||
$crit->addPromo($promo);
|
||||
|
||||
if ($battery != null)
|
||||
{
|
||||
$crit->addEntry($battery, $trade_in, 1);
|
||||
error_log('adding entry for battery - ' . $battery->getID());
|
||||
}
|
||||
|
||||
$invoice = $ic->generateInvoice($crit);
|
||||
|
||||
// remove previous invoice
|
||||
$old_invoice = $jo->getInvoice();
|
||||
$em->remove($old_invoice);
|
||||
$em->flush();
|
||||
|
||||
// save job order
|
||||
$jo->setServiceType($stype_id);
|
||||
|
||||
// save invoice
|
||||
$jo->setInvoice($invoice);
|
||||
$em->persist($invoice);
|
||||
|
||||
// add event log
|
||||
$event = new JOEvent();
|
||||
$event->setDateHappen(new DateTime())
|
||||
->setTypeID(JOEventType::RIDER_EDIT)
|
||||
->setJobOrder($jo)
|
||||
->setRider($rider);
|
||||
$em->persist($event);
|
||||
|
||||
$em->flush();
|
||||
// TODO: send mqtt event (?)
|
||||
|
||||
$data = [];
|
||||
return new APIResponse(true, 'Job order service changed.', $data);
|
||||
}
|
||||
|
||||
protected function checkMissingParameters(Request $req, $params = [])
|
||||
|
|
@ -1074,4 +1306,10 @@ class RiderController extends APIController
|
|||
return $msg;
|
||||
}
|
||||
|
||||
protected function debugRequest(Request $req)
|
||||
{
|
||||
$all = $req->request->all();
|
||||
error_log(print_r($all, true));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue