diff --git a/src/Model/Observer/OrderCreate.php b/src/Model/Observer/OrderCreate.php index 3aa543e..7e5adea 100644 --- a/src/Model/Observer/OrderCreate.php +++ b/src/Model/Observer/OrderCreate.php @@ -15,6 +15,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface private $registry; private $order; private $serviceOrder; + private $serviceCustomer; /** * Constructor @@ -22,6 +23,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface * @param \Magento\Framework\Registry $registry * @param \Retailcrm\Retailcrm\Model\Logger\Logger $logger * @param \Retailcrm\Retailcrm\Model\Service\Order $serviceOrder + * @param \Retailcrm\Retailcrm\Model\Service\Customer $serviceCustomer * @param Helper $helper * @param ApiClient $api */ @@ -29,12 +31,14 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface \Magento\Framework\Registry $registry, \Retailcrm\Retailcrm\Model\Logger\Logger $logger, \Retailcrm\Retailcrm\Model\Service\Order $serviceOrder, + \Retailcrm\Retailcrm\Model\Service\Customer $serviceCustomer, Helper $helper, ApiClient $api ) { $this->logger = $logger; $this->registry = $registry; $this->serviceOrder = $serviceOrder; + $this->serviceCustomer = $serviceCustomer; $this->helper = $helper; $this->api = $api; $this->order = []; @@ -55,6 +59,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface return false; } + /** @var \Magento\Sales\Model\Order $order */ $order = $observer->getEvent()->getOrder(); $this->api->setSite($this->helper->getSite($order->getStore())); @@ -62,13 +67,8 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface return false; } - $billingAddress = $order->getBillingAddress(); $this->order = $this->serviceOrder->process($order); - - $this->setCustomer( - $order, - $billingAddress - ); + $this->setCustomer($order); Helper::filterRecursive($this->order); @@ -80,16 +80,22 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface } /** - * @param $order - * @param $billingAddress + * @param \Magento\Sales\Model\Order $order + * @param \Magento\Sales\Model\Order\Address $billingAddress */ - private function setCustomer($order, $billingAddress) + private function setCustomer($order) { if ($order->getCustomerIsGuest() == 1) { $customer = $this->getCustomerByEmail($order->getCustomerEmail()); if ($customer !== false) { $this->order['customer']['id'] = $customer['id']; + } else { + $newCustomer = $this->serviceCustomer->prepareCustomerFromOrder($order); + $response = $this->api->customersCreate($newCustomer); + if ($response && isset($response['id'])) { + $this->order['customer']['id'] = $response['id']; + } } } @@ -97,18 +103,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface if ($this->existsInCrm($order->getCustomerId(), 'customersGet')) { $this->order['customer']['externalId'] = $order->getCustomerId(); } else { - $preparedCustomer = [ - 'externalId' => $order->getCustomerId(), - 'firstName' => $order->getCustomerFirstname(), - 'lastName' => $order->getCustomerLastname(), - 'email' => $order->getCustomerEmail() - ]; - - if ($billingAddress->getTelephone()) { - $preparedCustomer['phones'][] = [ - 'number' => $billingAddress->getTelephone() - ]; - } + $preparedCustomer = $this->serviceCustomer->process($order->getCustomer()); if ($this->api->customersCreate($preparedCustomer)) { $this->order['customer']['externalId'] = $order->getCustomerId(); diff --git a/src/Model/Service/Customer.php b/src/Model/Service/Customer.php index bce18f8..1ba449c 100644 --- a/src/Model/Service/Customer.php +++ b/src/Model/Service/Customer.php @@ -25,7 +25,6 @@ class Customer implements CustomerManagerInterface * @param \Magento\Customer\Model\Customer $customer * * @return array $preparedCustomer - * @throws \Magento\Framework\Exception\LocalizedException */ public function process(\Magento\Customer\Model\Customer $customer) { @@ -60,6 +59,49 @@ class Customer implements CustomerManagerInterface return $preparedCustomer; } + /** + * @param \Magento\Sales\Model\Order $order + * + * @return array + */ + public function prepareCustomerFromOrder(\Magento\Sales\Model\Order $order) + { + $billing = $order->getBillingAddress(); + + $preparedCustomer = [ + 'externalId' => uniqid(), + 'email' => $billing->getEmail(), + 'firstName' => $billing->getFirstname(), + 'patronymic' => $billing->getMiddlename(), + 'lastName' => $billing->getLastname(), + 'createdAt' => $order->getCreatedAt(), + 'address' => [ + 'countryIso' => $billing->getCountryId(), + 'index' => $billing->getPostcode(), + 'region' => $billing->getRegion(), + 'city' => $billing->getCity(), + 'street' => $billing->getStreet(), + 'text' => sprintf( + '%s %s %s %s', + $billing->getPostcode(), + $billing->getRegion(), + $billing->getCity(), + $billing->getStreet() + ) + ] + ]; + + if ($billing->getTelephone()) { + $preparedCustomer['phones'] = [ + [ + 'number' => $billing->getTelephone() + ] + ]; + } + + return $preparedCustomer; + } + private function getAddress(\Magento\Customer\Model\Customer $customer) { $billingAddress = $customer->getDefaultBillingAddress(); diff --git a/src/Model/Service/Order.php b/src/Model/Service/Order.php index a130aac..5b34420 100644 --- a/src/Model/Service/Order.php +++ b/src/Model/Service/Order.php @@ -34,7 +34,6 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface { $items = $order->getAllItems(); $products = $this->addProducts($items); - $billingAddress = $order->getBillingAddress(); $shippingAddress = $order->getShippingAddress(); $shipping = $this->getShippingCode($order->getShippingMethod()); @@ -42,11 +41,11 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface 'externalId' => $order->getId(), 'number' => $order->getRealOrderId(), 'createdAt' => $order->getCreatedAt(), - 'lastName' => $order->getCustomerLastname(), - 'firstName' => $order->getCustomerFirstname(), - 'patronymic' => $order->getCustomerMiddlename(), - 'email' => $order->getCustomerEmail(), - 'phone' => $billingAddress->getTelephone(), + 'lastName' => $shippingAddress->getLastname(), + 'firstName' => $shippingAddress->getFirstname(), + 'patronymic' => $shippingAddress->getMiddlename(), + 'email' => $shippingAddress->getEmail(), + 'phone' => $shippingAddress->getTelephone(), 'status' => $this->config->getValue('retailcrm/retailcrm_status/' . $order->getStatus()), 'items' => $products, 'delivery' => [ @@ -73,8 +72,8 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface ] ]; - if ($billingAddress->getData('country_id')) { - $preparedOrder['countryIso'] = $billingAddress->getData('country_id'); + if ($shippingAddress->getData('country_id')) { + $preparedOrder['countryIso'] = $shippingAddress->getData('country_id'); } if ($this->helper->getGeneralSettings('api_version') == 'v4') { diff --git a/src/Test/Unit/Model/Observer/OrderCreateTest.php b/src/Test/Unit/Model/Observer/OrderCreateTest.php index 2899799..c734619 100644 --- a/src/Test/Unit/Model/Observer/OrderCreateTest.php +++ b/src/Test/Unit/Model/Observer/OrderCreateTest.php @@ -20,6 +20,8 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase private $mockLogger; private $mockServiceOrder; private $mockHelper; + private $mockServiceCustomer; + private $mockCustomer; public function setUp() { @@ -56,6 +58,12 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase $this->mockOrder = $this->getMockBuilder(\Magento\Sales\Model\Order::class) ->disableOriginalConstructor() + ->setMethods([ + 'getCustomer', + 'getId', + 'getBillingAddress', + 'getStore' + ]) ->getMock(); $this->mockStore = $this->getMockBuilder(\Magento\Store\Model\Store::class) @@ -81,10 +89,62 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->disableOriginalConstructor() ->getMock(); + $this->mockServiceCustomer = $this->getMockBuilder(\Retailcrm\Retailcrm\Model\Service\Customer::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->mockServiceCustomer->expects($this->any())->method('process')->willReturn($this->getCustomerTestData()); + $this->mockServiceCustomer + ->expects($this->any()) + ->method('prepareCustomerFromOrder') + ->willReturn( + $this->getCustomerTestData() + ); + + $this->mockCustomer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class) + ->disableOriginalConstructor() + ->setMethods([ + 'getId', + 'getEmail', + 'getFirstname', + 'getMiddlename', + 'getLastname', + 'getStore', + 'getGender', + 'getDob', + 'getDefaultBillingAddress', + 'getAddressesCollection' + ]) + ->getMock(); + + $testData = $this->getAfterSaveCustomerTestData(); + + // mock Customer + $this->mockCustomer->expects($this->any()) + ->method('getId') + ->willReturn($testData['id']); + + $this->mockCustomer->expects($this->any()) + ->method('getEmail') + ->willReturn($testData['email']); + + $this->mockCustomer->expects($this->any()) + ->method('getFirstname') + ->willReturn($testData['firstname']); + + $this->mockCustomer->expects($this->any()) + ->method('getMiddlename') + ->willReturn($testData['middlename']); + + $this->mockCustomer->expects($this->any()) + ->method('getLastname') + ->willReturn($testData['lastname']); + $this->unit = new \Retailcrm\Retailcrm\Model\Observer\OrderCreate( $this->mockRegistry, $this->mockLogger, $this->mockServiceOrder, + $this->mockServiceCustomer, $this->mockHelper, $this->mockApi ); @@ -177,6 +237,10 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->method('getStore') ->willReturn($this->mockStore); + $this->mockOrder->expects($this->any()) + ->method('getCustomer') + ->willReturn($this->mockCustomer); + // mock Event $this->mockEvent->expects($this->any()) ->method('getOrder') @@ -379,4 +443,37 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ] ]; } + + /** + * Get test customer data + * + * @return array + */ + private function getAfterSaveCustomerTestData() + { + return [ + 'id' => 1, + 'email' => 'test@mail.com', + 'firstname' => 'TestFirstname', + 'lastname' => 'Testlastname', + 'middlename' => 'Testmiddlename', + 'birthday' => '1990-01-01', + 'gender' => 1 + ]; + } + + /** + * @return array + */ + private function getCustomerTestData() + { + return [ + 'externalId' => 1, + 'email' => 'test@mail.com', + 'firstName' => 'TestFirstname', + 'lastName' => 'Testlastname', + 'patronymic' => 'Testmiddlename', + 'createdAt' => \date('Y-m-d H:i:s') + ]; + } } diff --git a/src/Test/Unit/Model/Service/OrderTest.php b/src/Test/Unit/Model/Service/OrderTest.php index e568740..321232d 100644 --- a/src/Test/Unit/Model/Service/OrderTest.php +++ b/src/Test/Unit/Model/Service/OrderTest.php @@ -90,7 +90,14 @@ class OrderTest extends \PHPUnit\Framework\TestCase $mockShippingAddress = $this->getMockBuilder(\Magento\Sales\Model\Order\Address::class) ->disableOriginalConstructor() - ->setMethods(['getData']) + ->setMethods([ + 'getData', + 'getTelephone', + 'getFirstname', + 'getLastname', + 'getMiddlename', + 'getEmail' + ]) ->getMock(); $mockShippingAddress->expects($this->any()) ->method('getData') @@ -103,11 +110,11 @@ class OrderTest extends \PHPUnit\Framework\TestCase )) ->will($this->returnCallback([$this, 'getCallbackDataAddress'])); - $mockBillingAddress = $this->getMockBuilder(\Magento\Sales\Model\Order\Address::class) - ->disableOriginalConstructor() - ->setMethods(['getTelephone']) - ->getMock(); - $mockBillingAddress->expects($this->any())->method('getTelephone')->willReturn('89000000000'); + $mockShippingAddress->expects($this->any())->method('getTelephone')->willReturn('89000000000'); + $mockShippingAddress->expects($this->any())->method('getFirstname')->willReturn('Test'); + $mockShippingAddress->expects($this->any())->method('getLastname')->willReturn('Test'); + $mockShippingAddress->expects($this->any())->method('getMiddlename')->willReturn('Test'); + $mockShippingAddress->expects($this->any())->method('getEmail')->willReturn('test@mail.com'); $mockPaymentMethod = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class) ->disableOriginalConstructor() @@ -121,16 +128,11 @@ class OrderTest extends \PHPUnit\Framework\TestCase $mockPayment->expects($this->any())->method('getMethodInstance')->willReturn($mockPaymentMethod); $this->mockOrder->expects($this->any())->method('getAllItems')->willReturn([$mockItem]); - $this->mockOrder->expects($this->any())->method('getBillingAddress')->willReturn($mockBillingAddress); $this->mockOrder->expects($this->any())->method('getShippingAddress')->willReturn($mockShippingAddress); $this->mockOrder->expects($this->any())->method('getShippingMethod')->willReturn('flatrate_flatrate'); $this->mockOrder->expects($this->any())->method('getId')->willReturn(1); $this->mockOrder->expects($this->any())->method('getRealOrderId')->willReturn('000000001'); $this->mockOrder->expects($this->any())->method('getCreatedAt')->willReturn(date('Y-m-d H:i:s')); - $this->mockOrder->expects($this->any())->method('getCustomerLastname')->willReturn('Test'); - $this->mockOrder->expects($this->any())->method('getCustomerFirstname')->willReturn(date('Test')); - $this->mockOrder->expects($this->any())->method('getCustomerMiddlename')->willReturn(date('Test')); - $this->mockOrder->expects($this->any())->method('getCustomerEmail')->willReturn('test@mail.com'); $this->mockOrder->expects($this->any())->method('getStatus')->willReturn('processing'); $this->mockOrder->expects($this->any())->method('getShippingAmount')->willReturn(100); $this->mockOrder->expects($this->any())->method('getDiscountAmount')->willReturn(0);