From 765b3a99b906846bfac79c6fc1fb740bf77487aa Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 6 Feb 2023 10:39:30 +0000 Subject: [PATCH] Add getActiveBatteries function. #733 --- src/Controller/APIController.php | 35 +++++++++++------------ src/Controller/TAPI/BatteryController.php | 35 +++++++++++------------ src/Controller/VehicleController.php | 29 +++++++++---------- src/Entity/Vehicle.php | 10 +++++++ templates/vehicle/form.html.twig | 11 +------ 5 files changed, 56 insertions(+), 64 deletions(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 6d7849eb..05f50243 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -823,27 +823,24 @@ class APIController extends Controller implements LoggedController // batteries $batt_list = []; - $batts = $vehicle->getBatteries(); + // $batts = $vehicle->getBatteries(); + $batts = $vehicle->getActiveBatteries(); foreach ($batts as $batt) { - // check if battery is active - if ($batt->isActive()) - { - // 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), - ]; - } + // 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 diff --git a/src/Controller/TAPI/BatteryController.php b/src/Controller/TAPI/BatteryController.php index 5a2fca50..0874b792 100644 --- a/src/Controller/TAPI/BatteryController.php +++ b/src/Controller/TAPI/BatteryController.php @@ -45,27 +45,24 @@ class BatteryController extends APIController // batteries $batt_list = []; - $batts = $vehicle->getBatteries(); + // $batts = $vehicle->getBatteries(); + $batts = $vehicle->getActiveBatteries(); foreach ($batts as $batt) { - // check if battery is active - if ($batt->isActive()) - { - // 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), - ]; - } + // 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 diff --git a/src/Controller/VehicleController.php b/src/Controller/VehicleController.php index 39c386b0..afe45fc5 100644 --- a/src/Controller/VehicleController.php +++ b/src/Controller/VehicleController.php @@ -495,23 +495,20 @@ class VehicleController extends Controller $battery_index = []; // get compatible batteries from selected manufacturer - foreach ($vobj->getBatteries() as $battery) + // foreach ($vobj->getBatteries() as $battery) + foreach ($vobj->getActiveBatteries() as $battery) { - // need to check if battery is active - if ($battery->isActive()) - { - $batteries[] = [ - 'id' => $battery->getID(), - 'mfg_name' => $battery->getManufacturer()->getName(), - 'model_name' => $battery->getModel()->getName(), - 'size_name' => $battery->getSize()->getName(), - 'prod_code' => $battery->getProductCode(), - 'sell_price' => $battery->getSellingPrice(), - 'warr_private' => $battery->getWarrantyPrivate(), - 'warr_commercial' => $battery->getWarrantyCommercial(), - ]; - $battery_index[$battery->getID()] = 1; - } + $batteries[] = [ + 'id' => $battery->getID(), + 'mfg_name' => $battery->getManufacturer()->getName(), + 'model_name' => $battery->getModel()->getName(), + 'size_name' => $battery->getSize()->getName(), + 'prod_code' => $battery->getProductCode(), + 'sell_price' => $battery->getSellingPrice(), + 'warr_private' => $battery->getWarrantyPrivate(), + 'warr_commercial' => $battery->getWarrantyCommercial(), + ]; + $battery_index[$battery->getID()] = 1; } // add all other batteries, because they want options diff --git a/src/Entity/Vehicle.php b/src/Entity/Vehicle.php index 61fdcec9..97a35da4 100644 --- a/src/Entity/Vehicle.php +++ b/src/Entity/Vehicle.php @@ -4,6 +4,8 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; + use Symfony\Component\Validator\Constraints as Assert; /** @@ -177,4 +179,12 @@ class Vehicle { return $this->cust_vehicles; } + + public function getActiveBatteries() + { + $crit = Criteria::create(); + $crit->where(Criteria::expr()->eq('flag_active', true)); + + return $this->batteries->matching($crit); + } } diff --git a/templates/vehicle/form.html.twig b/templates/vehicle/form.html.twig index 764186bf..99ecbc0f 100644 --- a/templates/vehicle/form.html.twig +++ b/templates/vehicle/form.html.twig @@ -233,14 +233,13 @@ $(function() { var batteryIds = []; var battMfgModelSize = [] - {% for batt in obj.getBatteries %} + {% for batt in obj.getActiveBatteries %} trow = { id: "{{ batt.getID }}", manufacturer: "{{ batt.getManufacturer.getName|default('') }} ", model: "{{ batt.getModel.getName|default('') }}", size: "{{ batt.getSize.getName|default('') }}", sell_price: "{{ batt.getSellingPrice }}", - flag_active: "{{ batt.isActive }}" }; battRows.push(trow); @@ -342,14 +341,6 @@ $(function() { layout: { scroll: true }, - rows: { - beforeTemplate: function(row, data, index) { - var is_active = data.flag_active; - if (is_active == false) { - $(row).addClass('hide'); - } - } - }, columns: [ { field: 'id',