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

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.         if($event->getContext()->hasState('noLivePrice')){
  316.             return;
  317.         }
  318.         $this->event $event;
  319.         /**
  320.          * Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
  321.          */
  322.         if (!($this->customerNumber $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
  323.             get_class($this)))) {
  324.             return;
  325.         }
  326.         /**
  327.          * Prüfen ob der Benutzer eine Artikel-Whitelist hat.
  328.          */
  329.         $customFields $this->event->getSalesChannelContext()->getCustomer()->getCustomFields();
  330.         if (array_key_exists('custom_product_listing_numbers'$customFields) &&
  331.             $customFields['custom_product_listing_numbers'] != null &&
  332.             $customFields['custom_product_listing_numbers'] != &&
  333.             $customFields['custom_product_listing_numbers'] != '') {
  334.             $allowedArticles explode(','$customFields['custom_product_listing_numbers']);
  335.         } else {
  336.             return;
  337.         }
  338.         /**
  339.          * Configeinstellungen prüfen und setzen.
  340.          */
  341.         if (!$this->checkConfig()) {
  342.             return;
  343.         }
  344.         $productArray = array();
  345.         $products $event->getEntities();
  346.         //** Abort, if empty */
  347.         if (false === $products) {
  348.             return;
  349.         }
  350.         $articles = [];
  351.         $counter 0
  352.         /** @var SalesChannelProductEntity $product */
  353.         foreach($products as $product ) {
  354.            
  355.             // benutzer holen. -> custom field holen.(siehe oben) String in array umwandeln. Prüfen ob artikelnummer vorhanden. Wenn ja dann durchlaufen, wenn nein, continue.
  356.             if (!in_array($product->getProductNumber(), $allowedArticles)) {
  357.                 continue;
  358.             }
  359.             $counter++;
  360.             if ($product->getParentId()) {
  361.                 /**
  362.                  * @var ProductEntity $parentProduct
  363.                  */
  364.                 $parentProduct $this->getProduct($product->getParentId(), ['children']);
  365.                 $childArticles = array();
  366.                 foreach ($parentProduct->getChildren()->getElements() as $children) {
  367.                     $childArticle['position-id'] = $counter;
  368.                     $childArticle['article-number'] = $children->getProductNumber();
  369.                     $childArticle['quantity'] = 1;
  370.                     $childArticle['quantity-unit'] = $children->getPackUnit();
  371.                     $childArticles[] = $childArticle;
  372.                 }
  373.                 $childResult $this->getArticleInfo($childArticles);
  374.                 if ($childResult['head']['status'] == 'NOK') {
  375.                     return;
  376.                 }
  377.                 $countProductItems sizeof($childResult);
  378.                 $from 0;
  379.                 $to 0;
  380.                 $x 0;
  381.                 foreach ($childResult['articles']['article'] as $childArticle) {
  382.                     // $unitPrice = (float)$childArticle['conditions']['condition'][0]['condition-value'];
  383.                     // $mwstValue = (float)$childArticle['conditions']['condition'][2]['condition-value'];
  384.                     // $unitPrice = $unitPrice + $mwstValue;
  385.                     $unitPrice = (float)$childArticle['price'] / (float)$childArticle['quantity'];
  386.                     if ($x === 0) {
  387.                         $from $unitPrice;
  388.                         $to $unitPrice;
  389.                     } elseif ($unitPrice $from) {
  390.                         $from $unitPrice;
  391.                     } elseif ($unitPrice $to) {
  392.                         $to $unitPrice;
  393.                     }
  394.                     $x++;
  395.                 }
  396.                 // /**
  397.                 //  * @var CalculatedCheapestPrice $productListingPrice
  398.                 //  */
  399.                 // $productListingPrice = $product->getCalculatedCheapestPrice();
  400.                 // /**
  401.                 //  * @var CalculatedPrice $fromPrice , $toPrice
  402.                 //  */
  403.                 // $fromPrice = new CalculatedPrice(
  404.                 //     $from,
  405.                 //     $from,
  406.                 //     $productListingPrice->getCalculatedTaxes(),
  407.                 //     $productListingPrice->getTaxRules(),
  408.                 //     1);
  409.                 // $toPrice = new CalculatedPrice(
  410.                 //     $to,
  411.                 //     $to,
  412.                 //     $productListingPrice->getCalculatedTaxes(),
  413.                 //     $productListingPrice->getTaxRules(),
  414.                 //     1);
  415.                 // /**
  416.                 //  * @var CalculatedCheapestPrice $newListingPrice
  417.                 //  */
  418.                 // $newListingPrice = new CalculatedCheapestPrice($from, $to, $productListingPrice->getCalculatedTaxes(),
  419.                 //     $productListingPrice->getTaxRules());
  420.                 // $product->getCheapestPrice()->getPrice()->first()->setGross($to);
  421.                 // $product->getCheapestPrice()->getPrice()->first()->setNet($to);
  422.             }
  423.             $productArray[] = $product;
  424.             $article['position-id'] = $counter;
  425.             $article['article-number'] = $product->getProductNumber();
  426.             $article['quantity'] = 1;
  427.             $article['quantity-unit'] = $product->getPackUnit();
  428.             $articles[] = $article;
  429.         }
  430.         if (count($articles) <= 0) {
  431.             return;
  432.         }
  433.       
  434.         $this->result $this->getArticleInfo($articles);
  435.         if ($this->result['head']['status'] == 'NOK') {
  436.             return;
  437.         }
  438.         $countProductItems sizeof($productArray);
  439.         $x 0;
  440.         foreach ($productArray as $productItem) {
  441.             if ($countProductItems 1) {
  442.                 // $unitPrice = (float)$this->result['articles']['article'][$x]['conditions']['condition'][0]['condition-value'];
  443.                 $totalPrice = (float)$this->result['articles']['article'][$x]['price'];
  444.                 $unitPrice = (float)$totalPrice / (float)$this->result['articles']['article'][$x]['quantity'];
  445.                 $mwstRate 0.0// (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-rate'];
  446.                 $mwstValue 0.0// (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-value'];
  447.             } else {
  448.                 // $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
  449.                 $totalPrice = (float)$this->result['articles']['article']['price'];
  450.                 $unitPrice = (float)$totalPrice / (float)$this->result['articles']['article']['quantity'];
  451.                 $mwstRate 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
  452.                 $mwstValue 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
  453.             }
  454.             /**
  455.              * @var TaxRule $newTaxRule
  456.              */
  457.             $newTaxRule = new TaxRule($mwstRate);
  458.             /**
  459.              * @var TaxRuleCollection $newTaxRuleCollection
  460.              */
  461.             $newTaxRuleCollection = new TaxRuleCollection();
  462.             $newTaxRuleCollection->add($newTaxRule);
  463.             /**
  464.              * @var CalculatedTax $newCalculateTax
  465.              */
  466.             $newCalculateTax = new CalculatedTax($mwstValue$mwstRate$totalPrice);
  467.             /**
  468.              * @var CalculatedTaxCollection $newCalculatedTaxCollection
  469.              */
  470.             $newCalculatedTaxCollection = new CalculatedTaxCollection();
  471.             $newCalculatedTaxCollection->add($newCalculateTax);
  472.             $newCalculatedCheapestPrice = new CalculatedCheapestPrice($totalPrice$totalPrice$newCalculatedTaxCollection,
  473.             $newTaxRuleCollection1);
  474.             $productItem->setCalculatedCheapestPrice($newCalculatedCheapestPrice);
  475.           
  476.             $productItem->setPrice(new PriceCollection([
  477.                 new Price(
  478.                     $this->event->getContext()->getCurrencyId(),
  479.                     ($unitPrice $mwstValue),
  480.                     $totalPrice,
  481.                     false
  482.                 ),
  483.             ]));
  484.             $x++;
  485.         }
  486.     }
  487.     /**
  488.      * @param ProductPageLoadedEvent $event
  489.      */
  490.     public function onProductPageLoaded(ProductPageLoadedEvent|SalesChannelEntityLoadedEvent $event)
  491.     {
  492.         $this->event $event;
  493.         /**
  494.          * Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
  495.          */
  496.         if (!($this->customerNumber $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
  497.             get_class($this)))) {
  498.             return;
  499.         } else {
  500.             if (array_key_exists('custom_product_listing_numbers',
  501.                 $this->event->getSalesChannelContext()->getCustomer()->getCustomFields())) {
  502.                 $articles explode(',',
  503.                     $this->event->getSalesChannelContext()->getCustomer()->getCustomFields()['custom_product_listing_numbers']);
  504.                 if (!in_array($this->event->getPage()->getProduct()->getProductNumber(), $articles)) {
  505.                     $redirectResponse = new RedirectResponse($this->router->generate('frontend.home.page'));
  506.                     return $redirectResponse->send();
  507.                 }
  508.             }
  509.         }
  510.         /**
  511.          * Configeinstellungen prüfen und setzen.
  512.          */
  513.         if (!$this->checkConfig()) {
  514.             return;
  515.         }
  516.         /**
  517.          * @var SalesChannelProductEntity $product
  518.          */
  519.         $product $this->event->getPage()->getProduct();
  520.         /**
  521.          * @todo Die position-id noch richtig machen
  522.          * @var array $article
  523.          */
  524.         $article['position-id'] = 1;
  525.         $article['article-number'] = $product->getProductNumber();
  526.         $article['quantity'] = 1;
  527.         $article['quantity-unit'] = $product->getPackUnit();
  528.         $this->result $this->getArticleInfo([$article]);
  529.         if ($this->result['head']['status'] == 'NOK') {
  530.             return;
  531.         }
  532.         // $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
  533.         $totalPrice = (float)$this->result['articles']['article']['price'];
  534.         $unitPrice $totalPrice / (float)$this->result['articles']['article']['quantity'];
  535.         $mwstRate 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
  536.         $mwstValue 0.0// (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
  537.         /**
  538.          * @var CalculatedPrice $price
  539.          */
  540.         $price $product->getCalculatedPrice();
  541.         /**
  542.          * @var TaxRule $newTaxRule
  543.          */
  544.         $newTaxRule = new TaxRule($mwstRate);
  545.         /** a
  546.          * @var TaxRuleCollection $newTaxRuleCollection
  547.          */
  548.         $newTaxRuleCollection = new TaxRuleCollection();
  549.         $newTaxRuleCollection->add($newTaxRule);
  550.         /**
  551.          * @var CalculatedTax $newCalculateTax
  552.          */
  553.         $newCalculateTax = new CalculatedTax($mwstValue$mwstRate$unitPrice);
  554.         /**
  555.          * @var CalculatedTaxCollection $newCalculatedTaxCollection
  556.          */
  557.         $newCalculatedTaxCollection = new CalculatedTaxCollection();
  558.         $newCalculatedTaxCollection->add($newCalculateTax);
  559.         /**
  560.          * @var TaxRule $newTaxRule
  561.          */
  562.         $newTaxRule = new TaxRule(0.0); // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value']);
  563.         $price->getTaxRules()->add($newTaxRule);
  564.         // Get from request or pick default
  565.         if (array_key_exists('free-shipping-threshold'$this->result['shipping-cost'])) {
  566.             $freeShippingThreshold = ['freeShippingThreshold' => $this->result['shipping-cost']['free-shipping-threshold']];
  567.         } else {
  568.             $freeShippingThreshold = ['freeShippingThreshold' => $this->calculateShippingThreshold($event->getSalesChannelContext())];
  569.         }
  570.         $this->event->getContext()->addArrayExtension('deliveryInformations'$freeShippingThreshold);
  571.         /**
  572.          * @var CalculatedPrice $newPrice
  573.          */
  574.         $newPrice = new CalculatedPrice(
  575.             $totalPrice,
  576.             $totalPrice,
  577.             $newCalculatedTaxCollection,
  578.             $newTaxRuleCollection,
  579.             1,
  580.             null,
  581.             ListPrice::createFromUnitPrice($totalPrice$totalPrice)
  582.         );
  583.         $newCalculatedCheapestPrice = new CalculatedCheapestPrice($totalPrice$totalPrice$newCalculatedTaxCollection,
  584.             $newTaxRuleCollection1);
  585.         $product->setCalculatedCheapestPrice($newCalculatedCheapestPrice);
  586.         $product->setCalculatedPrice($newPrice);
  587.         $product->getPrice()->first()->setNet($totalPrice);
  588.         $product->getPrice()->first()->setGross($totalPrice);
  589.         $product->getCheapestPrice()->getPrice()->first()->setGross($totalPrice);
  590.         $product->getCheapestPrice()->getPrice()->first()->setNet($totalPrice);
  591.         $product->getCalculatedPrices()->clear();
  592.         $product->setCheapestPrice(null);
  593.         $newCPContainer = new CheapestPriceContainer([]);
  594.         $product->setCheapestPriceContainer($newCPContainer);
  595.     }
  596.     /**
  597.      * @param array $articles
  598.      * @param int $shopInstance
  599.      * @param string $currency
  600.      * @param string $language
  601.      * @return
  602.      */
  603.     private function getArticleInfo(
  604.         array $articles,
  605.         int $shopInstance 1,
  606.         string $language 'DE'
  607.     ) {
  608.         $params = [
  609.             'customer' => $this->customerNumber,
  610.             'shop-instance' => $shopInstance,
  611.             'currency' => $this->getCurrencyISO()
  612.         ];
  613.         $buildRequestHelper = new BuildRequestHelper($params$articles$this->errorHandler);
  614.         /**
  615.          * @var string $request
  616.          */
  617.         $request $this->requestHandler->buildRequest($buildRequestHelperget_class($this));
  618.         $this->error $this->errorHandler->error($this->errorHandler::GENERAL_ERRORget_class($this),
  619.             "Request: " print_r($requesttrue));
  620.         $this->errorHandler->writeErrorLog($this->error);
  621.         if ($request !== '') {
  622.             /**
  623.              * @var string $result
  624.              */
  625.             $result $this->requestHandler->sendRequest($this->host$this->portself::PATH$request);
  626.         } else {
  627.             $this->error $this->errorHandler->error($this->errorHandler::UNKNOWN_REQUESTget_class($this),
  628.                 'Fehler im Aufbau des Request');
  629.             $this->errorHandler->writeErrorLog($this->error);
  630.             return;
  631.         }
  632.         /**
  633.          * @var array $resultArray
  634.          */
  635.         $resultArray $this->requestHandler->parseResult($result);
  636.         if ($resultArray['head']['status'] == 'NOK') {
  637.             $this->error $this->errorHandler->error($this->errorHandler::SAP_RESPONSE_ERRORget_class($this),
  638.                 'SAP Status Fehler. - ' print_r($resultArraytrue));
  639.             $this->errorHandler->writeErrorLog($this->error);
  640.             // } else {
  641.             //  $this->error = $this->errorHandler->error($this->errorHandler::WRONG_ARTICLE_ERROR, get_class($this), 'LivePrice-Response after success. - ' . print_r($resultArray, true));
  642.             //  $this->errorHandler->writeErrorLog($this->error);
  643.         }
  644.         return $resultArray;
  645.     }
  646.     /**
  647.      * @return bool
  648.      */
  649.     private function checkConfig(): bool
  650.     {
  651.         if ($this->foundationService->getConfigStatus()) {
  652.             $this->host $this->foundationService->getHost();
  653.             $this->port $this->foundationService->getPort();
  654.             return true;
  655.         }
  656.         $this->error $this->errorHandler->error($this->errorHandler::CONFIG_NOT_VALIDget_class($this),
  657.             'Konnte die Konfiguration nicht ermitteln');
  658.         $this->errorHandler->writeErrorLog($this->error);
  659.         return false;
  660.     }
  661.     /**
  662.      * @param string $productID
  663.      * @return ProductEntity
  664.      */
  665.     public function getProduct(string $productID, array $associations null): ProductEntity
  666.     {
  667.         /**
  668.          * @var Criteria $criteria
  669.          */
  670.         $criteria = new Criteria([$productID]);
  671.         if ($associations !== null) {
  672.             foreach ($associations as $association) {
  673.                 $criteria->addAssociation($association);
  674.             }
  675.         }
  676.         /**
  677.          * @var EntityCollection $products
  678.          */
  679.         $products $this->productRepository->search($criteria,
  680.             \Shopware\Core\Framework\Context::createDefaultContext());
  681.         /**
  682.          * @var array $productList
  683.          */
  684.         $productList $products->getElements();
  685.         /**
  686.          * @var ProductEntity $product
  687.          */
  688.         $product $productList[$productID];
  689.         return $product;
  690.     }
  691.     /**
  692.      * fts - Retrieve currency iso code from saleschannel context
  693.      * @return string
  694.      */
  695.     function getCurrencyISO(): string
  696.     {
  697.         return $this->event?->getSalesChannelContext()?->getCurrency()->getIsoCode() ?? "EUR";
  698.     }
  699.     /**
  700.      * If the current currency is not EUR, then the default threshold (250€)
  701.      * will be converted to the appropriate currency using the currency factor.
  702.      * @return int
  703.      */
  704.     private function calculateShippingThreshold(SalesChannelContext $context): int
  705.     {
  706.         if($context->getCurrency()->getIsoCode() === "PLN") {
  707.             return OrderSimulateSubscriber::ZLOTY_THRESHOLD;
  708.         } else {
  709.             return OrderSimulateSubscriber::EUR_THRESHOLD;
  710.         }
  711.     }
  712. }