Skip to content

Commit

Permalink
Merge pull request #4877 from coopcycle/reset-index-on-packages
Browse files Browse the repository at this point in the history
Fix crash when adding a package to a package set where a package was previously deleted
  • Loading branch information
Atala authored Feb 11, 2025
2 parents 970a2f7 + 27f90d2 commit ce10051
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/Doctrine/EventSubscriber/TaskSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ private function handleStateChangesForTasks(EntitymanagerInterface $em, array $t
continue;
}

// if all tasks of a delivery are cancelled, cancel the linked order
$tasks = $delivery->getTasks();
$cancelOrder = true;
foreach ($tasks as $task) {
Expand All @@ -233,7 +234,8 @@ private function handleStateChangesForTasks(EntitymanagerInterface $em, array $t
}
}

if ($cancelOrder && $order->getState() !== OrderInterface::STATE_CANCELLED) {
// do not cancel order if order is "refused"
if ($cancelOrder && $order->getState() !== OrderInterface::STATE_CANCELLED && $order->getState() !== OrderInterface::STATE_REFUSED) {
$this->orderManager->cancel($order, 'All tasks were cancelled');
$em->flush();
}
Expand Down
5 changes: 4 additions & 1 deletion src/Entity/PackageSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ public function setName($name)
*/
public function getPackages()
{
return $this->packages->filter(
$filtered = $this->packages->filter(
function ($package) {
return !$package->isDeleted();
}
);

// reset index after filtering
return new ArrayCollection(array_values($filtered->toArray()));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Entity/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Store extends LocalBusiness implements TaggableInterface, OrganizationAwar
/**
* The deliveries of this store will be linked by default to this rider
* @var User
*/
*/
private $defaultCourier;

protected string $billingMethod = 'unit';
Expand All @@ -219,7 +219,8 @@ class Store extends LocalBusiness implements TaggableInterface, OrganizationAwar
*/
protected ?string $storeGLN = null;

public function __construct() {
public function __construct()
{
$this->deliveries = new ArrayCollection();
$this->owners = new ArrayCollection();
$this->addresses = new ArrayCollection();
Expand Down Expand Up @@ -552,7 +553,7 @@ public function setMultiDropEnabled($multiDropEnabled)

public function getTimeSlots()
{
return $this->timeSlots->map(fn (StoreTimeSlot $sts): TimeSlot => $sts->getTimeSlot());
return $this->timeSlots->map(fn(StoreTimeSlot $sts): TimeSlot => $sts->getTimeSlot());
}

public function setTimeSlots($timeSlots): void
Expand Down Expand Up @@ -611,13 +612,13 @@ public function setTimeSlots($timeSlots): void
public function getPackages()
{
if (null !== $this->packageSet) {
return array_values($this->packageSet->getPackages()->toArray());
return $this->packageSet->getPackages();
}

return [];
}

public function isTransporterEnabled(): bool
public function isTransporterEnabled(): bool
{
return !is_null($this->transporter);
}
Expand Down Expand Up @@ -672,5 +673,4 @@ public function getStoreGLN(): ?string
{
return $this->storeGLN;
}

}

0 comments on commit ce10051

Please sign in to comment.