diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 015338f..85e4bd4 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -37,6 +37,13 @@ class ControllerModuleRetailcrm extends Controller 'post.order.add', 'module/retailcrm/order_create' ); + + $this->model_extension_event + ->addEvent( + 'retailcrm', + 'post.order.history.add', + 'module/retailcrm/order_edit' + ); } } @@ -278,7 +285,6 @@ class ControllerModuleRetailcrm extends Controller */ public function order_create($order_id) { - $this->load->model('checkout/order'); $this->load->model('account/order'); diff --git a/catalog/controller/module/retailcrm.php b/catalog/controller/module/retailcrm.php index bdd9fea..089a74f 100644 --- a/catalog/controller/module/retailcrm.php +++ b/catalog/controller/module/retailcrm.php @@ -42,4 +42,32 @@ class ControllerModuleRetailcrm extends Controller $this->model_retailcrm_order->sendToCrm($data, $data['order_id']); } } + + public function order_edit($order_id) { + $this->load->model('checkout/order'); + $this->load->model('account/order'); + + $data = $this->model_checkout_order->getOrder($order_id); + + if($data['order_status_id'] == 0) return; + + $data['products'] = $this->model_account_order->getOrderProducts($order_id); + + if (!isset($data['fromApi'])) { + $this->load->model('setting/setting'); + $status = $this->model_setting_setting->getSetting('retailcrm'); + + if ($data['order_status_id'] > 0) { + $data['order_status'] = $status['retailcrm_status'][$data['order_status_id']]; + } + + $data['totals'][] = array( + 'code' => 'shipping', + 'value' => isset($this->session->data['shipping_method']) ? $this->session->data['shipping_method']['cost'] : '' + ); + + $this->load->model('retailcrm/order'); + $this->model_retailcrm_order->changeInCrm($data, $data['order_id']); + } + } } diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index 8cd772d..ac01816 100644 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -57,9 +57,17 @@ class ModelRetailcrmOrder extends Model { $payment_code = $order_data['payment_code']; $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; + // Совместимость с 1.5.5, когда этот метод вызывается из model/checkout/order->createOrder(), а не из controller/module/retailcrm->order_create() + 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']; + } + + $delivery_code = $order_data['shipping_code']; $order['delivery'] = array( - 'code' => $settings['retailcrm_delivery'][$delivery_code], + 'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '', 'cost' => $deliveryCost, 'address' => array( 'index' => $order_data['shipping_postcode'], @@ -94,5 +102,82 @@ class ModelRetailcrmOrder extends Model { $this->retailcrm->ordersCreate($order); } } + + public function changeInCrm($order_data, $order_id) + { + $this->load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { + require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; + + $this->retailcrm = new RetailcrmProxy( + $settings['retailcrm_url'], + $settings['retailcrm_apikey'], + DIR_SYSTEM . 'logs/retailcrm.log' + ); + + $order = array(); + + $payment_code = $order_data['payment_code']; + $delivery_code = $order_data['shipping_code']; + + $order['externalId'] = $order_id; + $order['firstName'] = $order_data['firstname']; + $order['lastName'] = $order_data['lastname']; + $order['email'] = $order_data['email']; + $order['phone'] = $order_data['telephone']; + $order['customerComment'] = $order_data['comment']; + + $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']; + } + } + + $order['createdAt'] = date('Y-m-d H:i:s'); + $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; + + $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; + + $order['delivery'] = array( + 'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '', + 'cost' => $deliveryCost, + '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'] + )) + ) + ); + + $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + + foreach ($orderProducts as $product) { + $order['items'][] = array( + 'productId' => $product['product_id'], + 'productName' => $product['name'], + 'initialPrice' => $product['price'], + 'quantity' => $product['quantity'], + ); + } + + if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { + $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; + } + + $this->retailcrm->ordersEdit($order); + } + } }