mirror of
https://github.com/retailcrm/prestashop-module.git
synced 2025-03-01 19:03:14 +03:00
Added updating order address from RetailCRM
This commit is contained in:
parent
45ce3247f6
commit
20c8e83a59
@ -52,6 +52,12 @@ class RetailcrmAddressBuilder extends RetailcrmAbstractDataBuilder
|
||||
*/
|
||||
const MODE_ORDER_DELIVERY = 2;
|
||||
|
||||
|
||||
/**
|
||||
* Divider for order delivery addressline1 and addressline 2
|
||||
*/
|
||||
const ADDRESS_LINE_DIVIDER = '||';
|
||||
|
||||
/**
|
||||
* @var Address|\AddressCore
|
||||
*/
|
||||
@ -243,7 +249,11 @@ class RetailcrmAddressBuilder extends RetailcrmAbstractDataBuilder
|
||||
'index' => $this->address->postcode,
|
||||
'city' => $this->address->city,
|
||||
'countryIso' => Country::getIsoById($this->address->id_country),
|
||||
'text' => sprintf("%s %s", $this->address->address1, $this->address->address2),
|
||||
'text' => (empty($this->address->address2) ? $this->address->address1 :
|
||||
implode(self::ADDRESS_LINE_DIVIDER, [
|
||||
$this->address->address1,
|
||||
$this->address->address2,
|
||||
])),
|
||||
'region' => $state
|
||||
));
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
@ -169,14 +170,24 @@ class RetailcrmCustomerAddressBuilder extends RetailcrmAbstractBuilder implement
|
||||
}
|
||||
|
||||
$this->customerAddress->id_customer = $this->idCustomer;
|
||||
$this->customerAddress->alias = !empty($this->alias) ? $this->alias : 'default';
|
||||
$this->customerAddress->lastname = $this->lastName;
|
||||
$this->customerAddress->firstname = $this->firstName;
|
||||
$this->customerAddress->address1 = isset($this->dataCrm['text']) ? $this->dataCrm['text'] : '--';
|
||||
|
||||
$this->customerAddress->id_country = isset($this->dataCrm['countryIso'])
|
||||
? Country::getByIso($this->dataCrm['countryIso'])
|
||||
: Configuration::get('PS_COUNTRY_DEFAULT');
|
||||
$this->setAddressField('alias', $this->alias, 'default');
|
||||
$this->setAddressField('lastname', $this->lastName, '');
|
||||
$this->setAddressField('firstname', $this->firstName, '');
|
||||
|
||||
$addressLine = $this->buildAddressLine();
|
||||
$this->setAddressField('address1', $addressLine[0], '--');
|
||||
$this->setAddressField('address2', $addressLine[1], '');
|
||||
|
||||
$countryIso = isset($this->dataCrm['countryIso']) ? Country::getByIso($this->dataCrm['countryIso']) : null;
|
||||
$this->setAddressField('id_country', $countryIso, Configuration::get('PS_COUNTRY_DEFAULT'));
|
||||
|
||||
if (isset($this->dataCrm['city'])) {
|
||||
$this->setAddressField('city', $this->dataCrm['city'], '--');
|
||||
}
|
||||
if (isset($this->dataCrm['index'])) {
|
||||
$this->setAddressField('postcode', $this->dataCrm['index'], '');
|
||||
}
|
||||
|
||||
if (isset($this->dataCrm['region'])) {
|
||||
$state = State::getIdByName($this->dataCrm['region']);
|
||||
@ -186,9 +197,7 @@ class RetailcrmCustomerAddressBuilder extends RetailcrmAbstractBuilder implement
|
||||
}
|
||||
}
|
||||
|
||||
$this->customerAddress->city = isset($this->dataCrm['city']) ? $this->dataCrm['city'] : '--';
|
||||
$this->customerAddress->postcode = isset($this->dataCrm['index']) ? $this->dataCrm['index'] : '';
|
||||
$this->customerAddress->phone = !empty($this->phone) ? $this->phone : '';
|
||||
$this->setAddressField('phone', $this->phone, '');
|
||||
|
||||
$this->customerAddress = RetailcrmTools::filter(
|
||||
'RetailcrmFilterSaveCustomerAddress',
|
||||
@ -199,5 +208,35 @@ class RetailcrmCustomerAddressBuilder extends RetailcrmAbstractBuilder implement
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function setAddressField($field, $value, $default = null)
|
||||
{
|
||||
if (!property_exists($this->customerAddress, $field)) {
|
||||
throw new InvalidArgumentException("Property $field not exist in the object");
|
||||
}
|
||||
|
||||
if ($value !== null) {
|
||||
$this->customerAddress->$field = $value;
|
||||
} elseif (empty($this->customerAddress->$field)) {
|
||||
$this->customerAddress->$field = $default;
|
||||
}
|
||||
}
|
||||
|
||||
private function buildAddressLine()
|
||||
{
|
||||
$addressLine = [
|
||||
null,
|
||||
null
|
||||
];
|
||||
|
||||
if (isset($this->dataCrm['text'])) {
|
||||
$addressLine = explode(RetailcrmAddressBuilder::ADDRESS_LINE_DIVIDER, $this->dataCrm['text'], 2);
|
||||
if (count($addressLine) == 1) {
|
||||
$addressLine[] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $addressLine;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,6 +234,7 @@ class RetailcrmHistory
|
||||
if (isset($order_history['deleted']) && $order_history['deleted'] == true) {
|
||||
continue;
|
||||
}
|
||||
$infoOrder = null;
|
||||
|
||||
if (!array_key_exists('externalId', $order_history)) {
|
||||
|
||||
@ -324,7 +325,6 @@ class RetailcrmHistory
|
||||
}
|
||||
}
|
||||
|
||||
$address = new Address();
|
||||
$customer = null;
|
||||
$customerId = null;
|
||||
|
||||
@ -380,10 +380,7 @@ class RetailcrmHistory
|
||||
->build();
|
||||
|
||||
$customer = $customerBuilder->getData()->getCustomer();
|
||||
|
||||
if (!empty($address)) {
|
||||
$address = $customerBuilder->getData()->getCustomerAddress();
|
||||
}
|
||||
$address = $customerBuilder->getData()->getCustomerAddress();
|
||||
}
|
||||
|
||||
if (empty($customer->id) && !empty($customer->email)) {
|
||||
@ -764,33 +761,56 @@ class RetailcrmHistory
|
||||
/*
|
||||
* check delivery changes
|
||||
*/
|
||||
if (isset($order['status']) && isset($order['delivery'])) {
|
||||
if ($order['status'] == 'new') {
|
||||
$customerData = self::$api->customersGet($orderToUpdate->id_customer, 'externalId');
|
||||
$customerForAddress = $customerData['customer'];
|
||||
if (isset($order['delivery'])
|
||||
|| isset($order['firstName'])
|
||||
|| isset($order['lastName'])
|
||||
|| isset($order['phone'])
|
||||
) {
|
||||
$addressBuilder = new RetailcrmCustomerAddressBuilder();
|
||||
|
||||
$customerBuilder = new RetailcrmCustomerBuilder();
|
||||
|
||||
if (isset($customerData['customer']['address'])) {
|
||||
$customerForAddress['address'] = array_replace(
|
||||
$customerData['customer']['address'],
|
||||
$order['delivery']['address']
|
||||
);
|
||||
} else {
|
||||
$customerForAddress['address'] = $order['delivery']['address'];
|
||||
$orderAddressCrm = [];
|
||||
// TODO: check changed fields and skip getCRMOrder() if it's possible
|
||||
// It is possible if there are valid address in the database
|
||||
// and if there's no address1 & address2 changed (text, street, building, etc...)
|
||||
if (isset($order['delivery']['address'])) {
|
||||
$orderAddressCrm = $order['delivery']['address'];
|
||||
// get full order address
|
||||
$infoOrder = self::getCRMOrder($order['externalId']);
|
||||
if (isset($infoOrder['delivery']['address'])) {
|
||||
$orderAddressCrm = $infoOrder['delivery']['address'];
|
||||
}
|
||||
}
|
||||
|
||||
$customerBuilder
|
||||
->setDataCrm($customerForAddress)
|
||||
->build();
|
||||
$address = $addressBuilder
|
||||
->setCustomerAddress(new Address($orderToUpdate->id_address_delivery))
|
||||
->setIdCustomer($orderToUpdate->id_customer)
|
||||
->setDataCrm($orderAddressCrm)
|
||||
->setFirstName(isset($order['firstName']) ? $order['firstName'] : null)
|
||||
->setLastName(isset($order['lastName']) ? $order['lastName'] : null)
|
||||
->setPhone(isset($order['phone']) ? $order['phone'] : null)
|
||||
->build()
|
||||
->getData();
|
||||
|
||||
$address = $customerBuilder->getData()->getCustomerAddress();
|
||||
if (!is_null($address)) {
|
||||
$address->id = $orderToUpdate->id_address_delivery;
|
||||
|
||||
$address->update();
|
||||
$validate = $address->validateFields(false, true);
|
||||
if ($validate === true) {
|
||||
// Modifying an address in order creates another address
|
||||
// instead of changing the original one. This issue has been fixed in PS 1.7.7
|
||||
if (version_compare(_PS_VERSION_, '1.7.7', '<')) {
|
||||
$address->id = null;
|
||||
$address->add();
|
||||
$orderToUpdate->id_address_delivery = $address->id;
|
||||
$orderToUpdate->update();
|
||||
} else {
|
||||
$address->id = $orderToUpdate->id_address_delivery;
|
||||
$address->update();
|
||||
}
|
||||
} else {
|
||||
RetailcrmLogger::writeCaller(__METHOD__, sprintf(
|
||||
'Error validating address for order %s: %s',
|
||||
$orderToUpdate->id,
|
||||
$validate
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -900,7 +920,9 @@ class RetailcrmHistory
|
||||
if (isset($order['items']) || isset($order['delivery']['cost'])) {
|
||||
|
||||
// get full order
|
||||
$infoOrder = self::getCRMOrder($order['externalId']);
|
||||
if (empty($infoOrder)) {
|
||||
$infoOrder = self::getCRMOrder($order['externalId']);
|
||||
}
|
||||
|
||||
// items
|
||||
if (isset($order['items']) && is_array($order['items'])) {
|
||||
@ -1304,11 +1326,11 @@ class RetailcrmHistory
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($crmOrder) && !empty($crmOrder['delivery']) && !empty($crmOrder['delivery']['address'])) {
|
||||
$data->setCrmOrderShippingAddress($crmOrder['delivery']['address']);
|
||||
}
|
||||
|
||||
if ($data->feasible()) {
|
||||
if (!empty($crmOrder) && !empty($crmOrder['delivery']) && !empty($crmOrder['delivery']['address'])) {
|
||||
$data->setCrmOrderShippingAddress($crmOrder['delivery']['address']);
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $switcher->setData($data)
|
||||
->build()
|
||||
|
@ -256,14 +256,16 @@ class RetailcrmOrderBuilder
|
||||
$customer = $this->createdCustomer;
|
||||
} else {
|
||||
$crmCustomer = RetailcrmTools::mergeCustomerAddress($customer, $this->buildRegularAddress());
|
||||
if (isset($crmCustomer['tags'])) {
|
||||
unset($crmCustomer['tags']);
|
||||
}
|
||||
if (!RetailcrmTools::isEqualCustomerAddress($customer, $crmCustomer)) {
|
||||
if (isset($crmCustomer['tags'])) {
|
||||
unset($crmCustomer['tags']);
|
||||
}
|
||||
|
||||
$response = $this->api->customersEdit($crmCustomer);
|
||||
$response = $this->api->customersEdit($crmCustomer);
|
||||
|
||||
if ($response instanceof RetailcrmApiResponse && $response->isSuccessful()) {
|
||||
$customer = $crmCustomer;
|
||||
if ($response instanceof RetailcrmApiResponse && $response->isSuccessful()) {
|
||||
$customer = $crmCustomer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -901,16 +903,14 @@ class RetailcrmOrderBuilder
|
||||
}
|
||||
|
||||
if (isset($payment[$paymentType]) && !empty($payment[$paymentType])) {
|
||||
$order_payment = array(
|
||||
'externalId' => $order->id . '#' . $order->reference,
|
||||
'amount' => round($order->total_paid_real, 2),
|
||||
'status' => ((round($order->total_paid_real, 2) > 0) ? 'paid' : null),
|
||||
'type' => $payment[$paymentType]
|
||||
$crmOrder['payments'] = array(
|
||||
array(
|
||||
'externalId' => $order->id . '#' . $order->reference,
|
||||
'amount' => round($order->total_paid_real, 2),
|
||||
'status' => ((round($order->total_paid_real, 2) > 0) ? 'paid' : null),
|
||||
'type' => $payment[$paymentType]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($order_payment)) {
|
||||
$crmOrder['payments'][] = $order_payment;
|
||||
} else {
|
||||
$crmOrder['payments'] = array();
|
||||
}
|
||||
|
@ -484,6 +484,49 @@ class RetailcrmTools
|
||||
return array_merge($customer, $address, array('phones' => $customerPhones));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare 'address' and 'phones' fields from crm customer info arrays
|
||||
* @param $customer1
|
||||
* @param $customer2
|
||||
* @return bool <b>true</b> if addresses are equal, <b>false</b> otherwise
|
||||
*/
|
||||
public static function isEqualCustomerAddress($customer1, $customer2)
|
||||
{
|
||||
$customer1Phones = isset($customer1['phones']) ? $customer1['phones'] : array();
|
||||
$customer2Phones = isset($customer2['phones']) ? $customer2['phones'] : array();
|
||||
|
||||
$squashedCustomer1Phones = array_filter(array_map(function ($val) {
|
||||
return isset($val['number']) ? $val['number'] : null;
|
||||
}, $customer1Phones));
|
||||
$squashedCustomer2Phones = array_filter(array_map(function ($val) {
|
||||
return isset($val['number']) ? $val['number'] : null;
|
||||
}, $customer2Phones));
|
||||
|
||||
if (count($squashedCustomer1Phones) !== count($squashedCustomer2Phones)
|
||||
|| !empty(array_diff_assoc($squashedCustomer1Phones, $squashedCustomer2Phones))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$customer1Address = isset($customer1['address']) ? $customer1['address'] : array();
|
||||
$customer2Address = isset($customer2['address']) ? $customer2['address'] : array();
|
||||
|
||||
if (isset($customer1Address['id'])) {
|
||||
unset($customer1Address['id']);
|
||||
}
|
||||
if (isset($customer2Address['id'])) {
|
||||
unset($customer2Address['id']);
|
||||
}
|
||||
|
||||
if (count($customer1Address) !== count($customer2Address)
|
||||
|| !empty(array_diff_assoc($customer2Address, $customer1Address))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* http_response_code polyfill
|
||||
*
|
||||
|
@ -656,21 +656,6 @@ class RetailCRM extends Module
|
||||
public function hookActionOrderEdited($params)
|
||||
{
|
||||
if ($this->api) {
|
||||
$order = array(
|
||||
'externalId' => $params['order']->id,
|
||||
'firstName' => $params['customer']->firstname,
|
||||
'lastName' => $params['customer']->lastname,
|
||||
'email' => $params['customer']->email,
|
||||
'createdAt' => RetailcrmTools::verifyDate($params['order']->date_add, 'Y-m-d H:i:s')
|
||||
? $params['order']->date_add : date('Y-m-d H:i:s'),
|
||||
'delivery' => array('cost' => $params['order']->total_shipping),
|
||||
'discountManualAmount' => round($params['order']->total_discounts, 2)
|
||||
);
|
||||
|
||||
if (((float) $order['discountManualAmount']) > ((float) $params['order']->total_paid)) {
|
||||
$order['discountManualAmount'] = $params['order']->total_paid;
|
||||
}
|
||||
|
||||
try {
|
||||
$orderdb = new Order($params['order']->id);
|
||||
} catch (PrestaShopDatabaseException $exception) {
|
||||
@ -681,7 +666,7 @@ class RetailCRM extends Module
|
||||
RetailcrmLogger::writeNoCaller($exception->getTraceAsString());
|
||||
}
|
||||
|
||||
$orderCrm = $this->api->ordersGet($order['externalId']);
|
||||
$orderCrm = $this->api->ordersGet($params['order']->id);
|
||||
|
||||
if (!($orderCrm instanceof RetailcrmApiResponse) || !$orderCrm->isSuccessful()) {
|
||||
/** @var Order|\OrderCore $order */
|
||||
@ -698,6 +683,21 @@ class RetailCRM extends Module
|
||||
return false;
|
||||
}
|
||||
|
||||
$order = array(
|
||||
'externalId' => $params['order']->id,
|
||||
'firstName' => $params['customer']->firstname,
|
||||
'lastName' => $params['customer']->lastname,
|
||||
'email' => $params['customer']->email,
|
||||
'createdAt' => RetailcrmTools::verifyDate($params['order']->date_add, 'Y-m-d H:i:s')
|
||||
? $params['order']->date_add : date('Y-m-d H:i:s'),
|
||||
'delivery' => array('cost' => $params['order']->total_shipping),
|
||||
'discountManualAmount' => round($params['order']->total_discounts, 2)
|
||||
);
|
||||
|
||||
if (((float) $order['discountManualAmount']) > ((float) $params['order']->total_paid)) {
|
||||
$order['discountManualAmount'] = $params['order']->total_paid;
|
||||
}
|
||||
|
||||
$comment = $orderdb->getFirstMessage();
|
||||
|
||||
if ($comment !== false) {
|
||||
|
@ -53,6 +53,34 @@ class RetailcrmCustomerAddressBuilderTest extends RetailcrmTestCase
|
||||
$this->assertEquals('+7999999999', $addressResult->phone);
|
||||
}
|
||||
|
||||
public function testAddressOverriding()
|
||||
{
|
||||
$this->customerAddress = new RetailcrmCustomerAddressBuilder();
|
||||
$this->customerAddress
|
||||
->setDataCrm($this->getDataBuilder())
|
||||
->setFirstName('Test')
|
||||
->setLastName('Test2')
|
||||
->setPhone('+7999999999')
|
||||
->build();
|
||||
$addressResult = $this->customerAddress->getData();
|
||||
|
||||
$this->customerAddress
|
||||
->setCustomerAddress($addressResult)
|
||||
->setDataCrm($this->getDataBuilderOverride())
|
||||
->setFirstName('Test override')
|
||||
->setPhone('+7111111111')
|
||||
->build();
|
||||
|
||||
$addressResultOverridden = $this->customerAddress->getData();
|
||||
$this->assertEquals('Test override', $addressResultOverridden->firstname);
|
||||
$this->assertEquals('Test2', $addressResultOverridden->lastname);
|
||||
$this->assertEquals(Country::getByIso('RU'), $addressResultOverridden->id_country);
|
||||
$this->assertEquals('г. Москва Override', $addressResultOverridden->city);
|
||||
$this->assertEquals(State::getIdByName('Moscow'), $addressResultOverridden->id_state);
|
||||
$this->assertEquals('444444', $addressResultOverridden->postcode);
|
||||
$this->assertEquals('+7111111111', $addressResultOverridden->phone);
|
||||
}
|
||||
|
||||
private function getDataBuilder() {
|
||||
return array(
|
||||
'id' => 9718,
|
||||
@ -63,5 +91,14 @@ class RetailcrmCustomerAddressBuilderTest extends RetailcrmTestCase
|
||||
'text' => 'MAY'
|
||||
);
|
||||
}
|
||||
|
||||
private function getDataBuilderOverride()
|
||||
{
|
||||
return array(
|
||||
'id' => 9718,
|
||||
'city' => 'г. Москва Override',
|
||||
'index' => '444444',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,6 +99,32 @@ class RetailcrmAddressBuilderTest extends RetailcrmTestCase
|
||||
$this->assertFieldsNotEmpty($result['delivery']['address'], array('countryIso'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getAddressLines
|
||||
*/
|
||||
public function testAddressLineConcatenation($addressLine1, $addressLine2, $addressText)
|
||||
{
|
||||
$address = $this->address;
|
||||
$address->address1 = $addressLine1;
|
||||
$address->address2 = $addressLine2;
|
||||
|
||||
$builder = new RetailcrmAddressBuilder();
|
||||
$result = $builder
|
||||
->setMode(RetailcrmAddressBuilder::MODE_ORDER_DELIVERY)
|
||||
->setAddress($this->address)
|
||||
->setIsMain(true)
|
||||
->setWithExternalId(true)
|
||||
->setExternalIdSuffix('suffix')
|
||||
->build()
|
||||
->getDataArray();
|
||||
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertArrayHasKey('delivery', $result);
|
||||
$this->assertArrayHasKey('address', $result['delivery']);
|
||||
$this->assertArrayHasKey('text', $result['delivery']['address']);
|
||||
$this->assertEquals($result['delivery']['address']['text'], $addressText);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts address fields
|
||||
*
|
||||
@ -112,6 +138,27 @@ class RetailcrmAddressBuilderTest extends RetailcrmTestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function getAddressLines()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'addressline 1',
|
||||
'addressline 2',
|
||||
'addressline 1' . RetailcrmAddressBuilder::ADDRESS_LINE_DIVIDER . 'addressline 2'
|
||||
),
|
||||
array(
|
||||
'addressline 1',
|
||||
'',
|
||||
'addressline 1'
|
||||
),
|
||||
array(
|
||||
'',
|
||||
'addressline 2',
|
||||
RetailcrmAddressBuilder::ADDRESS_LINE_DIVIDER . 'addressline 2'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns address fields names
|
||||
*
|
||||
|
@ -84,7 +84,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
$this->assertEquals(true, RetailcrmHistory::customersHistory());
|
||||
}
|
||||
|
||||
public function orderCreate($apiMock)
|
||||
private function orderCreate($apiMock)
|
||||
{
|
||||
RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
|
||||
RetailcrmHistory::$api = $apiMock;
|
||||
@ -100,7 +100,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
$this->assertInstanceOf('Order', $order);
|
||||
}
|
||||
|
||||
public function switchCustomer()
|
||||
private function switchCustomer()
|
||||
{
|
||||
RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
|
||||
RetailcrmHistory::$api = $this->apiMock;
|
||||
@ -276,6 +276,116 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
RetailcrmHistory::ordersHistory();
|
||||
}
|
||||
|
||||
|
||||
public function testOrderAddressUpdate()
|
||||
{
|
||||
$orderId = RetailcrmTestHelper::getMaxOrderId();
|
||||
$crmOrder = $this->getApiOrderAddressUpdate($orderId);
|
||||
|
||||
$this->apiMock->expects($this->any())
|
||||
->method('ordersHistory')
|
||||
->willReturn(
|
||||
new RetailcrmApiResponse(
|
||||
'200',
|
||||
json_encode(
|
||||
$this->getHistoryAddressUpdated($orderId)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$this->apiMock->expects($this->any())
|
||||
->method('ordersGet')
|
||||
->willReturn(
|
||||
new RetailcrmApiResponse(
|
||||
'200',
|
||||
json_encode(
|
||||
array(
|
||||
'order' => $crmOrder
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
|
||||
RetailcrmHistory::$api = $this->apiMock;
|
||||
|
||||
$order = new Order($orderId);
|
||||
$idAddress = $order->id_address_delivery;
|
||||
|
||||
RetailcrmHistory::ordersHistory();
|
||||
|
||||
$orderAfter = new Order($orderId);
|
||||
$idAddressAfter = $orderAfter->id_address_delivery;
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.7.7', '<')) {
|
||||
$this->assertNotEquals($idAddress, $idAddressAfter);
|
||||
}
|
||||
|
||||
$builder = new RetailcrmAddressBuilder();
|
||||
$result = $builder
|
||||
->setMode(RetailcrmAddressBuilder::MODE_ORDER_DELIVERY)
|
||||
->setAddressId($idAddressAfter)
|
||||
->build()
|
||||
->getDataArray();
|
||||
|
||||
$this->assertEquals($crmOrder['delivery']['address']['countryIso'], $result['countryIso']);
|
||||
unset($crmOrder['delivery']['address']['countryIso']);
|
||||
|
||||
$this->assertEquals($crmOrder['delivery']['address'], $result['delivery']['address']);
|
||||
}
|
||||
|
||||
|
||||
public function testOrderNameUpdate()
|
||||
{
|
||||
$orderId = RetailcrmTestHelper::getMaxOrderId();
|
||||
$crmOrder = $this->getApiOrderNameAndPhoneUpdate($orderId);
|
||||
|
||||
$this->apiMock->expects($this->any())
|
||||
->method('ordersHistory')
|
||||
->willReturn(
|
||||
new RetailcrmApiResponse(
|
||||
'200',
|
||||
json_encode(
|
||||
$this->getHistoryNameAndPhoneUpdated($orderId)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$this->apiMock->expects($this->any())
|
||||
->method('ordersGet')
|
||||
->willReturn(
|
||||
new RetailcrmApiResponse(
|
||||
'200',
|
||||
json_encode(
|
||||
array(
|
||||
'order' => $crmOrder
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
|
||||
RetailcrmHistory::$api = $this->apiMock;
|
||||
|
||||
$order = new Order($orderId);
|
||||
$idAddress = $order->id_address_delivery;
|
||||
|
||||
RetailcrmHistory::ordersHistory();
|
||||
|
||||
$orderAfter = new Order($orderId);
|
||||
$idAddressAfter = $orderAfter->id_address_delivery;
|
||||
$addressAfter = new Address($idAddressAfter);
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.7.7', '<')) {
|
||||
$this->assertNotEquals($idAddress, $idAddressAfter);
|
||||
}
|
||||
|
||||
$this->assertEquals($crmOrder['firstName'], $addressAfter->firstname);
|
||||
$this->assertEquals($crmOrder['lastName'], $addressAfter->lastname);
|
||||
$this->assertEquals($crmOrder['phone'], $addressAfter->phone);
|
||||
}
|
||||
|
||||
|
||||
private function getHistoryExistOrder()
|
||||
{
|
||||
return array(
|
||||
@ -394,7 +504,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
'id_customer' => 2222,
|
||||
'index' => '111111',
|
||||
'countryIso' => 'RU',
|
||||
'region' => 'Test region',
|
||||
'region' => 'Moscow',
|
||||
'city' => 'Test',
|
||||
'text' => 'Test text address'
|
||||
),
|
||||
@ -426,7 +536,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
'id_customer' => 2222,
|
||||
'index' => '111111',
|
||||
'countryIso' => 'RU',
|
||||
'region' => 'Test region',
|
||||
'region' => 'Moscow',
|
||||
'city' => 'Test',
|
||||
'text' => 'Test text address'
|
||||
)
|
||||
@ -513,7 +623,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
'id_customer' => 2222,
|
||||
'index' => '111111',
|
||||
'countryIso' => 'RU',
|
||||
'region' => 'Test region',
|
||||
'region' => 'Moscow',
|
||||
'city' => 'Test',
|
||||
'text' => 'Test text address'
|
||||
),
|
||||
@ -554,7 +664,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
'id_customer' => 2222,
|
||||
'index' => '111111',
|
||||
'countryIso' => 'RU',
|
||||
'region' => 'Test region',
|
||||
'region' => 'Moscow',
|
||||
'city' => 'Test',
|
||||
'text' => 'Test text address'
|
||||
)
|
||||
@ -570,7 +680,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
'id_customer' => 2222,
|
||||
'index' => '111111',
|
||||
'countryIso' => 'RU',
|
||||
'region' => 'Test region',
|
||||
'region' => 'Moscow',
|
||||
'city' => 'Test',
|
||||
'text' => 'Test text address'
|
||||
)
|
||||
@ -761,5 +871,121 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function getHistoryAddressUpdated($orderId)
|
||||
{
|
||||
return array(
|
||||
'success' => true,
|
||||
'history' => array(
|
||||
array(
|
||||
'id' => 19752,
|
||||
'createdAt' => '2018-01-01 00:00:00',
|
||||
'source' => 'api',
|
||||
'field' => 'delivery_address.city',
|
||||
'apiKey' => array('current' => false),
|
||||
'oldValue' => 'Order City old',
|
||||
'newValue' => 'Order City new',
|
||||
'order' => array(
|
||||
'id' => 6025,
|
||||
'externalId' => (string)$orderId,
|
||||
'site' => '127.0.0.1:8000',
|
||||
'status' => 'new'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'id' => 19753,
|
||||
'createdAt' => '2018-01-01 00:00:00',
|
||||
'source' => 'api',
|
||||
'field' => 'delivery_address.index',
|
||||
'apiKey' => array('current' => false),
|
||||
'oldValue' => '111',
|
||||
'newValue' => '222',
|
||||
'order' => array(
|
||||
'id' => 6025,
|
||||
'externalId' => (string)$orderId,
|
||||
'site' => '127.0.0.1:8000',
|
||||
'status' => 'new'
|
||||
)
|
||||
)
|
||||
),
|
||||
'pagination' => array(
|
||||
'limit' => 20,
|
||||
'totalCount' => 2,
|
||||
'currentPage' => 1,
|
||||
'totalPageCount' => 1
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
private function getHistoryNameAndPhoneUpdated($orderId)
|
||||
{
|
||||
return array(
|
||||
'success' => true,
|
||||
'history' => array(
|
||||
array(
|
||||
'id' => 19752,
|
||||
'createdAt' => '2018-01-01 00:00:00',
|
||||
'source' => 'api',
|
||||
'field' => 'first_name',
|
||||
'apiKey' => array('current' => false),
|
||||
'oldValue' => 'name old',
|
||||
'newValue' => 'name new',
|
||||
'order' => array(
|
||||
'id' => 6025,
|
||||
'externalId' => (string)$orderId,
|
||||
'site' => '127.0.0.1:8000',
|
||||
'status' => 'new'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'id' => 19753,
|
||||
'createdAt' => '2018-01-01 00:00:00',
|
||||
'source' => 'api',
|
||||
'field' => 'phone',
|
||||
'apiKey' => array('current' => false),
|
||||
'oldValue' => '111',
|
||||
'newValue' => '222222',
|
||||
'order' => array(
|
||||
'id' => 6025,
|
||||
'externalId' => (string)$orderId,
|
||||
'site' => '127.0.0.1:8000',
|
||||
'status' => 'new'
|
||||
)
|
||||
)
|
||||
),
|
||||
'pagination' => array(
|
||||
'limit' => 20,
|
||||
'totalCount' => 2,
|
||||
'currentPage' => 1,
|
||||
'totalPageCount' => 1
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function getApiOrderAddressUpdate($orderId)
|
||||
{
|
||||
$order = $this->getApiOrder();
|
||||
|
||||
$order['externalId'] = (string)$orderId;
|
||||
$order['delivery']['address']['region'] = 'Buenos Aires'; // todo to get real state id
|
||||
$order['delivery']['address']['city'] = 'Order City new';
|
||||
$order['delivery']['address']['index'] = '222';
|
||||
unset($order['delivery']['address']['id_customer']);
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
|
||||
private function getApiOrderNameAndPhoneUpdate($orderId)
|
||||
{
|
||||
$order = $this->getApiOrder();
|
||||
|
||||
$order['externalId'] = (string)$orderId;
|
||||
$order['firstName'] = 'name new';
|
||||
$order['phone'] = '222222';
|
||||
|
||||
return $order;
|
||||
}
|
||||
}
|
||||
|
||||
|
201
tests/lib/RetailcrmToolsTest.php
Normal file
201
tests/lib/RetailcrmToolsTest.php
Normal file
@ -0,0 +1,201 @@
|
||||
<?php
|
||||
|
||||
class RetailcrmToolsTest extends RetailcrmTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider equalCustomerAddresses
|
||||
*/
|
||||
public function testIsEqualCustomerAddress($address1, $address2, $result)
|
||||
{
|
||||
$this->assertEquals($result, RetailcrmTools::isEqualCustomerAddress($address1, $address2));
|
||||
}
|
||||
|
||||
public function equalCustomerAddresses()
|
||||
{
|
||||
return [
|
||||
'Equal addresses' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
true
|
||||
],
|
||||
'Changed phone' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '222']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
false
|
||||
],
|
||||
'Changed index' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '222',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
false
|
||||
],
|
||||
'Reduced address' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
]
|
||||
],
|
||||
false
|
||||
],
|
||||
'Expanded address' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
false
|
||||
],
|
||||
'Reduced phone' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111',],
|
||||
['number' => '222',]
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
false
|
||||
],
|
||||
'Expanded phone' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111',],
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '222'],
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
false
|
||||
],
|
||||
'Replaced field' => [
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111',],
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'text' => 'Address line 1 (client Address 2)',
|
||||
]
|
||||
],
|
||||
[
|
||||
'phones' => [
|
||||
['number' => '111']
|
||||
],
|
||||
'address' => [
|
||||
'index' => '398055',
|
||||
'city' => 'Order City here',
|
||||
'region' => 'Region',
|
||||
]
|
||||
],
|
||||
false
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user