custom/static-plugins/CioPaymentMethodCostcenter/src/Subscriber/CheckoutSubscriber.php line 113

Open in your IDE?
  1. <?php
  2. namespace CioPaymentMethodCostcenter\Subscriber;
  3. use CioCostcenter\Definition\Costcenter\CostcenterEntity;
  4. use CioCostcenter\Definition\CostcenterAddress\CostcenterAddressEntity;
  5. use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
  6. use CioCustomerPermissionGroups\Service\CustomerPermissionService;
  7. use CioPaymentMethodCostcenter\Controller\ChooseAddressController;
  8. use CioPaymentMethodCostcenter\Error\OnlyCostcenterAddressError;
  9. use Shopware\Core\Checkout\Cart\Cart;
  10. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  11. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  12. use Shopware\Core\Checkout\Customer\CustomerEntity;
  13. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  20. use Shopware\Core\Framework\Event\ShopwareSalesChannelEvent;
  21. use Shopware\Core\Framework\Uuid\Uuid;
  22. use Shopware\Storefront\Event\StorefrontRenderEvent;
  23. use Symfony\Component\DependencyInjection\Container;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\RequestStack;
  27. use Symfony\Contracts\EventDispatcher\Event;
  28. class CheckoutSubscriber implements EventSubscriberInterface
  29. {
  30.     private ?Request $request;
  31.     private EntityRepositoryInterface $orderRepository;
  32.     private EntityRepository $customerAddressRepository;
  33.     private EntityRepository $customerRepository;
  34.     private EntityRepository $costcenterAddressRepository;
  35.     private EntityRepository $costcenterRepository;
  36.     private Container $container;
  37.     private CustomerPermissionService $checkCustomerPermissionsService;
  38.     public function __construct(RequestStack $requestStackEntityRepository $orderRepositoryEntityRepository $customerAddressRepositoryEntityRepository $customerRepositoryEntityRepository $costcenterAddressRepositoryEntityRepository $costcenterRepositoryContainer $containerCustomerPermissionService $checkCustomerPermissionsService)
  39.     {
  40.         $this->request $requestStack->getCurrentRequest();
  41.         $this->orderRepository $orderRepository;
  42.         $this->customerAddressRepository $customerAddressRepository;
  43.         $this->customerRepository $customerRepository;
  44.         $this->costcenterAddressRepository $costcenterAddressRepository;
  45.         $this->costcenterRepository $costcenterRepository;
  46.         $this->container $container;
  47.         $this->checkCustomerPermissionsService $checkCustomerPermissionsService;
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  52.         return [
  53.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlacedEvent',
  54.             StorefrontRenderEvent::class => 'onStorefrontRender',
  55.             CustomerLogoutEvent::class => 'onCustomerLogoutEvent',
  56.             CartConvertedEvent::class => 'onCartConvertedEvent',
  57.             CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent'
  58.         ];
  59.     }
  60.     public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
  61.     {
  62.         $event->addRoles([
  63.             [
  64.                 'title' => 'CHECKOUT_CAN_USE_PROFILE_ADDRESS',
  65.                 'description' => 'Kunde kann im Profil gepflegte Adressen verwenden.'
  66.             ],
  67.             [
  68.                 'title' => 'CHECKOUT_CAN_USE_COSTCENTER_ADDRESS',
  69.                 'description' => 'Kunde kann die seinen Kostenstellen zugehörigen Adressen als Lieferadresse verwenden.'
  70.             ],
  71.             [
  72.                 'title' => 'ALL_COSTCODES',
  73.                 'description' => 'Kunde kann im Checkout nicht nur seine im Account hinterlegten Kostenstellen verwenden.'
  74.             ]
  75.         ]);
  76.     }
  77.     public function onCheckoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event)
  78.     {
  79.         $customFields = [];
  80.         if ($currentCustomFields $event->getOrder()->getCustomFields()) {
  81.             if (is_array($currentCustomFields)) {
  82.                 $customFields $currentCustomFields;
  83.             }
  84.         }
  85.         if ($this->request) {
  86.             $customFields array_merge($customFields, [
  87.                 'cio_costcenter' => $this->request->request->get('cio_costcenter')
  88.             ]);
  89.         }
  90.         $event->getOrder()->setCustomFields($customFields);
  91.         try {
  92.             $this->orderRepository->update([
  93.                 [
  94.                     'id' => $event->getOrder()->getId(),
  95.                     'customFields' => $customFields
  96.                 ]
  97.             ], $event->getContext());
  98.         } catch (\Throwable $e) {
  99.         }
  100.     }
  101.     public function onStorefrontRender(StorefrontRenderEvent $event)
  102.     {
  103.         $route $event->getRequest()->get('_route');
  104.         $eventParams $event->getParameters();
  105.         if ($route == 'frontend.checkout.confirm.page') {
  106.             $session $event->getRequest()->getSession();
  107.             $currentCustomer $this->getCurrentCustomer($event);
  108.             $costcenterAdresses = [];
  109.             $costcenters = [];
  110.             $personallyCostcentersOfCustomer = [];
  111.             $costcenterBillingAdresses = [];
  112.             $overrideBillingAddress null;
  113.             $defaultBillingAddress $currentCustomer->getDefaultBillingAddress();
  114.             $currentlySelectedOverrideBillingAddress null;
  115.             /** @var Cart $cart */
  116.             $cart $event->getParameters()['page']->getCart();
  117.             if ($currentCustomer instanceof CustomerEntity) {
  118.                 if ($currentCustomer->getExtension('costcenters') instanceof EntityCollection) {
  119.                     $costcenters $currentCustomer->getExtension('costcenters')->getElements();
  120.                     $personallyCostcentersOfCustomer $costcenters;
  121.                 }
  122.                 if ($this->checkCustomerPermissionsService->check($event->getSalesChannelContext()->getCustomer(), 'ALL_COSTCODES'$event->getContext()) === true) {
  123.                     $costcenters $this->costcenterRepository->search(new Criteria(), $event->getContext())->getElements();
  124.                 }
  125.                 usort($costcenters, function (CostcenterEntity $aCostcenterEntity $b) {
  126.                     /** @var $a CostcenterEntity */
  127.                     /** @var $b CostcenterEntity */
  128.                     return strcmp($a->getNumber(), $b->getNumber());
  129.                 });
  130.                 usort($personallyCostcentersOfCustomer, function (CostcenterEntity $aCostcenterEntity $b) {
  131.                     /** @var $a CostcenterEntity */
  132.                     /** @var $b CostcenterEntity */
  133.                     return strcmp($a->getNumber(), $b->getNumber());
  134.                 });
  135.                 // load all costcenter addresses for costcenter
  136.                 $costcenterIds = [];
  137.                 foreach ($costcenters as $costcenter) {
  138.                     if ($costcenter instanceof CostcenterEntity) {
  139.                         $costcenterIds[] = $costcenter->getId();
  140.                     }
  141.                 }
  142.                 $criteria = new Criteria();
  143.                 $criteria->addFilter(new EqualsAnyFilter('costcenterId'$costcenterIds));
  144.                 $criteria->addAssociation('country');
  145.                 $costcenterAddressStorage = [];
  146.                 /** @var CostcenterAddressEntity $costcenterAddress */
  147.                 foreach ($this->costcenterAddressRepository->search($criteria$event->getContext())->getElements() as $costcenterAddress) {
  148.                     if (!isset($costcenterAddressStorage[$costcenterAddress->getCostcenterId()])) {
  149.                         $costcenterAddressStorage[$costcenterAddress->getCostcenterId()] = [];
  150.                     }
  151.                     $costcenterAddressStorage[$costcenterAddress->getCostcenterId()][] = $costcenterAddress;
  152.                 }
  153.                 /** @var CostcenterEntity $costcenter */
  154.                 foreach ($costcenters as $costcenter) {
  155.                     $costcenterCompany $costcenter->getCompany();
  156.                     $costcenterBillingAdresses[$costcenter->getNumber()] = [
  157.                         'id' => $costcenterCompany->getId(),
  158.                         'costcenterNummer' => $costcenter->getNumber(),
  159.                         'salutationName' => $costcenterCompany->getSalutation() ? $costcenterCompany->getSalutation()->getDisplayName() : '',
  160.                         'company' => $costcenterCompany->getCompany() ?? '',
  161.                         'department' => $costcenterCompany->getDepartment() ?? '',
  162.                         'firstName' => $costcenterCompany->getFirstName(),
  163.                         'lastName' => $costcenterCompany->getLastName(),
  164.                         'street' => $costcenterCompany->getStreet(),
  165.                         'zipcode' => $costcenterCompany->getZipcode(),
  166.                         'city' => $costcenterCompany->getCity(),
  167.                         'country' => $costcenterCompany->getCountry() ? $costcenterCompany->getCountry()->getName() : '',
  168.                         'data' => json_encode([
  169.                             'id' => $costcenterCompany->getId(),
  170.                             'salutationName' => $costcenterCompany->getSalutation() ? $costcenterCompany->getSalutation()->getDisplayName() : '',
  171.                             'company' => $costcenterCompany->getCompany() ?? '',
  172.                             'department' => $costcenterCompany->getDepartment() ?? '',
  173.                             'firstName' => $costcenterCompany->getFirstName(),
  174.                             'lastName' => $costcenterCompany->getLastName(),
  175.                             'street' => $costcenterCompany->getStreet(),
  176.                             'zipcode' => $costcenterCompany->getZipcode(),
  177.                             'city' => $costcenterCompany->getCity(),
  178.                             'country' => $costcenterCompany->getCountry() ? $costcenterCompany->getCountry()->getName() : '',
  179.                             'costcenterId' => $costcenter->getId(),
  180.                             'costcenterNumber' => $costcenter->getNumber(),
  181.                             'salutationId' => $costcenterCompany->getSalutationId(),
  182.                             'countryId' => $costcenterCompany->getCountry(),
  183.                             'countryStateId' => null,
  184.                             'title' => '',
  185.                             'phoneNumber' => '',
  186.                             'additionalAddressLine1' => '',
  187.                             'additionalAddressLine2' => ''
  188.                         ])
  189.                     ];
  190.                     if (!isset($costcenterAddressStorage[$costcenter->getId()])) {
  191.                         $costcenterAddressStorage[$costcenter->getId()] = [];
  192.                     }
  193.                     foreach ($costcenterAddressStorage[$costcenter->getId()] as $costcenterAddress) {
  194.                         /** @var CostcenterAddressEntity $costcenterAddress */
  195.                         $costcenterAdresses[] = [
  196.                             'id' => $costcenterAddress->getId(),
  197.                             'costcenterId' => $costcenter->getId(),
  198.                             'costcenterNumber' => $costcenter->getNumber(),
  199.                             'company' => $costcenterAddress->getCompany(),
  200.                             'department' => $costcenterAddress->getDepartment(),
  201.                             'firstName' => $event->getSalesChannelContext()->getCustomer()->getFirstName(),
  202.                             'lastName' => $event->getSalesChannelContext()->getCustomer()->getLastName(),
  203.                             'street' => $costcenterAddress->getStreet(),
  204.                             'zipcode' => $costcenterAddress->getZipcode(),
  205.                             'city' => $costcenterAddress->getCity(),
  206.                             'country' => $costcenterAddress->getCountry()?->getName(),
  207.                             'label' => join(' | ', [
  208.                                 ($costcenterAddress->getCompany() ? $costcenterAddress->getCompany() : $costcenterAddress->getFirstName() . " " $costcenterAddress->getLastName()),
  209.                                 $costcenterAddress->getStreet(),
  210.                                 $costcenterAddress->getZipcode(),
  211.                                 $costcenterAddress->getCity(),
  212.                                 $costcenterAddress->getCountry()?->getName()
  213.                             ]),
  214.                             'data' => json_encode([
  215.                                 'id' => $costcenterAddress->getId(),
  216.                                 'company' => $costcenterAddress->getCompany(),
  217.                                 'department' => $costcenterAddress->getDepartment(),
  218.                                 'firstName' => $event->getSalesChannelContext()->getCustomer()->getFirstName(),
  219.                                 'lastName' => $event->getSalesChannelContext()->getCustomer()->getLastName(),
  220.                                 'street' => $costcenterAddress->getStreet(),
  221.                                 'zipcode' => $costcenterAddress->getZipcode(),
  222.                                 'city' => $costcenterAddress->getCity(),
  223.                                 'country' => $costcenterAddress->getCountry()?->getName(),
  224.                                 'costcenterId' => $costcenter->getId(),
  225.                                 'costcenterNumber' => $costcenter->getNumber(),
  226.                                 'salutationId' => '',
  227.                                 'countryId' => '',
  228.                                 'countryStateId' => '',
  229.                                 'title' => '',
  230.                                 'phoneNumber' => '',
  231.                                 'additionalAddressLine1' => '',
  232.                                 'additionalAddressLine2' => ''
  233.                             ])
  234.                         ];
  235.                     }
  236.                 }
  237.                 if (isset($eventParams['context']) && $eventParams['context']->getPaymentMethod() && $eventParams['context']->getPaymentMethod()->getName() == 'Kostenstelle') {
  238.                     $overrideBillingAddress true;
  239.                 }
  240.             }
  241.             if ($session->get(ChooseAddressController::ADDRESS_TYPE_SESSION_DATA_KEY) === null) {
  242.                 if ($this->checkCustomerPermissionsService->check($event->getSalesChannelContext()->getCustomer(), 'CHECKOUT_CAN_USE_PROFILE_ADDRESS'$event->getContext()) === false) {
  243.                     if (count($costcenterAdresses) > 0) {
  244.                         $firstCostcenterAddress $costcenterAdresses[array_key_first($costcenterAdresses)];
  245.                         $firstCostcenterAddressData json_decode($firstCostcenterAddress['data'], true);
  246.                         $session->set(ChooseAddressController::ADDRESS_TYPE_SESSION_DATA_KEYjson_encode($firstCostcenterAddressData));
  247.                         $session->set(ChooseAddressController::ADDRESS_TYPE_SESSION_TYPE_KEYChooseAddressController::ADDRESS_TYPE_COSTCENTER);
  248.                     } else {
  249.                         $cart->addErrors(new OnlyCostcenterAddressError());
  250.                     }
  251.                 }
  252.             }
  253.             $addressResultCollection $this->customerAddressRepository->search(
  254.                 (new Criteria())->addFilter(new EqualsFilter('customerId'$event->getSalesChannelContext()->getCustomer()->getId())),
  255.                 $event->getContext()
  256.             );
  257.             $sessionType null;
  258.             try {
  259.                 $sessionData $event->getRequest()->getSession()->get(ChooseAddressController::ADDRESS_TYPE_SESSION_DATA_KEY);
  260.                 $sessionType $event->getRequest()->getSession()->get(ChooseAddressController::ADDRESS_TYPE_SESSION_TYPE_KEY);
  261.                 $currentSelectedOverrideAddress json_decode($sessionDatatrue);
  262.             } catch (\Throwable $throwable) {
  263.             }
  264.             if ($sessionType === ChooseAddressController::ADDRESS_TYPE_COSTCENTER) {
  265.                 $currentlySelectedOverrideAddress = [
  266.                     'type' => ChooseAddressController::ADDRESS_TYPE_COSTCENTER,
  267.                     'data' => json_decode($session->get(ChooseAddressController::ADDRESS_TYPE_SESSION_DATA_KEY), true)
  268.                 ];
  269.             } else {
  270.                 $currentlySelectedOverrideAddress = [
  271.                     'type' => ChooseAddressController::ADDRESS_TYPE_CUSTOMER,
  272.                     'data' => null
  273.                 ];
  274.             }
  275.             $selectedCostcenterIdOfCustomer = isset($costcenters[0]) ? $costcenters[0]->get('id') : null;
  276.             if ($currentlySelectedOverrideAddress['data'] && $currentlySelectedOverrideAddress['data']['costcenterId']) {
  277.                 $selectedCostcenterIdOfCustomer $currentlySelectedOverrideAddress['data']['costcenterId'];
  278.             } elseif($personallyCostcentersOfCustomer) {
  279.                 $selectedCostcenterIdOfCustomer $personallyCostcentersOfCustomer[0]->get('id');
  280.             }
  281.             // type of current selected address (default customer address or costcenter address)
  282.             $event->setParameter('currentAddressType'$sessionType);
  283.             // list of all default customer addresses
  284.             $event->setParameter('allAvailableDefaultShippingAddresses'$addressResultCollection);
  285.             // list of addresses from costcenters
  286.             $event->setParameter('addressBookShippingAddressesForCheckout'$costcenterAdresses);
  287.             // array of currently showed override address
  288.             $event->setParameter('currentlySelectedOverrideAddress'$currentlySelectedOverrideAddress);
  289.             // array of available costcenters
  290.             $event->setParameter('allAvailableCostcentersOfCustomer'$costcenters);
  291.             $event->setParameter('selectedCostcenterIdOfCustomer'$selectedCostcenterIdOfCustomer);
  292.             // array of available billing addresses from costcenter companys
  293.             $event->setParameter('allAvailableDefaultBillingAddress'$costcenterBillingAdresses);
  294.             // array of currently address key
  295.             $event->setParameter('overrideBillingAddress'$overrideBillingAddress);
  296.         }
  297.     }
  298.     public function onCustomerLogoutEvent(CustomerLogoutEvent $event)
  299.     {
  300.         // reset selection in session
  301.         $this->request->getSession()->set(ChooseAddressController::ADDRESS_TYPE_SESSION_TYPE_KEYnull);
  302.         $this->request->getSession()->set(ChooseAddressController::ADDRESS_TYPE_SESSION_DATA_KEYnull);
  303.     }
  304.     public function onCartConvertedEvent(CartConvertedEvent $event)
  305.     {
  306.         $request $this->request;
  307.         $isOverrideActive $request->get('cioOverrideShippingAddressActive') !== null;
  308.         $addressOverrideData $request->get('cioOverrideShippingAddressData');
  309.         $billingAddressOverrideData $request->get('cioOverrideBillingAddressData');
  310.         if ($addressOverrideData || $billingAddressOverrideData) {
  311.             try {
  312.                 /** @var EntityRepositoryInterface $orderRepository */
  313.                 $orderRepository $this->container->get('order_address.repository');
  314.                 /** @var EntityRepositoryInterface $orderDeliveryRepository */
  315.                 $orderDeliveryRepository $this->container->get('order_delivery.repository');
  316.                 /** @var EntityRepositoryInterface $salutationRepository */
  317.                 $salutationRepository $this->container->get('salutation.repository');
  318.                 /** @var EntityRepositoryInterface $countryStateRepository */
  319.                 $countryStateRepository $this->container->get('country_state.repository');
  320.                 /** @var EntityRepositoryInterface $countryRepository */
  321.                 $countryRepository $this->container->get('country.repository');
  322.                 $originalOrder $event->getConvertedCart();
  323.                 if ($billingAddressOverrideData) {
  324.                     $currentCustomer $this->getCurrentCustomer($event);
  325.                     $originalOrderBillingAddress $currentCustomer->getDefaultBillingAddress();
  326.                     $currentSelectedOverrideBillingAddress json_decode($billingAddressOverrideDatatrue);
  327.                     $currentSelectedOverrideBillingAddress json_decode($currentSelectedOverrideBillingAddresstrue);
  328.                     $salutation null;
  329.                     if ($currentSelectedOverrideBillingAddress['salutationId'] && Uuid::isValid($currentSelectedOverrideBillingAddress['salutationId'])) {
  330.                         $salutation $salutationRepository->search(new Criteria([$currentSelectedOverrideBillingAddress['salutationId']]), $event->getContext())->first();
  331.                     }
  332.                     $salutationId $salutation $salutation->getId() : $originalOrderBillingAddress->getSalutationId();
  333.                     $countryState null;
  334.                     if ($currentSelectedOverrideBillingAddress['countryStateId'] && Uuid::isValid($currentSelectedOverrideBillingAddress['countryStateId'])) {
  335.                         $countryState $countryStateRepository->search(new Criteria([$currentSelectedOverrideBillingAddress['countryStateId']]), $event->getContext())->first();
  336.                     }
  337.                     $countryStateId $countryState $countryState->getId() : $originalOrderBillingAddress->getCountryStateId();
  338.                     $country null;
  339.                     if ($currentSelectedOverrideBillingAddress['countryId'] && Uuid::isValid($currentSelectedOverrideBillingAddress['countryId'])) {
  340.                         $country $countryRepository->search(new Criteria([$currentSelectedOverrideBillingAddress['countryId']]), $event->getContext())->first();
  341.                     }
  342.                     $countryId $country $country->getId() : $originalOrderBillingAddress->getCountryId();
  343.                     $newOrderAddressId Uuid::randomHex();
  344.                     $originalOrder['addresses'][0] = [
  345.                         'id' => $newOrderAddressId,
  346.                         'company' => $currentSelectedOverrideBillingAddress['company'],
  347.                         'department' => $currentSelectedOverrideBillingAddress['department'],
  348.                         'title' => $currentSelectedOverrideBillingAddress['title'],
  349.                         'firstName' => $currentSelectedOverrideBillingAddress['firstName'],
  350.                         'lastName' => $currentSelectedOverrideBillingAddress['lastName'],
  351.                         'street' => $currentSelectedOverrideBillingAddress['street'],
  352.                         'zipcode' => (string)$currentSelectedOverrideBillingAddress['zipcode'],
  353.                         'phoneNumber' => (string)$currentSelectedOverrideBillingAddress['phoneNumber'],
  354.                         'additionalAddressLine1' => (string)$currentSelectedOverrideBillingAddress['additionalAddressLine1'],
  355.                         'additionalAddressLine2' => (string)$currentSelectedOverrideBillingAddress['additionalAddressLine2'],
  356.                         'city' => $currentSelectedOverrideBillingAddress['city'],
  357.                         'salutationId' => $salutationId,
  358.                         'countryStateId' => $countryStateId,
  359.                         'countryId' => $countryId
  360.                     ];
  361.                     $originalOrder['billingAddressId'] = $newOrderAddressId;
  362.                 }
  363.                 if ($addressOverrideData) {
  364.                     $currentSelectedOverrideAddress json_decode($addressOverrideDatatrue);
  365.                     /** @var array $originalOrderShippingAddress */
  366.                     $originalOrderShippingAddress $event->getConvertedCart()['deliveries'][0]['shippingOrderAddress'];
  367.                     // start validate foreign keys or fallback to default
  368.                     $salutation null;
  369.                     if ($currentSelectedOverrideAddress['salutationId'] && Uuid::isValid($currentSelectedOverrideAddress['salutationId'])) {
  370.                         $salutation $salutationRepository->search(new Criteria([$currentSelectedOverrideAddress['salutationId']]), $event->getContext())->first();
  371.                     }
  372.                     $salutationId $salutation $salutation->getId() : (array_key_exists('salutationId'$originalOrderShippingAddress) ? $originalOrderShippingAddress['salutationId'] : null);
  373.                     $countryState null;
  374.                     if ($currentSelectedOverrideAddress['countryStateId'] && Uuid::isValid($currentSelectedOverrideAddress['countryStateId'])) {
  375.                         $countryState $countryStateRepository->search(new Criteria([$currentSelectedOverrideAddress['countryStateId']]), $event->getContext())->first();
  376.                     }
  377.                     $countryStateId $countryState $countryState->getId() : (array_key_exists('countryStateId'$originalOrderShippingAddress) ? $originalOrderShippingAddress['countryStateId'] : null);
  378.                     $country null;
  379.                     if ($currentSelectedOverrideAddress['countryId'] && Uuid::isValid($currentSelectedOverrideAddress['countryId'])) {
  380.                         $country $countryRepository->search(new Criteria([$currentSelectedOverrideAddress['countryId']]), $event->getContext())->first();
  381.                     }
  382.                     $countryId $country $country->getId() : (array_key_exists('countryId'$originalOrderShippingAddress) ? $originalOrderShippingAddress['countryId'] : null);
  383.                     // end validate foreign keys or fallback to default
  384.                     $newOrderAddressId Uuid::randomHex();
  385.                     $originalOrder['deliveries'][0]['shippingOrderAddress'] = [
  386.                         'id' => $newOrderAddressId,
  387.                         'company' => $currentSelectedOverrideAddress['company'],
  388.                         'department' => $currentSelectedOverrideAddress['department'],
  389.                         'title' => $currentSelectedOverrideAddress['title'],
  390.                         'firstName' => $currentSelectedOverrideAddress['firstName'],
  391.                         'lastName' => $currentSelectedOverrideAddress['lastName'],
  392.                         'street' => $currentSelectedOverrideAddress['street'],
  393.                         'zipcode' => (string)$currentSelectedOverrideAddress['zipcode'],
  394.                         'phoneNumber' => (string)$currentSelectedOverrideAddress['phoneNumber'],
  395.                         'additionalAddressLine1' => (string)$currentSelectedOverrideAddress['additionalAddressLine1'],
  396.                         'additionalAddressLine2' => (string)$currentSelectedOverrideAddress['additionalAddressLine2'],
  397.                         'city' => $currentSelectedOverrideAddress['city'],
  398.                         'salutationId' => $salutationId,
  399.                         'countryStateId' => $countryStateId,
  400.                         'countryId' => $countryId
  401.                     ];
  402.                     if ($originalOrderShippingAddress['id'] === $originalOrder['billingAddressId']) {
  403.                         $originalOrder['billingAddressId'] = $originalOrderShippingAddress['id'];
  404.                         $originalOrder['addresses'][0] = $originalOrderShippingAddress;
  405.                     }
  406.                 }
  407.                 $event->setConvertedCart($originalOrder);
  408.             } catch (\Throwable $throwable) {
  409.                 dd($throwable);
  410.             }
  411.         }
  412.         $request->getSession()->set(ChooseAddressController::ADDRESS_TYPE_SESSION_TYPE_KEYnull);
  413.         $request->getSession()->set(ChooseAddressController::ADDRESS_TYPE_SESSION_DATA_KEYnull);
  414.     }
  415.     public function getCurrentCustomer(ShopwareSalesChannelEvent $event): ?CustomerEntity
  416.     {
  417.         $criteria = new Criteria();
  418.         $criteria->addFilter(new EqualsFilter('id'$event->getSalesChannelContext()->getCustomer()->getId()));
  419.         $criteria->addAssociation('costcenters');
  420.         $criteria->addAssociation('defaultBillingAddress');
  421.         $criteria->addAssociation('company.salutation');
  422.         return $this->customerRepository->search($criteria$event->getContext())->first();
  423.     }
  424. }