204 lines
6.1 KiB
PHP
204 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CustomerAppAPI;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
|
|
|
use App\Service\InvoiceGeneratorInterface;
|
|
use App\Service\PriceTierManager;
|
|
use App\Ramcar\InvoiceCriteria;
|
|
use App\Ramcar\TradeInType;
|
|
use App\Ramcar\TransactionOrigin;
|
|
use App\Entity\CustomerVehicle;
|
|
use App\Entity\Promo;
|
|
use App\Entity\Battery;
|
|
use App\Entity\BatterySize;
|
|
use App\Entity\Customer;
|
|
use App\Entity\CustomerMetadata;
|
|
|
|
class InvoiceController extends ApiController
|
|
{
|
|
public function getEstimate(Request $req, InvoiceGeneratorInterface $ic, PriceTierManager $pt_manager)
|
|
{
|
|
// $this->debugRequest($req);
|
|
|
|
// validate params
|
|
$validity = $this->validateRequest($req, [
|
|
'service_type',
|
|
'cv_id',
|
|
// 'batt_id',
|
|
]);
|
|
|
|
if (!$validity['is_valid']) {
|
|
return new ApiResponse(false, $validity['error']);
|
|
}
|
|
|
|
// customer
|
|
$cust = $this->session->getCustomer();
|
|
if ($cust == null) {
|
|
return new ApiResponse(false, 'No customer information found.');
|
|
}
|
|
|
|
// get customer location from customer_metadata using customer id
|
|
$lng = $req->request->get('longitude');
|
|
$lat = $req->request->get('latitude');
|
|
|
|
if ((empty($lng)) || (empty($lat)))
|
|
{
|
|
// use customer metadata location as basis
|
|
$coordinates = $this->getCustomerMetadata($cust);
|
|
}
|
|
else
|
|
$coordinates = new Point($lng, $lat);
|
|
|
|
// 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_batt = $req->request->get('trade_in_batt');
|
|
$trade_in_type = $req->request->get('trade_in_type');
|
|
|
|
switch ($trade_in_type) {
|
|
case TradeInType::MOTOLITE:
|
|
case TradeInType::OTHER:
|
|
break;
|
|
|
|
default:
|
|
$trade_in_type = '';
|
|
break;
|
|
}
|
|
|
|
// add the actual battery item first
|
|
$icrit->addEntry($batt, null, 1);
|
|
|
|
// DEBUG
|
|
|
|
// if we have a trade in, add it as well
|
|
if (!empty($trade_in_type) && !empty($trade_in_batt)) {
|
|
$ti_batt_size_obj = $this->em->getRepository(BatterySize::class)->find($trade_in_batt);
|
|
if (!empty($ti_batt_size_obj)) {
|
|
$icrit->addTradeInEntry($ti_batt_size_obj, $trade_in_type, 1);
|
|
}
|
|
}
|
|
|
|
// set if taxable
|
|
$icrit->setIsTaxable();
|
|
|
|
// set JO source
|
|
$icrit->setSource(TransactionOrigin::MOBILE_APP);
|
|
|
|
// set price tier
|
|
$pt_id = 0;
|
|
if ($coordinates != null)
|
|
{
|
|
error_log('coordinates are not null');
|
|
$pt_id = $pt_manager->getPriceTier($coordinates);
|
|
}
|
|
else
|
|
error_log('null?');
|
|
|
|
$icrit->setPriceTier($pt_id);
|
|
|
|
// 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);
|
|
}
|
|
|
|
protected function getCustomerMetadata(Customer $cust)
|
|
{
|
|
$coordinates = null;
|
|
|
|
// check if customer already has existing metadata
|
|
$c_meta = $this->em->getRepository(CustomerMetadata::class)->findOneBy(['customer' => $cust]);
|
|
if ($c_meta != null)
|
|
{
|
|
$meta_data = $c_meta->getAllMetaInfo();
|
|
foreach ($meta_data as $m_info)
|
|
{
|
|
if ((isset($m_info['longitude'])) && (isset($m_info['latitude'])))
|
|
{
|
|
$lng = $m_info['longitude'];
|
|
$lat = $m_info['latitude'];
|
|
|
|
$coordinates = new Point($lng, $lat);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $coordinates;
|
|
}
|
|
}
|