Add form for warranty submission. #236
This commit is contained in:
parent
08247f30f6
commit
2e022c893c
2 changed files with 231 additions and 4 deletions
|
|
@ -4,9 +4,11 @@ namespace App\Controller;
|
||||||
|
|
||||||
use App\Entity\Warranty;
|
use App\Entity\Warranty;
|
||||||
use App\Entity\SAPBattery;
|
use App\Entity\SAPBattery;
|
||||||
use App\Entity\BatteryManufacturer;
|
use App\Entity\BatteryModel;
|
||||||
|
use App\Entity\BatterySize;
|
||||||
|
|
||||||
use App\Ramcar\WarrantyClass;
|
use App\Ramcar\WarrantyClass;
|
||||||
|
use App\Ramcar\WarrantyStatus;
|
||||||
|
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
@ -146,9 +148,38 @@ class WarrantyController extends Controller
|
||||||
->setFirstName($req->request->get('first_name'))
|
->setFirstName($req->request->get('first_name'))
|
||||||
->setLastName($req->request->get('last_name'))
|
->setLastName($req->request->get('last_name'))
|
||||||
->setMobileNumber($req->request->get('mobile_number'))
|
->setMobileNumber($req->request->get('mobile_number'))
|
||||||
->setDatePurchase(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_purchase')));
|
->setDatePurchase(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_purchase')))
|
||||||
|
->setDateExpire(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_expire')))
|
||||||
|
->setDateClaim(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_claim')))
|
||||||
|
->setClaimFrom($req->request->get('claim_from'))
|
||||||
|
->setStatus($req->request->get('status'));
|
||||||
|
|
||||||
// need to get the battery and SAP battery set
|
// custom validation for battery model
|
||||||
|
$model = $em->getRepository(BatteryModel::class)
|
||||||
|
->find($req->request->get('battery_model'));
|
||||||
|
|
||||||
|
if (empty($model))
|
||||||
|
$error_array['battery_model'] = 'Invalid model selected.';
|
||||||
|
else
|
||||||
|
$obj->setBatteryModel($model);
|
||||||
|
|
||||||
|
// custom validation for battery size
|
||||||
|
$size = $em->getRepository(BatterySize::class)
|
||||||
|
->find($req->request->get('battery_size'));
|
||||||
|
|
||||||
|
if (empty($size))
|
||||||
|
$error_array['battery_size'] = 'Invalid size selected.';
|
||||||
|
else
|
||||||
|
$obj->setBatterySize($size);
|
||||||
|
|
||||||
|
// custom validation for SAP battery
|
||||||
|
$sap = $em->getRepository(SAPBattery::class)
|
||||||
|
->find($req->request->get('sap_battery'));
|
||||||
|
|
||||||
|
if (empty($sap))
|
||||||
|
$error_array['sap_battery'] = 'Invalid SAP battery selected.';
|
||||||
|
else
|
||||||
|
$obj->setSAPBattery($sap);
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
$errors = $validator->validate($obj);
|
$errors = $validator->validate($obj);
|
||||||
|
|
@ -163,6 +194,25 @@ class WarrantyController extends Controller
|
||||||
foreach ($errors as $error) {
|
foreach ($errors as $error) {
|
||||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if any errors were found
|
||||||
|
if (!empty($error_array)) {
|
||||||
|
// return validation failure response
|
||||||
|
return $this->json([
|
||||||
|
'success' => false,
|
||||||
|
'errors' => $error_array
|
||||||
|
], 422);
|
||||||
|
} else {
|
||||||
|
// validated! save the entity
|
||||||
|
$em->persist($obj);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
// return successful response
|
||||||
|
return $this->json([
|
||||||
|
'success' => 'Changes have been saved!'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -209,9 +259,11 @@ class WarrantyController extends Controller
|
||||||
{
|
{
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
|
||||||
$params['bmfgs'] = $em->getRepository(BatteryManufacturer::class)->findAll();
|
$params['batt_models'] = $em->getRepository(BatteryModel::class)->findAll();
|
||||||
|
$params['batt_sizes'] = $em->getRepository(BatterySize::class)->findAll();
|
||||||
$params['sap_batts'] = $em->getRepository(SAPBattery::class)->findAll();
|
$params['sap_batts'] = $em->getRepository(SAPBattery::class)->findAll();
|
||||||
$params['warranty_classes'] = WarrantyClass::getCollection();
|
$params['warranty_classes'] = WarrantyClass::getCollection();
|
||||||
|
$params['warranty_statuses'] = WarrantyStatus::getCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if datatable filter is present and append to query
|
// check if datatable filter is present and append to query
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,99 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-group m-form__group row">
|
||||||
|
<div class="col-lg-4">
|
||||||
|
<label data-field="sap_battery">
|
||||||
|
SAP Battery
|
||||||
|
</label>
|
||||||
|
<select name="sap_battery" class="form-control m-input">
|
||||||
|
{% for sap_battery in sap_batts %}
|
||||||
|
<option value="{{ sap_battery.getID }}"{{ obj.getSAPBattery.getID|default(0) == sap_battery.getID ? ' selected' }}>{{ sap_battery.getID }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<div class="form-control-feedback hide" data-field="sap_battery"></div>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-4">
|
||||||
|
<label data-field="battery_model">
|
||||||
|
Battery Model
|
||||||
|
</label>
|
||||||
|
<select name="battery_model" class="form-control m-input">
|
||||||
|
{% for model in batt_models %}
|
||||||
|
<option value="{{ model.getID }}"{{ obj.getModel.getID|default(0) == model.getID ? ' selected' }}>{{ model.getName }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<div class="form-control-feedback hide" data-field="battery_model"></div>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-4">
|
||||||
|
<label data-field="size">
|
||||||
|
Battery Size
|
||||||
|
</label>
|
||||||
|
<select class="form-control m-input" id="battery_size" name="battery_size">
|
||||||
|
<option value=""></option>
|
||||||
|
{% for size in batt_sizes %}
|
||||||
|
<option value="{{ size.getID }}"{{ obj.getSize.getID|default(0) == size.getID ? ' selected' }}>{{ size.getName }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<div class="form-control-feedback hide" data-field="size"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group m-form__group row">
|
||||||
|
<div class="col-lg-6">
|
||||||
|
<label data-field="date_purchase">Purchase Date</label>
|
||||||
|
<div class="input-group date dp">
|
||||||
|
<input type="text" name="date_purchase" class="form-control m-input" data-default-value="{{ obj.getDatePurchase|default("now")|date('Y-m-d') }}" value="{{ obj.getDatePurchase|default("now")|date('d M Y') }}" readonly placeholder="Select a date" >
|
||||||
|
<span class="input-group-addon">
|
||||||
|
<i class="la la-calendar glyphicon-th"></i>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="form-control-feedback hide" data-field="date_purchase"></div>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-6">
|
||||||
|
<label data-field="date_expire">Expiry Date</label>
|
||||||
|
<input type="text" name="date_expire" class="form-control m-input" value="{{ obj.getDateExpire is empty? "" : obj.getDateExpire|date('Y-m-d') }}" disabled>
|
||||||
|
<div class="form-control-feedback hide" data-field="date_expire"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group m-form__group row">
|
||||||
|
<div class="col-lg-4">
|
||||||
|
<label data-field="date_claim">Claim Date</label>
|
||||||
|
<div class="input-group date dp">
|
||||||
|
<input type="text" name="date_claim" class="form-control m-input" data-default-value="{{ obj.getDateClaim is empty? "" : obj.getDateClaim|date('Y-m-d') }}" value="{{ obj.getDateClaim is empty? "" : obj.getDateClaim|date('Y-m-d') }}" readonly placeholder="Select a date" >
|
||||||
|
<span class="input-group-addon">
|
||||||
|
<i class="la la-calendar glyphicon-th"></i>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="form-control-feedback hide" data-field="date_claim"></div>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-4">
|
||||||
|
<label data-field="claim_from">
|
||||||
|
Claimed From
|
||||||
|
</label>
|
||||||
|
<input type="text" name="claim_from" class="form-control m-input" value="{{ obj.getClaimedFrom }}" data-name="claim_from">
|
||||||
|
<div class="form-control-feedback hide" data-field="claim_from"></div>
|
||||||
|
</div>
|
||||||
|
<div class="col-lg-4">
|
||||||
|
<label data-field="status">
|
||||||
|
Status
|
||||||
|
</label>
|
||||||
|
<select class="form-control m-input" id="status" name="status">
|
||||||
|
{% for key, status in warranty_statuses %}
|
||||||
|
<option value="{{ key }}"{{ obj.getStatus == key ? ' selected' }}>{{ status }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<div class="form-control-feedback hide" data-field="status"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="m-portlet__foot m-portlet__foot--fit">
|
||||||
|
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-12">
|
||||||
|
<button type="submit" class="btn btn-success">Submit</button>
|
||||||
|
<a href="{{ url('warranty_list') }}" class="btn btn-secondary">Back</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
@ -100,3 +193,85 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block scripts %}
|
||||||
|
<script>
|
||||||
|
$(function() {
|
||||||
|
$("#row-form").submit(function(e) {
|
||||||
|
var form = $(this);
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
method: "POST",
|
||||||
|
url: form.prop('action'),
|
||||||
|
data: form.serialize()
|
||||||
|
}).done(function(response) {
|
||||||
|
// remove all error classes
|
||||||
|
removeErrors();
|
||||||
|
swal({
|
||||||
|
title: 'Done!',
|
||||||
|
text: 'Your changes have been saved!',
|
||||||
|
type: 'success',
|
||||||
|
onClose: function() {
|
||||||
|
window.location.href = "{{ url('service_list') }}";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).fail(function(response) {
|
||||||
|
if (response.status == 422 || response.status == 403) {
|
||||||
|
var errors = response.responseJSON.errors;
|
||||||
|
var firstfield = false;
|
||||||
|
|
||||||
|
// remove all error classes first
|
||||||
|
removeErrors();
|
||||||
|
|
||||||
|
// display errors contextually
|
||||||
|
$.each(errors, function(field, msg) {
|
||||||
|
var formfield = $("[name='" + field + "']");
|
||||||
|
var label = $("label[data-field='" + field + "']");
|
||||||
|
var msgbox = $(".form-control-feedback[data-field='" + field + "']");
|
||||||
|
|
||||||
|
// add error classes to bad fields
|
||||||
|
formfield.addClass('form-control-danger');
|
||||||
|
label.addClass('has-danger');
|
||||||
|
msgbox.html(msg).addClass('has-danger').removeClass('hide');
|
||||||
|
|
||||||
|
// check if this field comes first in DOM
|
||||||
|
var domfield = formfield.get(0);
|
||||||
|
|
||||||
|
if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) {
|
||||||
|
firstfield = domfield;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// focus on first bad field
|
||||||
|
firstfield.focus();
|
||||||
|
|
||||||
|
// scroll to above that field to make it visible
|
||||||
|
$('html, body').animate({
|
||||||
|
scrollTop: $(firstfield).offset().top - 200
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// remove all error classes
|
||||||
|
function removeErrors() {
|
||||||
|
$(".form-control-danger").removeClass('form-control-danger');
|
||||||
|
$("[data-field]").removeClass('has-danger');
|
||||||
|
$(".form-control-feedback[data-field]").addClass('hide');
|
||||||
|
}
|
||||||
|
|
||||||
|
// datepicker
|
||||||
|
$(".dp").datepicker({
|
||||||
|
format: "dd M yyyy",
|
||||||
|
todayHighlight: true,
|
||||||
|
autoclose: true,
|
||||||
|
pickerPosition: 'bottom-left',
|
||||||
|
bootcssVer: 3,
|
||||||
|
clearBtn: true
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue