custom/plugins/NextagFlow/src/Flow/ProcessOrderAction.php line 82

Open in your IDE?
  1. <?php
  2. namespace Nextag\Flow\Flow;
  3. use Exception;
  4. use Elasticsearch\ClientBuilder;
  5. use Nextag\Checkout\Content\Checkout\CheckoutService;
  6. use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
  7. use Shopware\Core\Framework\Event\FlowEvent;
  8. use Nextag\Flow\Helper\Log;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. class ProcessOrderAction extends FlowAction
  13. {
  14.     private Log $log;
  15.     private \Nextag\Mail\Helper\MailGenerator $mailGenerator;
  16.     private $checkoutService;
  17.     private $giftcardHelper;
  18.     private $promotionHelper;
  19.     private $orderExtensionRepository;
  20.     private $config;
  21.     private $wordpress;
  22.     private $productRepository;
  23.     private $languageRepository;
  24.     private $elasticClient null;
  25.     protected $eventHelper;
  26.     protected $abacus;
  27.     protected $indexPrefix;
  28.     protected $hostname;
  29.     protected $port;
  30.     protected $locales;
  31.     protected $languageCache;
  32.     public function __construct(
  33.         \Shopware\Core\System\SystemConfig\SystemConfigService $config,
  34.         Log $log,
  35.         EntityRepositoryInterface $orderExtensionRepository,
  36.         \Nextag\Mail\Helper\MailGenerator $mailGenerator,
  37.         CheckoutService $checkoutService,
  38.         \Nextag\Giftcard\Helper\Giftcard $giftcardHelper,
  39.         \Nextag\Product\Helper\Event $eventHelper,
  40.         \Nextag\Giftcard\Helper\Promotion $promotionHelper,    
  41.         \Nextag\Checkout\Helper\Abacus $abacus,
  42.         \Nextag\Connector\Helper\Wordpress $wordpress,
  43.         EntityRepositoryInterface $productRepository,
  44.         EntityRepositoryInterface $languageRepository
  45.     ) {
  46.         $this->config $config;
  47.         $this->log $log;
  48.         $this->orderExtensionRepository $orderExtensionRepository;
  49.         $this->checkoutService $checkoutService;
  50.         $this->giftcardHelper $giftcardHelper;
  51.         $this->eventHelper $eventHelper;
  52.         $this->promotionHelper $promotionHelper;
  53.         $this->mailGenerator $mailGenerator;      
  54.         $this->abacus $abacus;
  55.         $this->wordpress $wordpress;
  56.         $this->productRepository $productRepository;
  57.         $this->languageRepository $languageRepository;        
  58.     }
  59.     public static function getName(): string
  60.     {
  61.         return 'action.flow.nextag.processorder';
  62.     }
  63.     public static function getSubscribedEvents(): array
  64.     {
  65.         return [
  66.             self::getName() => 'handle'
  67.         ];
  68.     }
  69.     public function requirements(): array
  70.     {
  71.         return [];
  72.     }
  73.     public function handle(FlowEvent $event): void
  74.     {
  75.         try {
  76.             $this->log->info("Processorderaction start");
  77.             $language "de";
  78.             $context $event->getContext();
  79.             $config $event->getConfig();
  80.             $order null;
  81.             try {
  82.                 $order $event->getEvent()->getOrder();
  83.             } catch (Exception $e) {
  84.                 $this->log->error($e);
  85.                 $this->log->error(new \Exception("Flow ProcessOrder: Order not found"));
  86.                 return;
  87.             }
  88.             if ($order === null) {
  89.                 $this->log->error(new \Exception("Flow ProcessOrder: Order not found"));
  90.                 return;
  91.             }
  92.             $this->log->info("Ordernumber: ".$order->getOrdernumber());
  93.             try {
  94.                 if (isset($config["processgiftcards"]) && $config["processgiftcards"]["status"]) {
  95.                     $this->log->info("process giftcards");
  96.                     $this->giftcardHelper->processGiftcardsUsed($order$context);
  97.                     $this->giftcardHelper->processGiftcardsBuys($order$language$context);
  98.                 }
  99.                 if (isset($config["processpromotions"]) && $config["processpromotions"]["status"]) {
  100.                     $this->log->info("process promotions");
  101.                     $this->promotionHelper->processPromotionUsed($order$context);
  102.                 }
  103.                 if (isset($config['processwordpressevents']) && $config['processwordpressevents']['status']) {
  104.                     $this->log->info("processwordpressevents");
  105.                     $this->wordpress->processWordpressEvents($order);   
  106.                 } else {
  107.                     $this->log->info("not processwordpressevents");
  108.                 }
  109.             } catch (Exception $e) {
  110.                 $this->log->error($e);
  111.             }
  112.             $resultExportOrder true;
  113.          
  114.             try {
  115.                 $criteria = new Criteria();
  116.                 $criteria->addFilter(new EqualsFilter("orderId"$order->getId()));
  117.                 $orderExtension $this->orderExtensionRepository->search($criteria$context)->getEntities()->first();
  118.                 $data = [];
  119.                 if ($orderExtension) {
  120.                     $data["id"] = $orderExtension->getId(); 
  121.                     if ($orderExtension->getComment()) {
  122.                         $data["comment"] = $orderExtension->getComment();
  123.                     }
  124.                     if ($orderExtension->getDeliverydate()) {
  125.                         $data["deliverydate"] = $orderExtension->getDeliverydate();
  126.                     }
  127.                     if ($orderExtension->getDeliverytype()) {
  128.                         $data["deliverytype"] = $orderExtension->getDeliverytype();
  129.                     }
  130.                     if ($orderExtension->getDeliverylocation()) {
  131.                         $data["deliverylocation"] = $orderExtension->getDeliverylocation();
  132.                     }
  133.                     if ($orderExtension->getDeliverylocationcomment()) {
  134.                         $data["deliverylocationcomment"] = $orderExtension->getDeliverylocationcomment();
  135.                     }
  136.                     if ($orderExtension->getGiftmessagefrom()) {
  137.                         $data["giftmessagefrom"] = $orderExtension->getGiftmessagefrom();
  138.                     }
  139.                     if ($orderExtension->getGiftmessageto()) {
  140.                         $data["giftmessageto"] = $orderExtension->getGiftmessageto();
  141.                     }
  142.                     if ($orderExtension->getGiftmessage()) {
  143.                         $data["giftmessage"] = $orderExtension->getGiftmessage();
  144.                     }
  145.                     if ($orderExtension->getGiftbox1qty()) {
  146.                         $data["giftbox1qty"] = $orderExtension->getGiftbox1qty();
  147.                     }
  148.                     if ($orderExtension->getGiftbox2qty()) {
  149.                         $data["giftbox2qty"] = $orderExtension->getGiftbox2qty();
  150.                     }
  151.                     if ($orderExtension->getGiftbox3qty()) {
  152.                         $data["giftbox3qty"] = $orderExtension->getGiftbox3qty();
  153.                     }
  154.                     if ($orderExtension->getGiftbox4qty()) {
  155.                         $data["giftbox14qty"] = $orderExtension->getGiftbox4qty();
  156.                     }
  157.                     if ($orderExtension->getGiftbox5qty()) {
  158.                         $data["giftbox5qty"] = $orderExtension->getGiftbox5qty();
  159.                     }
  160.                     if ($orderExtension->getGiftbox6qty()) {
  161.                         $data["giftbox6qty"] = $orderExtension->getGiftbox6qty();
  162.                     }
  163.                     if ($orderExtension->getGiftbox7qty()) {
  164.                         $data["giftbox7qty"] = $orderExtension->getGiftbox7qty();
  165.                     }
  166.                     if ($orderExtension->getGiftbox8qty()) {
  167.                         $data["giftbox8qty"] = $orderExtension->getGiftbox8qty();
  168.                     }
  169.                     if ($orderExtension->getGiftbox1comment()) {
  170.                         $data["giftbox1comment"] = $orderExtension->getGiftbox1comment();
  171.                     }
  172.                     if ($orderExtension->getGiftbox2comment()) {
  173.                         $data["giftbox2comment"] = $orderExtension->getGiftbox2comment();
  174.                     }
  175.                     if ($orderExtension->getGiftbox3comment()) {
  176.                         $data["giftbox3comment"] = $orderExtension->getGiftbox3comment();
  177.                     }
  178.                     if ($orderExtension->getGiftbox4comment()) {
  179.                         $data["giftbox4comment"] = $orderExtension->getGiftbox4comment();
  180.                     }
  181.                     if ($orderExtension->getGiftbox5comment()) {
  182.                         $data["giftbox5comment"] = $orderExtension->getGiftbox5comment();
  183.                     }
  184.                     if ($orderExtension->getGiftbox6comment()) {
  185.                         $data["giftbox6comment"] = $orderExtension->getGiftbox6comment();
  186.                     }
  187.                     if ($orderExtension->getGiftbox7comment()) {
  188.                         $data["giftbox7comment"] = $orderExtension->getGiftbox7comment();
  189.                     }
  190.                     if ($orderExtension->getGiftbox8comment()) {
  191.                         $data["giftbox8comment"] = $orderExtension->getGiftbox8comment();
  192.                     }
  193.                     if ($orderExtension->getRetouren()) {
  194.                         $data["retouren"] = $orderExtension->getRetouren();
  195.                     }
  196.                     if ($orderExtension->getVoigtkundennummer()) {
  197.                         $data["voigtkundennummer"] = $orderExtension->getVoigtkundennummer();
  198.                     }
  199.                     if ($orderExtension->getLanguage()) {
  200.                         $data["language"] = $orderExtension->getLanguage();
  201.                     }
  202.                     if ($resultExportOrder) {
  203.                         $data["exportordererp"] = 1;
  204.                     } else {
  205.                         $data["exportordererp"] = 2;
  206.                     }
  207.                     $this->checkoutService->save($data$event->getEvent());
  208.                 }
  209.             } catch (Exception $e) {
  210.                 $this->log->error($e);
  211.             }
  212.             $config["exportorder"]["result"] = $resultExportOrder;
  213.             $this->reduceProductStocks($context$order);
  214.             $this->log->info("process order emails");
  215.             $this->mailGenerator->processOrderEmails($order$config$orderExtension);
  216.             $this->eventHelper->sendEvent($order,"de-CH");   
  217.            
  218.             try {
  219.                if ($config["exportorder"]["status"]) {
  220.                     if ($this->config->get("NextagCheckout.config.abacusExportActive")) {
  221.                         $this->log->info("Abacus order export active"false);
  222.                         $this->abacus->exportOrder($order,$context);
  223.                     } else {
  224.                         $this->log->info("Abacus order export inactive",false);
  225.                     }            
  226.                }
  227.             } catch (Exception $e) {
  228.                 $this->log->error($e);
  229.             }
  230.             $this->log->info("Processorderaction end");
  231.         } catch (\Exception $e) {
  232.             $this->log->error($e);
  233.             throw $e;
  234.         }
  235.     }
  236.     private function reduceProductStocks($context,$order) {
  237.         try {
  238.             $lc $this->config->get("NextagFilter.config.languageCodes");
  239.             if ($lc) {
  240.                 $this->locales array_map(
  241.                     'trim',
  242.                     explode(","$lc)
  243.                 );
  244.             }
  245.             $criteria = new Criteria();
  246.             $criteria->addAssociation('locale');
  247.             $lc $this->languageRepository->search($criteria$context);
  248.             foreach ($lc as $k => $v) {
  249.                 $this->languageCache[str_replace("-""_"$v->getLocale()->getCode())] = $k;
  250.             }
  251.             foreach($order->getLineItems() as $lineItem) {
  252.                 $customFields $lineItem->getPayload()["customFields"];
  253.                 $customData = [];
  254.                 if (isset($lineItem->getPayload()["customData"]) && !is_array($lineItem->getPayload()["customData"])) {
  255.                     $customData json_decode($lineItem->getPayload()["customData"], true);
  256.                 }
  257.                 if (!isset($customData["isGiftcard"])  && !isset($lineItem->getPayload()["isShopPromotion"]) && !isset($lineItem->getPayload()["isUsedPromotion"]) && !isset($lineItem->getPayload()["isGiftcardUsed"])) {
  258.                     $availableQty $lineItem->getPayload()["stock"];
  259.                     $quantity $lineItem->getQuantity();
  260.                     $newQuantity $availableQty $quantity;
  261.                     if ($newQuantity 0) {
  262.                         $newQuantity 0;                        
  263.                     }
  264.                     if ($newQuantity == 0) {
  265.                         $d = [
  266.                             "id" => $lineItem->getReferencedId(),
  267.                             "stock" => $newQuantity,
  268.                             "customFields" => [
  269.                                 "custom_produkt_label_produkt_ausverkauft" => true
  270.                             ]
  271.                         ];
  272.                     } else {
  273.                         $d = [
  274.                             "id" => $lineItem->getReferencedId(),
  275.                             "stock" => $newQuantity
  276.                         ];
  277.                     }  
  278.                                       
  279.                     $this->updateElasticsearchProduct($lineItem,$d);                 
  280.                     $data[] = $d;
  281.                 }               
  282.             }
  283.             if (!empty($data)) {
  284.                 $this->productRepository->upsert($data,$context);            
  285.             }
  286.         } catch (Exception $e) {
  287.             $this->log->error($e);
  288.         }
  289.     }
  290.     private function updateElasticsearchProduct($lineItem$data)
  291.     {
  292.         try {
  293.             //Elasticindexes updaten
  294.             $this->indexPrefix $this->config->get("NextagFilter.config.elasticIndexprefix");
  295.             $this->hostname $this->config->get("NextagFilter.config.elasticHostname");
  296.             $this->port $this->config->get("NextagFilter.config.elasticPort");
  297.             
  298.             if ($this->hostname) {
  299.                 $hosts = [
  300.                     $this->hostname ":" $this->port
  301.                 ];
  302.                 if (!$this->elasticClient) {
  303.                     $this->elasticClient ClientBuilder::create()
  304.                     ->setHosts($hosts)->build();
  305.                 }
  306.             }
  307.        
  308.             foreach ($this->locales as $locale) {
  309.                 if (isset($this->languageCache[$locale])) {
  310.                     $languageId $this->languageCache[$locale];                                    
  311.                     $alias strtolower($this->indexPrefix $languageId);
  312.                     $searchParams = [
  313.                         "index" => $alias,
  314.                         "size" => 1,
  315.                         "q" => "productNumber:".$lineItem->getPayload()["productNumber"]
  316.                     ];
  317.                     $searchItem $this->elasticClient->search($searchParams);                   
  318.                     if (!empty($searchItem["hits"]["hits"])) {
  319.                         $item = [
  320.                             "stock" => $data["stock"]
  321.                         ];
  322.                         if (isset($data["customFields"]) && isset($data["customFields"]["custom_produkt_label_produkt_ausverkauft"])) {   
  323.                             $item["custom_produkt_label_produkt_ausverkauft"] = $data["customFields"]["custom_produkt_label_produkt_ausverkauft"];
  324.                             $item["custom_produkt_label_produkt_ausverkauft_lowercase"] = $data["customFields"]["custom_produkt_label_produkt_ausverkauft"];
  325.                         }
  326.                         $payload = [
  327.                             'index' => $alias,
  328.                             'id' => (string) $searchItem["hits"]["hits"][0]["_source"]["elasticid"],
  329.                             'body' => ["doc" => $item]                        
  330.                         ];
  331.                         //Update
  332.                         $result $this->elasticClient->update($payload);
  333.                     }                    
  334.                 }
  335.             }           
  336.         } catch (\Exception $exception) {
  337.             $this->log->error($exception);
  338.         }
  339.     }
  340. }