Add API call for get estimate
This commit is contained in:
parent
7ea7e238cc
commit
9d04ff7879
2 changed files with 98 additions and 3 deletions
|
|
@ -12,6 +12,9 @@ use Symfony\Component\HttpFoundation\JsonResponse;
|
|||
|
||||
use App\Ramcar\APIResult;
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\InvoiceCriteria;
|
||||
|
||||
use App\Service\InvoiceCreator;
|
||||
|
||||
use App\Entity\MobileSession;
|
||||
use App\Entity\Customer;
|
||||
|
|
@ -19,6 +22,8 @@ use App\Entity\VehicleManufacturer;
|
|||
use App\Entity\Vehicle;
|
||||
use App\Entity\CustomerVehicle;
|
||||
use App\Entity\JobOrder;
|
||||
use App\Entity\Promo;
|
||||
use App\Entity\Battery;
|
||||
|
||||
use DateTime;
|
||||
|
||||
|
|
@ -673,15 +678,105 @@ class APIController extends Controller
|
|||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function getEstimate(Request $req)
|
||||
public function getEstimate(Request $req, InvoiceCreator $ic)
|
||||
{
|
||||
// check required parameters and api key
|
||||
$required_params = [];
|
||||
$required_params = [
|
||||
'cv_id',
|
||||
'batt_id',
|
||||
'trade_in',
|
||||
];
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
// customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No customer information found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// make invoice criteria
|
||||
$icrit = new InvoiceCriteria();
|
||||
|
||||
// check promo
|
||||
$promo_id = $req->request->get('promo_id');
|
||||
if (!empty($promo_id))
|
||||
{
|
||||
$promo = $em->getRepository(Promo::class)->find($promo_id);
|
||||
if ($promo == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid promo id');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// put in criteria
|
||||
$icrit->addPromo($promo);
|
||||
}
|
||||
|
||||
// check customer vehicle
|
||||
$cv = $em->getRepository(CustomerVehicle::class)->find($req->request->get('cv_id'));
|
||||
if ($cv == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid customer vehicle id');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// check if customer owns vehicle
|
||||
if ($cust->getID() != $cv->getCustomer()->getID())
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Customer does not own vehicle');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// check battery
|
||||
$batt = $em->getRepository(Battery::class)->find($req->request->get('batt_id'));
|
||||
if ($batt == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid battery id');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// put battery in criteria
|
||||
$icrit->addBattery($batt);
|
||||
|
||||
// TODO: check trade-in
|
||||
|
||||
// send to invoice generator
|
||||
$invoice = $ic->processCriteria($icrit);
|
||||
|
||||
// make invoice json data
|
||||
$data = [
|
||||
'total_price' => $invoice->getTotalPrice(),
|
||||
'vat_ex_price' => $invoice->getVATExclusivePrice(),
|
||||
'vat' => $invoice->getVAT(),
|
||||
'discount' => $invoice->getDiscount(),
|
||||
'trade_in' => $invoice->getTradeIn(),
|
||||
];
|
||||
$items = $invoice->getItems();
|
||||
$items_data = [];
|
||||
foreach ($items as $item)
|
||||
{
|
||||
$items_data[] = [
|
||||
'title' => $item->getTitle(),
|
||||
'qty' => $item->getQuantity(),
|
||||
'price' => $item->getPrice(),
|
||||
];
|
||||
}
|
||||
|
||||
$data['items'] = $items_data;
|
||||
|
||||
// set data
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ class Invoice
|
|||
// user that created the invoice
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="User", inversedBy="invoices")
|
||||
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
|
||||
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
|
||||
*/
|
||||
protected $created_by;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue