vendor/store.shopware.com/swagb2bplatform/components/Budget/BridgePlatform/BudgetPreferenceSubscriber.php line 71

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Budget\BridgePlatform;
  3. use Shopware\B2B\Budget\Framework\BudgetRepository;
  4. use Shopware\B2B\Budget\Framework\BudgetService;
  5. use Shopware\B2B\Common\IdValue;
  6. use Shopware\B2B\Common\Repository\NotFoundException;
  7. use Shopware\B2B\Currency\Framework\CurrencyService;
  8. use Shopware\B2B\Order\BridgePlatform\OrderContextCreatedEvent;
  9. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  10. use Shopware\B2B\StoreFrontAuthentication\Framework\OwnershipContext;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class BudgetPreferenceSubscriber implements EventSubscriberInterface
  14. {
  15.     private CurrencyService $currencyService;
  16.     private BudgetService $budgetService;
  17.     private AuthenticationService $authenticationService;
  18.     private BudgetRepository $budgetRepository;
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CheckoutConfirmPageLoadedEvent::class => 'updateCartPreference',
  23.             OrderContextCreatedEvent::class => 'addBudgetPreference',
  24.         ];
  25.     }
  26.     public function __construct(
  27.         AuthenticationService $authenticationService,
  28.         BudgetRepository $budgetRepository,
  29.         CurrencyService $currencyService,
  30.         BudgetService $budgetService
  31.     ) {
  32.         $this->authenticationService $authenticationService;
  33.         $this->budgetRepository $budgetRepository;
  34.         $this->currencyService $currencyService;
  35.         $this->budgetService $budgetService;
  36.     }
  37.     public function updateCartPreference(CheckoutConfirmPageLoadedEvent $event): void
  38.     {
  39.         if (!$this->authenticationService->isB2b()) {
  40.             return;
  41.         }
  42.         $ownershipContext $this->authenticationService->getIdentity()->getOwnershipContext();
  43.         try {
  44.             $this->budgetRepository->fetchOrderBudgetPreferenceByCart($ownershipContext);
  45.             return;
  46.         } catch (NotFoundException $e) {
  47.             //nth
  48.         }
  49.         $amount $event->getPage()->getCart()->getPrice()->getNetPrice();
  50.         $context $this->authenticationService->getIdentity()->getOwnershipContext();
  51.         $budgetId $this->findPreferredBudgetId($context$amount);
  52.         $this->budgetRepository
  53.             ->setOrderBudgetPreferenceByCart($budgetId);
  54.     }
  55.     public function addBudgetPreference(OrderContextCreatedEvent $event): void
  56.     {
  57.         $orderContext $event->getOrderContext();
  58.         $ownershipContext $this->authenticationService->getIdentity()->getOwnershipContext();
  59.         try {
  60.             $budgetId $this->budgetRepository
  61.                 ->fetchOrderBudgetPreferenceByCart($ownershipContext);
  62.         } catch (NotFoundException $e) {
  63.             return;
  64.         }
  65.         $this->budgetRepository
  66.             ->setOrderBudgetPreferenceByOrderContextId($orderContext->id$budgetId);
  67.         $this->budgetRepository
  68.             ->setOrderBudgetPreferenceByCart(IdValue::null());
  69.     }
  70.     /**
  71.      * @internal
  72.      */
  73.     protected function findPreferredBudgetId(OwnershipContext $contextfloat $amount): IdValue
  74.     {
  75.         $currencyContext $this->currencyService->createCurrencyContext();
  76.         $budgets $this->budgetService
  77.             ->getUserSelectableBudgetsWithStatus($context$amount$currencyContext);
  78.         $budgetId IdValue::null();
  79.         foreach ($budgets as $budget) {
  80.             if ($budget->currentStatus->isSufficient) {
  81.                 $budgetId $budget->id;
  82.                 break;
  83.             }
  84.         }
  85.         return $budgetId;
  86.     }
  87. }