Create customer if not exist within manual upload

This commit is contained in:
RenCurs 2021-07-19 10:37:41 +03:00 committed by GitHub
parent a1f97bc996
commit 4f15310fbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -39,7 +39,10 @@ class OrderManager {
public function createOrder($order_data, $order_products, $order_totals) { public function createOrder($order_data, $order_products, $order_totals) {
$order = $this->prepareOrder($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); $customer = $this->customer_manager->getCustomerForOrder($order_data);
if (!empty($customer)) { if (!empty($customer)) {
$order['customer'] = $customer; $order['customer'] = $customer;
@ -167,4 +170,15 @@ class OrderManager {
$this->api->ordersPaymentEdit($order_payment); $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');
}
} }

View File

@ -10,11 +10,32 @@ class OrderManagerTest extends TestCase {
public function testCreateOrderWithCustomer() { public function testCreateOrderWithCustomer() {
$proxy = $this->getMockBuilder(\RetailcrmProxy::class) $proxy = $this->getMockBuilder(\RetailcrmProxy::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->setMethods(['ordersCreate']) ->setMethods(['ordersCreate','customersGet'])
->getMock(); ->getMock();
$proxy->expects($this->once())->method('ordersCreate'); $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); $order_manager = $this->getOrderManager($proxy);
$orderCheckoutModel = $this->loadModel('checkout/order'); $orderCheckoutModel = $this->loadModel('checkout/order');