custom/plugins/SwagB2bPlatform/components/Budget/BridgePlatform/BudgetTransactionOrderSync.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Budget\BridgePlatform;
  3. use Shopware\B2B\Budget\Framework\BudgetService;
  4. use Shopware\B2B\Budget\Framework\InsufficientBudgetException;
  5. use Shopware\B2B\Currency\Framework\CurrencyService;
  6. use Shopware\B2B\Order\BridgePlatform\OrderContextStateChangedEvent;
  7. use Shopware\B2B\Order\Framework\OrderContextRepository;
  8. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  9. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class BudgetTransactionOrderSync implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var AuthenticationService
  15.      */
  16.     private $authenticationService;
  17.     /**
  18.      * @var CurrencyService
  19.      */
  20.     private $currencyService;
  21.     /**
  22.      * @var OrderContextRepository
  23.      */
  24.     private $orderContextRepository;
  25.     /**
  26.      * @var BudgetService
  27.      */
  28.     private $budgetService;
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CheckoutOrderPlacedEvent::class => 'addTransaction',
  33.             OrderContextStateChangedEvent::class => 'updateTransactionStatus',
  34.         ];
  35.     }
  36.     public function __construct(
  37.         AuthenticationService $authenticationService,
  38.         OrderContextRepository $orderContextRepository,
  39.         BudgetService $budgetService,
  40.         CurrencyService $currencyService
  41.     ) {
  42.         $this->authenticationService $authenticationService;
  43.         $this->currencyService $currencyService;
  44.         $this->orderContextRepository $orderContextRepository;
  45.         $this->budgetService $budgetService;
  46.     }
  47.     public function addTransaction(CheckoutOrderPlacedEvent $event): void
  48.     {
  49.         if (!$this->authenticationService->isB2b()) {
  50.             return;
  51.         }
  52.         $order $event->getOrder();
  53.         $currencyContext $this->currencyService->createCurrencyContext();
  54.         $identity $this->authenticationService->getIdentity();
  55.         $amount $order->getAmountNet() + $order->getShippingTotal();
  56.         $orderContext $this->orderContextRepository
  57.             ->fetchOneOrderContextByOrderNumber($order->getOrderNumber());
  58.         try {
  59.             $this->budgetService
  60.                 ->addTransaction($orderContext$amount$currencyContext$identity->getOwnershipContext());
  61.         } catch (InsufficientBudgetException $e) {
  62.             if (!$identity->isSuperAdmin()) {
  63.                 throw $e;
  64.             }
  65.         }
  66.     }
  67.     public function updateTransactionStatus(OrderContextStateChangedEvent $event): void
  68.     {
  69.         $this->budgetService->updateTransactionStatus($event->getOrderContext());
  70.     }
  71. }