Add API calls for estimate and job order request
This commit is contained in:
parent
9d04ff7879
commit
ad8100fb26
4 changed files with 150 additions and 4 deletions
|
|
@ -10,9 +10,13 @@ use Symfony\Component\HttpFoundation\Response;
|
|||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||
|
||||
use App\Ramcar\APIResult;
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\InvoiceCriteria;
|
||||
use App\Ramcar\ServiceType;
|
||||
use App\Ramcar\WarrantyClass;
|
||||
|
||||
use App\Service\InvoiceCreator;
|
||||
|
||||
|
|
@ -666,15 +670,155 @@ class APIController extends Controller
|
|||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function requestJobOrder(Request $req)
|
||||
public function requestJobOrder(Request $req, InvoiceCreator $ic)
|
||||
{
|
||||
// check required parameters and api key
|
||||
$required_params = [];
|
||||
$required_params = [
|
||||
'service_type',
|
||||
'cv_id',
|
||||
'batt_id',
|
||||
'trade_in',
|
||||
'long',
|
||||
'lat',
|
||||
'warranty',
|
||||
];
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
$jo = new JobOrder();
|
||||
$jo->setSource('mobile')
|
||||
->setStatus(JOStatus::PENDING)
|
||||
->setDeliveryInstructions('')
|
||||
->setAgentNotes('')
|
||||
->setDeliveryAddress('Set by mobile application');
|
||||
|
||||
// customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No customer information found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
$jo->setCustomer($cust);
|
||||
|
||||
// validate service type
|
||||
$stype = $req->request->get('service_type');
|
||||
if (!ServiceType::validate($stype))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid service type');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
$jo->setServiceType($stype);
|
||||
|
||||
// validate warranty
|
||||
$warr = $req->request->get('warranty');
|
||||
if (!WarrantyClass::validate($warr))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid warranty class');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
$jo->setWarrantyClass($warr);
|
||||
|
||||
// longitude and latitude
|
||||
$long = $req->request->get('long');
|
||||
$lat = $req->request->get('lat');
|
||||
$point = new Point($long, $lat);
|
||||
$jo->setCoordinates($point);
|
||||
|
||||
// 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();
|
||||
}
|
||||
$jo->setCustomerVehicle($cv);
|
||||
|
||||
// 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);
|
||||
$jo->setInvoice($invoice);
|
||||
|
||||
$em->persist($jo);
|
||||
$em->persist($invoice);
|
||||
$em->flush();
|
||||
|
||||
// make invoice json data
|
||||
$invoice_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(),
|
||||
];
|
||||
}
|
||||
$invoice_data['items'] = $items_data;
|
||||
|
||||
// make job order data
|
||||
$data = [
|
||||
'jo_id' => $jo->getID(),
|
||||
'invoice' => $invoice_data
|
||||
];
|
||||
|
||||
|
||||
// set data
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace App\Entity;
|
|||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use DateTime;
|
||||
use App\Ramcar\InvoiceStatus;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
|
|
@ -105,6 +106,7 @@ class Invoice
|
|||
{
|
||||
$this->date_create = new DateTime();
|
||||
$this->items = new ArrayCollection();
|
||||
$this->status = InvoiceStatus::DRAFT;
|
||||
}
|
||||
|
||||
public function getID()
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ class JobOrder
|
|||
// user that created the job order
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="User", inversedBy="job_orders_created")
|
||||
* @ORM\JoinColumn(name="create_user_id", referencedColumnName="id")
|
||||
* @ORM\JoinColumn(name="create_user_id", referencedColumnName="id", nullable=true)
|
||||
*/
|
||||
protected $created_by;
|
||||
|
||||
|
|
|
|||
|
|
@ -274,7 +274,7 @@
|
|||
</select>
|
||||
<div class="form-control-feedback hide" data-field="invoice_promo"></div>
|
||||
{% else %}
|
||||
<input type="text" id="invoice-promo" class="form-control m-input" value="{{ obj.getInvoice ? obj.getInvoice.getPromo.getName : 'None' }}" disabled>
|
||||
<input type="text" id="invoice-promo" class="form-control m-input" value="{{ obj.getInvoice.getPromo.getName|default('None') }}" disabled>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="col-lg-3">
|
||||
|
|
|
|||
Loading…
Reference in a new issue