vendor/store.shopware.com/swagb2bplatform/components/Budget/BridgePlatform/BudgetTransactionOrderSync.php line 69

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.     private AuthenticationService $authenticationService;
  14.     private CurrencyService $currencyService;
  15.     private OrderContextRepository $orderContextRepository;
  16.     private BudgetService $budgetService;
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             CheckoutOrderPlacedEvent::class => 'addTransaction',
  21.             OrderContextStateChangedEvent::class => 'updateTransactionStatus',
  22.         ];
  23.     }
  24.     public function __construct(
  25.         AuthenticationService $authenticationService,
  26.         OrderContextRepository $orderContextRepository,
  27.         BudgetService $budgetService,
  28.         CurrencyService $currencyService
  29.     ) {
  30.         $this->authenticationService $authenticationService;
  31.         $this->currencyService $currencyService;
  32.         $this->orderContextRepository $orderContextRepository;
  33.         $this->budgetService $budgetService;
  34.     }
  35.     public function addTransaction(CheckoutOrderPlacedEvent $event): void
  36.     {
  37.         if (!$this->authenticationService->isB2b()) {
  38.             return;
  39.         }
  40.         $order $event->getOrder();
  41.         $currencyContext $this->currencyService->createCurrencyContext();
  42.         $identity $this->authenticationService->getIdentity();
  43.         $amount $order->getAmountNet() + $order->getShippingTotal();
  44.         $orderContext $this->orderContextRepository
  45.             ->fetchOneOrderContextByOrderNumber($order->getOrderNumber());
  46.         try {
  47.             $this->budgetService
  48.                 ->addTransaction($orderContext$amount$currencyContext$identity->getOwnershipContext());
  49.         } catch (InsufficientBudgetException $e) {
  50.             if (!$identity->isSuperAdmin()) {
  51.                 throw $e;
  52.             }
  53.         }
  54.     }
  55.     public function updateTransactionStatus(OrderContextStateChangedEvent $event): void
  56.     {
  57.         $this->budgetService->updateTransactionStatus($event->getOrderContext());
  58.     }
  59. }