Merge branch 'master' of gitlab.com:jankstudio/resq

This commit is contained in:
Kendrick Chan 2018-06-16 11:53:15 +08:00
commit a9efe274e3
10 changed files with 329 additions and 69 deletions

View file

@ -0,0 +1,168 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\Vehicle;
class MergeDuplicateVehiclesCommand extends Command
{
protected $em;
public function __construct(ObjectManager $om)
{
$this->em = $om;
parent::__construct();
}
protected function configure()
{
$this->setName('vehicle:merge')
->setDescription('Merge duplicate vehicle makes.')
->setHelp('Finds duplicate vehicle makes and merges them into one record. Deletes the duplicate record afterwards.');
}
protected function buildVehicleIndex()
{
$vindex = [];
$vehicles = $this->em->getRepository(Vehicle::class)->findAll();
foreach ($vehicles as $v)
{
$mfg_id = $v->getManufacturer()->getID();
$clean_make = strtolower(trim($v->getMake()));
if (!isset($vindex[$mfg_id][$clean_make]))
$vindex[$mfg_id][$clean_make] = [];
$vindex[$mfg_id][$clean_make][] = $v;
}
return $vindex;
}
protected function getDupeModels($source, $vlist, &$found_dupes)
{
$vfrom = $source->getModelYearFrom();
$vto = $source->getModelYearTo();
$id = $source->getID();
$dupes = [];
foreach ($vlist as $v)
{
// skip ourselves
if ($id == $v->getID())
continue;
// same model years
if ($vfrom == $v->getModelYearFrom() && $vto == $v->getModelYearTo())
{
// mark as duplicate
$found_dupes[$v->getID()] = true;
$dupes[$v->getID()] = $v;
/*
$dupes[$v->getID()] = [
'id' => $v->getID(),
'make' => $v->getMake(),
'y_from' => $v->getModelYearFrom(),
'y_to' => $v->getModelYearTo(),
'y_formatted' => $v->getModelYearFormatted(),
];
*/
}
}
return $dupes;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// get entity manager
$em = $this->em;
// get all vehicle makes
$vindex = $this->buildVehicleIndex();
// collect all dupes
$dupes = [];
$found_dupes = [];
$source_index = [];
// manufacturers
foreach ($vindex as $mfg_id => $vmfgs)
{
// makes
foreach ($vmfgs as $make => $data)
{
if (count($data) > 1)
{
// check if they have the same model years
foreach ($data as $v)
{
// if we already found a dupe with him, skip
if (isset($found_dupes[$v->getID()]))
continue;
// get dupes
$v_dupes = $this->getDupeModels($v, $data, $found_dupes);
if (count($v_dupes) > 0)
{
$source_index[$v->getID()] = $v;
$dupes[$v->getID()] = $v_dupes;
}
}
}
}
}
// process the duplicates
foreach ($dupes as $source_id => $sub_dupes)
{
$source = $source_index[$source_id];
$output->writeln('processing - ' . $source_id);
$output->writeln('removing duplicates for ' . $source->getManufacturer()->getName() . ' ' . $source->getMake() . ' ' . $source->getModelYearFormatted());
foreach ($sub_dupes as $dupe)
{
// move customer vehicles
$cvs = $dupe->getCustomerVehicles();
foreach ($cvs as $cv)
{
$cv->setVehicle($source);
$output->writeln('adjusting customer vehicle ' . $cv->getPlateNumber());
}
// move battery compatibility
$batts = $dupe->getBatteries();
foreach ($batts as $batt)
{
// $source->addBattery($batt);
$batt->addVehicle($source);
$batt->removeVehicle($dupe);
$output->writeln('adding battery ' . $batt->getModel()->getName() . ' ' . $batt->getSize()->getName());
}
// flush
$em->flush();
// remove dupes
$output->writeln('removing duplicate - ' . $dupe->getID());
$em->remove($dupe);
$em->flush();
}
}
// print_r($dupes);
}
}

View file

@ -346,7 +346,7 @@ class APIController extends Controller
->setConfirmed($this->session->isConfirmed());
// update mobile phone of customer
$cust->setPhoneMobile(substr(2, $this->session->getPhoneNumber()));
$cust->setPhoneMobile(substr($this->session->getPhoneNumber(), 2));
$em->flush();
@ -1379,7 +1379,10 @@ class APIController extends Controller
$jo_data['date_fulfilled'] = $jo->getDateFulfill()->format('M d, Y');
break;
case JOStatus::CANCELLED:
$jo_data['date_cancelled'] = $jo->getDateCancel()->format('M d, Y');
$date_cancel = $jo->getDateCancel();
if ($date_cancel == null)
$date_cancel = new DateTime();
$jo_data['date_cancelled'] = $date_cancel->format('M d, Y');
break;
}

View file

@ -2053,6 +2053,7 @@ class JobOrderController extends BaseController
// cancel job order
$obj->setStatus(JOStatus::CANCELLED)
->setDateCancel(new DateTime())
->setCancelReason($cancel_reason);
// set rider available

View file

@ -476,7 +476,10 @@ class RAPIController extends Controller
// TODO: refactor this into a jo handler class, so we don't have to repeat for control center
// set jo status to cancelled
$jo->setStatus(JOStatus::CANCELLED);
// TODO: set reason
$jo->setStatus(JOStatus::CANCELLED)
->setDateCancel(new DateTime());
$em->flush();
// send mqtt event

View file

@ -181,7 +181,15 @@ class Battery
public function addVehicle(Vehicle $vehicle)
{
$this->vehicles[$vehicle->getID()] = $vehicle;
if (!isset($this->vehicles[$vehicle->getID()]))
$this->vehicles[$vehicle->getID()] = $vehicle;
return $this;
}
public function removeVehicle(Vehicle $vehicle)
{
if (isset($this->vehicles[$vehicle->getID()]))
unset($this->vehicles[$vehicle->getID()]);
return $this;
}

View file

@ -37,7 +37,7 @@ class CustomerVehicle
// vehicle
/**
* @ORM\ManyToOne(targetEntity="Vehicle", inversedBy="customers")
* @ORM\ManyToOne(targetEntity="Vehicle", inversedBy="cust_vehicles")
* @ORM\JoinColumn(name="vehicle_id", referencedColumnName="id")
* @Assert\NotBlank()
*/

View file

@ -24,7 +24,7 @@ class Vehicle
/**
* @ORM\OneToMany(targetEntity="CustomerVehicle", mappedBy="vehicle")
*/
protected $customers;
protected $cust_vehicles;
// manufacturer
/**
@ -55,7 +55,7 @@ class Vehicle
// link to batteries compatible with this vehicle
/**
* @ORM\ManyToMany(targetEntity="Battery", mappedBy="vehicles", fetch="EXTRA_LAZY")
* @ORM\ManyToMany(targetEntity="Battery", mappedBy="vehicles", indexBy="id", fetch="EXTRA_LAZY")
*/
protected $batteries;
@ -68,7 +68,7 @@ class Vehicle
public function __construct()
{
$this->customers = new ArrayCollection();
$this->cust_vehicles = new ArrayCollection();
$this->batteries = new ArrayCollection();
$this->flag_mobile = true;
}
@ -145,7 +145,8 @@ class Vehicle
public function addBattery(Battery $battery)
{
$this->batteries->add($battery);
$this->batteries[$battery->getID()] = $battery;
return $this;
}
@ -170,4 +171,9 @@ class Vehicle
{
return $this->flag_mobile;
}
public function getCustomerVehicles()
{
return $this->cust_vehicles;
}
}

View file

@ -116,6 +116,9 @@
"ref": "aaddfdf43cdecd4cf91f992052d76c2cadc04543"
}
},
"setasign/fpdf": {
"version": "1.8.1"
},
"symfony/cache": {
"version": "v4.0.2"
},

View file

@ -49,7 +49,7 @@
{% if error %}
<div class="m-alert m-alert--outline alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"></button>
<span>{{ error }}</span>
<span>{{ error.getMessage }}</span>
</div>
{% endif %}
<div class="form-group m-form__group">
@ -177,7 +177,6 @@
<script src="/assets/demo/default/base/scripts.bundle.js" type="text/javascript"></script>
<!--end::Base Scripts -->
<!--begin::Page Snippets -->
<script src="/assets/snippets/pages/user/login.js" type="text/javascript"></script>
<!--end::Page Snippets -->
</body>
<!-- end::Body -->

View file

@ -13,7 +13,7 @@
<div class="m-content">
<!--Begin::Section-->
<div class="row">
<div class="col-xl-8">
<div class="col-xl-10">
<div class="m-portlet m-portlet--mobile">
<div class="m-portlet__head">
<div class="m-portlet__head-caption">
@ -94,6 +94,24 @@
</span>
</div>
</div>
{% if mode == 'update' %}
<div class="m-form__seperator m-form__seperator--dashed"></div>
<div class="m-form__section m-form__section--last">
<div class="m-form__heading">
<h3 class="m-form__heading-title">
Compatible Batteries
</h3>
</div>
<div class="form-group m-form__group row">
<div class="col-lg-12">
<div class="m_datatable" id="data-batts"></div>
</div>
</div>
</div>
{% endif %}
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
@ -113,72 +131,123 @@
{% endblock %}
{% block scripts %}
<script>
$(function() {
$("#row-form").submit(function(e) {
var form = $(this);
<script>
$(function() {
$("#row-form").submit(function(e) {
var form = $(this);
e.preventDefault();
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('vehicle_list') }}";
}
});
}).fail(function(response) {
if (response.status == 422) {
var errors = response.responseJSON.errors;
var firstfield = false;
$.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('vehicle_list') }}";
}
});
}).fail(function(response) {
if (response.status == 422) {
var errors = response.responseJSON.errors;
var firstfield = false;
// remove all error classes first
removeErrors();
// 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 + "']");
// 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');
// 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);
// check if this field comes first in DOM
var domfield = formfield.get(0);
if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) {
firstfield = domfield;
}
});
if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) {
firstfield = domfield;
}
});
// focus on first bad field
firstfield.focus();
// 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);
}
});
});
// 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');
var battRows = [];
{% for batt in obj.getBatteries %}
trow = {
id: "{{ batt.getID }}",
model: "{{ batt.getModel.getName|default('') }}",
size: "{{ batt.getSize.getName|default('') }}",
sell_price: "{{ batt.getSellingPrice }}"
};
battRows.push(trow);
{% endfor %}
// battery data table
var battOptions = {
data: {
type: 'local',
source: battRows,
saveState: {
cookie: false,
webstorage: false
}
});
</script>
},
layout: {
scroll: true
},
columns: [
{
field: 'id',
title: 'ID',
width: 50
},
{
field: 'size',
title: 'Size'
},
{
field: 'model',
title: 'Model',
width: 150
},
{
field: 'sell_price',
title: 'Price'
}
],
pagination: false
};
var battTable = $("#data-batts").mDatatable(battOptions);
// 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');
}
});
</script>
{% endblock %}