125 lines
3.7 KiB
PHP
125 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CustomerAppAPI;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
|
|
|
use App\Service\InvoiceGeneratorInterface;
|
|
use App\Ramcar\InvoiceCriteria;
|
|
use App\Ramcar\TradeInType;
|
|
|
|
class EstimateController extends ApiController
|
|
{
|
|
public function getEstimate(Request $req, InvoiceGeneratorInterface $ic)
|
|
{
|
|
// $this->debugRequest($req);
|
|
|
|
// validate params
|
|
$this->validateRequest($req, [
|
|
'service_type',
|
|
'cv_id',
|
|
// 'batt_id',
|
|
'trade_in',
|
|
]);
|
|
|
|
// customer
|
|
$cust = $this->session->getCustomer();
|
|
if ($cust == null) {
|
|
return new ApiResponse(false, 'No customer information found.');
|
|
}
|
|
|
|
// make invoice criteria
|
|
$icrit = new InvoiceCriteria();
|
|
$icrit->setServiceType($req->request->get('service_type'));
|
|
|
|
// check promo
|
|
$promo_id = $req->request->get('promo_id');
|
|
if (!empty($promo_id)) {
|
|
$promo = $this->em->getRepository(Promo::class)->find($promo_id);
|
|
if ($promo == null) {
|
|
return new ApiResponse(false, 'Invalid promo id.');
|
|
}
|
|
|
|
// put in criteria
|
|
$icrit->addPromo($promo);
|
|
}
|
|
|
|
// check customer vehicle
|
|
$cv = $this->em->getRepository(CustomerVehicle::class)->find($req->request->get('cv_id'));
|
|
if ($cv == null) {
|
|
return new ApiResponse(false, 'Invalid customer vehicle id.');
|
|
}
|
|
$icrit->setCustomerVehicle($cv);
|
|
|
|
// check if customer owns vehicle
|
|
if ($cust->getID() != $cv->getCustomer()->getID()) {
|
|
return new ApiResponse(false, 'Customer does not own vehicle.');
|
|
}
|
|
|
|
// check battery
|
|
$batt_id = $req->request->get('batt_id');
|
|
if ($batt_id != null) {
|
|
$batt = $this->em->getRepository(Battery::class)->find($batt_id);
|
|
if ($batt == null) {
|
|
return new ApiResponse(false, 'Invalid battery id.');
|
|
}
|
|
} else
|
|
$batt = null;
|
|
|
|
/*
|
|
// put battery in criteria
|
|
$icrit->addBattery($batt);
|
|
*/
|
|
|
|
// check trade-in
|
|
// only allow motolite, other, none
|
|
$trade_in = $req->request->get('trade_in');
|
|
switch ($trade_in) {
|
|
case TradeInType::MOTOLITE:
|
|
case TradeInType::OTHER:
|
|
break;
|
|
|
|
default:
|
|
$trade_in = '';
|
|
break;
|
|
}
|
|
|
|
$icrit->addEntry($batt, $trade_in, 1);
|
|
|
|
// send to invoice generator
|
|
$invoice = $ic->generateInvoice($icrit);
|
|
|
|
// make invoice json data
|
|
$data = [
|
|
'total_price' => (float) $invoice->getTotalPrice(),
|
|
'vat_ex_price' => (float) $invoice->getVATExclusivePrice(),
|
|
'vat' => (float) $invoice->getVAT(),
|
|
'discount' => (float) $invoice->getDiscount(),
|
|
'trade_in' => (float) $invoice->getTradeIn(),
|
|
];
|
|
$items = $invoice->getItems();
|
|
$items_data = [];
|
|
foreach ($items as $item) {
|
|
$my_data = [
|
|
'title' => $item->getTitle(),
|
|
'qty' => (int) $item->getQuantity() + 0,
|
|
'price' => (float) $item->getPrice() + 0.0,
|
|
];
|
|
|
|
$item_batt = $item->getBattery();
|
|
if ($item_batt != null) {
|
|
$my_data['image_url'] = $this->getBatteryImageURL($req, $item_batt);
|
|
}
|
|
|
|
$items_data[] = $my_data;
|
|
}
|
|
|
|
$data['items'] = $items_data;
|
|
|
|
// error_log(print_r($data, true));
|
|
|
|
// response
|
|
return new ApiResponse(true, '', $data);
|
|
}
|
|
}
|