Your IP : 216.73.217.13


Current Path : /var/www/www.indacotrentino.com/www/app/code/Torresani/MessageQueue/Observer/
Upload File :
Current File : //var/www/www.indacotrentino.com/www/app/code/Torresani/MessageQueue/Observer/Pushqueue.php

<?php

namespace Torresani\MessageQueue\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\MessageQueue\PublisherInterface;
use Torresani\MessageQueue\Api\MessageInterface;
use \Magento\Framework\App\DeploymentConfig;

class Pushqueue implements ObserverInterface
{
    public $orderFacory;
    public $publisher;
    public $message;
    private DeploymentConfig $deploymentConfig;

    public function __construct(
        \Magento\Sales\Model\Order $orderFacory,
        PublisherInterface $publisher,
        MessageInterface $message,
        DeploymentConfig $deploymentConfig,
        $name = null
    ) {
        $this->orderFacory = $orderFacory;
        $this->publisher = $publisher;
        $this->message = $message;
        $this->deploymentConfig = $deploymentConfig;
        //parent::__construct($name);
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $topic = 'order';
        $orderIds = $observer->getEvent()->getOrderIds();
        $lastorderId = $orderIds[0];
        try {
            $order = $this->orderFacory->load($lastorderId);

            foreach($order->getAllVisibleItems() as $item){
                $itemsData = array();
                $orderData[]= $item->getData();
                /*$orderData[$itemsData['price']]= $item->getPrice();
                $orderData[$itemsData['quoteItem_id']]= $item->getQuoteItemId();
                $orderData[$itemsData['item_id']]= $item->getItemId();*/
            }
//            $orderData[] = $order->getData();

            // read messagge on RabbitMQ
            //
            $action = "traditional";
            $activityRegister = "true";
            $activityErpinsert = "true";
            $activityWarehouse = "true";
            $activitySellerEmail = "false";
            $activityBooking = "true";
            $activityTransfer = "true";
            $activityPickinglist = "true";
            $activityShipmentshippypro = "true";
            $activityShipmentsurf = "false";
            $activityMail = "true";
            $activityPicking = "true";
            $activityPickup = "true";
            $activityClose = "true";
            $activityCancel = "false";

            // TODO Is marketplace or surf
            if ($order->getStore()->getWebsite()->getId() > 1) {
                $action = "surf";
                $activityRegister = "true";
                $activityErpinsert = "true";
                $activityWarehouse = "true";
                $activitySellerEmail = "false";
                $activityBooking = "true";
                $activityTransfer = "false";
                $activityPickinglist = "false";
                $activityShipmentshippypro = "false";
                $activityShipmentsurf = "false";
                $activityMail = "false";
                $activityPicking = "false";
                $activityPickup = "false";
                $activityClose = "false";
                $activityCancel = "false";
            }

            $message = '{
                "action": "' . $action . '",
                "indaco": {
                    "instance_id": "' . $this->deploymentConfig->get('indaco/instance_id') . '"
                },
                "order": {
                    "entity_id": "' . $order->getId() . '",
                    "increment_id": "' . $order->getIncrementId() .'",
                    "datetime": "' . $order->getCreatedAt() . '",
                    "store": {
                        "storeview_id": "' . $order->getStore()->getId() . '",
                        "storeview_code": "' . $order->getStore()->getCode() . '",
                        "store_id": "' . $order->getStore()->getGroup()->getId() . '",
                        "store_code": "' . $order->getStore()->getGroup()->getCode() . '",
                        "website_id": "' . $order->getStore()->getWebsite()->getId() . '",
                        "website_code": "' . $order->getStore()->getWebsite()->getCode() . '"
                    }
                },
                "activity": {
                    "register": ' . $activityRegister . ',
                    "erpInsert": ' . $activityErpinsert . ',
                    "warehouse": ' . $activityWarehouse . ',
                    "sellerEmail": ' . $activitySellerEmail .',
                    "booking": ' . $activityBooking . ',
                    "transfer": ' . $activityTransfer . ',
                    "pickingList": ' . $activityPickinglist . ',
                    "shipmentShippyPro": ' . $activityShipmentshippypro . ',
                    "shipmentSurf": ' . $activityShipmentsurf . ',
                    "mail": ' . $activityMail . ',
                    "picking": ' . $activityPicking . ',
                    "pickup": ' . $activityPickup . ',
                    "close": ' . $activityClose . ',
                    "cancel": ' . $activityCancel . '
                }
            }';
            $message = preg_replace('/\s+/', '', $message);

            $this->message->setMessage($message);
            $this->publisher->publish($topic, $this->message);

            return $this;

        } catch (Exception $e) {
            print_r($e->getMessage());
        }

    }


}