Set priority properly for job order and handle priority down button #360

This commit is contained in:
Kendrick Chan 2020-03-02 08:33:50 +08:00
parent 07753274c3
commit 977559b88d
2 changed files with 37 additions and 8 deletions

View file

@ -543,16 +543,14 @@ class RiderController extends Controller
*/
public function priorityUpJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo)
{
error_log("HERE");
error_log($rider->getID());
error_log($jo->getID());
$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
@ -575,9 +573,26 @@ class RiderController extends Controller
*/
public function priorityDownJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo)
{
error_log("HERE");
error_log($rider->getID());
error_log($jo->getID());
$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

@ -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);