custom/plugins/SwagB2bPlatform/components/Budget/BridgePlatform/BudgetPreferenceSubscriber.php line 83

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.     /**
  16.      * @var CurrencyService
  17.      */
  18.     private $currencyService;
  19.     /**
  20.      * @var BudgetService
  21.      */
  22.     private $budgetService;
  23.     /**
  24.      * @var AuthenticationService
  25.      */
  26.     private $authenticationService;
  27.     /**
  28.      * @var BudgetRepository
  29.      */
  30.     private $budgetRepository;
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CheckoutConfirmPageLoadedEvent::class => 'updateCartPreference',
  35.             OrderContextCreatedEvent::class => 'addBudgetPreference',
  36.         ];
  37.     }
  38.     public function __construct(
  39.         AuthenticationService $authenticationService,
  40.         BudgetRepository $budgetRepository,
  41.         CurrencyService $currencyService,
  42.         BudgetService $budgetService
  43.     ) {
  44.         $this->authenticationService $authenticationService;
  45.         $this->budgetRepository $budgetRepository;
  46.         $this->currencyService $currencyService;
  47.         $this->budgetService $budgetService;
  48.     }
  49.     public function updateCartPreference(CheckoutConfirmPageLoadedEvent $event): void
  50.     {
  51.         if (!$this->authenticationService->isB2b()) {
  52.             return;
  53.         }
  54.         $ownershipContext $this->authenticationService->getIdentity()->getOwnershipContext();
  55.         try {
  56.             $this->budgetRepository->fetchOrderBudgetPreferenceByCart($ownershipContext);
  57.             return;
  58.         } catch (NotFoundException $e) {
  59.             //nth
  60.         }
  61.         $amount $event->getPage()->getCart()->getPrice()->getNetPrice();
  62.         $context $this->authenticationService->getIdentity()->getOwnershipContext();
  63.         $budgetId $this->findPreferredBudgetId($context$amount);
  64.         $this->budgetRepository
  65.             ->setOrderBudgetPreferenceByCart($budgetId);
  66.     }
  67.     public function addBudgetPreference(OrderContextCreatedEvent $event): void
  68.     {
  69.         $orderContext $event->getOrderContext();
  70.         $ownershipContext $this->authenticationService->getIdentity()->getOwnershipContext();
  71.         try {
  72.             $budgetId $this->budgetRepository
  73.                 ->fetchOrderBudgetPreferenceByCart($ownershipContext);
  74.         } catch (NotFoundException $e) {
  75.             return;
  76.         }
  77.         $this->budgetRepository
  78.             ->setOrderBudgetPreferenceByOrderContextId($orderContext->id$budgetId);
  79.         $this->budgetRepository
  80.             ->setOrderBudgetPreferenceByCart(IdValue::null());
  81.     }
  82.     /**
  83.      * @internal
  84.      */
  85.     protected function findPreferredBudgetId(OwnershipContext $contextfloat $amount): IdValue
  86.     {
  87.         $currencyContext $this->currencyService->createCurrencyContext();
  88.         $budgets $this->budgetService
  89.             ->getUserSelectableBudgetsWithStatus($context$amount$currencyContext);
  90.         $budgetId IdValue::null();
  91.         foreach ($budgets as $budget) {
  92.             if ($budget->currentStatus->isSufficient) {
  93.                 $budgetId $budget->id;
  94.                 break;
  95.             }
  96.         }
  97.         return $budgetId;
  98.     }
  99. }