Resolve "Add button for battery compatibility in vehicle form" #1176
4 changed files with 274 additions and 24 deletions
|
|
@ -39,6 +39,11 @@ battery_delete:
|
||||||
controller: App\Controller\BatteryController::destroy
|
controller: App\Controller\BatteryController::destroy
|
||||||
methods: [DELETE]
|
methods: [DELETE]
|
||||||
|
|
||||||
|
battery_ajax_get:
|
||||||
|
path: /ajax/battery_find
|
||||||
|
controller: App\Controller\BatteryController::getBattery
|
||||||
|
methods: [GET]
|
||||||
|
|
||||||
# battery manufacturers
|
# battery manufacturers
|
||||||
|
|
||||||
bmfg_list:
|
bmfg_list:
|
||||||
|
|
|
||||||
|
|
@ -415,6 +415,57 @@ class BatteryController extends Controller
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getBattery(Request $req)
|
||||||
|
{
|
||||||
|
// no access check, grant all for this ajax call
|
||||||
|
|
||||||
|
// parse the id: model size
|
||||||
|
$bmfg_id = $req->query->get('mfg_id');
|
||||||
|
$bmodel_id = $req->query->get('model_id');
|
||||||
|
$bsize_id = $req->query->get('size_id');
|
||||||
|
|
||||||
|
// find the battery using model and size
|
||||||
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
$query = $em->createQuery('SELECT b FROM App\Entity\Battery b
|
||||||
|
JOIN b.model bm
|
||||||
|
JOIN b.size bs
|
||||||
|
JOIN b.manufacturer bmfg
|
||||||
|
WHERE bm.id = :bm_id
|
||||||
|
AND bs.id = :bs_id
|
||||||
|
AND bmfg.id = :bmfg_id')
|
||||||
|
->setParameter('bmfg_id', $bmfg_id)
|
||||||
|
->setParameter('bm_id', $bmodel_id)
|
||||||
|
->setParameter('bs_id', $bsize_id);
|
||||||
|
|
||||||
|
$result = $query->getResult();
|
||||||
|
|
||||||
|
if (empty($result))
|
||||||
|
{
|
||||||
|
// TODO: will have to change from 422 to 404
|
||||||
|
return $this->json([
|
||||||
|
'success' => false,
|
||||||
|
'errors' => "Battery does not exist",
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
$batteries = [];
|
||||||
|
|
||||||
|
foreach ($result as $row)
|
||||||
|
{
|
||||||
|
$batteries[] = [
|
||||||
|
'id' => $row->getID(),
|
||||||
|
'manufacturer' => $row->getManufacturer()->getName(),
|
||||||
|
'model' => $row->getModel()->getName(),
|
||||||
|
'size' => $row->getSize()->getName(),
|
||||||
|
'price' => $row->getSellingPrice(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->json([
|
||||||
|
'data' => $batteries
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
// check if datatable filter is present and append to query
|
// check if datatable filter is present and append to query
|
||||||
protected function setQueryFilters($datatable, &$query) {
|
protected function setQueryFilters($datatable, &$query) {
|
||||||
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@ namespace App\Controller;
|
||||||
|
|
||||||
use App\Entity\Vehicle;
|
use App\Entity\Vehicle;
|
||||||
use App\Entity\VehicleManufacturer;
|
use App\Entity\VehicleManufacturer;
|
||||||
|
use App\Entity\BatteryManufacturer;
|
||||||
use App\Entity\Battery;
|
use App\Entity\Battery;
|
||||||
|
use App\Entity\BatteryModel;
|
||||||
|
use App\Entity\BatterySize;
|
||||||
|
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
@ -225,6 +228,11 @@ class VehicleController extends Controller
|
||||||
$params['years'] = $this->generateYearOptions();
|
$params['years'] = $this->generateYearOptions();
|
||||||
$params['obj'] = $row;
|
$params['obj'] = $row;
|
||||||
|
|
||||||
|
// get battery information for compatible batteries
|
||||||
|
$params['bmfgs'] = $em->getRepository(BatteryManufacturer::class)->findAll();
|
||||||
|
$params['bmodels'] = $em->getRepository(BatteryModel::class)->findAll();
|
||||||
|
$params['bsizes'] = $em->getRepository(BatterySize::class)->findAll();
|
||||||
|
|
||||||
// response
|
// response
|
||||||
return $this->render('vehicle/form.html.twig', $params);
|
return $this->render('vehicle/form.html.twig', $params);
|
||||||
}
|
}
|
||||||
|
|
@ -278,6 +286,7 @@ class VehicleController extends Controller
|
||||||
$batteries = $req->request->get('batteries');
|
$batteries = $req->request->get('batteries');
|
||||||
if (!empty($batteries))
|
if (!empty($batteries))
|
||||||
{
|
{
|
||||||
|
// TODO: need to move the checking for batteries to a function
|
||||||
// need to check if a battery has been removed
|
// need to check if a battery has been removed
|
||||||
if (count($current_batteries) > count($batteries))
|
if (count($current_batteries) > count($batteries))
|
||||||
{
|
{
|
||||||
|
|
@ -302,6 +311,96 @@ class VehicleController extends Controller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// need to check if a battery has been added
|
||||||
|
if (count($current_batteries) < count($batteries))
|
||||||
|
{
|
||||||
|
// get the ids of current batteries
|
||||||
|
$cbatt_ids = [];
|
||||||
|
foreach ($current_batteries as $cbatt)
|
||||||
|
{
|
||||||
|
$cbatt_ids = [
|
||||||
|
$cbatt->getID(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the battery to add
|
||||||
|
foreach ($batteries as $batt)
|
||||||
|
{
|
||||||
|
$batt_id = $batt;
|
||||||
|
if (in_array($batt_id, $cbatt_ids))
|
||||||
|
{
|
||||||
|
// do nothing since battery already there
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// batt is the new battery
|
||||||
|
$battery = $em->getRepository(Battery::class)->find($batt_id);
|
||||||
|
|
||||||
|
if (!empty($battery))
|
||||||
|
{
|
||||||
|
$battery->addVehicle($row);
|
||||||
|
|
||||||
|
$em->persist($battery);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// check if battery was deleted then another one was added
|
||||||
|
if (count($current_batteries) == count($batteries))
|
||||||
|
{
|
||||||
|
// find deleted battery
|
||||||
|
foreach ($current_batteries as $cbatt)
|
||||||
|
{
|
||||||
|
$cbatt_id = $cbatt->getID();
|
||||||
|
if (in_array($cbatt_id, $batteries))
|
||||||
|
{
|
||||||
|
// do nothing, move to next element
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// cbatt_id has been deleted
|
||||||
|
$del_battery = $em->getRepository(Battery::class)->find($cbatt_id);
|
||||||
|
|
||||||
|
if (!empty($del_battery))
|
||||||
|
{
|
||||||
|
$del_battery->removeVehicle($row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the battery to add
|
||||||
|
$cbatt_ids = [];
|
||||||
|
foreach ($current_batteries as $cbatt)
|
||||||
|
{
|
||||||
|
$cbatt_ids = [
|
||||||
|
$cbatt->getID(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($batteries as $batt)
|
||||||
|
{
|
||||||
|
$batt_id = $batt;
|
||||||
|
if (in_array($batt_id, $cbatt_ids))
|
||||||
|
{
|
||||||
|
// do nothing since battery already there
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// batt is the new battery
|
||||||
|
$new_battery = $em->getRepository(Battery::class)->find($batt_id);
|
||||||
|
|
||||||
|
if (!empty($new_battery))
|
||||||
|
{
|
||||||
|
$new_battery->addVehicle($row);
|
||||||
|
|
||||||
|
$em->persist($new_battery);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -96,23 +96,50 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if mode == 'update' %}
|
{% if mode == 'update' %}
|
||||||
<div class="m-form__seperator m-form__seperator--dashed"></div>
|
<div class="m-form__seperator m-form__seperator--dashed"></div>
|
||||||
|
<div class="m-form__section m-form__section--last">
|
||||||
<div class="m-form__section m-form__section--last">
|
<div class="m-form__heading">
|
||||||
<div class="m-form__heading">
|
<h3 class="m-form__heading-title">
|
||||||
<h3 class="m-form__heading-title">
|
Compatible Batteries
|
||||||
Compatible Batteries
|
</h3>
|
||||||
</h3>
|
</div>
|
||||||
</div>
|
<div class="form-group m-form__group row">
|
||||||
<div class="form-group m-form__group row">
|
<div class="col-lg-12">
|
||||||
<div class="col-lg-12">
|
<div class="m_datatable" id="data-batts"></div>
|
||||||
<div class="m_datatable" id="data-batts"></div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<div class="form-group m-form__group row">
|
||||||
</div>
|
<label class="col-lg-1 col-form-label" data-field="battery_list">
|
||||||
|
Add Battery:
|
||||||
|
</label>
|
||||||
|
<div class="col-lg-3">
|
||||||
|
<select name="bmfg_list" class="form-control m-input" id="bmfg">
|
||||||
|
{% for bmfg in bmfgs %}
|
||||||
|
<option value="{{ bmfg.getID }}">{{ bmfg.getName }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-3">
|
||||||
|
<select name="bmodel_list" class="form-control m-input" id="bmodel">
|
||||||
|
{% for bmodel in bmodels %}
|
||||||
|
<option value="{{ bmodel.getID }}">{{ bmodel.getName }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-3">
|
||||||
|
<select name="bsize_list" class="form-control m-input" id="bsize">
|
||||||
|
{% for bsize in bsizes %}
|
||||||
|
<option value="{{ bsize.getID }}">{{ bsize.getName }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-3">
|
||||||
|
<button type="button" class="btn btn-primary" id="btn-add-battery" >Add to List</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
</div>
|
||||||
</div>
|
|
||||||
<div class="m-portlet__foot m-portlet__foot--fit">
|
<div class="m-portlet__foot m-portlet__foot--fit">
|
||||||
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
|
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|
@ -201,19 +228,23 @@ $(function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// initialize battery arrays
|
||||||
var battRows = [];
|
var battRows = [];
|
||||||
var batteryIds = [];
|
var batteryIds = [];
|
||||||
|
var battMfgModelSize = []
|
||||||
|
|
||||||
{% for batt in obj.getBatteries %}
|
{% for batt in obj.getBatteries %}
|
||||||
trow = {
|
trow = {
|
||||||
id: "{{ batt.getID }}",
|
id: "{{ batt.getID }}",
|
||||||
model: "{{ batt.getModel.getName|default('') }}",
|
manufacturer: "{{ batt.getManufacturer.getName|default('') }} ",
|
||||||
size: "{{ batt.getSize.getName|default('') }}",
|
model: "{{ batt.getModel.getName|default('') }}",
|
||||||
sell_price: "{{ batt.getSellingPrice }}"
|
size: "{{ batt.getSize.getName|default('') }}",
|
||||||
};
|
sell_price: "{{ batt.getSellingPrice }}"
|
||||||
|
};
|
||||||
|
|
||||||
battRows.push(trow);
|
battRows.push(trow);
|
||||||
batteryIds.push({{ batt.getID }});
|
batteryIds.push({{ batt.getID }});
|
||||||
|
battMfgModelSize.push({{ batt.getManufacturer.getID }} + ' ' + {{ batt.getModel.getID }} + ' ' + {{ batt.getSize.getID }});
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
// remove battery from table
|
// remove battery from table
|
||||||
|
|
@ -237,6 +268,66 @@ $(function() {
|
||||||
battTable.reload();
|
battTable.reload();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// add a battery to the table
|
||||||
|
$("#btn-add-battery").click(function() {
|
||||||
|
var bmfg_id = $("#bmfg").val();
|
||||||
|
var bmodel_id = $("#bmodel").val();
|
||||||
|
var bsize_id = $("#bsize").val();
|
||||||
|
|
||||||
|
var batt_key = (bmfg_id + ' ' + bmodel_id + ' ' + bsize_id);
|
||||||
|
|
||||||
|
if (battMfgModelSize.indexOf(batt_key) !== -1) {
|
||||||
|
swal({
|
||||||
|
title: 'Whoops',
|
||||||
|
text: 'This battery is already on the list.',
|
||||||
|
type: 'warning'
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the battery given the model and size
|
||||||
|
$.ajax({
|
||||||
|
method: "GET",
|
||||||
|
url: "{{ url('battery_ajax_get') }}",
|
||||||
|
data: {mfg_id: bmfg_id, model_id: bmodel_id, size_id: bsize_id}
|
||||||
|
}).done(function(response) {
|
||||||
|
if (response.data.length > 0) {
|
||||||
|
var batt = response.data[0];
|
||||||
|
|
||||||
|
batteryIds.push(batt.id);
|
||||||
|
|
||||||
|
brow = {
|
||||||
|
id: batt.id,
|
||||||
|
manufacturer: batt.manufacturer,
|
||||||
|
model: batt.model,
|
||||||
|
size: batt.size,
|
||||||
|
sell_price: batt.price
|
||||||
|
};
|
||||||
|
|
||||||
|
battRows.push(brow);
|
||||||
|
|
||||||
|
// add battery to arrays
|
||||||
|
battMfgModelSize.push(batt_key);
|
||||||
|
|
||||||
|
// refresh the data table
|
||||||
|
battTable.originalDataSet = battRows;
|
||||||
|
battTable.reload();
|
||||||
|
}
|
||||||
|
}).fail(function(response) {
|
||||||
|
// TODO: change response status to 404. For some strange reason,
|
||||||
|
// if status is set to 422, the swal does not display.
|
||||||
|
// Instead, we get the technical error box.
|
||||||
|
if (response.status == 422) {
|
||||||
|
swal({
|
||||||
|
title: 'Whoops',
|
||||||
|
text: 'This battery does not exist.',
|
||||||
|
type: 'warning'
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// battery data table
|
// battery data table
|
||||||
var battOptions = {
|
var battOptions = {
|
||||||
data: {
|
data: {
|
||||||
|
|
@ -256,6 +347,10 @@ $(function() {
|
||||||
title: 'ID',
|
title: 'ID',
|
||||||
width: 50
|
width: 50
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
field: 'manufacturer',
|
||||||
|
title: 'Manufacturer',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
field: 'size',
|
field: 'size',
|
||||||
title: 'Size'
|
title: 'Size'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue