Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
r0xsh committed Nov 7, 2023
1 parent 63b95d9 commit 6df464b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Pricing/PricingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AppBundle\Sylius\Customer\CustomerInterface;
use AppBundle\Sylius\Order\OrderFactory;
use AppBundle\Sylius\Order\OrderInterface;
use AppBundle\Sylius\Order\OrderItemInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -57,4 +58,54 @@ public function createOrder(Delivery $delivery): ?OrderInterface

return null;
}

//TODO: Create a updateOrder method, should dispatch a Order::Recalculated event to keep track of changes price
// Check if the order is already payed
public function updateOrder(Delivery $delivery): ?OrderInterface
{
/** @var ?OrderInterface $order */
$order = $delivery->getOrder();

if ($order === null) {
throw new \InvalidArgumentException('Order does not exist.');
}

// Check if the order is already paid
if ($order->hasPayments()) {
throw new \LogicException('Cannot update a paid order.');
}

$store = $delivery->getStore();

if (null !== $store && $store->getCreateOrders()) {
$price = $this->deliveryManager->getPrice($delivery, $store->getPricingRuleSet());

if (null === $price) {
$this->logger->error('Price could not be calculated');

return null;
}

$price = (int) $price;

assert($order->getItems()->count() === 1);
$order->getItems()->map(function ($item) use ($price) {
$item->setUnitPrice($price);
});

$order->recalculateItemsTotal();
$order->recalculateAdjustmentsTotal();

$this->entityManager->persist($order);
$this->entityManager->flush();

}
// Perform the necessary updates to the order
// ...

// Dispatch the Order::Recalculated event
$event = new OrderRecalculatedEvent($order);

Check failure on line 107 in src/Pricing/PricingManager.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.1)

Instantiated class AppBundle\Pricing\OrderRecalculatedEvent not found.
$this->eventDispatcher->dispatch($event, OrderRecalculatedEvent::NAME);

Check failure on line 108 in src/Pricing/PricingManager.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.1)

Access to constant NAME on an unknown class AppBundle\Pricing\OrderRecalculatedEvent.
return null;
}
}
5 changes: 5 additions & 0 deletions src/Service/DeliveryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function getPrice(Delivery $delivery, PricingRuleSet $ruleSet)

if (count($delivery->getTasks()) > 2 || $ruleSet->hasOption(PricingRuleSet::OPTION_MAP_ALL_TASKS)) {
foreach ($delivery->getTasks() as $task) {
//TODO: continue on task cancelled
if ($task->getStatus() === Task::STATUS_CANCELLED) {
$this->logger->info(sprintf('Skipping cancelled task "%d"', $task->getId()));
continue;
}
foreach ($ruleSet->getRules() as $rule) {
if ($task->matchesPricingRule($rule, $this->expressionLanguage)) {

Expand Down
30 changes: 30 additions & 0 deletions src/Sylius/Order/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,34 @@ public function createForDelivery(Delivery $delivery, int $price, ?CustomerInter

return $order;
}

//TODO: Create a updateOrder method, should dispatch a Order::Recalculated event to keep track of changes price
/**
* Update the order and dispatch the Order::Recalculated event.
*
* @param Delivery $delivery The delivery associated with the order.
* @return OrderInterface|null The updated order or null if the order cannot be updated.
* @throws \LogicException If the order is already paid.
*/
public function updateOrder(Delivery $delivery): ?OrderInterface
{
$order = $delivery->getOrder();
if (null === $order) {
throw new \InvalidArgumentException('Order does not exist.');
}

// Check if the order is already paid
if ($order->hasPayments()) {
throw new \LogicException('Cannot update a paid order.');
}

// Perform the necessary updates to the order
// ...

// Dispatch the Order::Recalculated event
$event = new OrderRecalculatedEvent($order);

Check failure on line 152 in src/Sylius/Order/OrderFactory.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.1)

Instantiated class AppBundle\Sylius\Order\OrderRecalculatedEvent not found.
$this->eventDispatcher->dispatch($event, OrderRecalculatedEvent::NAME);

Check failure on line 153 in src/Sylius/Order/OrderFactory.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.1)

Access to constant NAME on an unknown class AppBundle\Sylius\Order\OrderRecalculatedEvent.

return $order;
}
}

0 comments on commit 6df464b

Please sign in to comment.