Merge pull request #43 from Neur0toxine/master

Fixed several errors in order generation
This commit is contained in:
Alex Lushpai 2019-07-01 11:40:06 +03:00 committed by GitHub
commit 1400687814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 141 additions and 152 deletions

View File

@ -1,3 +1,6 @@
## v2.3.1
* Исправлены баги, связанные с передачей заказов при их оформлении
## v2.3.0
* Добавлена возможность выгрузки заказов вручную

View File

@ -1 +1 @@
2.3.0
2.3.1

View File

@ -38,7 +38,7 @@ class RetailCRM extends Module
{
$this->name = 'retailcrm';
$this->tab = 'export';
$this->version = '2.3.0';
$this->version = '2.3.1';
$this->author = 'Retail Driver LCC';
$this->displayName = $this->l('RetailCRM');
$this->description = $this->l('Integration module for RetailCRM');
@ -268,6 +268,19 @@ class RetailCRM extends Module
}
}
/**
* Returns 'true' if provided date string is valid
*
* @param $date
* @param string $format
*
* @return bool
*/
public static function verifyDate($date, $format = "Y-m-d")
{
return (bool)date_create_from_format($format, $date);
}
/**
* Build array with order data for retailCRM from PrestaShop order data
*
@ -324,14 +337,15 @@ class RetailCRM extends Module
$customer = new Customer($order->id_customer);
}
$crmOrder = array(
$crmOrder = array_filter(array(
'externalId' => $order->id,
'createdAt' => $order->date_add,
'createdAt' => static::verifyDate($order->date_add, 'Y-m-d H:i:s')
? $order->date_add : date('Y-m-d H:i:s'),
'status' => $order_status,
'firstName' => $customer->firstname,
'lastName' => $customer->lastname,
'email' => $customer->email,
);
));
$addressCollection = $cart->getAddressCollection();
$address = new Address($order->id_address_delivery);
@ -457,8 +471,10 @@ class RetailCRM extends Module
'firstName' => $object->firstname,
'lastName' => $object->lastname,
'email' => $object->email,
'createdAt' => $object->date_add,
'birthday' => $object->birthday
'createdAt' => self::verifyDate($object->date_add, 'Y-m-d H:i:s')
? $object->date_add : date('Y-m-d H:i:s'),
'birthday' => self::verifyDate($object->birthday, 'Y-m-d')
? $object->birthday : date('Y-m-d', 0)
),
$address
);
@ -791,17 +807,22 @@ class RetailCRM extends Module
public function hookActionCustomerAccountAdd($params)
{
if ($this->api) {
$customer = $params['newCustomer'];
$customerSend = static::buildCrmCustomer($customer);
$this->api->customersCreate($customerSend);
return $customerSend;
return true;
}
return false;
}
// this hook added in 1.7
public function hookActionCustomerAccountUpdate($params)
{
if ($this->api) {
$customer = $params['customer'];
$customerSend = static::buildCrmCustomer($customer);
@ -826,11 +847,14 @@ class RetailCRM extends Module
$customerSend = array_merge($customerSend, $address['customer']);
}
$customerSend = array_merge($customerSend, $address['customer']);
$customerSend = array_merge($customerSend, isset($address['customer']) ? $address['customer'] : []);
$this->api->customersEdit($customerSend);
return $customerSend;
return true;
}
return false;
}
// this hook added in 1.7
@ -863,12 +887,14 @@ 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' => $params['order']->date_add,
'createdAt' => self::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)
);
@ -905,11 +931,22 @@ class RetailCRM extends Module
$order['customer']['externalId'] = $params['order']->id_customer;
$this->api->ordersEdit($order);
return $order;
return true;
}
return false;
}
private static function addressParse($address)
{
if (!isset($customer)) {
$customer = [];
}
if (!isset($order)) {
$order = [];
}
if ($address instanceof Address) {
$postcode = $address->postcode;
$city = $address->city;
@ -947,6 +984,14 @@ class RetailCRM extends Module
private static function getPhone($address)
{
if (!isset($customer)) {
$customer = [];
}
if (!isset($order)) {
$order = [];
}
if (!empty($address->phone_mobile)){
$order['phone'] = $address->phone_mobile;
$customer['phones'][] = array('number'=> $address->phone_mobile);
@ -975,17 +1020,11 @@ class RetailCRM extends Module
if (isset($params['orderStatus'])) {
$cart = $params['cart'];
$addressCollection = $cart->getAddressCollection();
$address = array_shift($addressCollection);
$address = static::addressParse($address);
$customer = static::buildCrmCustomer($params['customer'], $address);
$order = static::buildCrmOrder($params['order'], $params['customer'], $params['cart'], false);
$this->api->ordersCreate($order);
return $order;
return true;
} elseif (isset($params['newOrderStatus'])) {
$statusCode = $params['newOrderStatus']->id;
@ -1002,7 +1041,7 @@ class RetailCRM extends Module
)
);
return $orderStatus;
return true;
}
}
@ -1047,7 +1086,7 @@ class RetailCRM extends Module
if (isset($updatePayment)) {
$this->api->ordersPaymentEdit($updatePayment);
return $updatePayment;
return true;
} else {
$createPayment = array(
'externalId' => $params['paymentCC']->id,
@ -1062,7 +1101,7 @@ class RetailCRM extends Module
$this->api->ordersPaymentCreate($createPayment);
return $createPayment;
return true;
}
return false;

View File

@ -35,34 +35,16 @@ class RetailCRMTest extends RetailcrmTestCase
{
$newCustomer = new Customer(1);
$params = array('newCustomer' => $newCustomer);
$customer = $this->retailcrmModule->hookActionCustomerAccountAdd($params);
$this->assertNotEmpty($customer);
$this->assertArrayHasKey('externalId', $customer);
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('lastName', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertArrayHasKey('createdAt', $customer);
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountAdd($params));
}
public function testHookActionCustomerAccountUpdate()
{
$customer = new Customer(1);
$params = array('customer' => $customer);
$customer = $this->retailcrmModule->hookActionCustomerAccountUpdate($params);
$this->assertNotEmpty($customer);
$this->assertArrayHasKey('externalId', $customer);
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('lastName', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertArrayHasKey('birthday', $customer);
$this->assertArrayHasKey('address', $customer);
$this->assertArrayHasKey('index', $customer['address']);
$this->assertArrayHasKey('city', $customer['address']);
$this->assertArrayHasKey('text', $customer['address']);
$this->assertArrayHasKey('countryIso', $customer['address']);
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountUpdate($params));
}
public function testHookActionOrderEdited()
@ -71,15 +53,7 @@ class RetailCRMTest extends RetailcrmTestCase
$customer = new Customer($order->id_customer);
$params = array('order' => $order, 'customer' => $customer);
$orderSend = $this->retailcrmModule->hookActionOrderEdited($params);
$this->assertNotNull($orderSend);
$this->assertArrayHasKey('externalId', $orderSend);
$this->assertArrayHasKey('firstName', $orderSend);
$this->assertArrayHasKey('lastName', $orderSend);
$this->assertArrayHasKey('email', $orderSend);
$this->assertArrayHasKey('delivery', $orderSend);
$this->assertArrayHasKey('items', $orderSend);
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
}
/**
@ -115,33 +89,7 @@ class RetailCRMTest extends RetailcrmTestCase
);
}
$result = $this->retailcrmModule->hookActionOrderStatusPostUpdate($params);
if ($newOrder === false) {
$this->assertEquals('completed', $result);
} else {
$this->assertArrayHasKey('status', $result);
$this->assertArrayHasKey('externalId', $result);
$this->assertArrayHasKey('firstName', $result);
$this->assertArrayHasKey('lastName', $result);
$this->assertArrayHasKey('email', $result);
$this->assertArrayHasKey('delivery', $result);
$this->assertArrayHasKey('address', $result['delivery']);
$this->assertArrayHasKey('city', $result['delivery']['address']);
$this->assertArrayHasKey('text', $result['delivery']['address']);
$this->assertArrayHasKey('index', $result['delivery']['address']);
$this->assertArrayHasKey('countryIso', $result);
$this->assertArrayHasKey('items', $result);
$this->assertArrayHasKey('customer', $result);
$this->assertArrayHasKey('externalId', $result['customer']);
if ($apiVersion == 5) {
$this->assertArrayHasKey('payments', $result);
$this->assertInternalType('array', $result['payments']);
} else {
$this->assertArrayHasKey('paymentType', $result);
}
}
$this->assertTrue($this->retailcrmModule->hookActionOrderStatusPostUpdate($params));
}
/**
@ -167,9 +115,8 @@ class RetailCRMTest extends RetailcrmTestCase
$result = $this->retailcrmModule->hookActionPaymentCCAdd($params);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('type', $result);
$this->assertArrayHasKey('amount', $result);
$this->assertInternalType('bool', $result);
$this->assertTrue($result);
RetailcrmTestHelper::deleteOrderPayment($orderPayment->id);
}