Merge branch '360-cmb-allow-reordering-of-rider-jo' into '270-final-cmb-fixes'

Resolve "CMB: Allow reordering of rider JO"

See merge request jankstudio/resq!403
This commit is contained in:
Kendrick Chan 2020-03-02 00:35:04 +00:00
commit 7124a7d2c8
6 changed files with 116 additions and 2 deletions

View file

@ -46,3 +46,13 @@ rider_active_jo:
path: /riders/{id}/activejo/{jo_id}
controller: App\Controller\RiderController::riderActiveJO
methods: [GET]
rider_priority_up_jo:
path: /riders/{id}/priority_up/{jo_id}
controller: App\Controller\RiderController::priorityUpJO
methods: [GET]
rider_priority_down_jo:
path: /riders/{id}/priority_down/{jo_id}
controller: App\Controller\RiderController::priorityDownJO
methods: [GET]

View file

@ -534,6 +534,66 @@ class RiderController extends Controller
$mclient->sendRiderEvent($jo, $payload);
return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]);
}
/**
* @ParamConverter("rider", class="App\Entity\Rider")
* @ParamConverter("jo", class="App\Entity\JobOrder", options={"id": "jo_id"})
*/
public function priorityUpJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo)
{
$jos = $rider->getOpenJobOrders();
// set new priority
$old_prio = $jo->getPriority();
$new_prio = $old_prio - 1;
$jo->setPriority($new_prio);
// go through all rider open JOs and set priority when needed
foreach ($jos as $rider_jo)
{
// check if it's the same
if ($rider_jo->getID() == $jo->getID())
continue;
// if priority is the same as old priority, move it down
if ($new_prio == $rider_jo->getPriority())
$rider_jo->setPriority($rider_jo->getPriority() + 1);
}
$em->flush();
return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]);
}
/**
* @ParamConverter("rider", class="App\Entity\Rider")
* @ParamConverter("jo", class="App\Entity\JobOrder", options={"id": "jo_id"})
*/
public function priorityDownJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo)
{
$jos = $rider->getOpenJobOrders();
// set new priority
$old_prio = $jo->getPriority();
$new_prio = $old_prio + 1;
$jo->setPriority($new_prio);
// go through all rider open JOs and set priority when needed
foreach ($jos as $rider_jo)
{
// check if it's the same
if ($rider_jo->getID() == $jo->getID())
continue;
// if priority is the same as old priority, move it down
if ($new_prio == $rider_jo->getPriority())
$rider_jo->setPriority($rider_jo->getPriority() - 1);
}
$em->flush();
return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]);
}
}

View file

@ -280,6 +280,14 @@ class JobOrder
*/
protected $hub_rejections;
// priority order for riders
// NOTE: this is a workaround since changeing rider to jo rider assignment with details requires
// too many changes and may break too many things.
/**
* @ORM\Column(type="integer", options={"default": 0}))
*/
protected $priority;
// meta
/**
* @ORM\Column(type="json")
@ -304,6 +312,7 @@ class JobOrder
$this->flag_rider_rating = false;
$this->flag_coolant = false;
$this->priority = 0;
$this->meta = [];
}
@ -811,6 +820,17 @@ class JobOrder
return $this->hub_rejections;
}
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
public function getPriority()
{
return $this->priority;
}
public function addMeta($id, $value)
{
$this->meta[$id] = $value;
@ -825,5 +845,4 @@ class JobOrder
return $this->meta[$id];
}
}

View file

@ -61,6 +61,7 @@ class Rider
// job orders that the rider has done
/**
* @ORM\OneToMany(targetEntity="JobOrder", mappedBy="rider")
* @ORM\OrderBy({"priority" = "ASC"})
*/
protected $job_orders;

View file

@ -523,6 +523,19 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
}
}
// set priority based on rider's existing open job orders
$rider_jos = $rider->getOpenJobOrders();
// get maximum priority then add 1
// NOTE: this can be a bit buggy due to concurrency issues
// ideally have to lock jo table, but that isn't feasible right now
$priority = 0;
foreach ($rider_jos as $rider_jo)
{
if ($priority < $rider_jo->getPriority())
$priority = $rider_jo->getPriority() + 1;
}
// get discount and set to meta
$discount = $req->request->get('invoice_discount', []);
@ -560,7 +573,8 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
->setModeOfPayment($req->request->get('mode_of_payment'))
->setLandmark($req->request->get('landmark'))
->setHub($hub)
->setRider($rider);
->setRider($rider)
->setPriority($priority);
$jo->addMeta('discount', $discount);
$jo->addMeta('service_charges', $service_charges);

View file

@ -180,6 +180,16 @@
<td>{{ jo.getDeliveryAddress|default('') }}</td>
<td>{% if jo.getID == active_jo_id %}<span class="m-badge m-badge--success m-badge--wide">Active</span>{% endif %}</td>
<td>
<a href="{{ url('rider_priority_up_jo', {'id': obj.getID, 'jo_id': jo.getID}) }}" class="btn m-btn m-btn--icon">
<span>
<i class="fa fa-angle-up"></i>
</span>
</a>
<a href="{{ url('rider_priority_down_jo', {'id': obj.getID, 'jo_id': jo.getID}) }}" class="btn m-btn m-btn--icon">
<span>
<i class="fa fa-angle-down"></i>
</span>
</a>
{% if jo.getID != active_jo_id %}
<!-- TODO: make this submit via ajax post -->
<a href="{{ url('rider_active_jo', {'id': obj.getID, 'jo_id': jo.getID}) }}" class="btn btn-danger m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">