rmaQueueCollectionFactory = $rmaQueueCollectionFactory; $this->rmaSync = $rmaSync; $this->rmaMapper = $rmaMapper; $this->rmaRepository = $rmaRepository; $this->orderMapper = $orderMapper; } /** * Fetch data from NetSuite * * @param int $pageSize * @param int $page * @return array */ public function fetch($pageSize = 50, $page = 1, $additionalParams = []) { $queue = $this->rmaQueueCollectionFactory->create(); $lastSyncRecordDate = $queue->getLastSyncRecordDate(); if ($lastSyncRecordDate) { $lastSyncRecordDate = $this->helper->convertUtcToLocalTime($lastSyncRecordDate, 'm/d/Y g:i a'); } // only fetch records in the past year $yearAgo = date('m/d/Y', strtotime('-1 year')); if (!$lastSyncRecordDate || strtotime($lastSyncRecordDate) < strtotime($yearAgo)) { $lastSyncRecordDate = $yearAgo; } $filter = (bool)$lastSyncRecordDate ? ['filter' => ['datecreated', 'after', $lastSyncRecordDate]] : []; return $this->netSuiteAPI->fetchReturnAuthorizations($pageSize, $page, $filter); } /** * {@inheritdoc} */ public function getPageSize() { return $this->pageSize; } /** * Convert NetSuite data to Magento format * * @param array $data * @return array */ public function convertToMagentoFormat($data) { return $this->rmaMapper->convertToMagentoFormat($data); } /** * adds data to the queue. * * @param array $data * @return void */ public function enqueue($data) { $queue = $this->rmaQueueCollectionFactory->create(); foreach ($data as $item) { $queue->insertOrUpdate($item); } } /** * Get next queue items to process * * @param int $batchSize * @return array */ public function dequeue($batchSize = 50) { return $this->rmaQueueCollectionFactory->create()->getNext($batchSize); } /** * Process queue item * * @param $queueItem * @return array */ public function processQueueItem($queueItem) { $data = $this->serializer->unserialize($queueItem->getData('data')); if ($queueItem->getRmaId()) { // update the rma $result = $this->rmaSync->update($data, $queueItem->getRmaId()); } else { $order = $this->rmaSync->getOrder($data); // set the increment id to queue item for plugin to skip email sending $queueItem->setOrderIncrementId($order->getRealorderId()); $queueItem->save(); $result = $this->rmaSync->createOrSkip($data, $order); if (!isset($result['error']) && isset($result['data'])) { $queueItem->setRmaId($result['data']->getRmaId()); } } return $result; } }