diff --git a/README.md b/README.md index 78b7f5d..005de54 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Opencart module =============== -Module allows integrate CMS Opencart with [retailCRM](http://retailcrm.pro) +Module allows integrate CMS Opencart 1.5.x.x with [retailCRM](http://retailcrm.pro) #### Features: @@ -24,7 +24,7 @@ cp -r opencart-module/* /path/to/site/root * Specify directories matching -#### Export orders to retailCRM (for opencart 1.5.x, for version 2.x this step is unnecessary) +#### Export orders to retailCRM ##### VQmod diff --git a/README.ru.md b/README.ru.md index 44a8723..e7ee824 100644 --- a/README.ru.md +++ b/README.ru.md @@ -1,7 +1,7 @@ Opencart module =============== -Модуль интеграции CMS Openacart c [RetailCRM](http://retailcrm.ru) +Модуль интеграции CMS Openacart 1.5.x.x c [RetailCRM](http://retailcrm.ru) #### Модуль позволяет: @@ -27,7 +27,7 @@ cp -r opencart-module/* /path/to/site/root На странице настроек модуля укажите URL Вашей CRM и ключ авторизации, после сохранения этих данных укажите соответствия справочников типов доставок, оплат и статусов заказа. -#### Выгрузка новых заказов в CRM (для версии opencart 1.5.x.x, для версии 2.0 и старше не требуется) +#### Выгрузка новых заказов в CRM ##### VQmod diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 3df6c4b..131d154 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -27,22 +27,6 @@ class ControllerModuleRetailcrm extends Controller $this->load->model('setting/setting'); $this->model_setting_setting ->editSetting('retailcrm', array('retailcrm_status' => 1)); - - $this->load->model('extension/event'); - - $this->model_extension_event - ->addEvent( - 'retailcrm', - version_compare(VERSION, '2.2', '>=') ? 'catalog/model/checkout/order/addOrder/after' : 'post.order.add', - 'module/retailcrm/order_create' - ); - - $this->model_extension_event - ->addEvent( - 'retailcrm', - version_compare(VERSION, '2.2', '>=') ? 'catalog/model/checkout/order/addOrderHistory/after' : 'post.order.history.add', - 'module/retailcrm/order_edit' - ); } /** @@ -55,9 +39,6 @@ class ControllerModuleRetailcrm extends Controller $this->load->model('setting/setting'); $this->model_setting_setting ->editSetting('retailcrm', array('retailcrm_status' => 0)); - - $this->load->model('extension/event'); - $this->model_extension_event->deleteEvent('retailcrm'); } /** @@ -69,7 +50,6 @@ class ControllerModuleRetailcrm extends Controller { $this->load->model('setting/setting'); - $this->load->model('extension/module'); $this->load->model('retailcrm/references'); $this->load->language('module/retailcrm'); $this->document->setTitle($this->language->get('heading_title')); @@ -87,7 +67,7 @@ class ControllerModuleRetailcrm extends Controller 'SSL' ); - $this->response->redirect($redirect); + $this->redirect($redirect); } $text_strings = array( @@ -107,8 +87,9 @@ class ControllerModuleRetailcrm extends Controller 'retailcrm_dict_payment', ); - $this->load->model('extension/extension'); - $_data = &$data; + + $_data = &$this->data; + $this->load->model('setting/extension'); foreach ($text_strings as $text) { $_data[$text] = $this->language->get($text); @@ -210,13 +191,13 @@ class ControllerModuleRetailcrm extends Controller $this->load->model('design/layout'); $_data['layouts'] = $this->model_design_layout->getLayouts(); - $_data['header'] = $this->load->controller('common/header'); - $_data['column_left'] = $this->load->controller('common/column_left'); - $_data['footer'] = $this->load->controller('common/footer'); - - $this->response->setOutput( - $this->load->view('module/retailcrm.tpl', $_data) + $this->template = 'module/retailcrm.1.x.tpl'; + $this->children = array( + 'common/header', + 'common/footer', ); + + $this->response->setOutput($this->render()); } /** diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 0907b06..9797cc5 100644 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -4,19 +4,12 @@ class ModelRetailcrmHistory extends Model { protected $createResult; - private $opencartApiClient; - public function request() { $this->load->model('setting/setting'); $this->load->model('setting/store'); - $this->load->model('user/api'); $this->load->model('sale/order'); - if (version_compare(VERSION, '2.1.0.0', '>=')) { - $this->load->model('customer/customer'); - } else { - $this->load->model('sale/customer'); - } + $this->load->model('sale/customer'); $this->load->model('retailcrm/references'); $this->load->model('catalog/product'); $this->load->model('catalog/option'); @@ -36,8 +29,6 @@ class ModelRetailcrmHistory extends Model return false; } - $this->opencartApiClient = new OpencartApiClient($this->registry); - $crm = new RetailcrmProxy( $settings['retailcrm_url'], $settings['retailcrm_apikey'], @@ -190,7 +181,8 @@ class ModelRetailcrmHistory extends Model $data['order_product'] = array(); foreach ($order['items'] as $item) { - //$product = $this->model_catalog_product->getProduct($item['offer']['externalId']); + $product = $this->model_catalog_product->getProduct($item['offer']['externalId']); + $productId = $item['offer']['externalId']; $options = array(); if(mb_strpos($item['offer']['externalId'], '#') > 1) { @@ -201,25 +193,44 @@ class ModelRetailcrmHistory extends Model foreach($optionsFromCRM as $optionFromCRM) { $optionData = explode('-', $optionFromCRM); $productOptionId = $optionData[0]; - $optionValueId = $optionData[1]; + $productOptionValueId = $optionData[1]; $productOptions = $this->model_catalog_product->getProductOptions($productId); foreach($productOptions as $productOption) { - if($productOptionId == $productOption['product_option_id']) { + if($productOption['product_option_id'] == $productOptionId) { foreach($productOption['product_option_value'] as $productOptionValue) { - if($productOptionValue['option_value_id'] == $optionValueId) { - $options[$productOptionId] = $productOptionValue['product_option_value_id']; + if($productOptionValue['option_value_id'] == $productOptionValueId) { + $productOptionValue = $this->model_catalog_option->getOptionValue($productOptionValueId); + + $options[] = array( + 'order_option_id' => 'default', // default или NULL для автоинкремента http://stackoverflow.com/questions/8753371/how-to-insert-data-to-mysql-having-auto-incremented-primary-key + 'product_option_id' => $productOptionId, + 'product_option_value_id' => $productOptionValueId, + 'name' => $productOption['name'], + 'value' => $productOptionValue['name'], + 'type' => $productOption['type'] + ); } } } } } } + $data['order_product'][] = array( 'product_id' => $productId, + 'name' => $item['offer']['name'], 'quantity' => $item['quantity'], - 'option' => $options + 'price' => $item['initialPrice'], + 'total' => $item['initialPrice'] * $item['quantity'], + 'model' => $product['model'], + 'order_option' => $options, + + // this data will not retrive from crm + 'order_product_id' => '', + 'tax' => 0, + 'reward' => 0 ); } @@ -261,7 +272,7 @@ class ModelRetailcrmHistory extends Model $data['order_status_id'] = $tmpOrder['order_status_id']; } - $this->opencartApiClient->editOrder($order['externalId'], $data); + $this->model_sale_order->editOrder($order['externalId'], $data); } } @@ -308,25 +319,13 @@ class ModelRetailcrmHistory extends Model ), ); - if (version_compare(VERSION, '2.1.0.0', '>=')) { - $this->model_customer_customer->addCustomer($cData); - } else { - $this->model_sale_customer->addCustomer($cData); - } + $this->model_sale_customer->addCustomer($cData); if (!empty($order['email'])) { - if (version_compare(VERSION, '2.1.0.0', '>=')) { - $tryToFind = $this->model_customer_customer->getCustomerByEmail($order['email']); - } else { - $tryToFind = $this->model_sale_customer->getCustomerByEmail($order['email']); - } + $tryToFind = $this->model_sale_customer->getCustomerByEmail($order['email']); $customer_id = $tryToFind['customer_id']; } else { - if (version_compare(VERSION, '2.1.0.0', '>=')) { - $last = $this->model_customer_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1)); - } else { - $last = $this->model_sale_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1)); - } + $last = $this->model_sale_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1)); $customer_id = $last[0]['customer_id']; } @@ -452,7 +451,7 @@ class ModelRetailcrmHistory extends Model $data['fromApi'] = true; $data['order_status_id'] = 1; - $this->opencartApiClient->addOrder($data); + $this->model_sale_order->addOrder($data); $last = $this->model_sale_order->getOrders($data = array('order' => 'DESC', 'limit' => 1, 'start' => 0)); diff --git a/admin/model/retailcrm/icml.php b/admin/model/retailcrm/icml.php index 2d6b5c5..ca7883e 100644 --- a/admin/model/retailcrm/icml.php +++ b/admin/model/retailcrm/icml.php @@ -18,8 +18,8 @@ class ModelRetailcrmIcml extends Model $this->load->language('module/retailcrm'); $this->load->model('catalog/category'); $this->load->model('catalog/product'); - $this->load->model('catalog/option'); $this->load->model('catalog/manufacturer'); + $this->load->model('catalog/option'); $string = ' @@ -102,9 +102,12 @@ class ModelRetailcrmIcml extends Model foreach ($products as $product) { // Формируем офферы отнсительно доступных опций $options = $this->model_catalog_product->getProductOptions($product['product_id']); + $offerOptions = array('select', 'radio'); + $requiredOptions = array(); $notRequiredOptions = array(); + // Оставляем опции связанные с вариациями товаров, сортируем по параметру обязательный или нет foreach($options as $option) { if(in_array($option['type'], $offerOptions)) { @@ -115,7 +118,9 @@ class ModelRetailcrmIcml extends Model } } } + $offers = array(); + // Сначала совмещаем все обязательные опции foreach($requiredOptions as $requiredOption) { // Если первая итерация @@ -132,6 +137,7 @@ class ModelRetailcrmIcml extends Model } } } + // Совмещаем или добавляем необязательные опции, учитывая тот факт что обязательных опций может и не быть. foreach($notRequiredOptions as $notRequiredOption) { // Если обязательных опцией не оказалось и первая итерация @@ -148,6 +154,7 @@ class ModelRetailcrmIcml extends Model } } } + if(empty($offers)) { $offers = array('0:0-0' => '0'); } @@ -158,21 +165,26 @@ class ModelRetailcrmIcml extends Model foreach($optionsString as $optionString) { $option = explode('-', $optionString); $optionIds = explode(':', $option[0]); + if($optionString != '0:0-0') { $optionData = $this->getOptionData($optionIds[1], $option[1]); + $options[$optionIds[0]] = array( 'name' => $optionData['optionName'], 'value' => $optionData['optionValue'], 'value_id' => $option[1] ); } + } ksort($options); + $offerId = array(); foreach($options as $optionKey => $optionData) { $offerId[] = $optionKey.'-'.$optionData['value_id']; } $offerId = implode('_', $offerId); + $e = $this->eOffers->appendChild($this->dd->createElement('offer')); if(!empty($offerId)) $e->setAttribute('id', $product['product_id'].'#'.$offerId); @@ -180,6 +192,7 @@ class ModelRetailcrmIcml extends Model $e->setAttribute('id', $product['product_id']); $e->setAttribute('productId', $product['product_id']); $e->setAttribute('quantity', $product['quantity']); + /** * Offer activity */ @@ -189,11 +202,13 @@ class ModelRetailcrmIcml extends Model )->appendChild( $this->dd->createTextNode($activity) ); + /** * Offer categories */ $categories = $this->model_catalog_product ->getProductCategories($product['product_id']); + if (!empty($categories)) { foreach ($categories as $category) { $e->appendChild($this->dd->createElement('categoryId')) @@ -202,6 +217,7 @@ class ModelRetailcrmIcml extends Model ); } } + /** * Name & price */ @@ -213,14 +229,17 @@ class ModelRetailcrmIcml extends Model $optionsString[] = $option['name'].': '.$option['value']; } $optionsString = ' ('.implode(', ', $optionsString).')'; + $e->appendChild($this->dd->createElement('name')) ->appendChild($this->dd->createTextNode($product['name'].$optionsString)); } else { $e->appendChild($this->dd->createElement('name')) ->appendChild($this->dd->createTextNode($product['name'])); } + $e->appendChild($this->dd->createElement('price')) ->appendChild($this->dd->createTextNode($product['price'] + $optionsTotalCost)); + /** * Vendor */ @@ -232,6 +251,7 @@ class ModelRetailcrmIcml extends Model ) ); } + /** * Image */ @@ -240,6 +260,7 @@ class ModelRetailcrmIcml extends Model $e->appendChild($this->dd->createElement('picture')) ->appendChild($this->dd->createTextNode($image)); } + /** * Url */ @@ -249,6 +270,7 @@ class ModelRetailcrmIcml extends Model ? HTTP_CATALOG : HTTPS_CATALOG ); + $e->appendChild($this->dd->createElement('url')) ->appendChild( $this->dd->createTextNode( @@ -257,6 +279,7 @@ class ModelRetailcrmIcml extends Model ) ) ); + // Options if(!empty($options)) { foreach($options as $optionKey => $optionData) { @@ -267,6 +290,8 @@ class ModelRetailcrmIcml extends Model $e->appendChild($param); } } + + if ($product['sku']) { $sku = $this->dd->createElement('param'); $sku->setAttribute('code', 'article'); @@ -274,6 +299,7 @@ class ModelRetailcrmIcml extends Model $sku->appendChild($this->dd->createTextNode($product['sku'])); $e->appendChild($sku); } + if ($product['weight'] != '') { $weight = $this->dd->createElement('param'); $weight->setAttribute('code', 'weight'); @@ -297,18 +323,6 @@ class ModelRetailcrmIcml extends Model { $this->load->model('tool/image'); - if (version_compare(VERSION, '2.2', '>=')) { - $currentTheme = $this->config->get('config_theme'); - $width = $this->config->get($currentTheme . '_image_related_width') ? $this->config->get($currentTheme . '_image_related_width') : 200; - $height = $this->config->get($currentTheme . '_image_related_height') ? $this->config->get($currentTheme . '_image_related_height') : 200; - - return $this->model_tool_image->resize( - $image, - $width, - $height - ); - } - return $this->model_tool_image->resize( $image, $this->config->get('config_image_product_width'), @@ -323,12 +337,14 @@ class ModelRetailcrmIcml extends Model $option = $this->model_catalog_option->getOption($optionId); $this->options[$optionId] = $option; } + if(!empty($this->optionValues[$optionValueId])) { $optionValue = $this->optionValues[$optionValueId]; } else { $optionValue = $this->model_catalog_option->getOptionValue($optionValueId); $this->optionValues[$optionValueId] = $optionValue; } + return array( 'optionName' => $option['name'], 'optionValue' => $optionValue['name'] diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index 2dc90b9..628584c 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -77,10 +77,30 @@ class ModelRetailcrmOrder extends Model { ); $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + $offerOptions = array('select', 'radio'); foreach ($orderProducts as $product) { + $offerId = ''; + if(!empty($product['option'])) { + $options = array(); + + foreach($product['option'] as $option) { + if(!in_array($option['type'], $offerOptions)) continue; + $options[$option['product_option_id']] = $option['option_value_id']; + } + + ksort($options); + + $offerId = array(); + foreach($options as $optionKey => $optionValue) { + $offerId[] = $optionKey.'-'.$optionValue; + } + $offerId = implode('_', $offerId); + } + + $order['items'][] = array( - 'productId' => $product['product_id'], + 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], 'initialPrice' => $product['price'], 'quantity' => $product['quantity'], @@ -95,6 +115,10 @@ class ModelRetailcrmOrder extends Model { } } + /** + * @param $order_data + * @param $order_id + */ public function changeInCrm($order_data, $order_id) { $this->load->model('setting/setting'); @@ -136,9 +160,7 @@ class ModelRetailcrmOrder extends Model { $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; $order['delivery'] = array( - 'code' => !empty($settings['retailcrm_delivery'][$delivery_code]) - ? $settings['retailcrm_delivery'][$delivery_code] - : null, + 'code' => $settings['retailcrm_delivery'][$delivery_code], 'cost' => $deliveryCost, 'address' => array( 'index' => $order_data['shipping_postcode'], @@ -156,16 +178,46 @@ class ModelRetailcrmOrder extends Model { ); $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + $offerOptions = array('select', 'radio'); foreach ($orderProducts as $product) { + if(!empty($product['order_option'])) { + $options = array(); + + $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); + foreach($productOptions as $key=>$productOption) { + $productOptionValues[$productOption['product_option_id']] = array(); + + foreach($productOption['product_option_value'] as $productOptionValue) { + $productOptionValues[$productOption['product_option_id']][$productOptionValue['product_option_value_id']] = $productOptionValue['option_value_id']; + } + } + + foreach($product['order_option'] as $option) { + if(!in_array($option['type'], $offerOptions)) continue; + $options[$option['product_option_id']] = $productOptionValues[$option['product_option_id']][$option['product_option_value_id']]; + } + + ksort($options); + + $offerId = array(); + foreach($options as $optionKey => $optionValue) { + $offerId[] = $optionKey.'-'.$optionValue; + } + $offerId = implode('_', $offerId); + } + $order['items'][] = array( - 'productId' => $product['product_id'], + 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], 'initialPrice' => $product['price'], 'quantity' => $product['quantity'], ); } + // Сделаем порядок товраных позиций в срм как и в магазине + $order['items'] = array_reverse($order['items']); + if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; } diff --git a/admin/view/template/module/retailcrm.1.x.tpl b/admin/view/template/module/retailcrm.1.x.tpl new file mode 100644 index 0000000..53e4841 --- /dev/null +++ b/admin/view/template/module/retailcrm.1.x.tpl @@ -0,0 +1,99 @@ + + +
+ + +
+ + + + + +
+
+

+
+
+
+
+ + +

+
+
+ +
+
+
+ +
+ + + + + +
+ + +

+ +

+ $value): ?> +
+ + +
+ + +

+ + +
+ + +
+ + +

+ $value): ?> +
+ + +
+ + + + + +
+
+
+
+ + + diff --git a/admin/view/template/module/retailcrm.tpl b/admin/view/template/module/retailcrm.tpl deleted file mode 100644 index 144f68b..0000000 --- a/admin/view/template/module/retailcrm.tpl +++ /dev/null @@ -1,110 +0,0 @@ - - - -
- -
- -
- - -
- - - - - - -
-
-
- - -

-
-
- -
-
-
- -
- - - - - -
- - -

- -

- $value): ?> -
- - -
- - -

- - -
- - -
- - -

- $value): ?> -
- - -
- - - - - -
-
-
-
-
- - diff --git a/catalog/controller/module/retailcrm.php b/catalog/controller/module/retailcrm.php index ba9cd45..089a74f 100644 --- a/catalog/controller/module/retailcrm.php +++ b/catalog/controller/module/retailcrm.php @@ -18,25 +18,13 @@ class ControllerModuleRetailcrm extends Controller * * @return void */ - public function order_create($parameter1, $parameter2 = null) + public function order_create($order_id) { $this->load->model('checkout/order'); $this->load->model('account/order'); - if($parameter2 != null) - $order_id = $parameter2; - else - $order_id = $parameter1; - $data = $this->model_checkout_order->getOrder($order_id); - $data['products'] = $this->model_account_order->getOrderProducts($order_id); - foreach($data['products'] as $key => $product) { - $productOptions = $this->model_account_order->getOrderOptions($order_id, $product['order_product_id']); - - if(!empty($productOptions)) - $data['products'][$key]['option'] = $productOptions; - } if (!isset($data['fromApi'])) { $this->load->model('setting/setting'); @@ -55,12 +43,7 @@ class ControllerModuleRetailcrm extends Controller } } - public function order_edit($parameter1, $parameter2 = null, $parameter3 = null, $parameter4 = null) { - if($parameter4 != null) - $order_id = $parameter3; - else - $order_id = $parameter1; - + public function order_edit($order_id) { $this->load->model('checkout/order'); $this->load->model('account/order'); @@ -70,13 +53,6 @@ class ControllerModuleRetailcrm extends Controller $data['products'] = $this->model_account_order->getOrderProducts($order_id); - foreach($data['products'] as $key => $product) { - $productOptions = $this->model_account_order->getOrderOptions($order_id, $product['order_product_id']); - - if(!empty($productOptions)) - $data['products'][$key]['option'] = $productOptions; - } - if (!isset($data['fromApi'])) { $this->load->model('setting/setting'); $status = $this->model_setting_setting->getSetting('retailcrm'); diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index 0f6309e..d37acae 100644 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -10,8 +10,6 @@ class ModelRetailcrmOrder extends Model { $settings = $this->model_setting_setting->getSetting('retailcrm'); if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { - $this->load->model('catalog/product'); - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; $this->retailcrm = new RetailcrmProxy( @@ -93,23 +91,12 @@ class ModelRetailcrmOrder extends Model { foreach ($orderProducts as $product) { $offerId = ''; - if(!empty($product['option'])) { $options = array(); - $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); - foreach($product['option'] as $option) { 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']; - } - } - } - } + $options[$option['product_option_id']] = $option['option_value_id']; } ksort($options); @@ -121,6 +108,7 @@ class ModelRetailcrmOrder extends Model { $offerId = implode('_', $offerId); } + $order['items'][] = array( 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], @@ -145,8 +133,6 @@ class ModelRetailcrmOrder extends Model { $settings = $this->model_setting_setting->getSetting('retailcrm'); if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { - $this->load->model('catalog/product'); - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; $this->retailcrm = new RetailcrmProxy( @@ -204,23 +190,12 @@ class ModelRetailcrmOrder extends Model { foreach ($orderProducts as $product) { $offerId = ''; - if(!empty($product['option'])) { $options = array(); - $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); - foreach($product['option'] as $option) { 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']; - } - } - } - } + $options[$option['product_option_id']] = $option['option_value_id']; } ksort($options); @@ -232,6 +207,7 @@ class ModelRetailcrmOrder extends Model { $offerId = implode('_', $offerId); } + $order['items'][] = array( 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], diff --git a/system/cron/dispatch.php b/system/cron/dispatch.php index dfd546b..f8bfae0 100644 --- a/system/cron/dispatch.php +++ b/system/cron/dispatch.php @@ -34,17 +34,10 @@ if (!defined('DIR_APPLICATION')) { require_once(DIR_SYSTEM . 'startup.php'); // Application Classes -if (version_compare(VERSION, '2.2', '>=')) { - require_once(DIR_SYSTEM . 'library/cart/currency.php'); - require_once(DIR_SYSTEM . 'library/cart/user.php'); - require_once(DIR_SYSTEM . 'library/cart/weight.php'); - require_once(DIR_SYSTEM . 'library/cart/length.php'); -} else { - require_once(DIR_SYSTEM . 'library/currency.php'); - require_once(DIR_SYSTEM . 'library/user.php'); - require_once(DIR_SYSTEM . 'library/weight.php'); - require_once(DIR_SYSTEM . 'library/length.php'); -} +require_once(DIR_SYSTEM . 'library/currency.php'); +require_once(DIR_SYSTEM . 'library/user.php'); +require_once(DIR_SYSTEM . 'library/weight.php'); +require_once(DIR_SYSTEM . 'library/length.php'); // Registry $registry = new Registry(); @@ -68,10 +61,7 @@ foreach ($query->rows as $setting) { if (!$setting['serialized']) { $config->set($setting['key'], $setting['value']); } else { - if (version_compare(VERSION, '2.1', '>=')) - $config->set($setting['key'], json_decode($setting['value']), true); - else - $config->set($setting['key'], unserialize($setting['value'])); + $config->set($setting['key'], unserialize($setting['value'])); } } @@ -83,13 +73,6 @@ $registry->set('url', $url); $log = new Log($config->get('config_error_filename')); $registry->set('log', $log); - -// Event -if (version_compare(VERSION, '2.2', '>=')) { - $event = new Event($registry); - $registry->set('event', $event); -} - function error_handler($errno, $errstr, $errfile, $errline) { global $log, $config; @@ -153,17 +136,10 @@ $registry->set('language', $language); $document = new Document(); $registry->set('document', $document); -if (version_compare(VERSION, '2.2', '>=')) { - $registry->set('currency', new Cart\Currency($registry)); - $registry->set('weight', new Cart\Weight($registry)); - $registry->set('length', new Cart\Length($registry)); - $registry->set('user', new Cart\User($registry)); -} else { - $registry->set('currency', new Currency($registry)); - $registry->set('weight', new Weight($registry)); - $registry->set('length', new Length($registry)); - $registry->set('user', new User($registry)); -} +$registry->set('currency', new Currency($registry)); +$registry->set('weight', new Weight($registry)); +$registry->set('length', new Length($registry)); +$registry->set('user', new User($registry)); $controller = new Front($registry); $action = new Action($cli_action); diff --git a/system/cron/getmyip.php b/system/cron/getmyip.php deleted file mode 100644 index 439471b..0000000 --- a/system/cron/getmyip.php +++ /dev/null @@ -1,2 +0,0 @@ -model_module_name */ - public function __get($name) { - return $this->registry->get($name); - } - - public function __construct(Registry &$registry) { - $this->registry = $registry; - - $settings = $this->model_setting_setting->getSetting('retailcrm'); - $this->cookieFileName = $settings['retailcrm_apikey']; - - $this->auth(); - } - - private function getCookieValue($cookieName) { - $cookieFile = file_get_contents(DIR_APPLICATION . '/' . $this->cookieFileName . '.txt'); - $cookieFile = explode("\n", $cookieFile); - - $cookies = array(); - foreach($cookieFile as $line) { - if(empty($line) OR $line{0} == '#') - continue; - - $params = explode("\t", $line); - $cookies[$params[5]] = $params[6]; - } - - if(isset($cookies[$cookieName])) - return $cookies[$cookieName]; - - return false; - } - - public function request($method, $getParams, $postParams) { - $opencartStoreInfo = $this->model_setting_store->getStore($this->opencartStoreId); - - if(version_compare(VERSION, '2.1.0', '>=') && !empty($this->apiToken)) { - $getParams['token'] = $this->apiToken; - } - - $postParams['fromApi'] = true; - - if ($opencartStoreInfo) { - $url = $opencartStoreInfo['ssl']; - } else { - $url = HTTPS_CATALOG; - } - - $curl = curl_init(); - - // Set SSL if required - if (substr($url, 0, 5) == 'https') { - curl_setopt($curl, CURLOPT_PORT, 443); - } - - curl_setopt($curl, CURLOPT_HEADER, false); - curl_setopt($curl, CURLINFO_HEADER_OUT, true); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curl, CURLOPT_FORBID_REUSE, false); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_URL, $url . 'index.php?route=api/' . $method . (!empty($getParams) ? '&' . http_build_query($getParams) : '')); - - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postParams)); - - curl_setopt($curl, CURLOPT_COOKIEFILE, DIR_APPLICATION . '/' . $this->cookieFileName . '.txt'); - curl_setopt($curl, CURLOPT_COOKIEJAR, DIR_APPLICATION . '/' . $this->cookieFileName . '.txt'); - - $json = json_decode(curl_exec($curl), true); - - curl_close($curl); - - return $json; - } - - private function auth() { - $apiUsers = $this->model_user_api->getApis(); - - $api = array(); - foreach ($apiUsers as $apiUser) { - if($apiUser['status'] == 1) { - if(version_compare(VERSION, '2.1.0', '>=')) { - $api = array( - 'api_id' => $apiUser['api_id'], - 'key' => $apiUser['key'] - ); - } else { - $api = array( - 'api_id' => $apiUser['api_id'], - 'username' => $apiUser['username'], - 'password' => $apiUser['password'] - ); - } - - break; - } - } - - if(!isset($api['api_id'])) - return false; - - if(version_compare(VERSION, '2.1.0', '>=')) { - $alreadyBinded = false; - - $innerIp = $this->getInnerIpAddr(); - $apiIps = $this->model_user_api->getApiIps($api['api_id']); - foreach($apiIps as $apiIp) { - if($apiIp['ip'] == $innerIp) - $alreadyBinded = true; - } - - if(!$alreadyBinded) { - $this->model_user_api->addApiIp($api['api_id'], $innerIp); - } - } - - $apiAnswer = $this->request('login', array(), $apiUser); - - if(version_compare(VERSION, '2.1.0', '>=')) { - $this->apiToken = $apiAnswer['token']; - } - - return $apiAnswer; - } - - public function editOrder($order_id, $data) { - $data['telephone'] = trim($data['telephone']); - $customer = array( - 'currency' => isset($data['currency']) ? $data['currency'] : '', - 'customer' => $data['customer'], - 'customer_id' => $data['customer_id'], - 'customer_group_id' => $data['customer_group_id'], - 'firstname' => $data['firstname'], - 'lastname' => $data['lastname'], - 'email' => $data['email'], - 'telephone' => !empty($data['telephone']) ? $data['telephone'] : '0000', - 'fax' => $data['fax'], - ); - $this->request('customer', array(), $customer); - - $products = array(); - foreach ($data['order_product'] as $order_product) { - $products[] = array( - 'product_id' => $order_product['product_id'], - 'quantity' => $order_product['quantity'], - 'option' => $order_product['option'] - ); - } - $this->request('cart/add', array(), array('product' => $products)); - - $payment_address = array( - 'payment_address' => $data['payment_address'], - 'firstname' => $data['payment_firstname'], - 'lastname' => $data['payment_lastname'], - 'company' => $data['payment_company'], - 'address_1'=> $data['payment_address_1'], - 'address_2' => $data['payment_address_2'], - 'city' => !empty($data['payment_city']) ? $data['payment_city'] : 'none', - 'postcode' => $data['payment_postcode'], - 'country_id' => $data['payment_country_id'], - 'zone_id' => !empty($data['payment_zone_id']) ? $data['payment_zone_id'] : 0, - ); - $this->request('payment/address', array(), $payment_address); - - $this->request('payment/methods', array(), array()); - $payment_method = array( - 'payment_method' => $data['payment_code'] - ); - $this->request('payment/method', array(), $payment_method); - - $shipping_address = array( - 'shipping_address' => $data['shipping_address'], - 'firstname' => $data['shipping_firstname'], - 'lastname' => $data['shipping_lastname'], - 'company' => $data['shipping_company'], - 'address_1' => $data['shipping_address_1'], - 'address_2' => $data['shipping_address_2'], - 'city' => !empty($data['shipping_city']) ? $data['shipping_city'] : 'none', - 'postcode' => $data['shipping_postcode'], - 'country_id' => $data['shipping_country_id'], - 'zone_id' => !empty($data['shipping_zone_id']) ? $data['shipping_zone_id'] : 0, - ); - $this->request('shipping/address', array(), $shipping_address); - - $this->request('shipping/methods', array(), array()); - $shipping_method = array( - 'shipping_method' => $data['shipping_code'] - ); - $this->request('shipping/method', array(), $shipping_method); - - $order = array( - 'shipping_method' => $data['shipping_code'], - 'payment_method' => $data['payment_code'], - 'order_status_id' => $data['order_status_id'], - 'comment' => $data['comment'], - 'affiliate_id' => $data['affiliate_id'], - ); - $this->request('order/edit', array('order_id' => $order_id), $order); - } - - public function addOrder($data) { - $currency = $this->getCookieValue('currency'); - if($currency) { - $a = $this->request('currency', array(), array('currency' => $currency)); - } - - $customer = array( - 'store_id' => $data['store_id'], - 'currency' => $currency != false ? $currency : '', - 'customer' => $data['customer'], - 'customer_id' => $data['customer_id'], - 'customer_group_id' => $data['customer_group_id'], - 'firstname' => $data['firstname'], - 'lastname' => $data['lastname'], - 'email' => $data['email'], - 'telephone' => $data['telephone'], - 'fax' => $data['fax'], - ); - $this->request('customer', array(), $customer); - - $products = array(); - foreach($data['order_product'] as $product) { - $product = array( - 'product_id' => $product['product_id'], - 'quantity' => $product['quantity'], - ); - $products[] = $product; - } - $this->request('cart/add', array(), array('product' => $products)); - - $payment_address = array( - 'payment_address' => $data['payment_address'], - 'firstname' => $data['payment_firstname'], - 'lastname' => $data['payment_lastname'], - 'company' => $data['payment_company'], - 'address_1' => $data['payment_address_1'], - 'address_2' => $data['payment_address_2'], - 'city' => $data['payment_city'], - 'postcode' => $data['payment_postcode'], - 'country_id' => $data['payment_country_id'], - 'zone_id' => $data['payment_zone_id'], - ); - $this->request('payment/address', array(), $payment_address); - - $shipping_address = array( - 'shipping_address' => $data['shipping_address'], - 'firstname' => $data['shipping_firstname'], - 'lastname' => $data['shipping_lastname'], - 'company' => $data['shipping_company'], - 'address_1' => $data['shipping_address_1'], - 'address_2' => $data['shipping_address_2'], - 'city' => $data['shipping_city'], - 'postcode' => $data['shipping_postcode'], - 'country_id' => $data['shipping_country_id'], - 'zone_id' => !empty($data['shipping_zone_id']) ? $data['shipping_zone_id'] : 0, - ); - $this->request('shipping/address', array(), $shipping_address); - - $this->request('shipping/methods', array(), array()); - $shipping_method = array( - 'shipping_method' => $data['shipping_code'] - ); - $this->request('shipping/method', array(), $shipping_method); - - $this->request('payment/methods', array(), array()); - $payment_method = array( - 'payment_method' => $data['payment_code'] - ); - $this->request('payment/method', array(), $payment_method); - - $order = array( - 'shipping_method' => $data['shipping_code'], - 'payment_method' => $data['payment_code'], - 'order_status_id' => $data['order_status_id'], - 'comment' => $data['comment'], - 'affiliate_id' => $data['affiliate_id'], - ); - $this->request('order/add', array(), $order); - } - - private function getInnerIpAddr() { - $opencartStoreInfo = $this->model_setting_store->getStore($this->opencartStoreId); - - if ($opencartStoreInfo) { - $url = $opencartStoreInfo['ssl']; - } else { - $url = HTTPS_CATALOG; - } - - $curl = curl_init(); - - // Set SSL if required - if (substr($url, 0, 5) == 'https') { - curl_setopt($curl, CURLOPT_PORT, 443); - } - - curl_setopt($curl, CURLOPT_HEADER, false); - curl_setopt($curl, CURLINFO_HEADER_OUT, true); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curl, CURLOPT_FORBID_REUSE, false); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_URL, $url . 'system/cron/getmyip.php'); - - return curl_exec($curl); - } -} diff --git a/vqmod/xml/retailcrm_create_order.xml b/vqmod/xml/retailcrm_create_order.xml new file mode 100644 index 0000000..371345e --- /dev/null +++ b/vqmod/xml/retailcrm_create_order.xml @@ -0,0 +1,47 @@ + + + Send order to RetailCRM when it created + 1.5.x + 2.3.x + retailcrm.ru + + + + cart->clear()]]> + load->model('account/order'); + $this->load->model('checkout/order'); + $this->load->model('retailcrm/order'); + + $order = $this->model_checkout_order->getOrder($this->session->data['order_id']); + $order['products'] = $this->model_account_order->getOrderProducts($this->session->data['order_id']); + $order['order_total'] = $this->model_account_order->getOrderTotals($this->session->data['order_id']); + + $this->model_retailcrm_order->sendToCrm($order, $this->session->data['order_id']); + ]]> + + + + + + db->query("UPDATE `" . DB_PREFIX . "order` SET total = '" . (float)$total . "', affiliate_id = '" . (int)$affiliate_id . "', commission = '" . (float)$commission . "' WHERE order_id = '" . (int)$order_id . "'");]]> + load->model('setting/setting'); + $status = $this->model_setting_setting->getSetting('retailcrm'); + + if (!empty($data['order_status_id'])) { + $data['order_status'] = $status['retailcrm_status'][$data['order_status_id']]; + } + + $this->load->model('retailcrm/order'); + if (isset ($order_query)) { + $this->model_retailcrm_order->changeInCrm($data, $order_id); + } else { + $this->model_retailcrm_order->sendToCrm($data, $order_id); + } + } + ]]> + + +