2017-03-22 11:52:11 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ModelExtensionRetailcrmOrder extends Model {
|
2018-01-23 16:34:20 +03:00
|
|
|
protected $settings;
|
|
|
|
protected $moduleTitle;
|
|
|
|
protected $retailcrmApiClient;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
public function __construct($registry)
|
2017-03-22 11:52:11 +03:00
|
|
|
{
|
2018-01-23 16:34:20 +03:00
|
|
|
parent::__construct($registry);
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->load->library('retailcrm/retailcrm');
|
|
|
|
|
|
|
|
$this->moduleTitle = $this->retailcrm->getModuleTitle();
|
|
|
|
$this->settings = $this->model_setting_setting->getSetting($this->moduleTitle);
|
|
|
|
$this->retailcrmApiClient = $this->retailcrm->getApiClient();
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Create order in CRM
|
|
|
|
*
|
|
|
|
* @param array $order_data
|
|
|
|
* @param int $order_id
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function sendToCrm($order_data, $order_id)
|
|
|
|
{
|
|
|
|
if(isset($this->request->post['fromApi']) || $this->retailcrmApiClient === false) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-07-21 10:27:08 +03:00
|
|
|
$order = $this->processOrder($order_data, $order_id);
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$customers = $this->retailcrmApiClient->customersList(
|
2017-07-11 17:39:48 +03:00
|
|
|
array(
|
|
|
|
'name' => $order_data['telephone'],
|
|
|
|
'email' => $order_data['email']
|
|
|
|
),
|
|
|
|
1,
|
|
|
|
100
|
|
|
|
);
|
|
|
|
|
|
|
|
if($customers) {
|
|
|
|
foreach ($customers['customers'] as $customer) {
|
|
|
|
$order['customer']['id'] = $customer['id'];
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
unset($customers);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$response = $this->retailcrmApiClient->ordersCreate($order);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-21 10:27:08 +03:00
|
|
|
if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) {
|
2017-07-11 17:39:48 +03:00
|
|
|
$this->createPayment($order_data, $order_id);
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Edit order in CRM
|
|
|
|
*
|
|
|
|
* @param array $order_data
|
|
|
|
* @param int $order_id
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-11 17:39:48 +03:00
|
|
|
public function changeInCrm($order_data, $order_id)
|
|
|
|
{
|
2018-01-23 16:34:20 +03:00
|
|
|
if(isset($this->request->post['fromApi']) || $this->retailcrmApiClient === false) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$order = $this->processOrder($order_data, $order_id);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$customers = $this->retailcrmApiClient->customersList(
|
2017-07-21 10:27:08 +03:00
|
|
|
array(
|
|
|
|
'name' => $order_data['telephone'],
|
|
|
|
'email' => $order_data['email']
|
|
|
|
),
|
|
|
|
1,
|
|
|
|
100
|
|
|
|
);
|
|
|
|
|
|
|
|
if($customers) {
|
|
|
|
foreach ($customers['customers'] as $customer) {
|
|
|
|
$order['customer']['id'] = $customer['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($customers);
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$response = $this->retailcrmApiClient->ordersEdit($order);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-21 10:27:08 +03:00
|
|
|
if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) {
|
2018-01-23 16:34:20 +03:00
|
|
|
$response_order = $this->retailcrmApiClient->ordersGet($order_id);
|
2017-09-21 10:09:26 +03:00
|
|
|
if ($response_order->isSuccessful()) {
|
|
|
|
$order_info = $response_order['order'];
|
|
|
|
}
|
2017-07-27 14:41:58 +03:00
|
|
|
|
|
|
|
foreach ($order_info['payments'] as $payment_data) {
|
2017-09-21 10:09:26 +03:00
|
|
|
if (isset($payment_data['externalId']) && $payment_data['externalId'] == $order_id) {
|
2017-07-27 14:41:58 +03:00
|
|
|
$payment = $payment_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($payment) && $payment['type'] != $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']]) {
|
2018-01-23 16:34:20 +03:00
|
|
|
$response = $this->retailcrmApiClient->ordersPaymentDelete($payment['id']);
|
2017-07-27 14:41:58 +03:00
|
|
|
|
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
$this->createPayment($order_data, $order_id);
|
|
|
|
}
|
2017-10-30 16:49:49 +03:00
|
|
|
} elseif (isset($payment) && $payment['type'] == $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']]) {
|
|
|
|
$this->editPayment($order_data, $order_id);
|
2017-07-27 14:41:58 +03:00
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Process order
|
|
|
|
*
|
|
|
|
* @param array $order_data
|
|
|
|
* @param int $order_id
|
|
|
|
*
|
|
|
|
* @return array $order
|
|
|
|
*/
|
2017-07-11 17:39:48 +03:00
|
|
|
protected function processOrder($order_data, $order_id)
|
2018-01-23 16:34:20 +03:00
|
|
|
{
|
2017-07-11 17:39:48 +03:00
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->load->model('catalog/product');
|
2017-07-21 10:27:08 +03:00
|
|
|
$this->settings = $this->model_setting_setting->getSetting($this->moduleTitle);
|
2017-04-07 12:35:06 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if (!empty($order_data['payment_code']) && isset($this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']])) {
|
|
|
|
$payment_code = $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']];
|
|
|
|
} else {
|
|
|
|
$payment_code = '';
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:22:13 +03:00
|
|
|
if (!empty($order_data['shipping_code'])) {
|
|
|
|
$shippingCode = explode('.', $order_data['shipping_code']);
|
|
|
|
$shippingModule = $shippingCode[0];
|
|
|
|
|
|
|
|
if (isset($this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']])) {
|
|
|
|
$delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']];
|
|
|
|
} elseif (isset($this->settings[$this->moduleTitle . '_delivery'][$shippingModule])) {
|
|
|
|
$delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$shippingModule];
|
|
|
|
}
|
2018-01-23 16:34:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$order['number'] = $order_data['order_id'];
|
2017-07-11 17:39:48 +03:00
|
|
|
$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-01-29 18:04:48 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
if(!empty($order_data['email'])) {
|
|
|
|
$order['email'] = $order_data['email'];
|
|
|
|
}
|
2018-02-26 13:22:13 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$deliveryCost = 0;
|
|
|
|
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
foreach ($orderTotals as $totals) {
|
|
|
|
if ($totals['code'] == 'shipping') {
|
|
|
|
$deliveryCost = $totals['value'];
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
if ($totals['code'] == 'coupon') {
|
|
|
|
$couponTotal = abs($totals['value']);
|
2017-04-12 11:18:53 +03:00
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-04-12 11:18:53 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$order['createdAt'] = $order_data['date_added'];
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-21 10:27:08 +03:00
|
|
|
if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') {
|
2018-01-23 16:34:20 +03:00
|
|
|
$order['paymentType'] = $payment_code;
|
2018-02-26 13:22:13 +03:00
|
|
|
if (isset($couponTotal)) {
|
|
|
|
$order['discount'] = $couponTotal;
|
|
|
|
}
|
2017-07-27 14:41:58 +03:00
|
|
|
} else {
|
2018-02-26 13:22:13 +03:00
|
|
|
if (isset($couponTotal)) {
|
|
|
|
$order['discountManualAmount'] = $couponTotal;
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$order['delivery'] = array(
|
2018-01-23 16:34:20 +03:00
|
|
|
'code' => $delivery_code,
|
2017-07-11 17:39:48 +03:00
|
|
|
'address' => array(
|
|
|
|
'index' => $order_data['shipping_postcode'],
|
|
|
|
'city' => $order_data['shipping_city'],
|
|
|
|
'countryIso' => $order_data['shipping_iso_code_2'],
|
|
|
|
'region' => $order_data['shipping_zone'],
|
|
|
|
'text' => implode(', ', array(
|
|
|
|
$order_data['shipping_postcode'],
|
|
|
|
$country,
|
|
|
|
$order_data['shipping_city'],
|
|
|
|
$order_data['shipping_address_1'],
|
|
|
|
$order_data['shipping_address_2']
|
|
|
|
))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!empty($deliveryCost)){
|
|
|
|
$order['delivery']['cost'] = $deliveryCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
|
|
|
|
$offerOptions = array('select', 'radio');
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
foreach ($orderProducts as $product) {
|
|
|
|
$offerId = '';
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
if (!empty($product['option'])) {
|
|
|
|
$options = array();
|
2017-05-05 14:52:01 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$productOptions = $this->model_catalog_product->getProductOptions($product['product_id']);
|
|
|
|
|
|
|
|
foreach($product['option'] as $option) {
|
|
|
|
if ($option['type'] == 'checkbox') {
|
|
|
|
$properties[] = array(
|
|
|
|
'code' => $option['product_option_value_id'],
|
|
|
|
'name' => $option['name'],
|
|
|
|
'value' => $option['value']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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'];
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
ksort($options);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$offerId = array();
|
|
|
|
foreach($options as $optionKey => $optionValue) {
|
|
|
|
$offerId[] = $optionKey.'-'.$optionValue;
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
$offerId = implode('_', $offerId);
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-21 10:27:08 +03:00
|
|
|
if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v3') {
|
2017-05-05 14:52:01 +03:00
|
|
|
$item = array(
|
2017-03-22 11:52:11 +03:00
|
|
|
'offer' => array(
|
|
|
|
'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
|
|
|
|
),
|
|
|
|
'productName' => $product['name'],
|
|
|
|
'initialPrice' => $product['price'],
|
|
|
|
'quantity' => $product['quantity'],
|
|
|
|
);
|
2017-07-11 17:39:48 +03:00
|
|
|
} else {
|
|
|
|
$item = array(
|
|
|
|
'productName' => $product['name'],
|
|
|
|
'initialPrice' => $product['price'],
|
|
|
|
'quantity' => $product['quantity'],
|
|
|
|
'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
|
|
|
|
);
|
|
|
|
}
|
2017-05-05 14:52:01 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
if (isset($properties)) $item['properties'] = $properties;
|
2017-05-05 14:52:01 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$order['items'][] = $item;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) {
|
2017-07-21 10:27:08 +03:00
|
|
|
$order['status'] = $this->settings[$this->moduleTitle . '_status'][$order_data['order_status_id']];
|
2018-01-23 16:34:20 +03:00
|
|
|
} elseif (isset($order_data['order_status_id']) && $order_data['order_status_id'] == 0) {
|
|
|
|
$order['status'] = $this->settings[$this->moduleTitle . '_missing_status'];
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $order_data['custom_field']) {
|
|
|
|
$customFields = $order_data['custom_field'];
|
|
|
|
|
|
|
|
foreach ($customFields as $key => $value) {
|
|
|
|
if (isset($this->settings[$this->moduleTitle . '_custom_field']['o_' . $key])) {
|
|
|
|
$customFieldsToCrm[$this->settings[$this->moduleTitle . '_custom_field']['o_' . $key]] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($customFieldsToCrm)) {
|
|
|
|
$order['customFields'] = $customFieldsToCrm;
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
return $order;
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Create payment
|
|
|
|
*
|
|
|
|
* @param array $order
|
|
|
|
* @param int $order_id
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-11 17:39:48 +03:00
|
|
|
protected function createPayment($order, $order_id)
|
2017-10-30 16:49:49 +03:00
|
|
|
{
|
2017-07-11 17:39:48 +03:00
|
|
|
$payment_code = $order['payment_code'];
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
foreach ($order['totals'] as $total) {
|
2018-01-23 16:34:20 +03:00
|
|
|
if ($total['code'] == 'total') {
|
|
|
|
$amount = $total['value'];
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$payment = array(
|
|
|
|
'externalId' => $order_id,
|
2017-07-21 10:27:08 +03:00
|
|
|
'type' => $this->settings[$this->moduleTitle . '_payment'][$payment_code],
|
2017-07-11 17:39:48 +03:00
|
|
|
'amount' => $amount
|
|
|
|
);
|
2017-05-31 13:08:07 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$payment['order'] = array(
|
|
|
|
'externalId' => $order_id
|
|
|
|
);
|
2017-04-07 12:35:53 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->retailcrmApiClient->ordersPaymentCreate($payment);
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Edit payment
|
|
|
|
*
|
|
|
|
* @param array $order
|
|
|
|
* @param int $order_id
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-11 17:39:48 +03:00
|
|
|
protected function editPayment($order, $order_id)
|
2017-10-30 16:49:49 +03:00
|
|
|
{
|
2017-07-11 17:39:48 +03:00
|
|
|
$payment_code = $order['payment_code'];
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
foreach ($order['totals'] as $total) {
|
2017-10-30 16:49:49 +03:00
|
|
|
if ($total['code'] == 'total') {
|
|
|
|
$amount = $total['value'];
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$payment = array(
|
|
|
|
'externalId' => $order_id,
|
2017-07-21 10:27:08 +03:00
|
|
|
'type' => $this->settings[$this->moduleTitle . '_payment'][$payment_code],
|
2017-07-11 17:39:48 +03:00
|
|
|
'amount' => $amount
|
|
|
|
);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->retailcrmApiClient->ordersPaymentEdit($payment);
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|