2016-01-29 18:04:48 +03:00
|
|
|
<?php
|
|
|
|
|
2017-03-22 12:12:54 +03:00
|
|
|
class ModelExtensionRetailcrmOrder extends Model {
|
2016-01-29 18:04:48 +03:00
|
|
|
|
|
|
|
public function sendToCrm($order_data, $order_id)
|
|
|
|
{
|
2016-04-14 13:55:11 +03:00
|
|
|
if(isset($this->request->post['fromApi'])) return;
|
|
|
|
|
2016-01-29 18:04:48 +03:00
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$settings = $this->model_setting_setting->getSetting('retailcrm');
|
|
|
|
|
|
|
|
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
|
2016-07-25 17:21:15 +03:00
|
|
|
$this->load->model('catalog/product');
|
|
|
|
|
2016-01-29 18:04:48 +03:00
|
|
|
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
|
|
|
|
|
|
|
|
$this->retailcrm = new RetailcrmProxy(
|
|
|
|
$settings['retailcrm_url'],
|
|
|
|
$settings['retailcrm_apikey'],
|
2017-03-22 12:12:54 +03:00
|
|
|
DIR_SYSTEM . 'storage/logs/retailcrm.log'
|
2016-01-29 18:04:48 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$order = array();
|
|
|
|
|
|
|
|
$customers = $this->retailcrm->customersList(
|
|
|
|
array(
|
|
|
|
'name' => $order_data['telephone'],
|
|
|
|
'email' => $order_data['email']
|
|
|
|
),
|
|
|
|
1,
|
|
|
|
100
|
|
|
|
);
|
|
|
|
|
2016-07-25 17:21:15 +03:00
|
|
|
if($customers) {
|
|
|
|
foreach ($customers['customers'] as $customer) {
|
|
|
|
$order['customer']['id'] = $customer['id'];
|
|
|
|
}
|
2016-01-29 18:04:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($customers);
|
|
|
|
|
|
|
|
$order['externalId'] = $order_id;
|
|
|
|
$order['firstName'] = $order_data['firstname'];
|
|
|
|
$order['lastName'] = $order_data['lastname'];
|
|
|
|
$order['phone'] = $order_data['telephone'];
|
|
|
|
$order['customerComment'] = $order_data['comment'];
|
|
|
|
|
2016-10-14 18:13:16 +03:00
|
|
|
if(!empty($order_data['email'])) {
|
|
|
|
$order['email'] = $order_data['email'];
|
|
|
|
}
|
|
|
|
|
2016-01-29 18:04:48 +03:00
|
|
|
$deliveryCost = 0;
|
2016-03-11 14:15:24 +03:00
|
|
|
$altTotals = isset($order_data['order_total']) ? $order_data['order_total'] : "";
|
|
|
|
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $altTotals ;
|
2017-04-07 12:35:53 +03:00
|
|
|
$couponTotal = 0;
|
2016-03-11 14:15:24 +03:00
|
|
|
|
|
|
|
if (!empty($orderTotals)) {
|
|
|
|
foreach ($orderTotals as $totals) {
|
|
|
|
if ($totals['code'] == 'shipping') {
|
|
|
|
$deliveryCost = $totals['value'];
|
|
|
|
}
|
2017-04-07 12:35:53 +03:00
|
|
|
|
|
|
|
if ($totals['code'] == 'coupon') {
|
|
|
|
$couponTotal = abs($totals['value']);
|
|
|
|
}
|
2016-01-29 18:04:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:35:53 +03:00
|
|
|
$order['discount'] = $couponTotal;
|
2016-10-14 18:13:16 +03:00
|
|
|
$order['createdAt'] = $order_data['date_added'];
|
2016-01-29 18:04:48 +03:00
|
|
|
|
2016-04-07 15:31:04 +03:00
|
|
|
$payment_code = $order_data['payment_code'];
|
|
|
|
$order['paymentType'] = $settings['retailcrm_payment'][$payment_code];
|
2016-01-29 18:04:48 +03:00
|
|
|
|
2016-04-08 14:59:15 +03:00
|
|
|
if(!isset($order_data['shipping_iso_code_2']) && isset($order_data['shipping_country_id'])) {
|
|
|
|
$this->load->model('localisation/country');
|
|
|
|
$shipping_country = $this->model_localisation_country->getCountry($order_data['shipping_country_id']);
|
|
|
|
$order_data['shipping_iso_code_2'] = $shipping_country['iso_code_2'];
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:58:45 +03:00
|
|
|
if(isset($settings['retailcrm_delivery'][$order_data['shipping_code']])) {
|
|
|
|
$delivery_code = $order_data['shipping_code'];
|
|
|
|
} else {
|
|
|
|
$delivery_code = stristr($order_data['shipping_code'], '.', TRUE);
|
|
|
|
}
|
|
|
|
|
2016-01-29 18:04:48 +03:00
|
|
|
$order['delivery'] = array(
|
2016-04-08 14:59:15 +03:00
|
|
|
'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '',
|
2016-01-29 18:04:48 +03:00
|
|
|
'cost' => $deliveryCost,
|
|
|
|
'address' => array(
|
|
|
|
'index' => $order_data['shipping_postcode'],
|
|
|
|
'city' => $order_data['shipping_city'],
|
2016-04-07 15:31:04 +03:00
|
|
|
'countryIso' => $order_data['shipping_iso_code_2'],
|
|
|
|
'region' => $order_data['shipping_zone'],
|
2016-01-29 18:04:48 +03:00
|
|
|
'text' => implode(', ', array(
|
|
|
|
$order_data['shipping_postcode'],
|
2016-04-07 15:31:04 +03:00
|
|
|
(isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '',
|
2016-01-29 18:04:48 +03:00
|
|
|
$order_data['shipping_city'],
|
|
|
|
$order_data['shipping_address_1'],
|
|
|
|
$order_data['shipping_address_2']
|
|
|
|
))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
|
2016-07-25 17:21:15 +03:00
|
|
|
$offerOptions = array('select', 'radio');
|
2016-01-29 18:04:48 +03:00
|
|
|
|
|
|
|
foreach ($orderProducts as $product) {
|
2016-07-25 17:21:15 +03:00
|
|
|
$offerId = '';
|
|
|
|
|
|
|
|
if(!empty($product['option'])) {
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
$productOptions = $this->model_catalog_product->getProductOptions($product['product_id']);
|
|
|
|
|
|
|
|
foreach($product['option'] as $option) {
|
2017-06-02 23:36:34 +03:00
|
|
|
if ($option['type'] == 'checkbox') {
|
2017-05-15 16:58:45 +03:00
|
|
|
$properties[] = array(
|
|
|
|
'code' => $option['product_option_value_id'],
|
|
|
|
'name' => $option['name'],
|
|
|
|
'value' => $option['value']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:21:15 +03:00
|
|
|
if(!in_array($option['type'], $offerOptions)) continue;
|
|
|
|
foreach($productOptions as $productOption) {
|
|
|
|
if($productOption['product_option_id'] = $option['product_option_id']) {
|
|
|
|
foreach($productOption['product_option_value'] as $productOptionValue) {
|
|
|
|
if($productOptionValue['product_option_value_id'] == $option['product_option_value_id']) {
|
|
|
|
$options[$option['product_option_id']] = $productOptionValue['option_value_id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($options);
|
|
|
|
|
|
|
|
$offerId = array();
|
|
|
|
foreach($options as $optionKey => $optionValue) {
|
|
|
|
$offerId[] = $optionKey.'-'.$optionValue;
|
|
|
|
}
|
|
|
|
$offerId = implode('_', $offerId);
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:58:45 +03:00
|
|
|
$item = array(
|
2016-08-31 17:28:11 +03:00
|
|
|
'offer' => array(
|
|
|
|
'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
|
|
|
|
),
|
2016-01-29 18:04:48 +03:00
|
|
|
'productName' => $product['name'],
|
|
|
|
'initialPrice' => $product['price'],
|
|
|
|
'quantity' => $product['quantity'],
|
|
|
|
);
|
2017-05-15 16:58:45 +03:00
|
|
|
|
|
|
|
if (isset($properties)) $item['properties'] = $properties;
|
|
|
|
|
|
|
|
$order['items'][] = $item;
|
2016-01-29 18:04:48 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 14:15:24 +03:00
|
|
|
if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) {
|
2016-01-29 18:04:48 +03:00
|
|
|
$order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->retailcrm->ordersCreate($order);
|
|
|
|
}
|
|
|
|
}
|
2016-04-08 14:59:15 +03:00
|
|
|
|
|
|
|
public function changeInCrm($order_data, $order_id)
|
|
|
|
{
|
2016-04-14 13:55:11 +03:00
|
|
|
if(isset($this->request->post['fromApi'])) return;
|
|
|
|
|
2016-04-08 14:59:15 +03:00
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$settings = $this->model_setting_setting->getSetting('retailcrm');
|
2017-05-18 13:11:12 +03:00
|
|
|
$settingPaid = $this->model_setting_setting->getSetting($order_data['payment_code']);
|
2016-04-08 14:59:15 +03:00
|
|
|
|
|
|
|
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
|
2016-07-25 17:21:15 +03:00
|
|
|
$this->load->model('catalog/product');
|
|
|
|
|
2016-04-08 14:59:15 +03:00
|
|
|
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
|
|
|
|
|
|
|
|
$this->retailcrm = new RetailcrmProxy(
|
|
|
|
$settings['retailcrm_url'],
|
|
|
|
$settings['retailcrm_apikey'],
|
2017-03-22 12:12:54 +03:00
|
|
|
DIR_SYSTEM . 'storage/logs/retailcrm.log'
|
2016-04-08 14:59:15 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$order = array();
|
|
|
|
|
|
|
|
$payment_code = $order_data['payment_code'];
|
|
|
|
$order['externalId'] = $order_id;
|
|
|
|
$order['firstName'] = $order_data['firstname'];
|
|
|
|
$order['lastName'] = $order_data['lastname'];
|
|
|
|
$order['phone'] = $order_data['telephone'];
|
|
|
|
$order['customerComment'] = $order_data['comment'];
|
|
|
|
|
2016-10-14 18:13:16 +03:00
|
|
|
if(!empty($order_data['email'])) {
|
|
|
|
$order['email'] = $order_data['email'];
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:59:15 +03:00
|
|
|
$deliveryCost = 0;
|
|
|
|
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ;
|
|
|
|
|
|
|
|
foreach ($orderTotals as $totals) {
|
|
|
|
if ($totals['code'] == 'shipping') {
|
|
|
|
$deliveryCost = $totals['value'];
|
|
|
|
}
|
2017-06-02 23:36:34 +03:00
|
|
|
|
|
|
|
if ($totals['code'] == 'coupon') {
|
|
|
|
$couponTotal = abs($totals['value']);
|
|
|
|
}
|
2016-04-08 14:59:15 +03:00
|
|
|
}
|
|
|
|
|
2017-06-02 23:36:34 +03:00
|
|
|
$order['discount'] = $couponTotal;
|
2016-10-14 18:13:16 +03:00
|
|
|
$order['createdAt'] = $order_data['date_added'];
|
2016-04-08 14:59:15 +03:00
|
|
|
$order['paymentType'] = $settings['retailcrm_payment'][$payment_code];
|
|
|
|
|
|
|
|
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
|
|
|
|
|
2017-05-15 16:58:45 +03:00
|
|
|
if(isset($settings['retailcrm_delivery'][$order_data['shipping_code']])) {
|
|
|
|
$delivery_code = $order_data['shipping_code'];
|
|
|
|
} else {
|
|
|
|
$delivery_code = stristr($order_data['shipping_code'], '.', TRUE);
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:59:15 +03:00
|
|
|
$order['delivery'] = array(
|
|
|
|
'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '',
|
|
|
|
'address' => array(
|
|
|
|
'index' => $order_data['shipping_postcode'],
|
|
|
|
'city' => $order_data['shipping_city'],
|
|
|
|
'country' => $order_data['shipping_country_id'],
|
|
|
|
'region' => $order_data['shipping_zone_id'],
|
|
|
|
'text' => implode(', ', array(
|
|
|
|
$order_data['shipping_postcode'],
|
|
|
|
$country,
|
|
|
|
$order_data['shipping_city'],
|
|
|
|
$order_data['shipping_address_1'],
|
|
|
|
$order_data['shipping_address_2']
|
|
|
|
))
|
|
|
|
)
|
|
|
|
);
|
2017-04-07 12:35:53 +03:00
|
|
|
|
2017-03-22 12:12:54 +03:00
|
|
|
if(!empty($deliveryCost)){
|
|
|
|
$order['delivery']['cost'] = $deliveryCost;
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:59:15 +03:00
|
|
|
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
|
2016-07-25 17:21:15 +03:00
|
|
|
$offerOptions = array('select', 'radio');
|
2016-04-08 14:59:15 +03:00
|
|
|
|
|
|
|
foreach ($orderProducts as $product) {
|
2016-07-25 17:21:15 +03:00
|
|
|
$offerId = '';
|
|
|
|
|
2017-04-07 12:35:53 +03:00
|
|
|
if (!empty($product['option'])) {
|
2016-07-25 17:21:15 +03:00
|
|
|
$options = array();
|
|
|
|
|
|
|
|
$productOptions = $this->model_catalog_product->getProductOptions($product['product_id']);
|
|
|
|
|
|
|
|
foreach($product['option'] as $option) {
|
2017-06-02 23:36:34 +03:00
|
|
|
if ($option['type'] == 'checkbox') {
|
2017-05-15 16:58:45 +03:00
|
|
|
$properties[] = array(
|
|
|
|
'code' => $option['product_option_value_id'],
|
|
|
|
'name' => $option['name'],
|
|
|
|
'value' => $option['value']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:21:15 +03:00
|
|
|
if(!in_array($option['type'], $offerOptions)) continue;
|
|
|
|
foreach($productOptions as $productOption) {
|
|
|
|
if($productOption['product_option_id'] = $option['product_option_id']) {
|
|
|
|
foreach($productOption['product_option_value'] as $productOptionValue) {
|
|
|
|
if($productOptionValue['product_option_value_id'] == $option['product_option_value_id']) {
|
|
|
|
$options[$option['product_option_id']] = $productOptionValue['option_value_id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($options);
|
|
|
|
|
|
|
|
$offerId = array();
|
|
|
|
foreach($options as $optionKey => $optionValue) {
|
|
|
|
$offerId[] = $optionKey.'-'.$optionValue;
|
|
|
|
}
|
|
|
|
$offerId = implode('_', $offerId);
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:58:45 +03:00
|
|
|
$item = array(
|
2016-08-31 17:28:11 +03:00
|
|
|
'offer' => array(
|
|
|
|
'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
|
|
|
|
),
|
2016-04-08 14:59:15 +03:00
|
|
|
'productName' => $product['name'],
|
|
|
|
'initialPrice' => $product['price'],
|
|
|
|
'quantity' => $product['quantity'],
|
|
|
|
);
|
2017-05-15 16:58:45 +03:00
|
|
|
|
|
|
|
if (isset($properties)) $item['properties'] = $properties;
|
|
|
|
|
|
|
|
$order['items'][] = $item;
|
2016-04-08 14:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) {
|
|
|
|
$order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
|
|
|
|
}
|
|
|
|
|
2017-05-18 13:11:12 +03:00
|
|
|
if ($order_data['order_status_id'] == $settingPaid[$order_data['payment_code'] . '_order_status_id']) {
|
|
|
|
$order['paymentStatus'] = 'paid';
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:59:15 +03:00
|
|
|
$this->retailcrm->ordersEdit($order);
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 18:04:48 +03:00
|
|
|
}
|