mirror of
https://github.com/retailcrm/prestashop-module.git
synced 2025-04-17 15:30:54 +00:00
переработка скрипта обратной интеграции
This commit is contained in:
parent
88fcb88dee
commit
52ee86eb71
2 changed files with 396 additions and 349 deletions
|
@ -9,6 +9,10 @@ if (file_exists(dirname(__FILE__) . '/../lib/custom/References.php')) {
|
|||
require(dirname(__FILE__) . '/../lib/classes/References.php');
|
||||
}
|
||||
|
||||
$default_lang = (int) Configuration::get('PS_LANG_DEFAULT');
|
||||
$default_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
|
||||
$default_country = (int) Configuration::get('PS_COUNTRY_DEFAULT');
|
||||
|
||||
$apiUrl = Configuration::get('RETAILCRM_ADDRESS');
|
||||
$apiKey = Configuration::get('RETAILCRM_API_TOKEN');
|
||||
|
||||
|
@ -27,6 +31,10 @@ $startFrom = ($lastSync === false)
|
|||
;
|
||||
|
||||
$history = array();
|
||||
$customerFix = array();
|
||||
$orderFix = array();
|
||||
|
||||
$address_id = 0;
|
||||
|
||||
/*
|
||||
* retrive orders from crm since last update
|
||||
|
@ -44,107 +52,129 @@ catch (InvalidJsonException $e) {
|
|||
/*
|
||||
* store recieved data into shop database
|
||||
*/
|
||||
if (!empty($history->orders)) {
|
||||
if (count($history->orders) > 0) {
|
||||
|
||||
/*
|
||||
* Customer object. Will be used for further updates.
|
||||
*/
|
||||
$this->customer = new Customer();
|
||||
|
||||
$statuses = array_flip((array)json_decode(Configuration::get('RETAILCRM_API_STATUS')));
|
||||
$deliveries = array_flip((array)json_decode(Configuration::get('RETAILCRM_API_DELIVERY')));
|
||||
$payments = array_flip((array)json_decode(Configuration::get('RETAILCRM_API_PAYMENT')));
|
||||
$statuses = array_filter(
|
||||
json_decode(
|
||||
Configuration::get('RETAILCRM_API_STATUS'), true
|
||||
)
|
||||
);
|
||||
|
||||
$statuses = array_flip($statuses);
|
||||
|
||||
$deliveries = array_filter(
|
||||
json_decode(
|
||||
Configuration::get('RETAILCRM_API_DELIVERY'), true
|
||||
)
|
||||
);
|
||||
|
||||
$deliveries = array_flip($deliveries);
|
||||
|
||||
$payments = array_filter(
|
||||
json_decode(
|
||||
Configuration::get('RETAILCRM_API_PAYMENT'), true
|
||||
)
|
||||
);
|
||||
|
||||
$payments = array_flip($payments);
|
||||
|
||||
foreach ($history->orders as $order) {
|
||||
|
||||
if (!array_key_exists('externalId', $order)) {
|
||||
/*
|
||||
* create customer if not exist
|
||||
*/
|
||||
$this->customer->getByEmail($order['customer']['email']);
|
||||
|
||||
$customer = new Customer();
|
||||
$customer->getByEmail($order['customer']['email']);
|
||||
|
||||
if (!array_key_exists('externalId', $order['customer'])) {
|
||||
if (Validate::isEmail($order['customer']['email'])) {
|
||||
|
||||
if (!$this->customer->id)
|
||||
if (!$customer->id)
|
||||
{
|
||||
$this->customer->firstname = $order['customer']['firstName'];
|
||||
$this->customer->lastname = $order['customer']['lastName'];
|
||||
$this->customer->email = $order['customer']['email'];
|
||||
$this->customer->passwd = substr(str_shuffle(strtolower(sha1(rand() . time()))),0, 5);
|
||||
$customer->firstname = $order['customer']['firstName'];
|
||||
$customer->lastname = $order['customer']['lastName'];
|
||||
$customer->email = $order['customer']['email'];
|
||||
$customer->passwd = substr(str_shuffle(strtolower(sha1(rand() . time()))),0, 5);
|
||||
|
||||
if($this->customer->add()) {
|
||||
if($customer->add()) {
|
||||
|
||||
/*
|
||||
* create customer address for delivery data
|
||||
*/
|
||||
|
||||
$this->customer->getByEmail($order['customer']['email']);
|
||||
$this->customer_id = $this->customer->id;
|
||||
$customer->getByEmail($order['customer']['email']);
|
||||
$customer_id = $customer->id;
|
||||
|
||||
$address = new Address();
|
||||
$address->id_customer = $this->customer->id;
|
||||
$address->id_country = $this->default_country;
|
||||
$address->lastname = $this->customer->lastname;
|
||||
$address->firstname = $this->customer->firstname;
|
||||
$address->id_customer = $customer->id;
|
||||
$address->id_country = $default_country;
|
||||
$address->lastname = $customer->lastname;
|
||||
$address->firstname = $customer->firstname;
|
||||
$address->alias = 'default';
|
||||
$address->postcode = $order['deliveryAddress']['index'];
|
||||
$address->city = $order['deliveryAddress']['city'];
|
||||
$address->address1 = $order['deliveryAddress']['text'];
|
||||
$address->phone = $order['phone'];
|
||||
$address->phone_mobile = $order['phone'];
|
||||
$address->postcode = $customer['address']['index'];
|
||||
$address->city = $customer['address']['city'];
|
||||
$address->address1 = $customer['address']['text'];
|
||||
$address->phone = $customer['phones'][0]['number'];
|
||||
|
||||
$address->add();
|
||||
|
||||
/*
|
||||
* store address record id for handle order data
|
||||
*/
|
||||
$addr = $this->customer->getAddresses($this->default_lang);
|
||||
$this->address_id = $addr[0]['id_address'];
|
||||
$addr = $customer->getAddresses($default_lang);
|
||||
$address_id = $addr[0]['id_address'];
|
||||
}
|
||||
} else {
|
||||
$addresses = $this->customer->getAddresses($this->default_lang);
|
||||
$this->address_id = $addresses[0]['id_address'];
|
||||
$this->customer_id = $this->customer->id;
|
||||
$addresses = $customer->getAddresses($default_lang);
|
||||
$address_id = $addresses[0]['id_address'];
|
||||
$customer_id = $customer->id;
|
||||
}
|
||||
|
||||
/*
|
||||
* collect customer ids for single fix request
|
||||
*/
|
||||
array_push(
|
||||
$this->customerFix,
|
||||
array(
|
||||
'id' => $order['customer']['id'],
|
||||
'externalId' => $this->customer_id
|
||||
)
|
||||
$customerFix,
|
||||
array(
|
||||
'id' => $order['customer']['id'],
|
||||
'externalId' => $customer_id
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$addresses = $this->customer->getAddresses($this->default_lang);
|
||||
$this->address_id = $addresses[0]['id_address'];
|
||||
$this->customer_id = $order['customer']['externalId'];
|
||||
$addresses = $customer->getAddresses($default_lang);
|
||||
$address_id = $addresses[0]['id_address'];
|
||||
$customer_id = $order['customer']['externalId'];
|
||||
}
|
||||
|
||||
$delivery = $order['deliveryType'];
|
||||
$delivery = $order['delivery']['code'];
|
||||
$payment = $order['paymentType'];
|
||||
$state = $order['status'];
|
||||
|
||||
$cart = new Cart();
|
||||
$cart->id_currency = $this->default_currency;
|
||||
$cart->id_lang = $this->default_lang;
|
||||
$cart->id_customer = $this->customer_id;
|
||||
$cart->id_address_delivery = (int) $this->address_id;
|
||||
$cart->id_address_invoice = (int) $this->address_id;
|
||||
$cart->id_currency = $default_currency;
|
||||
$cart->id_lang = $default_lang;
|
||||
$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];
|
||||
|
||||
$cart->add();
|
||||
|
||||
$products = array();
|
||||
|
||||
if(!empty($order['items'])) {
|
||||
foreach ($order['items'] as $item) {
|
||||
$product = array();
|
||||
$product['id_product'] = (int) $item['offer']['externalId'];
|
||||
$product['quantity'] = $item['quantity'];
|
||||
$product['id_address_delivery'] = (int) $this->address_id;
|
||||
$product['id_address_delivery'] = (int) $address_id;
|
||||
$products[] = $product;
|
||||
}
|
||||
}
|
||||
|
@ -157,58 +187,225 @@ if (!empty($history->orders)) {
|
|||
*/
|
||||
|
||||
$newOrder = new Order();
|
||||
$newOrder->id_address_delivery = (int) $this->address_id;
|
||||
$newOrder->id_address_invoice = (int) $this->address_id;
|
||||
$newOrder->id_address_delivery = (int) $address_id;
|
||||
$newOrder->id_address_invoice = (int) $address_id;
|
||||
$newOrder->id_cart = (int) $cart->id;
|
||||
$newOrder->id_currency = $this->default_currency;
|
||||
$newOrder->id_lang = $this->default_lang;
|
||||
$newOrder->id_customer = (int) $this->customer_id;
|
||||
$newOrder->id_currency = $default_currency;
|
||||
$newOrder->id_lang = $default_lang;
|
||||
$newOrder->id_customer = (int) $customer_id;
|
||||
$newOrder->id_carrier = (int) $deliveries[$delivery];
|
||||
$newOrder->payment = $payments[$payment];
|
||||
$newOrder->module = (Module::getInstanceByName('advancedcheckout') === false)
|
||||
? $payments[$payment]
|
||||
: 'advancedcheckout'
|
||||
? $payments[$payment]
|
||||
: 'advancedcheckout'
|
||||
;
|
||||
$newOrder->total_paid = $order['summ'] + $order['deliveryCost'];
|
||||
$newOrder->total_paid_tax_incl = $order['summ'] + $order['deliveryCost'];
|
||||
$newOrder->total_paid_tax_excl = $order['summ'] + $order['deliveryCost'];
|
||||
$newOrder->total_paid_real = $order['summ'] + $order['deliveryCost'];
|
||||
$newOrder->total_products = $order['summ'];
|
||||
$newOrder->total_products_wt = $order['summ'];
|
||||
$newOrder->total_shipping = $order['deliveryCost'];
|
||||
$newOrder->total_shipping_tax_incl = $order['deliveryCost'];
|
||||
$newOrder->total_shipping_tax_excl = $order['deliveryCost'];
|
||||
$newOrder->conversion_rate = 1.000000;
|
||||
$newOrder->current_state = (int) $statuses[$state];
|
||||
$newOrder->delivery_date = $order['deliveryDate'];
|
||||
$newOrder->date_add = $order['createdAt'];
|
||||
$newOrder->date_upd = $order['createdAt'];
|
||||
$newOrder->valid = 1;
|
||||
$newOrder->secure_key = md5(time());
|
||||
$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'];
|
||||
$newOrder->total_paid_real = $order['summ'] + $order['delivery']['cost'];
|
||||
$newOrder->total_products = $order['summ'];
|
||||
$newOrder->total_products_wt = $order['summ'];
|
||||
$newOrder->total_shipping = $order['delivery']['cost'];
|
||||
$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'];
|
||||
$newOrder->date_add = $order['createdAt'];
|
||||
$newOrder->date_upd = $order['createdAt'];
|
||||
$newOrder->valid = 1;
|
||||
$newOrder->secure_key = md5(time());
|
||||
|
||||
if (isset($order['discount']))
|
||||
{
|
||||
$newOrder->total_discounts = $order['discount'];
|
||||
}
|
||||
if (isset($order['discount']))
|
||||
{
|
||||
$newOrder->total_discounts = $order['discount'];
|
||||
}
|
||||
|
||||
$newOrder->add(false, false);
|
||||
$newOrder->add(false, false);
|
||||
|
||||
/*
|
||||
* collect order ids for single fix request
|
||||
*/
|
||||
array_push($orderFix, array('id' => $order['id'], 'externalId' => $newOrder->id));
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
$query = 'INSERT `'._DB_PREFIX_.'order_detail`
|
||||
(
|
||||
`id_order`, `id_order_invoice`, `id_shop`, `product_id`, `product_attribute_id`,
|
||||
`product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`,
|
||||
`product_reference`, `total_price_tax_excl`, `total_price_tax_incl`,
|
||||
`unit_price_tax_excl`, `unit_price_tax_incl`, `original_product_price`
|
||||
)
|
||||
|
||||
VALUES';
|
||||
|
||||
foreach ($product_list as $product) {
|
||||
$query .= '('
|
||||
.(int) $newOrder->id.',
|
||||
0,
|
||||
'. $this->context->shop->id.',
|
||||
'.(int) $product['product']->id.',
|
||||
0,
|
||||
'.implode('', array('\'', $product['product']->name, '\'')).',
|
||||
'.(int) $product['quantity'].',
|
||||
'.(int) $product['quantity'].',
|
||||
'.$product['product']->price.',
|
||||
'.implode('', array('\'', $product['product']->reference, '\'')).',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.'
|
||||
),';
|
||||
}
|
||||
|
||||
Db::getInstance()->execute(rtrim($query, ','));
|
||||
|
||||
try {
|
||||
$this->api->customersFixExternalIds($customerFix);
|
||||
}
|
||||
catch (CurlException $e) {
|
||||
error_log('customersFixExternalId: connection error', 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
||||
continue;
|
||||
}
|
||||
catch (InvalidJsonException $e) {
|
||||
error_log('customersFixExternalId: ' . $e->getMessage(), 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->api->ordersFixExternalIds($orderFix);
|
||||
}
|
||||
catch (CurlException $e) {
|
||||
error_log('ordersFixExternalId: connection error', 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
||||
continue;
|
||||
}
|
||||
catch (InvalidJsonException $e) {
|
||||
error_log('ordersFixExternalId: ' . $e->getMessage(), 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* take last order update only
|
||||
*/
|
||||
|
||||
if ($order['paymentType'] != null && $order['deliveryType'] != null && $order['status'] != null) {
|
||||
$orderToUpdate = new Order((int) $order['externalId']);
|
||||
|
||||
/*
|
||||
* collect order ids for single fix request
|
||||
* check status
|
||||
*/
|
||||
array_push($this->orderFix, array('id' => $order['id'], 'externalId' => $newOrder->id));
|
||||
|
||||
/*
|
||||
* Create order details
|
||||
*/
|
||||
$product_list = array();
|
||||
foreach ($order['items'] as $item) {
|
||||
$product = new Product((int) $item['offer']['externalId'], false, $this->default_lang);
|
||||
$qty = $item['quantity'];
|
||||
$product_list[] = array('product' =>$product, 'quantity' => $qty);
|
||||
$stype = $order['status'];
|
||||
if ($statuses[$stype] != null) {
|
||||
if ($statuses[$stype] != $orderToUpdate->current_state) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `current_state` = \''.$statuses[$stype].'\'
|
||||
WHERE `id_order` = '.(int) $order['externalId']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$query = 'INSERT `'._DB_PREFIX_.'order_detail`
|
||||
/*
|
||||
* check delivery type
|
||||
*/
|
||||
$dtype = $order['deliveryType'];
|
||||
if ($deliveries[$dtype] != null) {
|
||||
if ($deliveries[$dtype] != $orderToUpdate->id_carrier) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `id_carrier` = \''.$deliveries[$dtype].'\'
|
||||
WHERE `id_order` = '.(int) $order['externalId']
|
||||
);
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'order_carrier`
|
||||
SET `id_carrier` = \''.$deliveries[$dtype].'\'
|
||||
WHERE `id_order` = \''.$orderToUpdate->id.'\''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check payment type
|
||||
*/
|
||||
$ptype = $order['paymentType'];
|
||||
if ($payments[$ptype] != null) {
|
||||
if ($payments[$ptype] != $orderToUpdate->payment) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `payment` = \''.$payments[$ptype].'\'
|
||||
WHERE `id_order` = '.(int) $order['externalId']
|
||||
);
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'order_payment`
|
||||
SET `payment_method` = \''.$payments[$ptype].'\'
|
||||
WHERE `order_reference` = \''.$orderToUpdate->reference.'\''
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check items
|
||||
*/
|
||||
|
||||
/*
|
||||
* Clean deleted
|
||||
*/
|
||||
foreach ($order['items'] as $key => $item) {
|
||||
if (isset($item['deleted']) && $item['deleted'] == true) {
|
||||
Db::getInstance()->execute('
|
||||
DELETE FROM `'._DB_PREFIX_.'order_detail`
|
||||
WHERE `id_order` = '. $orderToUpdate->id .'
|
||||
AND `product_id` = '.$item['id']
|
||||
);
|
||||
|
||||
unset($order['items'][$key]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check quantity
|
||||
*/
|
||||
|
||||
foreach ($orderToUpdate->getProductsDetail() as $orderItem) {
|
||||
foreach ($order['items'] as $key => $item) {
|
||||
if ($item['offer']['externalId'] == $orderItem['product_id']) {
|
||||
if (isset($item['quantity']) && $item['quantity'] != $orderItem['product_quantity']) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'order_detail`
|
||||
SET `product_quantity` = '.$item['quantity'].',
|
||||
`product_quantity_in_stock` = '.$item['quantity'].'
|
||||
WHERE `id_order_detail` = '.$orderItem['id_order_detail']
|
||||
);
|
||||
}
|
||||
|
||||
unset($order['items'][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check new items
|
||||
*/
|
||||
if (!empty($order['items'])) {
|
||||
foreach ($order['items'] as $key => $newItem) {
|
||||
$product = new Product((int) $newItem['offer']['externalId'], false, $default_lang);
|
||||
$qty = $newItem['quantity'];
|
||||
$product_list[] = array('product' =>$product, 'quantity' => $qty);
|
||||
}
|
||||
|
||||
|
||||
$query = 'INSERT `'._DB_PREFIX_.'order_detail`
|
||||
(
|
||||
`id_order`, `id_order_invoice`, `id_shop`, `product_id`, `product_attribute_id`,
|
||||
`product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`,
|
||||
|
@ -218,9 +415,9 @@ if (!empty($history->orders)) {
|
|||
|
||||
VALUES';
|
||||
|
||||
foreach ($product_list as $product) {
|
||||
$query .= '('
|
||||
.(int) $newOrder->id.',
|
||||
foreach ($product_list as $product) {
|
||||
$query .= '('
|
||||
.(int) $orderToUpdate->id.',
|
||||
0,
|
||||
'. $this->context->shop->id.',
|
||||
'.(int) $product['product']->id.',
|
||||
|
@ -236,197 +433,45 @@ if (!empty($history->orders)) {
|
|||
'.$product['product']->price.',
|
||||
'.$product['product']->price.'
|
||||
),';
|
||||
}
|
||||
|
||||
Db::getInstance()->execute(rtrim($query, ','));
|
||||
unset($order['items'][$key]);
|
||||
}
|
||||
|
||||
Db::getInstance()->execute(rtrim($query, ','));
|
||||
|
||||
try {
|
||||
$this->api->customersFixExternalIds($this->customerFix);
|
||||
$this->api->ordesrFixExternalIds($this->orderFix);
|
||||
}
|
||||
catch (CurlException $e) {
|
||||
error_log('fixExternalId: connection error', 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
||||
continue;
|
||||
}
|
||||
catch (InvalidJsonException $e) {
|
||||
error_log('fixExternalId: ' . $e->getMessage(), 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
||||
continue;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (!in_array($order['id'], $toUpdate))
|
||||
{
|
||||
/*
|
||||
* take last order update only
|
||||
* Fix prices & discounts
|
||||
* Discounts only for whole order
|
||||
*/
|
||||
$orderDiscout = null;
|
||||
$orderTotal = $order['summ'];
|
||||
|
||||
if ($order['paymentType'] != null && $order['deliveryType'] != null && $order['status'] != null) {
|
||||
$orderToUpdate = new Order((int) $order['externalId']);
|
||||
|
||||
/*
|
||||
* check status
|
||||
*/
|
||||
$stype = $order['status'];
|
||||
if ($statuses[$stype] != null) {
|
||||
if ($statuses[$stype] != $orderToUpdate->current_state) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `current_state` = \''.$statuses[$stype].'\'
|
||||
WHERE `id_order` = '.(int) $order['externalId']);
|
||||
}
|
||||
if (isset($order['discount']) && $order['discount'] > 0) {
|
||||
if ($order['discount'] != $orderToUpdate->total_discounts) {
|
||||
$orderDiscout = ($orderDiscout == null) ? $order['discount'] : $order['discount'] + $orderDiscout;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check delivery type
|
||||
*/
|
||||
$dtype = $order['deliveryType'];
|
||||
if ($deliveries[$dtype] != null) {
|
||||
if ($deliveries[$dtype] != $orderToUpdate->id_carrier) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `id_carrier` = \''.$deliveries[$dtype].'\'
|
||||
WHERE `id_order` = '.(int) $order['externalId']);
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'order_carrier`
|
||||
SET `id_carrier` = \''.$deliveries[$dtype].'\'
|
||||
WHERE `id_order` = \''.$orderToUpdate->id.'\'');
|
||||
}
|
||||
if (isset($order['discountPercent']) && $order['discountPercent'] > 0) {
|
||||
$percent = ($order['summ'] * $order['discountPercent'])/100;
|
||||
if ($percent != $orderToUpdate->total_discounts) {
|
||||
$orderDiscout = ($orderDiscout == null) ? $percent : $percent + $orderDiscout;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check payment type
|
||||
*/
|
||||
$ptype = $order['paymentType'];
|
||||
if ($payments[$ptype] != null) {
|
||||
if ($payments[$ptype] != $orderToUpdate->payment) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `payment` = \''.$payments[$ptype].'\'
|
||||
WHERE `id_order` = '.(int) $order['externalId']);
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'order_payment`
|
||||
SET `payment_method` = \''.$payments[$ptype].'\'
|
||||
WHERE `order_reference` = \''.$orderToUpdate->reference.'\'');
|
||||
$totalDiscount = ($orderDiscout == null) ? $orderToUpdate->total_discounts : $orderDiscout;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check items
|
||||
*/
|
||||
|
||||
/*
|
||||
* Clean deleted
|
||||
*/
|
||||
foreach ($order['items'] as $key => $item) {
|
||||
if (isset($item['deleted']) && $item['deleted'] == true) {
|
||||
Db::getInstance()->execute('
|
||||
DELETE FROM `'._DB_PREFIX_.'order_detail`
|
||||
WHERE `id_order` = '. $orderToUpdate->id .'
|
||||
AND `product_id` = '.$item['id']);
|
||||
|
||||
unset($order['items'][$key]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check quantity
|
||||
*/
|
||||
|
||||
foreach ($orderToUpdate->getProductsDetail() as $orderItem) {
|
||||
foreach ($order['items'] as $key => $item) {
|
||||
if ($item['offer']['externalId'] == $orderItem['product_id']) {
|
||||
if (isset($item['quantity']) && $item['quantity'] != $orderItem['product_quantity']) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'order_detail`
|
||||
SET `product_quantity` = '.$item['quantity'].',
|
||||
`product_quantity_in_stock` = '.$item['quantity'].'
|
||||
WHERE `id_order_detail` = '.$orderItem['id_order_detail']);
|
||||
}
|
||||
|
||||
unset($order['items'][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check new items
|
||||
*/
|
||||
if (!empty($order['items'])) {
|
||||
foreach ($order['items'] as $key => $newItem) {
|
||||
$product = new Product((int) $newItem['offer']['externalId'], false, $this->default_lang);
|
||||
$qty = $newItem['quantity'];
|
||||
$product_list[] = array('product' =>$product, 'quantity' => $qty);
|
||||
}
|
||||
|
||||
|
||||
$query = 'INSERT `'._DB_PREFIX_.'order_detail`
|
||||
(
|
||||
`id_order`, `id_order_invoice`, `id_shop`, `product_id`, `product_attribute_id`,
|
||||
`product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`,
|
||||
`product_reference`, `total_price_tax_excl`, `total_price_tax_incl`,
|
||||
`unit_price_tax_excl`, `unit_price_tax_incl`, `original_product_price`
|
||||
)
|
||||
|
||||
VALUES';
|
||||
|
||||
foreach ($product_list as $product) {
|
||||
$query .= '('
|
||||
.(int) $orderToUpdate->id.',
|
||||
0,
|
||||
'. $this->context->shop->id.',
|
||||
'.(int) $product['product']->id.',
|
||||
0,
|
||||
'.implode('', array('\'', $product['product']->name, '\'')).',
|
||||
'.(int) $product['quantity'].',
|
||||
'.(int) $product['quantity'].',
|
||||
'.$product['product']->price.',
|
||||
'.implode('', array('\'', $product['product']->reference, '\'')).',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.',
|
||||
'.$product['product']->price.'
|
||||
),';
|
||||
}
|
||||
|
||||
Db::getInstance()->execute(rtrim($query, ','));
|
||||
unset($order['items'][$key]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Fix prices & discounts
|
||||
* Discounts only for whole order
|
||||
*/
|
||||
$orderDiscout = null;
|
||||
$orderTotal = $order['summ'];
|
||||
|
||||
if (isset($order['discount']) && $order['discount'] > 0) {
|
||||
if ($order['discount'] != $orderToUpdate->total_discounts) {
|
||||
$orderDiscout = ($orderDiscout == null) ? $order['discount'] : $order['discount'] + $orderDiscout;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($order['discountPercent']) && $order['discountPercent'] > 0) {
|
||||
$percent = ($order['summ'] * $order['discountPercent'])/100;
|
||||
if ($percent != $orderToUpdate->total_discounts) {
|
||||
$orderDiscout = ($orderDiscout == null) ? $percent : $percent + $orderDiscout;
|
||||
}
|
||||
}
|
||||
|
||||
$totalDiscount = ($orderDiscout == null) ? $orderToUpdate->total_discounts : $orderDiscout;
|
||||
|
||||
if ($totalDiscount != $orderToUpdate->total_discounts || $orderTotal != $orderToUpdate->total_paid) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `total_discounts` = '.$totalDiscount.',
|
||||
`total_discounts_tax_incl` = '.$totalDiscount.',
|
||||
`total_discounts_tax_excl` = '.$totalDiscount.',
|
||||
`total_paid` = '.$orderTotal.',
|
||||
`total_paid_tax_incl` = '.$orderTotal.',
|
||||
`total_paid_tax_excl` = '.$orderTotal.'
|
||||
WHERE `id_order` = '.(int) $order['externalId']);
|
||||
}
|
||||
if ($totalDiscount != $orderToUpdate->total_discounts || $orderTotal != $orderToUpdate->total_paid) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'orders`
|
||||
SET `total_discounts` = '.$totalDiscount.',
|
||||
`total_discounts_tax_incl` = '.$totalDiscount.',
|
||||
`total_discounts_tax_excl` = '.$totalDiscount.',
|
||||
`total_paid` = '.$orderTotal.',
|
||||
`total_paid_tax_incl` = '.$orderTotal.',
|
||||
`total_paid_tax_excl` = '.$orderTotal.'
|
||||
WHERE `id_order` = '.(int) $order['externalId']
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -297,89 +297,91 @@ class RetailCRM extends Module
|
|||
|
||||
if (!empty($address)) {
|
||||
foreach ($address as $idx => $a) {
|
||||
if (!strpos($a, ':')) {
|
||||
$a = preg_replace('/_/', ' ', $a);
|
||||
$a = preg_replace('/[\,\.]/', '', $a);
|
||||
$addressFields[] = array(
|
||||
'type' => 'select',
|
||||
'label' => $this->l((string) ucfirst($a)),
|
||||
'name' => 'RETAILCRM_API_ADDR[' . $idx . ']',
|
||||
'required' => false,
|
||||
'options' => array(
|
||||
'query' => array(
|
||||
array(
|
||||
'name' => '',
|
||||
'id_option' => ''
|
||||
if (!in_array($a, array('vat_number', 'phone_mobile', 'company'))) {
|
||||
if (!strpos($a, ':')) {
|
||||
$a = preg_replace('/_/', ' ', $a);
|
||||
$a = preg_replace('/[\,\.]/', '', $a);
|
||||
$addressFields[] = array(
|
||||
'type' => 'select',
|
||||
'label' => $this->l((string) ucfirst($a)),
|
||||
'name' => 'RETAILCRM_API_ADDR[' . $idx . ']',
|
||||
'required' => false,
|
||||
'options' => array(
|
||||
'query' => array(
|
||||
array(
|
||||
'name' => '',
|
||||
'id_option' => ''
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('FIRST_NAME'),
|
||||
'id_option' => 'first_name'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('LAST_NAME'),
|
||||
'id_option' => 'last_name'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('PHONE'),
|
||||
'id_option' => 'phone'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('EMAIL'),
|
||||
'id_option' => 'email'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('ADDRESS'),
|
||||
'id_option' => 'address'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('COUNTRY'),
|
||||
'id_option' => 'country'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('REGION'),
|
||||
'id_option' => 'region'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('CITY'),
|
||||
'id_option' => 'city'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('ZIP'),
|
||||
'id_option' => 'index'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('STREET'),
|
||||
'id_option' => 'street'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('BUILDING'),
|
||||
'id_option' => 'building'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('FLAT'),
|
||||
'id_option' => 'flat'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('INTERCOMCODE'),
|
||||
'id_option' => 'intercomcode'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('FLOOR'),
|
||||
'id_option' => 'floor'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('BLOCK'),
|
||||
'id_option' => 'block'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('HOUSE'),
|
||||
'ID' => 'house'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('FIRST_NAME'),
|
||||
'id_option' => 'first_name'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('LAST_NAME'),
|
||||
'id_option' => 'last_name'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('PHONE'),
|
||||
'id_option' => 'phone'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('EMAIL'),
|
||||
'id_option' => 'email'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('ADDRESS'),
|
||||
'id_option' => 'address'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('COUNTRY'),
|
||||
'id_option' => 'country'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('REGION'),
|
||||
'id_option' => 'region'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('CITY'),
|
||||
'id_option' => 'city'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('ZIP'),
|
||||
'id_option' => 'index'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('STREET'),
|
||||
'id_option' => 'street'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('BUILDING'),
|
||||
'id_option' => 'building'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('FLAT'),
|
||||
'id_option' => 'flat'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('INTERCOMCODE'),
|
||||
'id_option' => 'intercomcode'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('FLOOR'),
|
||||
'id_option' => 'floor'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('BLOCK'),
|
||||
'id_option' => 'block'
|
||||
),
|
||||
array(
|
||||
'name' => $this->l('HOUSE'),
|
||||
'ID' => 'house'
|
||||
)
|
||||
),
|
||||
'id' => 'id_option',
|
||||
'name' => 'name'
|
||||
)
|
||||
);
|
||||
'id' => 'id_option',
|
||||
'name' => 'name'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue