custom/plugins/WbfkMultipleTransactions/src/Subscriber/OrderLoadedSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\MultipleTransactions\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Price\CashRounding;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionCollection;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  7. use Shopware\Core\Checkout\Order\OrderEntity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Wbfk\MultipleTransactions\Core\Checkout\Order\TransactionsSummary;
  12. class OrderLoadedSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(private readonly CashRounding $cashRounding)
  15.     {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             'order.loaded' => 'calculateTransactionsSummary',
  21.         ];
  22.     }
  23.     public function calculateTransactionsSummary(EntityLoadedEvent $event): void
  24.     {
  25.         $orders $event->getEntities();
  26.         foreach ($orders as $order) {
  27.             $this->addTransactionsSummaryAsExtension($order);
  28.         }
  29.     }
  30.     private function addTransactionsSummaryAsExtension(OrderEntity $order): void
  31.     {
  32.         $alreadyPaid 0.0;
  33.         if (!$order->getTransactions()) {
  34.             return;
  35.         }
  36.         foreach ($order->getTransactions() as $transaction) {
  37.             if ($transaction->getStateMachineState()->getTechnicalName() !== OrderTransactionStates::STATE_PAID) {
  38.                 continue;
  39.             }
  40.             $alreadyPaid += $transaction->getAmount()->getTotalPrice();
  41.         }
  42.         $balance $order->getAmountTotal() - $alreadyPaid;
  43.         $cashRoundingConfig = new CashRoundingConfig(20.01true);
  44.         $alreadyPaid $this->cashRounding->cashRound($alreadyPaid$cashRoundingConfig);
  45.         $balance $this->cashRounding->cashRound($balance$cashRoundingConfig);
  46.         $transactionsSummary = new TransactionsSummary();
  47.         $transactionsSummary->alreadyPaid $alreadyPaid;
  48.         $transactionsSummary->balance $balance;
  49.         $transactionsSummary->transactions $order->getTransactions();
  50.         $order->addExtension('transactionsSummary'$transactionsSummary);
  51.     }
  52. }