Merge pull request #2 from gwinn/master

bug fixes, missing script
This commit is contained in:
Alex Lushpai 2016-01-16 18:04:58 +03:00
commit 8af172d005
6 changed files with 293 additions and 106 deletions

View File

@ -2,7 +2,7 @@
<module>
<name>retailcrm</name>
<displayName><![CDATA[RetailCRM]]></displayName>
<version><![CDATA[2.0]]></version>
<version><![CDATA[2.0.1]]></version>
<description><![CDATA[Integration module for RetailCRM]]></description>
<author><![CDATA[Retail Driver LCC]]></author>
<tab><![CDATA[export]]></tab>

View File

@ -2,7 +2,7 @@
<module>
<name>retailcrm</name>
<displayName><![CDATA[RetailCRM]]></displayName>
<version><![CDATA[2.0]]></version>
<version><![CDATA[2.0.1]]></version>
<description><![CDATA[Модуль интеграции с RetailCRM]]></description>
<author><![CDATA[Retail Driver LCC]]></author>
<tab><![CDATA[export]]></tab>

View File

@ -82,7 +82,7 @@ foreach ($orderRecords as $record) {
);
if (isset($postcode)) {
$order['delivery']['address']['postcode'] = $postcode;
$order['delivery']['address']['index'] = $postcode;
}
if (isset($city)) {

157
retailcrm/job/missing.php Normal file
View File

@ -0,0 +1,157 @@
<?php
$shortopts = 'o:';
$options = getopt($shortopts);
if (!isset($options['o'])) {
echo ('Parameter -o is missing');
exit();
}
require(dirname(__FILE__) . '/../../../config/config.inc.php');
require(dirname(__FILE__) . '/../../../init.php');
require(dirname(__FILE__) . '/../bootstrap.php');
$apiUrl = Configuration::get('RETAILCRM_ADDRESS');
$apiKey = Configuration::get('RETAILCRM_API_TOKEN');
if (!empty($apiUrl) && !empty($apiKey)) {
$api = new RetailcrmProxy($apiUrl, $apiKey, _PS_ROOT_DIR_ . '/retailcrm.log');
} else {
echo('Set api key & url first');
exit();
}
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'), true);
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
$status = json_decode(Configuration::get('RETAILCRM_API_STATUS'), true);
$orderInstance = new Order($options['o']);
$order = array(
'externalId' => $orderInstance->id,
'createdAt' => $orderInstance->date_add,
);
/**
* Add order customer info
*
*/
if (!empty($orderInstance->id_customer)) {
$orderCustomer = new Customer($orderInstance->id_customer);
$customer = array(
'externalId' => $orderCustomer->id,
'firstName' => $orderCustomer->firstname,
'lastname' => $orderCustomer->lastname,
'email' => $orderCustomer->email,
'createdAt' => $orderCustomer->date_add
);
$response = $api->customersEdit($customer);
if ($response) {
$order['customer']['externalId'] = $orderCustomer->id;
$order['firstName'] = $orderCustomer->firstname;
$order['lastName'] = $orderCustomer->lastname;
$order['email'] = $orderCustomer->email;
} else {
exit();
}
}
/**
* Add order status
*
*/
if ($orderInstance->current_state == 0) {
$order['status'] = 'completed';
} else {
$order['status'] = array_key_exists($orderInstance->current_state, $status)
? $status[$orderInstance->current_state]
: 'completed'
;
}
/**
* Add order address data
*
*/
$cart = new Cart($orderInstance->getCartIdStatic($orderInstance->id));
$addressCollection = $cart->getAddressCollection();
$address = array_shift($addressCollection);
if ($address instanceof Address) {
$phone = is_null($address->phone)
? is_null($address->phone_mobile) ? '' : $address->phone_mobile
: $address->phone
;
$postcode = $address->postcode;
$city = $address->city;
$addres_line = sprintf("%s %s", $address->address1, $address->address2);
}
if (!empty($postcode)) {
$order['delivery']['address']['index'] = $postcode;
}
if (!empty($city)) {
$order['delivery']['address']['city'] = $city;
}
if (!empty($addres_line)) {
$order['delivery']['address']['text'] = $addres_line;
}
if (!empty($phone)) {
$order['phone'] = $phone;
}
/**
* Add payment & shippment data
*/
if (Module::getInstanceByName('advancedcheckout') === false) {
$paymentType = $orderInstance->module;
} else {
$paymentType = $orderInstance->payment;
}
if (array_key_exists($paymentType, $payment) && !empty($payment[$paymentType])) {
$order['paymentType'] = $payment[$paymentType];
}
if (array_key_exists($orderInstance->id_carrier, $delivery) && !empty($delivery[$orderInstance->id_carrier])) {
$order['delivery']['code'] = $delivery[$orderInstance->id_carrier];
}
if (isset($orderInstance->total_shipping_tax_incl) && (int) $orderInstance->total_shipping_tax_incl > 0) {
$order['delivery']['cost'] = round($orderInstance->total_shipping_tax_incl, 2);
}
/**
* Add products
*
*/
$products = $orderInstance->getProducts();
foreach($products as $product) {
$item = array(
'productId' => $product['product_id'],
'productName' => $product['product_name'],
'quantity' => $product['product_quantity'],
'initialPrice' => round($product['product_price'], 2),
'purchasePrice' => round($product['purchase_supplier_price'], 2)
);
$order['items'][] = $item;
}
var_dump($order); die();
$api->ordersEdit($order);

View File

@ -31,13 +31,14 @@ $address_id = 0;
$history = $api->ordersHistory(new DateTime($startFrom));
if ($history->isSuccess() && count($history->orders) > 0) {
if ($history->isSuccessful() && count($history->orders) > 0) {
$statuses = array_flip(array_filter(json_decode(Configuration::get('RETAILCRM_API_STATUS'), true)));
$deliveries = array_flip(array_filter(json_decode(Configuration::get('RETAILCRM_API_DELIVERY'), true)));
$payments = array_flip(array_filter(json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true)));
foreach ($history->orders as $order) {
if (isset($order['deleted']) && $order['deleted'] == true) continue;
if (!array_key_exists('externalId', $order)) {
@ -56,51 +57,51 @@ if ($history->isSuccess() && count($history->orders) > 0) {
$customer->passwd = substr(str_shuffle(strtolower(sha1(rand() . time()))),0, 5);
if($customer->add()) {
$customer->getByEmail($order['customer']['email']);
$customer_id = $customer->id;
$address = new Address();
$address->id_customer = $customer->id;
$address->id_country = $default_country;
$address->lastname = $customer->lastname;
$address->firstname = $customer->firstname;
$address->alias = 'default';
$address->postcode = $customer['address']['index'];
$address->city = $customer['address']['city'];
$address->address1 = $customer['address']['text'];
$address->phone = $customer['phones'][0]['number'];
$address->postcode = $order['address']['index'];
$address->city = $order['address']['city'];
$address->address1 = $order['address']['text'];
$address->phone = $order['phone'];
$address->add();
$addr = $customer->getAddresses($default_lang);
$address_id = $addr[0]['id_address'];
}
} else {
$addresses = $customer->getAddresses($default_lang);
$address_id = $addresses[0]['id_address'];
$customer_id = $customer->id;
}
array_push(
$customerFix,
array(
'id' => $order['customer']['id'],
'externalId' => $customer_id
'externalId' => $customer->id
)
);
} else {
$addresses = $customer->getAddresses($default_lang);
$address_id = $addresses[0]['id_address'];
$customer_id = $order['customer']['externalId'];
}
$delivery = $order['delivery']['code'];
if (array_key_exists($delivery, $deliveries) && $deliveries[$delivery] != '') {
$deliveryType = $deliveries[$delivery];
}
$payment = $order['paymentType'];
if (array_key_exists($payment, $payments) && $payments[$payment] != '') {
$paymentType = $payments[$payment];
}
$state = $order['status'];
if (array_key_exists($state, $statuses) && $statuses[$state] != '') {
$orderStatus = $statuses[$state];
}
$cart = new Cart();
$cart->id_currency = $default_currency;
$cart->id_lang = $default_lang;
$cart->id_customer = $customer_id;
$cart->id_customer = $customer->id;
$cart->id_address_delivery = (int) $address_id;
$cart->id_address_invoice = (int) $address_id;
$cart->id_carrier = (int) $deliveries[$delivery];
@ -131,13 +132,15 @@ if ($history->isSuccess() && count($history->orders) > 0) {
$newOrder->id_cart = (int) $cart->id;
$newOrder->id_currency = $default_currency;
$newOrder->id_lang = $default_lang;
$newOrder->id_customer = (int) $customer_id;
$newOrder->id_carrier = (int) $deliveries[$delivery];
$newOrder->id_customer = (int) $customer->id;
if (isset($deliveryType)) $newOrder->id_carrier = (int) $deliveryType;
$newOrder->payment = $payments[$payment];
$newOrder->module = (Module::getInstanceByName('advancedcheckout') === false)
? $payments[$payment]
: 'advancedcheckout'
if (isset($paymentType)) {
$newOrder->module = (Module::getInstanceByName('advancedcheckout') === false)
? $paymentType
: 'advancedcheckout'
;
}
$newOrder->total_paid = $order['summ'] + $order['delivery']['cost'];
$newOrder->total_paid_tax_incl = $order['summ'] + $order['delivery']['cost'];
$newOrder->total_paid_tax_excl = $order['summ'] + $order['delivery']['cost'];
@ -148,8 +151,8 @@ if ($history->isSuccess() && count($history->orders) > 0) {
$newOrder->total_shipping_tax_incl = $order['delivery']['cost'];
$newOrder->total_shipping_tax_excl = $order['delivery']['cost'];
$newOrder->conversion_rate = 1.000000;
$newOrder->current_state = (int) $statuses[$state];
$newOrder->delivery_date = $order['delivery']['date'];
if (isset($orderStatus)) $newOrder->current_state = (int) $orderStatus;
if (!empty($order['delivery']['date'])) $newOrder->delivery_date = $order['delivery']['date'];
$newOrder->date_add = $order['createdAt'];
$newOrder->date_upd = $order['createdAt'];
$newOrder->valid = 1;
@ -171,10 +174,11 @@ if ($history->isSuccess() && count($history->orders) > 0) {
* Create order details
*/
$product_list = array();
foreach ($order['items'] as $item) {
$product = new Product((int) $item['offer']['externalId'], false, $default_lang);
$qty = $item['quantity'];
$product_list[] = array('product' =>$product, 'quantity' => $qty);
$product_list[] = array('product' => $product, 'quantity' => $qty);
}
$query = 'INSERT `'._DB_PREFIX_.'order_detail`
@ -187,11 +191,12 @@ if ($history->isSuccess() && count($history->orders) > 0) {
VALUES';
$context = new Context();
foreach ($product_list as $product) {
$query .= '('
.(int) $newOrder->id.',
0,
'. $this->context->shop->id.',
'. Context::getContext()->shop->id.',
'.(int) $product['product']->id.',
0,
'.implode('', array('\'', $product['product']->name, '\'')).',
@ -209,13 +214,16 @@ if ($history->isSuccess() && count($history->orders) > 0) {
Db::getInstance()->execute(rtrim($query, ','));
$this->api->customersFixExternalIds($customerFix);
$this->api->ordersFixExternalIds($orderFix);
$api->customersFixExternalIds($customerFix);
$api->ordersFixExternalIds($orderFix);
} else {
/*
* take last order update only
*/
if ($order['paymentType'] != null && $order['deliveryType'] != null && $order['status'] != null) {
if (!empty($order['paymentType']) &&
!empty($order['delivery']['code']) &&
!empty($order['status'])
) {
$orderToUpdate = new Order((int) $order['externalId']);
/*
@ -235,7 +243,7 @@ if ($history->isSuccess() && count($history->orders) > 0) {
/*
* check delivery type
*/
$dtype = $order['deliveryType'];
$dtype = $order['delivery']['code'];
if ($deliveries[$dtype] != null) {
if ($deliveries[$dtype] != $orderToUpdate->id_carrier) {
Db::getInstance()->execute('
@ -331,7 +339,7 @@ if ($history->isSuccess() && count($history->orders) > 0) {
$query .= '('
.(int) $orderToUpdate->id.',
0,
'. $this->context->shop->id.',
'. Context::getContext()->shop->id.',
'.(int) $product['product']->id.',
0,
'.implode('', array('\'', $product['product']->name, '\'')).',

View File

@ -20,6 +20,7 @@ class RetailCRM extends Module
{
$this->name = 'retailcrm';
$this->tab = 'export';
$this->version = '2.0.1';
$this->version = '2.0';
$this->author = 'Retail Driver LCC';
$this->displayName = $this->l('RetailCRM');
@ -271,8 +272,7 @@ class RetailCRM extends Module
$this->api->ordersEdit(
array(
'externalId' => $params['id_order'],
'paymentStatus' => 'paid',
'createdAt' => $params['cart']->date_upd
'paymentStatus' => 'paid'
)
);
@ -281,87 +281,109 @@ class RetailCRM extends Module
public function hookActionOrderStatusPostUpdate($params)
{
$address_id = Address::getFirstCustomerAddressId($params['cart']->id_customer);
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'address WHERE id_address=' . (int)$address_id;
$dbaddress = Db::getInstance()->ExecuteS($sql);
$address = $dbaddress[0];
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'));
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'));
$inCart = $params['cart']->getProducts();
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'), true);
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
$status = json_decode(Configuration::get('RETAILCRM_API_STATUS'), true);
if (isset($params['orderStatus'])) {
$this->api->customersEdit(
array(
'externalId' => $params['cart']->id_customer,
'lastName' => $params['customer']->lastname,
'firstName' => $params['customer']->firstname,
'email' => $params['customer']->email,
'phones' => array(array('number' => $address['phone'])),
'createdAt' => $params['customer']->date_add
)
$customer = array(
'externalId' => $params['customer']->id,
'lastName' => $params['customer']->lastname,
'firstName' => $params['customer']->firstname,
'email' => $params['customer']->email,
'createdAt' => $params['customer']->date_add
);
$items = array();
foreach ($inCart as $item) {
$items[] = array(
'initialPrice' => (!empty($item['rate'])) ? $item['price'] + ($item['price'] * $item['rate'] / 100) : $item['price'],
$order = array(
'externalId' => $params['order']->id,
'firstName' => $params['customer']->firstname,
'lastName' => $params['customer']->lastname,
'email' => $params['customer']->email,
'discount' => $params['order']->total_discounts,
'createdAt' => $params['order']->date_add,
'delivery' => array('cost' => $params['order']->total_shipping)
);
$cart = new Cart($params['cart']->id);
$addressCollection = $cart->getAddressCollection();
$address = array_shift($addressCollection);
if ($address instanceof Address) {
$phone = is_null($address->phone)
? is_null($address->phone_mobile) ? '' : $address->phone_mobile
: $address->phone
;
$postcode = $address->postcode;
$city = $address->city;
$addres_line = sprintf("%s %s", $address->address1, $address->address2);
}
if (!empty($postcode)) {
$customer['address']['index'] = $postcode;
$order['delivery']['address']['index'] = $postcode;
}
if (!empty($city)) {
$customer['address']['city'] = $city;
$order['delivery']['address']['city'] = $city;
}
if (!empty($addres_line)) {
$customer['address']['text'] = $addres_line;
$order['delivery']['address']['text'] = $addres_line;
}
if (!empty($phone)) {
$customer['phones'][] = array('number' => $phone);
$order['phone'] = $phone;
}
foreach ($cart->getProducts() as $item) {
$order['items'][] = array(
'initialPrice' => !empty($item['rate'])
? $item['price'] + ($item['price'] * $item['rate'] / 100)
: $item['price']
,
'quantity' => $item['quantity'],
'productId' => $item['id_product'],
'productName' => $item['name'],
'createdAt' => $item['date_add']
'productName' => $item['name']
);
}
$dTypeKey = $params['cart']->id_carrier;
$deliveryCode = $params['order']->id_carrier;
if (array_key_exists($deliveryCode, $delivery) && !empty($delivery[$deliveryCode])) {
$order['delivery']['code'] = $delivery[$deliveryCode];
}
if (Module::getInstanceByName('advancedcheckout') === false) {
$pTypeKey = $params['order']->module;
$paymentCode = $params['order']->module;
} else {
$pTypeKey = $params['order']->payment;
$paymentCode = $params['order']->payment;
}
$this->api->ordersCreate(
array(
'externalId' => $params['order']->id,
'orderType' => 'eshop-individual',
'orderMethod' => 'shopping-cart',
'status' => 'new',
'customerId' => $params['cart']->id_customer,
'firstName' => $params['customer']->firstname,
'lastName' => $params['customer']->lastname,
'phone' => $address['phone'],
'email' => $params['customer']->email,
'paymentType' => $payment->$pTypeKey,
'delivery' => array(
'code' => $delivery->$dTypeKey,
'cost' => $params['order']->total_shipping,
'address' => array(
'city' => $address['city'],
'index' => $address['postcode'],
'text' => $address['address1'],
)
),
'discount' => $params['order']->total_discounts,
'items' => $items,
'createdAt' => $params['order']->date_add
)
);
}
if (!empty($params['newOrderStatus'])) {
$statuses = OrderState::getOrderStates($this->default_lang);
$aStatuses = json_decode(Configuration::get('RETAILCRM_API_STATUS'));
foreach ($statuses as $status) {
if ($status['name'] == $params['newOrderStatus']->name) {
$currStatus = $status['id_order_state'];
$this->api->ordersEdit(
array(
'externalId' => $params['id_order'],
'status' => $aStatuses->$currStatus
)
);
}
if (array_key_exists($paymentCode, $payment) && !empty($payment[$paymentCode])) {
$order['paymentType'] = $payment[$paymentCode];
}
$statusCode = $params['orderStatus']->id;
if (array_key_exists($statusCode, $status) && !empty($status[$statusCode])) {
$order['status'] = $status[$statusCode];
} else {
$order['status'] = ['new'];
}
$customerCheck = $this->api->customersGet($customer['externalId']);
if ($customerCheck === false) {
$this->api->customersCreate($customer);
}
$order['customer']['externalId'] = $customer['externalId'];
$this->api->ordersCreate($order);
}
}
}