Skip to content

Commit

Permalink
Merge pull request #4768 from coopcycle/fix-auto-assign
Browse files Browse the repository at this point in the history
Fix order of assigned tasks when using the feature "Automatically assign store tasks to a rider"
  • Loading branch information
r0xsh authored Nov 28, 2024
2 parents 24058e5 + 8a3854c commit 41cb8a4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function getTaskList(Task $task, UserInterface $courier)
$taskListRepository = $this->objectManager->getRepository(TaskList::class);

// FIXME
// Using $task->getDoneBefore() causes problems with tasks spanning over several days
// Here it would assign the task to 2 distinct task lists
// 1. if task->assignedOn is set, we have explictly set the assignment date -> good, we get the proper TaskList
// 2. if not use the doneBefore date as default, but in this case their might be problems with tasks spanning on multiple days
// @see https://github.com/coopcycle/coopcycle-web/issues/874
$date = null !== $task->getAssignedOn() ? $task->getAssignedOn() : $task->getDoneBefore();

Expand Down
3 changes: 2 additions & 1 deletion src/Doctrine/EventSubscriber/TourSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function getSubscribedEvents()
}

/**
* Trickles down the assignment information from tour to tasks
* Trickles down the assignment information from tour to tasks.
* The tour is linked to the TaskList as an item, now we want to fill task.assignedTo
*/
public function onFlush(OnFlushEventArgs $args)
{
Expand Down
14 changes: 10 additions & 4 deletions src/Entity/Listener/DeliveryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace AppBundle\Entity\Listener;

use AppBundle\Doctrine\EventSubscriber\TaskSubscriber\TaskListProvider;
use AppBundle\Entity\Delivery;
use AppBundle\Entity\Task;
use AppBundle\Entity\TaskList\Item;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LifecycleEventArgs;

class DeliveryListener
{
public function __construct(protected EntityManagerInterface $entityManager)
public function __construct(
protected EntityManagerInterface $entityManager,
protected TaskListProvider $taskListProvider
)
{

}
Expand Down Expand Up @@ -65,10 +70,11 @@ public function postPersist(Delivery $delivery)
$courier = $delivery->getStore()->getDefaultCourier();

foreach ($delivery->getTasks() as $task) {
$task->assignTo($courier, $task->getBefore());
$this->entityManager->persist($task);
$taskList = $this->taskListProvider->getTaskList($task, $courier);
$taskList->appendTask($task);
$this->entityManager->persist($taskList);
}

$this->entityManager->flush();
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Entity/TaskList.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ public function addItem(Item $item) {
$item->setParent($this);
}

public function appendTask(Task $task) {
$item = new Item();
$item->setTask($task);
$item->setPosition($this->getItems()->count());
$this->addItem($item);

$task->assignTo($this->getCourier(), $this->getDate());

return $this;
}

/**
* Clear the assigned items
*/
Expand Down

0 comments on commit 41cb8a4

Please sign in to comment.