Fixed several errors in order generation

This commit is contained in:
Pavel 2019-06-18 11:54:05 +03:00
parent 3dcc75cb36
commit ef4a4a15a9
4 changed files with 141 additions and 152 deletions

View File

@ -1,3 +1,6 @@
## v2.3.1
* Исправлены баги, связанные с передачей заказов при их оформлении
## v2.3.0 ## 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->name = 'retailcrm';
$this->tab = 'export'; $this->tab = 'export';
$this->version = '2.3.0'; $this->version = '2.3.1';
$this->author = 'Retail Driver LCC'; $this->author = 'Retail Driver LCC';
$this->displayName = $this->l('RetailCRM'); $this->displayName = $this->l('RetailCRM');
$this->description = $this->l('Integration module for RetailCRM'); $this->description = $this->l('Integration module for RetailCRM');
@ -109,14 +109,14 @@ class RetailCRM extends Module
$this->integrationModule($api, $clientId, Configuration::get('RETAILCRM_API_VERSION'), false); $this->integrationModule($api, $clientId, Configuration::get('RETAILCRM_API_VERSION'), false);
return parent::uninstall() && return parent::uninstall() &&
Configuration::deleteByName('RETAILCRM_ADDRESS') && Configuration::deleteByName('RETAILCRM_ADDRESS') &&
Configuration::deleteByName('RETAILCRM_API_TOKEN') && Configuration::deleteByName('RETAILCRM_API_TOKEN') &&
Configuration::deleteByName('RETAILCRM_API_STATUS') && Configuration::deleteByName('RETAILCRM_API_STATUS') &&
Configuration::deleteByName('RETAILCRM_API_DELIVERY') && Configuration::deleteByName('RETAILCRM_API_DELIVERY') &&
Configuration::deleteByName('RETAILCRM_LAST_SYNC') && Configuration::deleteByName('RETAILCRM_LAST_SYNC') &&
Configuration::deleteByName('RETAILCRM_API_VERSION') && Configuration::deleteByName('RETAILCRM_API_VERSION') &&
Configuration::deleteByName('RETAILCRM_LAST_CUSTOMERS_SYNC') && Configuration::deleteByName('RETAILCRM_LAST_CUSTOMERS_SYNC') &&
Configuration::deleteByName('RETAILCRM_LAST_ORDERS_SYNC'); Configuration::deleteByName('RETAILCRM_LAST_ORDERS_SYNC');
} }
public function getContent() public function getContent()
@ -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 * Build array with order data for retailCRM from PrestaShop order data
* *
@ -304,7 +317,7 @@ class RetailCRM extends Module
if (!$isStatusExport) { if (!$isStatusExport) {
$order_status = $order_status =
array_key_exists($order->current_state, $status) array_key_exists($order->current_state, $status)
? $status[$order->current_state] : 'new'; ? $status[$order->current_state] : 'new';
} }
} else { } else {
$order_status = array_key_exists($order->current_state, $status) $order_status = array_key_exists($order->current_state, $status)
@ -324,14 +337,15 @@ class RetailCRM extends Module
$customer = new Customer($order->id_customer); $customer = new Customer($order->id_customer);
} }
$crmOrder = array( $crmOrder = array_filter(array(
'externalId' => $order->id, '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, 'status' => $order_status,
'firstName' => $customer->firstname, 'firstName' => $customer->firstname,
'lastName' => $customer->lastname, 'lastName' => $customer->lastname,
'email' => $customer->email, 'email' => $customer->email,
); ));
$addressCollection = $cart->getAddressCollection(); $addressCollection = $cart->getAddressCollection();
$address = new Address($order->id_address_delivery); $address = new Address($order->id_address_delivery);
@ -457,8 +471,10 @@ class RetailCRM extends Module
'firstName' => $object->firstname, 'firstName' => $object->firstname,
'lastName' => $object->lastname, 'lastName' => $object->lastname,
'email' => $object->email, 'email' => $object->email,
'createdAt' => $object->date_add, 'createdAt' => self::verifyDate($object->date_add, 'Y-m-d H:i:s')
'birthday' => $object->birthday ? $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 $address
); );
@ -560,10 +576,10 @@ class RetailCRM extends Module
'name' => 'RETAILCRM_DAEMON_COLLECTOR_ACTIVE', 'name' => 'RETAILCRM_DAEMON_COLLECTOR_ACTIVE',
'values' => array( 'values' => array(
'query' => array( 'query' => array(
array( array(
'id_option' => 1, 'id_option' => 1,
) )
), ),
'id' => 'id_option', 'id' => 'id_option',
'name' => 'name' 'name' => 'name'
) )
@ -711,8 +727,8 @@ class RetailCRM extends Module
if (isset($paymentSettingsDefault) && $paymentSettingsDefault != '') { if (isset($paymentSettingsDefault) && $paymentSettingsDefault != '') {
$paymentTypesDefault = json_decode($paymentSettingsDefault); $paymentTypesDefault = json_decode($paymentSettingsDefault);
if ($paymentTypesDefault) { if ($paymentTypesDefault) {
$name = 'RETAILCRM_API_PAYMENT_DEFAULT'; $name = 'RETAILCRM_API_PAYMENT_DEFAULT';
$helper->fields_value[$name] = $paymentTypesDefault; $helper->fields_value[$name] = $paymentTypesDefault;
} }
} }
@ -791,46 +807,54 @@ class RetailCRM extends Module
public function hookActionCustomerAccountAdd($params) public function hookActionCustomerAccountAdd($params)
{ {
$customer = $params['newCustomer']; if ($this->api) {
$customerSend = static::buildCrmCustomer($customer); $customer = $params['newCustomer'];
$customerSend = static::buildCrmCustomer($customer);
$this->api->customersCreate($customerSend); $this->api->customersCreate($customerSend);
return $customerSend; return true;
}
return false;
} }
// this hook added in 1.7 // this hook added in 1.7
public function hookActionCustomerAccountUpdate($params) public function hookActionCustomerAccountUpdate($params)
{ {
$customer = $params['customer']; if ($this->api) {
$customer = $params['customer'];
$customerSend = static::buildCrmCustomer($customer); $customerSend = static::buildCrmCustomer($customer);
$addreses = $customer->getAddresses($this->default_lang); $addreses = $customer->getAddresses($this->default_lang);
$address = array_shift($addreses); $address = array_shift($addreses);
if (!empty($address)){ if (!empty($address)){
if (is_object($address)) { if (is_object($address)) {
$address = static::addressParse($address); $address = static::addressParse($address);
} else { } else {
$address = new Address($address['id_address']); $address = new Address($address['id_address']);
$address = static::addressParse($address); $address = static::addressParse($address);
}
$customerSend = array_merge($customerSend, $address['customer']);
} }
$customerSend = array_merge($customerSend, $address['customer']); if (isset($params['cart'])){
$address = static::addressParse($params['cart']);
$customerSend = array_merge($customerSend, $address['customer']);
}
$customerSend = array_merge($customerSend, isset($address['customer']) ? $address['customer'] : []);
$this->api->customersEdit($customerSend);
return true;
} }
if (isset($params['cart'])){ return false;
$address = static::addressParse($params['cart']);
$customerSend = array_merge($customerSend, $address['customer']);
}
$customerSend = array_merge($customerSend, $address['customer']);
$this->api->customersEdit($customerSend);
return $customerSend;
} }
// this hook added in 1.7 // this hook added in 1.7
@ -863,53 +887,66 @@ class RetailCRM extends Module
public function hookActionOrderEdited($params) public function hookActionOrderEdited($params)
{ {
$order = array( if ($this->api) {
'externalId' => $params['order']->id, $order = array(
'firstName' => $params['customer']->firstname, 'externalId' => $params['order']->id,
'lastName' => $params['customer']->lastname, 'firstName' => $params['customer']->firstname,
'email' => $params['customer']->email, 'lastName' => $params['customer']->lastname,
'createdAt' => $params['order']->date_add, 'email' => $params['customer']->email,
'delivery' => array('cost' => $params['order']->total_shipping) '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)
);
if ($this->apiVersion != 5) { if ($this->apiVersion != 5) {
$order['discount'] = $params['order']->total_discounts; $order['discount'] = $params['order']->total_discounts;
} else {
$order['discountManualAmount'] = $params['order']->total_discounts;
}
$orderdb = new Order($params['order']->id);
$comment = $orderdb->getFirstMessage();
if ($comment !== false) {
$order['customerComment'] = $comment;
}
unset($comment);
foreach ($orderdb->getProducts() as $item) {
if (isset($item['product_attribute_id']) && $item['product_attribute_id'] > 0) {
$productId = $item['product_id'] . '#' . $item['product_attribute_id'];
} else { } else {
$productId = $item['product_id']; $order['discountManualAmount'] = $params['order']->total_discounts;
} }
$order['items'][] = array( $orderdb = new Order($params['order']->id);
'initialPrice' => $item['unit_price_tax_incl'],
'quantity' => $item['product_quantity'], $comment = $orderdb->getFirstMessage();
'offer' => array('externalId' => $productId), if ($comment !== false) {
'productName' => $item['product_name'], $order['customerComment'] = $comment;
); }
unset($comment);
foreach ($orderdb->getProducts() as $item) {
if (isset($item['product_attribute_id']) && $item['product_attribute_id'] > 0) {
$productId = $item['product_id'] . '#' . $item['product_attribute_id'];
} else {
$productId = $item['product_id'];
}
$order['items'][] = array(
'initialPrice' => $item['unit_price_tax_incl'],
'quantity' => $item['product_quantity'],
'offer' => array('externalId' => $productId),
'productName' => $item['product_name'],
);
}
$order['customer']['externalId'] = $params['order']->id_customer;
$this->api->ordersEdit($order);
return true;
} }
$order['customer']['externalId'] = $params['order']->id_customer; return false;
$this->api->ordersEdit($order);
return $order;
} }
private static function addressParse($address) private static function addressParse($address)
{ {
if (!isset($customer)) {
$customer = [];
}
if (!isset($order)) {
$order = [];
}
if ($address instanceof Address) { if ($address instanceof Address) {
$postcode = $address->postcode; $postcode = $address->postcode;
$city = $address->city; $city = $address->city;
@ -947,6 +984,14 @@ class RetailCRM extends Module
private static function getPhone($address) private static function getPhone($address)
{ {
if (!isset($customer)) {
$customer = [];
}
if (!isset($order)) {
$order = [];
}
if (!empty($address->phone_mobile)){ if (!empty($address->phone_mobile)){
$order['phone'] = $address->phone_mobile; $order['phone'] = $address->phone_mobile;
$customer['phones'][] = array('number'=> $address->phone_mobile); $customer['phones'][] = array('number'=> $address->phone_mobile);
@ -975,17 +1020,11 @@ class RetailCRM extends Module
if (isset($params['orderStatus'])) { if (isset($params['orderStatus'])) {
$cart = $params['cart']; $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); $order = static::buildCrmOrder($params['order'], $params['customer'], $params['cart'], false);
$this->api->ordersCreate($order); $this->api->ordersCreate($order);
return $order; return true;
} elseif (isset($params['newOrderStatus'])) { } elseif (isset($params['newOrderStatus'])) {
$statusCode = $params['newOrderStatus']->id; $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)) { if (isset($updatePayment)) {
$this->api->ordersPaymentEdit($updatePayment); $this->api->ordersPaymentEdit($updatePayment);
return $updatePayment; return true;
} else { } else {
$createPayment = array( $createPayment = array(
'externalId' => $params['paymentCC']->id, 'externalId' => $params['paymentCC']->id,
@ -1062,7 +1101,7 @@ class RetailCRM extends Module
$this->api->ordersPaymentCreate($createPayment); $this->api->ordersPaymentCreate($createPayment);
return $createPayment; return true;
} }
return false; return false;

View File

@ -35,34 +35,16 @@ class RetailCRMTest extends RetailcrmTestCase
{ {
$newCustomer = new Customer(1); $newCustomer = new Customer(1);
$params = array('newCustomer' => $newCustomer); $params = array('newCustomer' => $newCustomer);
$customer = $this->retailcrmModule->hookActionCustomerAccountAdd($params);
$this->assertNotEmpty($customer); $this->assertTrue($this->retailcrmModule->hookActionCustomerAccountAdd($params));
$this->assertArrayHasKey('externalId', $customer);
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('lastName', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertArrayHasKey('createdAt', $customer);
} }
public function testHookActionCustomerAccountUpdate() public function testHookActionCustomerAccountUpdate()
{ {
$customer = new Customer(1); $customer = new Customer(1);
$params = array('customer' => $customer); $params = array('customer' => $customer);
$customer = $this->retailcrmModule->hookActionCustomerAccountUpdate($params);
$this->assertNotEmpty($customer); $this->assertTrue($this->retailcrmModule->hookActionCustomerAccountUpdate($params));
$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']);
} }
public function testHookActionOrderEdited() public function testHookActionOrderEdited()
@ -71,15 +53,7 @@ class RetailCRMTest extends RetailcrmTestCase
$customer = new Customer($order->id_customer); $customer = new Customer($order->id_customer);
$params = array('order' => $order, 'customer' => $customer); $params = array('order' => $order, 'customer' => $customer);
$orderSend = $this->retailcrmModule->hookActionOrderEdited($params); $this->assertTrue($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);
} }
/** /**
@ -115,33 +89,7 @@ class RetailCRMTest extends RetailcrmTestCase
); );
} }
$result = $this->retailcrmModule->hookActionOrderStatusPostUpdate($params); $this->assertTrue($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);
}
}
} }
/** /**
@ -167,9 +115,8 @@ class RetailCRMTest extends RetailcrmTestCase
$result = $this->retailcrmModule->hookActionPaymentCCAdd($params); $result = $this->retailcrmModule->hookActionPaymentCCAdd($params);
$this->assertInternalType('array', $result); $this->assertInternalType('bool', $result);
$this->assertArrayHasKey('type', $result); $this->assertTrue($result);
$this->assertArrayHasKey('amount', $result);
RetailcrmTestHelper::deleteOrderPayment($orderPayment->id); RetailcrmTestHelper::deleteOrderPayment($orderPayment->id);
} }