diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index 00e2d110..330164aa 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -14,7 +14,8 @@ use App\Entity\JobOrder; use App\Entity\BatteryManufacturer; use App\Entity\Customer; use App\Entity\CustomerVehicle; -use App\Entity\Outlet; +//use App\Entity\Outlet; +use App\Entity\Hub; use App\Entity\Promo; use App\Entity\Rider; use App\Entity\Battery; @@ -23,6 +24,7 @@ use App\Service\InvoiceCreator; use App\Service\MapTools; use Doctrine\ORM\Query; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\LockMode; use Doctrine\ORM\PessimisticLockException; @@ -239,6 +241,10 @@ class JobOrderController extends BaseController // check which job order tier is being called for and confirm access $tier_params = $this->checkTier($tier); + // get current user + $user = $this->getUser(); + $hubs = $user->getHubs(); + // get query builder $qb = $this->getDoctrine() ->getRepository(JobOrder::class) @@ -250,7 +256,7 @@ class JobOrderController extends BaseController // count total records $tquery = $qb->select('COUNT(q)'); - $this->setQueryFilters($datatable, $tquery, $qb, $tier_params['jo_status']); + $this->setQueryFilters($datatable, $tquery, $qb, $hubs, $tier, $tier_params['jo_status']); $total = $tquery->getQuery() ->getSingleScalarResult(); @@ -274,7 +280,7 @@ class JobOrderController extends BaseController // build query $query = $qb->select('q'); - $this->setQueryFilters($datatable, $query, $qb, $tier_params['jo_status']); + $this->setQueryFilters($datatable, $query, $qb, $hubs, $tier, $tier_params['jo_status']); // check if sorting is present, otherwise use default if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { @@ -396,35 +402,35 @@ class JobOrderController extends BaseController $params['discount_apply'] = DiscountApply::getCollection(); $params['trade_in_types'] = TradeInType::getCollection(); - // get closest outlets - $outlets = $map_tools->getClosestOutlets($obj->getCoordinates(), 10, date("H:i:s")); + // get closest hubs + $hubs = $map_tools->getClosestHubs($obj->getCoordinates(), 10, date("H:i:s")); - $params['outlets'] = []; + $params['hubs'] = []; // format duration and distance into friendly time - foreach ($outlets as $outlet) { + foreach ($hubs as $hub) { // duration - $seconds = $outlet['duration']; + $seconds = $hub['duration']; if (!empty($seconds) && $seconds > 0) { $hours = floor($seconds / 3600); $minutes = ceil(($seconds / 60) % 60); - $outlet['duration'] = ($hours > 0 ? number_format($hours) . " hr" . ($hours > 1 ? "s" : '') . ($minutes > 0 ? ", " : '') : '') . ($minutes > 0 ? number_format($minutes) . " min" . ($minutes > 1 ? "s" : '') : ''); + $hub['duration'] = ($hours > 0 ? number_format($hours) . " hr" . ($hours > 1 ? "s" : '') . ($minutes > 0 ? ", " : '') : '') . ($minutes > 0 ? number_format($minutes) . " min" . ($minutes > 1 ? "s" : '') : ''); } else { - $outlet['duration'] = false; + $hub['duration'] = false; } // distance - $meters = $outlet['distance']; + $meters = $hub['distance']; if (!empty($meters) && $meters > 0) { - $outlet['distance'] = round($meters / 1000) . " km"; + $hub['distance'] = round($meters / 1000) . " km"; } else { - $outlet['distance'] = false; + $hub['distance'] = false; } - $params['outlets'][] = $outlet; + $params['hubs'][] = $hub; } $params['obj'] = $obj; @@ -465,15 +471,15 @@ class JobOrderController extends BaseController $error_array['coordinates'] = 'No map coordinates provided. Please click on a location on the map.'; } - // check if outlet is set - if (empty($req->request->get('outlet'))) { - $error_array['outlet'] = 'No outlet selected.'; + // check if hub is set + if (empty($req->request->get('hub'))) { + $error_array['hub'] = 'No hub selected.'; } else { - // get outlet - $outlet = $em->getRepository(Outlet::class)->find($req->request->get('outlet')); + // get hub + $hub = $em->getRepository(Hub::class)->find($req->request->get('hub')); - if (empty($outlet)) { - $error_array['outlet'] = 'Invalid outlet specified.'; + if (empty($hub)) { + $error_array['hub'] = 'Invalid hub specified.'; } } @@ -492,7 +498,7 @@ class JobOrderController extends BaseController ->setDeliveryInstructions($req->request->get('delivery_instructions')) ->setAgentNotes($req->request->get('agent_notes')) ->setDeliveryAddress($req->request->get('delivery_address')) - ->setOutlet($outlet); + ->setHub($hub); // validate $errors = $validator->validate($obj); @@ -552,6 +558,14 @@ class JobOrderController extends BaseController throw $this->createNotFoundException('The job order does not have an assigning status'); } + // check if hub is assigned to current user + $user_hubs = $this->getUser()->getHubs(); + if (!in_array($obj->getHub()->getID(), $user_hubs)) + { + $em->getConnection()->rollback(); + throw $this->createNotFoundException('The job order is not on a hub assigned to this user'); + } + // check if we are the assignor $assignor = $obj->getAssignedBy(); $user = $this->getUser(); @@ -583,35 +597,35 @@ class JobOrderController extends BaseController $params['discount_apply'] = DiscountApply::getCollection(); $params['trade_in_types'] = TradeInType::getCollection(); - // get closest outlets - $outlets = $map_tools->getClosestOutlets($obj->getCoordinates(), 10, date("H:i:s")); + // get closest hubs + $hubs = $map_tools->getClosestHubs($obj->getCoordinates(), 10, date("H:i:s")); - $params['outlets'] = []; + $params['hubs'] = []; // format duration and distance into friendly time - foreach ($outlets as $outlet) { + foreach ($hubs as $hub) { // duration - $seconds = $outlet['duration']; + $seconds = $hub['duration']; if (!empty($seconds) && $seconds > 0) { $hours = floor($seconds / 3600); $minutes = ceil(($seconds / 60) % 60); - $outlet['duration'] = ($hours > 0 ? number_format($hours) . " hr" . ($hours > 1 ? "s" : '') . ($minutes > 0 ? ", " : '') : '') . ($minutes > 0 ? number_format($minutes) . " min" . ($minutes > 1 ? "s" : '') : ''); + $hub['duration'] = ($hours > 0 ? number_format($hours) . " hr" . ($hours > 1 ? "s" : '') . ($minutes > 0 ? ", " : '') : '') . ($minutes > 0 ? number_format($minutes) . " min" . ($minutes > 1 ? "s" : '') : ''); } else { - $outlet['duration'] = false; + $hub['duration'] = false; } // distance - $meters = $outlet['distance']; + $meters = $hub['distance']; if (!empty($meters) && $meters > 0) { - $outlet['distance'] = round($meters / 1000) . " km"; + $hub['distance'] = round($meters / 1000) . " km"; } else { - $outlet['distance'] = false; + $hub['distance'] = false; } - $params['outlets'][] = $outlet; + $params['hubs'][] = $hub; } $params['obj'] = $obj; @@ -704,11 +718,18 @@ class JobOrderController extends BaseController // TODO: re-enable search, figure out how to group the orWhere filters into one, so can execute that plus the pending filter // check if datatable filter is present and append to query - protected function setQueryFilters($datatable, &$query, $qb, $status) + protected function setQueryFilters($datatable, &$query, $qb, $hubs, $tier, $status) { $query->where('q.status = :status') ->setParameter('status', $status); + // on assigning, filter by assigned hub + if ($tier == 'assign') + { + $query->andWhere('q.hub IN (:hubs)') + ->setParameter('hubs', $hubs, Connection::PARAM_STR_ARRAY); + } + // get only pending rows /* $query->where($qb->expr()->orX( diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 2d53dbbb..7875c655 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Ramcar\BaseController; use App\Entity\User; use App\Entity\Role; +use App\Entity\Hub; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; @@ -133,6 +134,7 @@ class UserController extends BaseController // get roles $em = $this->getDoctrine()->getManager(); $params['roles'] = $em->getRepository(Role::class)->findAll(); + $params['hubs'] = $em->getRepository(Hub::class)->findAll(); // response return $this->render('user/form.html.twig', $params); @@ -153,7 +155,8 @@ class UserController extends BaseController ->setEmail($req->request->get('email')) ->setContactNumber($req->request->get('contact_no')) ->setEnabled($req->request->get('enabled') ? true : false) - ->clearRoles(); + ->clearRoles() + ->clearHubs(); // set roles $roles = $req->request->get('roles'); @@ -173,6 +176,19 @@ class UserController extends BaseController } } + // set hubs + $hubs = $req->request->get('hubs'); + + if (!empty($hubs)) { + foreach ($hubs as $hub_id) { + // check if hub exists + $hub = $em->getRepository(Hub::class)->find($hub_id); + + if (!empty($hub)) + $obj->addHub($hub); + } + } + // validate $errors = $validator->validate($obj); @@ -238,6 +254,7 @@ class UserController extends BaseController // get roles $params['roles'] = $em->getRepository(Role::class)->findAll(); + $params['hubs'] = $em->getRepository(Hub::class)->findAll(); $params['obj'] = $obj; @@ -264,7 +281,8 @@ class UserController extends BaseController ->setEmail($req->request->get('email')) ->setContactNumber($req->request->get('contact_no')) ->setEnabled($req->request->get('enabled') ? true : false) - ->clearRoles(); + ->clearRoles() + ->clearHubs(); // set roles $roles = $req->request->get('roles'); @@ -279,6 +297,19 @@ class UserController extends BaseController } } + // set hubs + $hubs = $req->request->get('hubs'); + + if (!empty($hubs)) { + foreach ($hubs as $hub_id) { + // check if hub exists + $hub = $em->getRepository(Hub::class)->find($hub_id); + + if (!empty($hub)) + $obj->addHub($hub); + } + } + // validate $errors = $validator->validate($obj); diff --git a/src/Entity/Hub.php b/src/Entity/Hub.php index 74eb1b41..4e4d9b1d 100644 --- a/src/Entity/Hub.php +++ b/src/Entity/Hub.php @@ -23,11 +23,16 @@ class Hub */ protected $riders; - // outlets under this hub + // job orders assigned to hub /** - * @ORM\OneToMany(targetEntity="Outlet", mappedBy="hub") + * @ORM\OneToMany(targetEntity="JobOrder", mappedBy="hub") */ - protected $outlets; + protected $job_orders; + + /** + * @ORM\ManyToMany(targetEntity="User", mappedBy="hubs", fetch="EXTRA_LAZY") + */ + protected $users; public function __construct() { @@ -42,8 +47,18 @@ class Hub return $this->riders; } - public function getOutlets() + public function getUsers() { - return $this->outlets; + return $this->users; + } + + public function getUsersCount() + { + return $this->users->count(); + } + + public function getJobOrders() + { + return $this->job_orders; } } diff --git a/src/Entity/JobOrder.php b/src/Entity/JobOrder.php index fa5ac850..e4e98d6c 100644 --- a/src/Entity/JobOrder.php +++ b/src/Entity/JobOrder.php @@ -116,12 +116,12 @@ class JobOrder */ protected $cus_vehicle; - // assigned outlet + // assigned hub /** - * @ORM\ManyToOne(targetEntity="Outlet", inversedBy="job_orders") - * @ORM\JoinColumn(name="outlet_id", referencedColumnName="id") + * @ORM\ManyToOne(targetEntity="Hub", inversedBy="job_orders") + * @ORM\JoinColumn(name="hub_id", referencedColumnName="id") */ - protected $outlet; + protected $hub; // assigned rider /** @@ -324,15 +324,15 @@ class JobOrder return $this->cus_vehicle; } - public function setOutlet(Outlet $outlet) + public function setHub(Hub $hub) { - $this->outlet = $outlet; + $this->hub = $hub; return $this; } - public function getOutlet() + public function getHub() { - return $this->outlet; + return $this->hub; } public function setRider(Rider $rider) diff --git a/src/Entity/User.php b/src/Entity/User.php index 403c8f5f..88cb8cc9 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -42,6 +42,12 @@ class User implements AdvancedUserInterface, Serializable */ protected $roles; + /** + * @ORM\ManyToMany(targetEntity="Hub", inversedBy="users") + * @ORM\JoinTable(name="user_hubs") + */ + protected $hubs; + /** * @ORM\Column(type="boolean") */ @@ -100,6 +106,7 @@ class User implements AdvancedUserInterface, Serializable public function __construct() { $this->roles = new ArrayCollection(); + $this->hubs = new ArrayCollection(); $this->job_orders_created = new ArrayCollection(); $this->job_orders_assigned = new ArrayCollection(); $this->tickets = new ArrayCollection(); @@ -171,6 +178,32 @@ class User implements AdvancedUserInterface, Serializable return $this->roles; } + public function addHub(Hub $hub) + { + $this->hubs->add($hub); + return $this; + } + + public function clearHubs() + { + $this->hubs->clear(); + return $this; + } + + public function getHubs() + { + $str_hubs = []; + foreach ($this->hubs as $hub) + $str_hubs[] = $hub->getID(); + + return $str_hubs; + } + + public function getHubObjects() + { + return $this->hubs; + } + public function eraseCredentials() { return $this; diff --git a/src/Ramcar/Location.php b/src/Ramcar/Location.php index eae3e3a4..b83eeb56 100644 --- a/src/Ramcar/Location.php +++ b/src/Ramcar/Location.php @@ -95,7 +95,7 @@ trait Location public function getFullName() { - return $this->name . ' ' . $this->branch; + return $this->name . ($this->branch ? ' - ' . $this->branch : ''); } public function setAddress($address) diff --git a/src/Service/MapTools.php b/src/Service/MapTools.php index 0805d47d..693b3c21 100644 --- a/src/Service/MapTools.php +++ b/src/Service/MapTools.php @@ -23,7 +23,7 @@ class MapTools $this->gmaps_api_key = $gmaps_api_key; } - protected function mapGetDistances(Point $point, $outlets) + protected function mapGetDistances(Point $point, $hubs) { $client = new GuzzleClient(); @@ -32,9 +32,9 @@ class MapTools // destinations $dests = []; - foreach ($outlets as $outlet) + foreach ($hubs as $hub) { - $coord = $outlet->getCoordinates(); + $coord = $hub->getCoordinates(); $dests[] = round($coord->getLatitude(),5) . ',' . round($point->getLongitude(), 5); } $dests_value = implode('|', $dests); @@ -57,10 +57,10 @@ class MapTools return $res->getBody(); } - public function getClosestOutlets(Point $point, $limit, $time = false) + public function getClosestHubs(Point $point, $limit, $time = false) { - // get closest outlets based on st_distance function from db - $query = $this->em->createQuery('SELECT o, st_distance(o.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Outlet o' . ($time ? ' WHERE :time BETWEEN o.time_open AND o.time_close' : '') . ' ORDER BY dist') + // get closest hubs based on st_distance function from db + $query = $this->em->createQuery('SELECT h, st_distance(h.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Hub h' . ($time ? ' WHERE :time BETWEEN h.time_open AND h.time_close' : '') . ' ORDER BY dist') ->setParameter('lat', $point->getLatitude()) ->setParameter('lng', $point->getLongitude()); @@ -73,14 +73,14 @@ class MapTools // error_log($query->getSql()); $result = $query->getResult(); - $outlets = []; + $hubs = []; $final_data = []; foreach ($result as $row) { //error_log($row[0]->getName() . ' - ' . $row['dist']); - $outlets[] = $row[0]; + $hubs[] = $row[0]; $final_data[] = [ - 'outlet' => $row[0], + 'hub' => $row[0], 'db_distance' => $row['dist'], 'distance' => 0, 'duration' => 0, @@ -88,7 +88,7 @@ class MapTools } // get actual distance details with eta from google maps api - $raw_res = $this->mapGetDistances($point, $outlets); + $raw_res = $this->mapGetDistances($point, $hubs); $res = json_decode($raw_res, true); //error_log(print_r($res, true)); diff --git a/templates/job-order/form.html.twig b/templates/job-order/form.html.twig index fc7628bd..bc91ef46 100644 --- a/templates/job-order/form.html.twig +++ b/templates/job-order/form.html.twig @@ -379,18 +379,18 @@
| Outlet | +Hub | Branch | Distance | Travel Time | @@ -399,21 +399,21 @@Contact Numbers | ||
|---|---|---|---|---|---|---|---|
| {{ outlet.outlet.getName }} | -{{ outlet.outlet.getBranch }} | -{{ outlet.distance ? outlet.distance : '-' }} | -{{ outlet.duration ? outlet.duration : '-' }} | + {% for hub in hubs %} +||||
| {{ hub.hub.getName }} | +{{ hub.hub.getBranch }} | +{{ hub.distance ? hub.distance : '-' }} | +{{ hub.duration ? hub.duration : '-' }} | 0 | 0 | -{{ outlet.outlet.getContactNumbers }} | +{{ hub.hub.getContactNumbers }} |
|
@@ -653,14 +613,14 @@ $(function() {
{% endif %}
{% if mode == 'update-processing' %}
- // display outlet map
- var omap = new GMaps({
- div: '#outlet_map',
+ // display hub map
+ var hmap = new GMaps({
+ div: '#hub_map',
lat: {{ obj.getCoordinates.getLatitude }},
lng: {{ obj.getCoordinates.getLongitude }}
});
- omap.addMarker({
+ hmap.addMarker({
lat: {{ obj.getCoordinates.getLatitude }},
lng: {{ obj.getCoordinates.getLongitude }},
icon: '/assets/images/icon-destination.png',
@@ -668,12 +628,12 @@ $(function() {
content: "Destination"
});
- {% for outlet in outlets %}
- omap.addMarker({
- lat: {{ outlet.outlet.getCoordinates.getLatitude }},
- lng: {{ outlet.outlet.getCoordinates.getLongitude }},
- title: "{{ outlet.outlet.getName }}",
- content: "{{ outlet.outlet.getName }}",
+ {% for hub in hubs %}
+ hmap.addMarker({
+ lat: {{ hub.hub.getCoordinates.getLatitude }},
+ lng: {{ hub.hub.getCoordinates.getLongitude }},
+ title: "{{ hub.hub.getName }}",
+ content: "{{ hub.hub.getName }}",
icon: '/assets/images/icon-outlet.png'
});
{% endfor %}
@@ -695,8 +655,8 @@ $(function() {
fields['invoice_items'] = invoiceItems;
{% if mode == 'update-processing' %}
- // add selected outlet to data
- fields['outlet'] = selectedOutlet;
+ // add selected hub to data
+ fields['hub'] = selectedHub;
{% endif %}
{% if mode == 'update-assigning' %}
@@ -1049,21 +1009,21 @@ $(function() {
*/
{% if mode == 'update-processing' %}
- var selectedOutlet = '{{ obj.getOutlet ? obj.getOutlet.getID : "" }}';
+ var selectedHub = '{{ obj.getHub ? obj.getHub.getID : "" }}';
- $("#outlets-table tbody tr").click(function() {
+ $("#hubs-table tbody tr").click(function() {
var id = $(this).data('id');
var lat = $(this).data('lat');
var lng = $(this).data('lng');
- if (id != selectedOutlet) {
- // highlight this row, set outlet value
- $("#outlets-table").find('.m-table__row--primary').removeClass('m-table__row--primary');
+ if (id != selectedHub) {
+ // highlight this row, set hub value
+ $("#hubs-table").find('.m-table__row--primary').removeClass('m-table__row--primary');
$(this).addClass('m-table__row--primary');
// set value
- selectedOutlet = id;
+ selectedHub = id;
// center the map
omap.setCenter(lat, lng);
@@ -1072,7 +1032,7 @@ $(function() {
$(this).removeClass('m-table__row--primary');
// remove id value
- selectedOutlet = '';
+ selectedHub = '';
}
});
{% endif %}
@@ -1084,7 +1044,7 @@ $(function() {
var id = $(this).data('id');
if (id != selectedRider) {
- // highlight this row, set outlet value
+ // highlight this row, set hub value
$("#riders-table").find('.m-table__row--primary').removeClass('m-table__row--primary');
$(this).addClass('m-table__row--primary');
diff --git a/templates/user/form.html.twig b/templates/user/form.html.twig
index 05b4f03c..e648a032 100644
--- a/templates/user/form.html.twig
+++ b/templates/user/form.html.twig
@@ -113,7 +113,8 @@
{% endif %}
{% if mode != 'profile' %}
-
+
+
+
+ Roles @@ -138,6 +139,29 @@
+
{% endif %}
+
+ + Hub Assignment ++
+
+
+
+
+ {% for hub in hubs %}
+
+ {% endfor %}
+
+
+ Check all hubs assigned to this user
+ |