94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\TAPI;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Catalyst\ApiBundle\Controller\ApiController;
|
|
use Catalyst\ApiBundle\Component\Response as APIResponse;
|
|
use App\Ramcar\APIResult;
|
|
|
|
use App\Entity\Vehicle;
|
|
|
|
use Catalyst\AuthBundle\Service\ACLGenerator as ACLGenerator;
|
|
|
|
class BatteryController extends ApiController
|
|
{
|
|
protected $acl_gen;
|
|
|
|
public function __construct(ACLGenerator $acl_gen)
|
|
{
|
|
$this->acl_gen = $acl_gen;
|
|
}
|
|
|
|
public function getCompatibleBatteries(Request $req, $vid, EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('tapi_battery_compatible.list', null, 'No access.');
|
|
|
|
// check required parameters and api key
|
|
$required_params = [];
|
|
$msg = $this->checkRequiredParameters($req, $required_params);
|
|
if ($msg)
|
|
return new APIResponse(false, $msg);
|
|
|
|
// get vehicle
|
|
$vehicle = $em->getRepository(Vehicle::class)->find($vid);
|
|
if ($vehicle == null)
|
|
{
|
|
$message = 'Invalid vehicle id.';
|
|
return new APIResponse(false, $message);
|
|
}
|
|
|
|
// batteries
|
|
$batt_list = [];
|
|
// $batts = $vehicle->getBatteries();
|
|
$batts = $vehicle->getActiveBatteries();
|
|
foreach ($batts as $batt)
|
|
{
|
|
// TODO: Add warranty_tnv to battery information
|
|
$batt_list[] = [
|
|
'id' => $batt->getID(),
|
|
'mfg_id' => $batt->getManufacturer()->getID(),
|
|
'mfg_name' => $batt->getManufacturer()->getName(),
|
|
'model_id' => $batt->getModel()->getID(),
|
|
'model_name' => $batt->getModel()->getName(),
|
|
'size_id' => $batt->getSize()->getID(),
|
|
'size_name' => $batt->getSize()->getName(),
|
|
'price' => $batt->getSellingPrice(),
|
|
'wty_private' => $batt->getWarrantyPrivate(),
|
|
'wty_commercial' => $batt->getWarrantyCommercial(),
|
|
'image_url' => $this->getBatteryImageURL($req, $batt),
|
|
];
|
|
}
|
|
|
|
// data
|
|
$data = [
|
|
'vehicle' => [
|
|
'id' => $vehicle->getID(),
|
|
'mfg_id' => $vehicle->getManufacturer()->getID(),
|
|
'mfg_name' => $vehicle->getManufacturer()->getName(),
|
|
'make' => $vehicle->getMake(),
|
|
'model_year_from' => $vehicle->getModelYearFrom(),
|
|
'model_year_to' => $vehicle->getModelYearTo(),
|
|
],
|
|
'batteries' => $batt_list,
|
|
];
|
|
|
|
$message = 'Compatible batteries found.';
|
|
return new APIResponse(true, $message, $data);
|
|
}
|
|
|
|
// TODO: might have to put this in a common service since JobOrderController also calls this
|
|
protected function getBatteryImageURL($req, $batt)
|
|
{
|
|
// TODO: workaround for now, we get static image of battery based on model name
|
|
$filename = trim(strtolower($batt->getModel()->getName())) . '_mobile.jpg';
|
|
$file_path = $req->getSchemeAndHttpHost() . $this->generateUrl('static_battery_image') . '/' . $filename;
|
|
|
|
return $file_path;
|
|
}
|
|
}
|