helper = $helper; $this->logger = $logger; $this->json = $json; $this->netSuiteAPI = $netSuiteAPI; $this->orderMapper = $orderMapper; $this->customerMapper = $customerMapper; $this->ordersSyncService = $ordersSyncService; $this->customersSyncService = $customersSyncService; $this->customerQueueManager = $customerQueueManager; $this->orderRepository = $orderRepository; } /** * Run process from queue * * @param $data * @return void * @throws \Exception */ public function process($data) : void { // checking if the module is enabled if ($this->helper->getNetSuiteIsEnabled()) { // run process from queue and send request $data = $this->json->unserialize($data); // check if the order is already synced $order = $this->orderMapper->getOrderBy('increment_id', $data["order_id"]); if (!$order) { throw new \Exception('Order with #' . $data["order_id"] .' not found'); } if ($order->getNetSuiteId()) { return; } $filter = ['filter' => ['email', 'is', $data["customer_email"]]]; $result = $this->netSuiteAPI->fetchCustomersWithFilter(1, 1, $filter); if (!empty($result['error'])) { throw new \Exception('Error while fetching customer data: ' . $result['message']); } // customer is not found if (empty($result['data'])) { $this->logger->info("JR Netsuite Model Queue Consumer: Order " . $data["order_id"] . " Customer " . $data["customer_email"] . " not found"); return; // map customer data $mapCustomerData = $this->customerMapper->convertToNetSuiteFormatByEmail($data["customer_email"]); // update customer data to NetSuite $result = $this->netSuiteAPI->updateCustomer($mapCustomerData); if ($result['code'] == self::SUCCESS_RESULT) { $netsuiteCustomerId = $result['data']['internal_id']; // save the customer Id $this->customersSyncService->saveNetsuiteId($data["customer_email"], $netsuiteCustomerId); } else { throw new \Exception('Error while updating customer data: ' . $result['msg']); } } else { $netsuiteCustomerId = $result['data'][0]['internalid']; } // map order data $mapOrderData = $this->orderMapper->convertToNetSuiteFormat($order); // $this->logger->info('Map Order Data: ' . $this->json->serialize($mapOrderData)); $mapOrderData['entity'] = $netsuiteCustomerId; $result = $this->netSuiteAPI->updateOrder($mapOrderData); // update order status in case of successful result if ($result['code'] == self::SUCCESS_RESULT) { // save the order Id $this->ordersSyncService->saveNetsuiteId($order, $result['data']['internal_id']); } else { throw new \Exception('Error while updating order data: ' . $result['msg']); } } } }