From c3428b51a0d0f192b34cf674255061b9a7e211c4 Mon Sep 17 00:00:00 2001 From: Returnless Date: Fri, 23 Jul 2021 01:43:30 +0300 Subject: [PATCH] Implemented PartnersSourceAdapter, IntegrationPartner *Implemented method getOrderByMagento *Implemented method *Added Marketplace configs *Set v1.1.1 *Added partnersResource type --- Model/Api/OrderInfo.php | 84 ++++++------ Model/Config.php | 40 ++++++ Model/Config/Source/IntegrationPartner.php | 26 ++++ Model/PartnersSourceAdapter.php | 151 +++++++++++++++++++++ composer.json | 2 +- etc/adminhtml/system.xml | 11 ++ etc/di.xml | 11 ++ etc/module.xml | 2 +- 8 files changed, 287 insertions(+), 40 deletions(-) create mode 100644 Model/Config/Source/IntegrationPartner.php create mode 100644 Model/PartnersSourceAdapter.php diff --git a/Model/Api/OrderInfo.php b/Model/Api/OrderInfo.php index 676cca4..6d6d8ec 100644 --- a/Model/Api/OrderInfo.php +++ b/Model/Api/OrderInfo.php @@ -2,15 +2,16 @@ namespace Returnless\Connector\Model\Api; +use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\Module\ResourceInterface; -use Returnless\Connector\Api\OrderInfoInterface; use Magento\Sales\Model\OrderRepository; +use Returnless\Connector\Api\OrderInfoInterface; use Magento\Catalog\Model\ProductRepository; -use Magento\Framework\Api\SearchCriteriaBuilder; use Psr\Log\LoggerInterface; use Magento\Catalog\Helper\Image; use Returnless\Connector\Model\Config; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\App\ObjectManager; /** * Interface OrderInfo @@ -45,21 +46,11 @@ class OrderInfo implements OrderInfoInterface */ protected $logger; - /** - * @var OrderRepository - */ - protected $orderRepository; - /** * @var ProductRepository */ protected $productRepository; - /** - * @var SearchCriteriaBuilder - */ - protected $searchCriteriaBuilder; - /** * @var Image */ @@ -70,33 +61,44 @@ class OrderInfo implements OrderInfoInterface */ protected $config; + /** + * @var OrderRepository + */ + protected $orderRepository; + + /** + * @var SearchCriteriaBuilder + */ + protected $searchCriteriaBuilder; + /** * OrderInfo constructor. * - * @param OrderRepository $orderRepository * @param ProductRepository $productRepository - * @param SearchCriteriaBuilder $searchCriteriaBuilder * @param LoggerInterface $logger * @param Image $image * @param Config $config * @param ResourceInterface $moduleResource + * @param OrderRepository $orderRepository + * @param SearchCriteriaBuilder $searchCriteriaBuilder */ public function __construct( - OrderRepository $orderRepository, ProductRepository $productRepository, - SearchCriteriaBuilder $searchCriteriaBuilder, LoggerInterface $logger, Image $image, Config $config, - ResourceInterface $moduleResource - ) { - $this->orderRepository = $orderRepository; + ResourceInterface $moduleResource, + OrderRepository $orderRepository, + SearchCriteriaBuilder $searchCriteriaBuilder + ) + { $this->productRepository = $productRepository; - $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->logger = $logger; $this->image = $image; $this->config = $config; $this->moduleResource = $moduleResource; + $this->orderRepository = $orderRepository; + $this->searchCriteriaBuilder = $searchCriteriaBuilder; } /** @@ -112,7 +114,12 @@ public function getOrderInfoReturnless($incrementId) $this->logger->debug('[RET_ORDER_INFO] Increment Id', [$incrementId]); try { - $order = $this->getOrderByIncrementId($incrementId); + $order = $this->getOrderByMagento($incrementId); + if (!$order->getId() && $this->config->getMarketplaceSearchEnabled()) { + /** @var \Returnless\Connector\Model\PartnersSourceAdapter $partnersSourceAdapter */ + $partnersSourceAdapter = ObjectManager::getInstance()->get('Returnless\Connector\Model\PartnersSourceAdapter'); + $order = $partnersSourceAdapter->getOrderByMarketplace($incrementId); + } $orderInfo['id'] = $order->getIncrementId(); $orderInfo['order_id'] = $order->getEntityId(); @@ -253,6 +260,24 @@ public function getOrderInfoReturnless($incrementId) $this->returnResult($response); } + /** + * @param $incrementId + * @param string $searchKey + * @return \Magento\Framework\DataObject + */ + private function getOrderByMagento($incrementId, $searchKey = 'increment_id') + { + $searchCriteria = $this->searchCriteriaBuilder + ->addFilter( + $searchKey, + $incrementId, + 'eq' + ) + ->create(); + + return $this->orderRepository->getList($searchCriteria)->getFirstItem(); + } + /** * This method provides an ability to return Response Data * @@ -307,23 +332,6 @@ public function getImageByProduct($product) return $image; } - /** - * @param $incrementId - * @return mixed - */ - protected function getOrderByIncrementId($incrementId) - { - $searchCriteria = $this->searchCriteriaBuilder - ->addFilter( - 'increment_id', - $incrementId, - 'eq' - ) - ->create(); - - return $this->orderRepository->getList($searchCriteria)->getFirstItem(); - } - /** * @param $result */ diff --git a/Model/Config.php b/Model/Config.php index 06ff5c5..47327af 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -35,6 +35,16 @@ class Config */ const CONFIG_SEPARATE_BUNDLE_PRODUCTS = 'returnless_connector/general/bundle_enabled'; + /** + * const Path to config enabled marketplace search + */ + const CONFIG_MARKETPLACE_SEARCH_ENABLED = 'returnless_connector/marketplace_orders/enabled'; + + /** + * const Path to config marketplace integration partner + */ + const CONFIG_MARKETPLACE_SEARCH_VENDOR_ID = 'returnless_connector/marketplace_orders/integration_partner'; + /** * Config constructor. * @@ -61,6 +71,36 @@ public function getEnabled($store = null) return $enabled; } + /** + * @param null $store + * @return string + */ + public function getMarketplaceSearchEnabled($store = null) + { + $enabled = (string)$this->scopeConfig->getValue( + self::CONFIG_MARKETPLACE_SEARCH_ENABLED, + ScopeInterface::SCOPE_STORE, + $store + ); + + return $enabled; + } + + /** + * @param null $store + * @return string + */ + public function getMarketplaceSearchPartnerId($store = null) + { + $enabled = (string)$this->scopeConfig->getValue( + self::CONFIG_MARKETPLACE_SEARCH_VENDOR_ID, + ScopeInterface::SCOPE_STORE, + $store + ); + + return $enabled; + } + /** * @param null $store * @return string diff --git a/Model/Config/Source/IntegrationPartner.php b/Model/Config/Source/IntegrationPartner.php new file mode 100644 index 0000000..061dfb2 --- /dev/null +++ b/Model/Config/Source/IntegrationPartner.php @@ -0,0 +1,26 @@ + 'vendiro', + 'label' => __('Vendiro') + ] + ]; + } +} diff --git a/Model/PartnersSourceAdapter.php b/Model/PartnersSourceAdapter.php new file mode 100644 index 0000000..4ca9ff4 --- /dev/null +++ b/Model/PartnersSourceAdapter.php @@ -0,0 +1,151 @@ +config = $config; + $this->logger = $logger; + $this->orderRepository = $orderRepository; + $this->searchCriteriaBuilder = $searchCriteriaBuilder; + $this->partnersResource = $partnersResource; + } + + /** + * @deprecated + * + * @param $orderId + * @return \Magento\Framework\DataObject|mixed + */ + public function getOrderById($orderId) + { + $order = $this->getOrderByMagento($orderId); + if (!$order->getId() && $this->config->getMarketplaceSearchEnabled()) { + try { + $order = $this->getOrderByMarketplace($orderId); + } catch (\Throwable $e) { + $this->logger->critical('Returnless Error PartnersSourceAdapter', ['exception' => $e]); + } + } + + return $order; + } + + /** + * @param $orderId + * @return \Magento\Framework\DataObject|mixed + */ + public function getOrderByMarketplace($orderId) + { + $partnerId = $this->config->getMarketplaceSearchPartnerId(); + $collection = $this->getPartnersCollectionModel($partnerId); + $partnerOrder = $collection->addFieldToFilter( + $this->partnersResource[$partnerId]['keyForSearch'], + ['eq' => $orderId] + ) + ->addFieldToSelect('order_id') + ->getFirstItem(); + $order = $this->getOrderByMagento($partnerOrder->getOrderId(), 'entity_id'); + + return $order; + } + + /** + * @param $incrementId + * @param string $searchKey + * @return \Magento\Framework\DataObject + */ + private function getOrderByMagento($incrementId, $searchKey = 'increment_id') + { + $searchCriteria = $this->searchCriteriaBuilder + ->addFilter( + $searchKey, + $incrementId, + 'eq' + ) + ->create(); + + return $this->orderRepository->getList($searchCriteria)->getFirstItem(); + } + + /** + * @param $partnerId + * @return false|mixed + */ + private function getPartnersCollectionModel($partnerId) + { + $result = false; + if (!empty($this->partnersResource[$partnerId])) { + $result = $this->partnersResource[$partnerId]['model']; + } + + return $result; + } + + /** + * @return array + */ + public function getIntegrationPartners() + { + $result = []; + if(!empty($this->partnersResource)) { + foreach ($this->partnersResource as $partnerIntegrationCode => $value) { + $result[] = [ + 'value' => $partnerIntegrationCode, + 'label' => __($value['label']) + ]; + } + } + + return $result; + } +} diff --git a/composer.json b/composer.json index 3bc105e..6f6430c 100644 --- a/composer.json +++ b/composer.json @@ -17,5 +17,5 @@ "OSL-3.0", "AFL-3.0" ], - "version": "1.1.0" + "version": "1.1.1" } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index ee91ece..8868545 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -34,6 +34,17 @@ + + + + + Magento\Config\Model\Config\Source\Yesno + + + + Returnless\Connector\Model\Config\Source\IntegrationPartner + + diff --git a/etc/di.xml b/etc/di.xml index 3cc95dc..8f4b2c9 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -1,4 +1,15 @@ + + + + + Vendiro + marketplace_orderid + TIG\Vendiro\Model\ResourceModel\Order\Collection + + + + diff --git a/etc/module.xml b/etc/module.xml index f582b51..26ba2e4 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - +