custom/plugins/ProcLivePrice/src/Subscriber/LivePriceSubscriber.php line 153

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Proc\ProcLivePrice\Subscriber;
  3. use Proc\ProcOrderSimulate\Subscriber\OrderSimulateSubscriber;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CheapestPriceContainer;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Shopware\Core\Checkout\Cart\Price\Struct\ListPrice;
  11. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  12. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  13. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  14. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  15. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
  16. use Shopware\Core\Content\Product\ProductEntity;
  17. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CalculatedListingPrice;
  22. use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CalculatedCheapestPrice;
  23. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  24. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  25. use Swag\EnterpriseSearch\Search\Page\SearchPageLoadedEvent;
  26. use Shopware\Core\Framework\Event\NestedEvent;
  27. use Proc\ProcLivePrice\Helper\BuildRequestHelper;
  28. use Proc\ProcFoundation\Helper\ErrorHandler;
  29. use Proc\ProcFoundation\Helper\RequestHandler;
  30. use Proc\ProcFoundation\Service\ProcFoundationService;
  31. use Symfony\Component\HttpFoundation\RedirectResponse;
  32. use Symfony\Component\Routing\RouterInterface;
  33. use Shopware\Core\Content\Product\ProductEvents;
  34. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  35. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  36. /**
  37.  * Class LivePriceSubscriber
  38.  * @package Proc\ProcLivePrice\Subscriber
  39.  */
  40. class LivePriceSubscriber implements EventSubscriberInterface
  41. {
  42.     private const PATH '/iman/live-price';
  43.     private const CUSTOM_ZLOTY 'PLN';
  44.     /**
  45.      * @var RequestHandler
  46.      */
  47.     private $requestHandler;
  48.     /**
  49.      * @var ErrorHandler
  50.      */
  51.     private $errorHandler;
  52.     /**
  53.      * @var string
  54.      */
  55.     private
  56.         $host,
  57.         $port,
  58.         $customerNumber;
  59.     private $error null;
  60.     /**
  61.      * @var ProcFoundationService
  62.      */
  63.     private $foundationService;
  64.     /**
  65.      * @var EntityRepositoryInterface
  66.      */
  67.     private $productRepository;
  68.     /**
  69.      * @var ProductPageLoadedEvent | ProductListingResultEvent | SearchPageLoadedEvent
  70.      */
  71.     private $event;
  72.     /**
  73.      * @var array
  74.      */
  75.     private $result;
  76.     /**
  77.      * @var CalculatedTaxCollection
  78.      */
  79.     private $calculatedTaxCollection;
  80.     /**
  81.      * @var TaxRuleCollection
  82.      */
  83.     private $taxRuleCollection;
  84.     /**
  85.      * @var RouterInterface
  86.      */
  87.     private RouterInterface $router;
  88.     private AbstractProductPriceCalculator $calculator;
  89.     /**
  90.      * @inheritDoc
  91.      */
  92.     public static function getSubscribedEvents(): array
  93.     {
  94.         return [
  95.             ProductPageLoadedEvent::class => ['onProductPageLoaded'1],
  96.             ProductListingResultEvent::class => ['onListingResultLoaded'1],
  97.             // 'sales_channel.' . ProductEvents::PRODUCT_LOADED_EVENT => ['onSalesChanneLoaded', 10000],
  98.             SearchPageLoadedEvent::class => ['onListingResultLoaded']
  99.         ];
  100.     }
  101.     /**
  102.      * LivePriceSubscriber constructor.
  103.      * @param ProcFoundationService $foundationService
  104.      * @param RequestHandler $requestHandler
  105.      * @param ErrorHandler $errorHandler
  106.      * @param EntityRepositoryInterface $repositoryInterface
  107.      */
  108.     function __construct(
  109.         ProcFoundationService $foundationService,
  110.         RequestHandler $requestHandler,
  111.         ErrorHandler $errorHandler,
  112.         EntityRepositoryInterface $repositoryInterface,
  113.         RouterInterface $router,
  114.         AbstractProductPriceCalculator $calculator,
  115.     ) {
  116.         $this->foundationService $foundationService;
  117.         $this->requestHandler $requestHandler;
  118.         $this->errorHandler $errorHandler;
  119.         $this->productRepository $repositoryInterface;
  120.         $this->router $router;
  121.         $this->calculator $calculator;
  122.     }
  123.     /**
  124.      * @param NestedEvent $event
  125.      * @throws \Exception
  126.      */
  127.     public function onListingResultLoaded(NestedEvent $event)
  128.     {
  129.         $this->event $event;
  130.         /**
  131.          * Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
  132.          */
  133.         if (!($this->customerNumber $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
  134.             get_class($this)))) {
  135.             return;
  136.         }
  137.         /**
  138.          * Prüfen ob der Benutzer eine Artikel-Whitelist hat.
  139.          */
  140.         $customFields $this->event->getSalesChannelContext()->getCustomer()->getCustomFields();
  141.         if (array_key_exists('custom_product_listing_numbers'$customFields) &&
  142.             $customFields['custom_product_listing_numbers'] != null &&
  143.             $customFields['custom_product_listing_numbers'] != &&
  144.             $customFields['custom_product_listing_numbers'] != '') {
  145.             $allowedArticles explode(','$customFields['custom_product_listing_numbers']);
  146.         } else {
  147.             return;
  148.         }
  149.         /**
  150.          * Configeinstellungen prüfen und setzen.
  151.          */
  152.         if (!$this->checkConfig()) {
  153.             return;
  154.         }
  155.         $productArray = array();
  156.         if ($this->event instanceof ProductListingResultEvent) {
  157.             $resultItems $this->event->getResult()->getIterator();
  158.         } else {
  159.             $resultItems $this->event->getPage()->getListing()->getIterator();
  160.         }
  161.         $articles = [];
  162.         $counter 0;
  163.         while ($resultItems->current() !== null) {
  164.             /**
  165.              * @var SalesChannelProductEntity $product
  166.              */
  167.             $product $resultItems->current();
  168.             // benutzer holen. -> custom field holen.(siehe oben) String in array umwandeln. Prüfen ob artikelnummer vorhanden. Wenn ja dann durchlaufen, wenn nein, continue.
  169.             if (!in_array($product->getProductNumber(), $allowedArticles)) {
  170.                 $resultItems->next();
  171.                 continue;
  172.             }
  173.             $counter++;
  174.             if ($product->getParentId()) {
  175.                 /**
  176.                  * @var ProductEntity $parentProduct
  177.                  */
  178.                 $parentProduct $this->getProduct($product->getParentId(), ['children']);
  179.                 $childArticles = array();
  180.                 foreach ($parentProduct->getChildren()->getElements() as $children) {
  181.                     $childArticle['position-id'] = $counter;
  182.                     $childArticle['article-number'] = $children->getProductNumber();
  183.                     $childArticle['quantity'] = 1;
  184.                     $childArticle['quantity-unit'] = $children->getPackUnit();
  185.                     $childArticles[] = $childArticle;
  186.                 }
  187.                 $childResult $this->getArticleInfo($childArticles);
  188.                 if ($childResult['head']['status'] == 'NOK') {
  189.                     return;
  190.                 }
  191.                 $countProductItems sizeof($childResult);
  192.                 $from 0;
  193.                 $to 0;
  194.                 $x 0;
  195.                 foreach ($childResult['articles']['article'] as $childArticle) {
  196.                     // $unitPrice = (float)$childArticle['conditions']['condition'][0]['condition-value'];
  197.                     // $mwstValue = (float)$childArticle['conditions']['condition'][2]['condition-value'];
  198.                     // $unitPrice = $unitPrice + $mwstValue;
  199.                     $unitPrice = (float)$childArticle['price'] / (float)$childArticle['quantity'];
  200.                     if ($x === 0) {
  201.                         $from $unitPrice;
  202.                         $to $unitPrice;
  203.                     } elseif ($unitPrice $from) {
  204.                         $from $unitPrice;
  205.                     } elseif ($unitPrice $to) {
  206.                         $to $unitPrice;
  207.                     }
  208.                     $x++;
  209.                 }
  210.                 /**
  211.                  * @var CalculatedCheapestPrice $productListingPrice
  212.                  */
  213.                 $productListingPrice $product->getCalculatedCheapestPrice();
  214.                 /**
  215.                  * @var CalculatedPrice $fromPrice , $toPrice
  216.                  */
  217.                 $fromPrice = new CalculatedPrice(
  218.                     $from,
  219.                     $from,
  220.                     $productListingPrice->getCalculatedTaxes(),
  221.                     $productListingPrice->getTaxRules(),
  222.                     1);
  223.                 $toPrice = new CalculatedPrice(
  224.                     $to,
  225.                     $to,
  226.                     $productListingPrice->getCalculatedTaxes(),
  227.                     $productListingPrice->getTaxRules(),
  228.                     1);
  229.                 /**
  230.                  * @var CalculatedCheapestPrice $newListingPrice
  231.                  */
  232.                 $newListingPrice = new CalculatedCheapestPrice($from$to$productListingPrice->getCalculatedTaxes(),
  233.                     $productListingPrice->getTaxRules());
  234.                 $product->setCalculatedCheapestPrice($newListingPrice);
  235.                 $product->getCheapestPrice()->getPrice()->first()->setGross($to);
  236.                 $product->getCheapestPrice()->getPrice()->first()->setNet($to);
  237.             }
  238.             $productArray[] = $product;
  239.             $article['position-id'] = $counter;
  240.             $article['article-number'] = $product->getProductNumber();
  241.             $article['quantity'] = 1;
  242.             $article['quantity-unit'] = $product->getPackUnit();
  243.             $articles[] = $article;
  244.             $resultItems->next();
  245.         }
  246.         if (count($articles) <= 0) {
  247.             return;
  248.         }
  249.         $this->result $this->getArticleInfo($articles);
  250.         if ($this->result['head']['status'] == 'NOK') {
  251.            
  252.             return;
  253.         }
  254.         $countProductItems sizeof($productArray);
  255.         $x 0;
  256.         foreach ($productArray as $productItem) {
  257.             if ($countProductItems 1) {
  258.                 // $unitPrice = (float)$this->result['articles']['article'][$x]['conditions']['condition'][0]['condition-value'];
  259.                 $totalPrice = (float)$this->result['articles']['article'][$x]['price'];
  260.                 $unitPrice = (float)$totalPrice / (float)$this->result['articles']['article'][$x]['quantity'];
  261.                 $mwstRate 0.0// (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-rate'];
  262.                 $mwstValue 0.0// (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-value'];
  263.             } else {
  264.                 // $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
  265.                 $totalPrice = (float)$this->result['articles']['article']['price'];
  266.                 $unitPrice = (float)$totalPrice / (float)$this->result['articles']['article']['quantity'];
  267.                 $mwstRate 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
  268.                 $mwstValue 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
  269.             }
  270.             /**
  271.              * @var CalculatedPrice $price
  272.              */
  273.             $price $product->getCalculatedPrice();
  274.             /**
  275.              * @var TaxRule $newTaxRule
  276.              */
  277.             $newTaxRule = new TaxRule($mwstRate);
  278.             /**
  279.              * @var TaxRuleCollection $newTaxRuleCollection
  280.              */
  281.             $newTaxRuleCollection = new TaxRuleCollection();
  282.             $newTaxRuleCollection->add($newTaxRule);
  283.             /**
  284.              * @var CalculatedTax $newCalculateTax
  285.              */
  286.             $newCalculateTax = new CalculatedTax($mwstValue$mwstRate$totalPrice);
  287.             /**
  288.              * @var CalculatedTaxCollection $newCalculatedTaxCollection
  289.              */
  290.             $newCalculatedTaxCollection = new CalculatedTaxCollection();
  291.             $newCalculatedTaxCollection->add($newCalculateTax);
  292.             /**
  293.              * @var CalculatedPrice $newPrice
  294.              */
  295.             $newPrice = new CalculatedPrice(
  296.                 ($unitPrice $mwstValue),
  297.                 $totalPrice,
  298.                 $newCalculatedTaxCollection,
  299.                 $newTaxRuleCollection,
  300.                 $price->getQuantity(),
  301.                 $price->getReferencePrice(),
  302.                 $price->getListPrice()
  303.             );
  304.             $productItem->setCalculatedPrice($newPrice);
  305.             $x++;
  306.         }
  307.     }
  308.     
  309.     /**
  310.      * @param NestedEvent $event
  311.      * @throws \Exception
  312.      */
  313.     public function onSalesChanneLoaded(SalesChannelEntityLoadedEvent $event)
  314.     {   
  315.         
  316.         if($event->getContext()->hasState('noLivePrice')){
  317.             return;
  318.         }
  319.         
  320.         $this->event $event;
  321.         /**
  322.          * Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
  323.          */
  324.         if (!($this->customerNumber $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
  325.             get_class($this)))) {
  326.             return;
  327.         }
  328.         /**
  329.          * Prüfen ob der Benutzer eine Artikel-Whitelist hat.
  330.          */
  331.         $customFields $this->event->getSalesChannelContext()->getCustomer()->getCustomFields();
  332.         if (array_key_exists('custom_product_listing_numbers'$customFields) &&
  333.             $customFields['custom_product_listing_numbers'] != null &&
  334.             $customFields['custom_product_listing_numbers'] != &&
  335.             $customFields['custom_product_listing_numbers'] != '') {
  336.             $allowedArticles explode(','$customFields['custom_product_listing_numbers']);
  337.         } else {
  338.             return;
  339.         }
  340.         /**
  341.          * Configeinstellungen prüfen und setzen.
  342.          */
  343.         if (!$this->checkConfig()) { 
  344.             return;
  345.         }
  346.         $productArray = array();
  347.         $products $event->getEntities();
  348.         //** Abort, if empty */
  349.         if (false === $products) {
  350.             return;
  351.         }
  352.         $articles = [];
  353.         $counter 0
  354.         /** @var SalesChannelProductEntity $product */
  355.         foreach($products as $product ) {
  356.            
  357.             // benutzer holen. -> custom field holen.(siehe oben) String in array umwandeln. Prüfen ob artikelnummer vorhanden. Wenn ja dann durchlaufen, wenn nein, continue.
  358.             if (!in_array($product->getProductNumber(), $allowedArticles)) {
  359.                 continue;
  360.             }
  361.             $counter++;
  362.             if ($product->getParentId()) {
  363.                 /**
  364.                  * @var ProductEntity $parentProduct
  365.                  */
  366.                 $parentProduct $this->getProduct($product->getParentId(), ['children']);
  367.                 $childArticles = array();
  368.                 foreach ($parentProduct->getChildren()->getElements() as $children) {
  369.                     $childArticle['position-id'] = $counter;
  370.                     $childArticle['article-number'] = $children->getProductNumber();
  371.                     $childArticle['quantity'] = 1;
  372.                     $childArticle['quantity-unit'] = $children->getPackUnit();
  373.                     $childArticles[] = $childArticle;
  374.                 }
  375.                 $childResult $this->getArticleInfo($childArticles);
  376.                 if ($childResult['head']['status'] == 'NOK') {
  377.                     return;
  378.                 }
  379.                 $countProductItems sizeof($childResult);
  380.                 $from 0;
  381.                 $to 0;
  382.                 $x 0;
  383.                 foreach ($childResult['articles']['article'] as $childArticle) {
  384.                     // $unitPrice = (float)$childArticle['conditions']['condition'][0]['condition-value'];
  385.                     // $mwstValue = (float)$childArticle['conditions']['condition'][2]['condition-value'];
  386.                     // $unitPrice = $unitPrice + $mwstValue;
  387.                     $unitPrice = (float)$childArticle['price'] / (float)$childArticle['quantity'];
  388.                     if ($x === 0) {
  389.                         $from $unitPrice;
  390.                         $to $unitPrice;
  391.                     } elseif ($unitPrice $from) {
  392.                         $from $unitPrice;
  393.                     } elseif ($unitPrice $to) {
  394.                         $to $unitPrice;
  395.                     }
  396.                     $x++;
  397.                 }
  398.                 // /**
  399.                 //  * @var CalculatedCheapestPrice $productListingPrice
  400.                 //  */
  401.                 // $productListingPrice = $product->getCalculatedCheapestPrice();
  402.                 // /**
  403.                 //  * @var CalculatedPrice $fromPrice , $toPrice
  404.                 //  */
  405.                 // $fromPrice = new CalculatedPrice(
  406.                 //     $from,
  407.                 //     $from,
  408.                 //     $productListingPrice->getCalculatedTaxes(),
  409.                 //     $productListingPrice->getTaxRules(),
  410.                 //     1);
  411.                 // $toPrice = new CalculatedPrice(
  412.                 //     $to,
  413.                 //     $to,
  414.                 //     $productListingPrice->getCalculatedTaxes(),
  415.                 //     $productListingPrice->getTaxRules(),
  416.                 //     1);
  417.                 // /**
  418.                 //  * @var CalculatedCheapestPrice $newListingPrice
  419.                 //  */
  420.                 // $newListingPrice = new CalculatedCheapestPrice($from, $to, $productListingPrice->getCalculatedTaxes(),
  421.                 //     $productListingPrice->getTaxRules());
  422.                 // $product->getCheapestPrice()->getPrice()->first()->setGross($to);
  423.                 // $product->getCheapestPrice()->getPrice()->first()->setNet($to);
  424.             }
  425.             $productArray[] = $product;
  426.             $article['position-id'] = $counter;
  427.             $article['article-number'] = $product->getProductNumber();
  428.             $article['quantity'] = 1;
  429.             $article['quantity-unit'] = $product->getPackUnit();
  430.             $articles[] = $article;
  431.         }
  432.         
  433.         if (count($articles) <= 0) {
  434.             return;
  435.         }
  436.       
  437.         $this->result $this->getArticleInfo($articles);
  438.         if ($this->result['head']['status'] == 'NOK') {
  439.             return;
  440.         }
  441.         $countProductItems sizeof($productArray);
  442.         $x 0;
  443.         foreach ($productArray as $productItem) {
  444.             if ($countProductItems 1) {
  445.                 // $unitPrice = (float)$this->result['articles']['article'][$x]['conditions']['condition'][0]['condition-value'];
  446.                 $totalPrice = (float)$this->result['articles']['article'][$x]['price'];
  447.                 $unitPrice = (float)$totalPrice / (float)$productItem->getMinPurchase() ?? $this->result['articles']['article'][$x]['quantity'];
  448.                 $mwstRate 0.0// (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-rate'];
  449.                 $mwstValue 0.0// (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-value'];
  450.             } else {
  451.                 // $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
  452.                 $totalPrice = (float)$this->result['articles']['article']['price'];
  453.                 $unitPrice = (float)$totalPrice / (float)$productItem->getMinPurchase() ?? $this->result['articles']['article'][$x]['quantity'];
  454.                 $mwstRate 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
  455.                 $mwstValue 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
  456.             }
  457.             /**
  458.              * @var TaxRule $newTaxRule
  459.              */
  460.             $newTaxRule = new TaxRule($mwstRate);
  461.             /**
  462.              * @var TaxRuleCollection $newTaxRuleCollection
  463.              */
  464.             $newTaxRuleCollection = new TaxRuleCollection();
  465.             $newTaxRuleCollection->add($newTaxRule);
  466.             /**
  467.              * @var CalculatedTax $newCalculateTax
  468.              */
  469.             $newCalculateTax = new CalculatedTax($mwstValue$mwstRate$totalPrice);
  470.             /**
  471.              * @var CalculatedTaxCollection $newCalculatedTaxCollection
  472.              */
  473.             $newCalculatedTaxCollection = new CalculatedTaxCollection();
  474.             $newCalculatedTaxCollection->add($newCalculateTax);
  475.             $newCalculatedCheapestPrice = new CalculatedCheapestPrice($totalPrice$totalPrice$newCalculatedTaxCollection,
  476.             $newTaxRuleCollection1);
  477.             $productItem->setCalculatedCheapestPrice($newCalculatedCheapestPrice);
  478.            
  479.             $productItem->setPrice(new PriceCollection([
  480.                 new Price(
  481.                     $this->event->getContext()->getCurrencyId(),
  482.                     ($unitPrice $mwstValue),
  483.                     ($unitPrice $mwstValue),
  484.                     false
  485.                 ),
  486.             ]));
  487.            
  488.             $x++;
  489.         } 
  490.     }
  491.     /**
  492.      * @param ProductPageLoadedEvent $event
  493.      */
  494.     public function onProductPageLoaded(ProductPageLoadedEvent|SalesChannelEntityLoadedEvent $event)
  495.     {
  496.         $this->event $event;
  497.         /**
  498.          * Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
  499.          */
  500.         if (!($this->customerNumber $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
  501.             get_class($this)))) {
  502.             return;
  503.         } else {
  504.             if (array_key_exists('custom_product_listing_numbers',
  505.                 $this->event->getSalesChannelContext()->getCustomer()->getCustomFields())) {
  506.                 $articles explode(',',
  507.                     $this->event->getSalesChannelContext()->getCustomer()->getCustomFields()['custom_product_listing_numbers']);
  508.                 if (!in_array($this->event->getPage()->getProduct()->getProductNumber(), $articles)) {
  509.                     $redirectResponse = new RedirectResponse($this->router->generate('frontend.home.page'));
  510.                     return $redirectResponse->send();
  511.                 }
  512.             }
  513.         }
  514.         /**
  515.          * Configeinstellungen prüfen und setzen.
  516.          */
  517.         if (!$this->checkConfig()) {
  518.             return;
  519.         }
  520.         /**
  521.          * @var SalesChannelProductEntity $product
  522.          */
  523.         $product $this->event->getPage()->getProduct();
  524.         /**
  525.          * @todo Die position-id noch richtig machen
  526.          * @var array $article
  527.          */
  528.         $article['position-id'] = 1;
  529.         $article['article-number'] = $product->getProductNumber();
  530.         $article['quantity'] = 1;
  531.         $article['quantity-unit'] = $product->getPackUnit();
  532.         $this->result $this->getArticleInfo([$article]);
  533.         if ($this->result['head']['status'] == 'NOK') {
  534.             return;
  535.         }
  536.         // $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
  537.         $totalPrice = (float)$this->result['articles']['article']['price'];
  538.         $unitPrice $totalPrice / (float)$this->result['articles']['article']['quantity'];
  539.         $mwstRate 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
  540.         $mwstValue 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
  541.         /**
  542.          * @var CalculatedPrice $price
  543.          */
  544.         $price $product->getCalculatedPrice();
  545.         /**
  546.          * @var TaxRule $newTaxRule
  547.          */
  548.         $newTaxRule = new TaxRule($mwstRate);
  549.         /** a
  550.          * @var TaxRuleCollection $newTaxRuleCollection
  551.          */
  552.         $newTaxRuleCollection = new TaxRuleCollection();
  553.         $newTaxRuleCollection->add($newTaxRule);
  554.         /**
  555.          * @var CalculatedTax $newCalculateTax
  556.          */
  557.         $newCalculateTax = new CalculatedTax($mwstValue$mwstRate$unitPrice);
  558.         /**
  559.          * @var CalculatedTaxCollection $newCalculatedTaxCollection
  560.          */
  561.         $newCalculatedTaxCollection = new CalculatedTaxCollection();
  562.         $newCalculatedTaxCollection->add($newCalculateTax);
  563.         /**
  564.          * @var TaxRule $newTaxRule
  565.          */
  566.         $newTaxRule = new TaxRule(0.0); // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value']);
  567.         $price->getTaxRules()->add($newTaxRule);
  568.         // Get from request or pick default
  569.         if (array_key_exists('free-shipping-threshold'$this->result['shipping-cost'])) {
  570.             $freeShippingThreshold = ['freeShippingThreshold' => $this->result['shipping-cost']['free-shipping-threshold']];
  571.         } else {
  572.             $freeShippingThreshold = ['freeShippingThreshold' => $this->calculateShippingThreshold($event->getSalesChannelContext())];
  573.         }
  574.         $this->event->getContext()->addArrayExtension('deliveryInformations'$freeShippingThreshold);
  575.         /**
  576.          * @var CalculatedPrice $newPrice
  577.          */
  578.         $newPrice = new CalculatedPrice(
  579.             $totalPrice,
  580.             $totalPrice,
  581.             $newCalculatedTaxCollection,
  582.             $newTaxRuleCollection,
  583.             1,
  584.             null,
  585.             ListPrice::createFromUnitPrice($totalPrice$totalPrice)
  586.         );
  587.         $newCalculatedCheapestPrice = new CalculatedCheapestPrice($totalPrice$totalPrice$newCalculatedTaxCollection,
  588.             $newTaxRuleCollection1);
  589.         $product->setCalculatedCheapestPrice($newCalculatedCheapestPrice);
  590.         $product->setCalculatedPrice($newPrice);
  591.         $product->getPrice()->first()->setNet($totalPrice);
  592.         $product->getPrice()->first()->setGross($totalPrice);
  593.         $product->getCheapestPrice()->getPrice()->first()->setGross($totalPrice);
  594.         $product->getCheapestPrice()->getPrice()->first()->setNet($totalPrice);
  595.         $product->getCalculatedPrices()->clear();
  596.         $product->setCheapestPrice(null);
  597.         $newCPContainer = new CheapestPriceContainer([]);
  598.         $product->setCheapestPriceContainer($newCPContainer);
  599.     }
  600.     /**
  601.      * @param array $articles
  602.      * @param int $shopInstance
  603.      * @param string $currency
  604.      * @param string $language
  605.      * @return
  606.      */
  607.     private function getArticleInfo(
  608.         array $articles,
  609.         int $shopInstance 1,
  610.         string $language 'DE'
  611.     ) {
  612.         $params = [
  613.             'customer' => $this->customerNumber,
  614.             'shop-instance' => $shopInstance,
  615.             'currency' => $this->getCurrencyISO()
  616.         ];
  617.         $buildRequestHelper = new BuildRequestHelper($params$articles$this->errorHandler);
  618.         /**
  619.          * @var string $request
  620.          */
  621.         $request $this->requestHandler->buildRequest($buildRequestHelperget_class($this));
  622.         $this->error $this->errorHandler->error($this->errorHandler::GENERAL_ERRORget_class($this),
  623.             "Request: " print_r($requesttrue));
  624.         $this->errorHandler->writeErrorLog($this->error);
  625.         if ($request !== '') {
  626.             /**
  627.              * @var string $result
  628.              */
  629.             $result $this->requestHandler->sendRequest($this->host$this->portself::PATH$request);
  630.         } else {
  631.             $this->error $this->errorHandler->error($this->errorHandler::UNKNOWN_REQUESTget_class($this),
  632.                 'Fehler im Aufbau des Request');
  633.             $this->errorHandler->writeErrorLog($this->error);
  634.             return;
  635.         }
  636.         /**
  637.          * @var array $resultArray
  638.          */
  639.         $resultArray $this->requestHandler->parseResult($result);
  640.         if ($resultArray['head']['status'] == 'NOK') {
  641.             $this->error $this->errorHandler->error($this->errorHandler::SAP_RESPONSE_ERRORget_class($this),
  642.                 'SAP Status Fehler. - ' print_r($resultArraytrue));
  643.             $this->errorHandler->writeErrorLog($this->error);
  644.             // } else {
  645.             //  $this->error = $this->errorHandler->error($this->errorHandler::WRONG_ARTICLE_ERROR, get_class($this), 'LivePrice-Response after success. - ' . print_r($resultArray, true));
  646.             //  $this->errorHandler->writeErrorLog($this->error);
  647.         }
  648.         return $resultArray;
  649.     }
  650.     /**
  651.      * @return bool
  652.      */
  653.     private function checkConfig(): bool
  654.     {
  655.         if ($this->foundationService->getConfigStatus()) {
  656.             $this->host $this->foundationService->getHost();
  657.             $this->port $this->foundationService->getPort();
  658.             return true;
  659.         }
  660.         $this->error $this->errorHandler->error($this->errorHandler::CONFIG_NOT_VALIDget_class($this),
  661.             'Konnte die Konfiguration nicht ermitteln');
  662.         $this->errorHandler->writeErrorLog($this->error);
  663.         return false;
  664.     }
  665.     /**
  666.      * @param string $productID
  667.      * @return ProductEntity
  668.      */
  669.     public function getProduct(string $productID, array $associations null): ProductEntity
  670.     {
  671.         /**
  672.          * @var Criteria $criteria
  673.          */
  674.         $criteria = new Criteria([$productID]);
  675.         if ($associations !== null) {
  676.             foreach ($associations as $association) {
  677.                 $criteria->addAssociation($association);
  678.             }
  679.         }
  680.         /**
  681.          * @var EntityCollection $products
  682.          */
  683.         $products $this->productRepository->search($criteria,
  684.             \Shopware\Core\Framework\Context::createDefaultContext());
  685.         /**
  686.          * @var array $productList
  687.          */
  688.         $productList $products->getElements();
  689.         /**
  690.          * @var ProductEntity $product
  691.          */
  692.         $product $productList[$productID];
  693.         return $product;
  694.     }
  695.     /**
  696.      * fts - Retrieve currency iso code from saleschannel context
  697.      * @return string
  698.      */
  699.     function getCurrencyISO(): string
  700.     {
  701.         return $this->event?->getSalesChannelContext()?->getCurrency()->getIsoCode() ?? "EUR";
  702.     }
  703.     /**
  704.      * If the current currency is not EUR, then the default threshold (250€)
  705.      * will be converted to the appropriate currency using the currency factor.
  706.      * @return int
  707.      */
  708.     private function calculateShippingThreshold(SalesChannelContext $context): int
  709.     {
  710.         if($context->getCurrency()->getIsoCode() === "PLN") {
  711.             return OrderSimulateSubscriber::ZLOTY_THRESHOLD;
  712.         } else if($context->getCurrency()->getIsoCode() === "GBP") {
  713.             return OrderSimulateSubscriber::POUND_THRESHOLD;
  714.         } else {
  715.             return OrderSimulateSubscriber::EUR_THRESHOLD;
  716.         }
  717.     }
  718. }