From 4f15310fbb08ee1605776f9f9a191e263055b428 Mon Sep 17 00:00:00 2001 From: RenCurs <34103666+RenCurs@users.noreply.github.com> Date: Mon, 19 Jul 2021 10:37:41 +0300 Subject: [PATCH] Create customer if not exist within manual upload --- .../retailcrm/lib/service/OrderManager.php | 16 ++++++++++++- tests/system/lib/service/OrderManagerTest.php | 23 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/upload/system/library/retailcrm/lib/service/OrderManager.php b/src/upload/system/library/retailcrm/lib/service/OrderManager.php index 74851af..fe80f76 100644 --- a/src/upload/system/library/retailcrm/lib/service/OrderManager.php +++ b/src/upload/system/library/retailcrm/lib/service/OrderManager.php @@ -39,7 +39,10 @@ class OrderManager { public function createOrder($order_data, $order_products, $order_totals) { $order = $this->prepareOrder($order_data, $order_products, $order_totals); - if (!isset($order['customer'])) { + if (!isset($order['customer']) + || (isset($order['customer']['externalId']) + && !$this->checkExistCustomer($order['customer']['externalId'])) + ) { $customer = $this->customer_manager->getCustomerForOrder($order_data); if (!empty($customer)) { $order['customer'] = $customer; @@ -167,4 +170,15 @@ class OrderManager { $this->api->ordersPaymentEdit($order_payment); } } + + /** + * @param string $customerExternalId Customer's external id + * + * @return bool + */ + private function checkExistCustomer($customerExternalId) { + $result = $this->api->customersGet($customerExternalId); + + return $result && $result->isSuccessful() && $result->offsetExists('customer'); + } } diff --git a/tests/system/lib/service/OrderManagerTest.php b/tests/system/lib/service/OrderManagerTest.php index dd809b9..0eb06fc 100644 --- a/tests/system/lib/service/OrderManagerTest.php +++ b/tests/system/lib/service/OrderManagerTest.php @@ -10,11 +10,32 @@ class OrderManagerTest extends TestCase { public function testCreateOrderWithCustomer() { $proxy = $this->getMockBuilder(\RetailcrmProxy::class) ->disableOriginalConstructor() - ->setMethods(['ordersCreate']) + ->setMethods(['ordersCreate','customersGet']) ->getMock(); $proxy->expects($this->once())->method('ordersCreate'); + $proxy->expects($this->once()) + ->method('customersGet') + ->willReturn(new \RetailcrmApiResponse( + 200, + json_encode( + [ + 'success' => true, + 'pagination' => [ + 'limit'=> 20, + 'totalCount' => 0, + 'currentPage' => 1, + 'totalPageCount' => 0 + ], + 'customer' => [ + 'id' => 1, + 'externalId' => 1 + ] + ] + ) + )); + $order_manager = $this->getOrderManager($proxy); $orderCheckoutModel = $this->loadModel('checkout/order');