From 4d89e7420f142cdd3323bd3dfd8d570506848067 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 20 Feb 2024 02:09:57 -0500 Subject: [PATCH] Add checking for location and price tier when getting list of compatible batteries. #780 --- config/routes/tapi.yaml | 2 +- .../CustomerAppAPI/VehicleController.php | 41 +++++++++++++++-- src/Controller/TAPI/BatteryController.php | 44 +++++++++++++++++-- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/config/routes/tapi.yaml b/config/routes/tapi.yaml index afa2f084..5bf1a503 100644 --- a/config/routes/tapi.yaml +++ b/config/routes/tapi.yaml @@ -51,7 +51,7 @@ tapi_vehicle_make_list: tapi_battery_list: path: /tapi/vehicles/{vid}/compatible_batteries controller: App\Controller\TAPI\BatteryController::getCompatibleBatteries - methods: [GET] + methods: [POST] # promos tapi_promo_list: diff --git a/src/Controller/CustomerAppAPI/VehicleController.php b/src/Controller/CustomerAppAPI/VehicleController.php index ba76d7a4..c8fd15e9 100644 --- a/src/Controller/CustomerAppAPI/VehicleController.php +++ b/src/Controller/CustomerAppAPI/VehicleController.php @@ -4,16 +4,19 @@ 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\Entity\CustomerVehicle; use App\Entity\JobOrder; use App\Entity\VehicleManufacturer; use App\Entity\Vehicle; +use App\Entity\ItemType; use App\Ramcar\JOStatus; use App\Ramcar\ServiceType; use App\Ramcar\TradeInType; use App\Ramcar\InsuranceApplicationStatus; use App\Service\PayMongoConnector; +use App\Service\PriceTierManager; use DateTime; class VehicleController extends ApiController @@ -237,7 +240,7 @@ class VehicleController extends ApiController ]); } - public function getCompatibleBatteries(Request $req, $vid) + public function getCompatibleBatteries(Request $req, $vid, PriceTierManager $pt_manager) { // validate params $validity = $this->validateRequest($req); @@ -252,11 +255,43 @@ class VehicleController extends ApiController return new ApiResponse(false, 'Invalid vehicle.'); } + // get location from request + $lng = $req->query->get('longitude', ''); + $lat = $req->query->get('latitude', ''); + + $batts = $vehicle->getActiveBatteries(); + $pt_id = 0; + if ((!(empty($lng))) && (!(empty($lat)))) + { + // get the price tier + $coordinates = new Point($lng, $lat); + + $pt_id = $pt_manager->getPriceTier($coordinates); + } + // batteries $batt_list = []; - $batts = $vehicle->getActiveBatteries(); foreach ($batts as $batt) { // TODO: Add warranty_tnv to battery information + // check if customer location is in a price tier location + if ($pt_id == 0) + $price = $batt->getSellingPrice(); + else + { + // get item type for battery + $item_type = $this->em->getRepository(ItemType::class)->findOneBy(['code' => 'battery']); + if ($item_type == null) + $price = $batt->getSellingPrice(); + else + { + $item_type_id = $item_type->getID(); + $batt_id = $batt->getID(); + + // find the item price given price tier id and battery id + $price = $pt_manager->getItemPrice($pt_id, $item_type_id, $batt_id); + } + } + $batt_list[] = [ 'id' => $batt->getID(), 'mfg_id' => $batt->getManufacturer()->getID(), @@ -265,7 +300,7 @@ class VehicleController extends ApiController 'model_name' => $batt->getModel()->getName(), 'size_id' => $batt->getSize()->getID(), 'size_name' => $batt->getSize()->getName(), - 'price' => $batt->getSellingPrice(), + 'price' => $price, 'wty_private' => $batt->getWarrantyPrivate(), 'wty_commercial' => $batt->getWarrantyCommercial(), 'image_url' => $this->getBatteryImageURL($req, $batt), diff --git a/src/Controller/TAPI/BatteryController.php b/src/Controller/TAPI/BatteryController.php index 0705a3ab..0d38dd5f 100644 --- a/src/Controller/TAPI/BatteryController.php +++ b/src/Controller/TAPI/BatteryController.php @@ -13,6 +13,11 @@ use Catalyst\ApiBundle\Component\Response as APIResponse; use App\Ramcar\APIResult; use App\Entity\Vehicle; +use App\Entity\ItemType; + +use App\Service\PriceTierManager; + +use CrEOF\Spatial\PHP\Types\Geometry\Point; use Catalyst\AuthBundle\Service\ACLGenerator as ACLGenerator; @@ -25,7 +30,7 @@ class BatteryController extends ApiController $this->acl_gen = $acl_gen; } - public function getCompatibleBatteries(Request $req, $vid, EntityManagerInterface $em) + public function getCompatibleBatteries(Request $req, $vid, EntityManagerInterface $em, PriceTierManager $pt_manager) { $this->denyAccessUnlessGranted('tapi_battery_compatible.list', null, 'No access.'); @@ -43,13 +48,44 @@ class BatteryController extends ApiController return new APIResponse(false, $message); } + // get location from request + $lng = $req->request->get('longitude', ''); + $lat = $req->request->get('latitude', ''); + + $batts = $vehicle->getActiveBatteries(); + $pt_id = 0; + if ((!(empty($lng))) && (!(empty($lat)))) + { + // get the price tier + $coordinates = new Point($lng, $lat); + + $pt_id = $pt_manager->getPriceTier($coordinates); + } + // batteries $batt_list = []; - // $batts = $vehicle->getBatteries(); - $batts = $vehicle->getActiveBatteries(); foreach ($batts as $batt) { // TODO: Add warranty_tnv to battery information + // check if customer location is in a price tier location + if ($pt_id == 0) + $price = $batt->getSellingPrice(); + else + { + // get item type for battery + $item_type = $em->getRepository(ItemType::class)->findOneBy(['code' => 'battery']); + if ($item_type == null) + $price = $batt->getSellingPrice(); + else + { + $item_type_id = $item_type->getID(); + $batt_id = $batt->getID(); + + // find the item price given price tier id and battery id + $price = $pt_manager->getItemPrice($pt_id, $item_type_id, $batt_id); + } + } + $batt_list[] = [ 'id' => $batt->getID(), 'mfg_id' => $batt->getManufacturer()->getID(), @@ -58,7 +94,7 @@ class BatteryController extends ApiController 'model_name' => $batt->getModel()->getName(), 'size_id' => $batt->getSize()->getID(), 'size_name' => $batt->getSize()->getName(), - 'price' => $batt->getSellingPrice(), + 'price' => $price, 'wty_private' => $batt->getWarrantyPrivate(), 'wty_commercial' => $batt->getWarrantyCommercial(), 'image_url' => $this->getBatteryImageURL($req, $batt),