diff --git a/README.md b/README.md index 417fc0e..216764c 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ cp -r opencart-module/* /path/to/site/root Add to cron: ``` -*/5 * * * * /usr/bin/php /path/to/opencart/system/cron/history.php >> /path/to/opencart/system/logs/cronjob_history.log 2>&1 +*/5 * * * * /usr/bin/php /path/to/opencart/system/cron/history.php >> /path/to/opencart/system/storage/logs/cronjob_history.log 2>&1 ``` #### Setting product catalog export @@ -42,7 +42,7 @@ Add to cron: Add to cron: ``` -* */4 * * * /usr/bin/php /path/to/opencart/system/cron/icml.php >> /path/to/opencart/system/logs/cronjob_icml.log 2>&1 +* */4 * * * /usr/bin/php /path/to/opencart/system/cron/icml.php >> /path/to/opencart/system/storage/logs/cronjob_icml.log 2>&1 ``` Your export file will be available by following url diff --git a/README.ru.md b/README.ru.md index 0e5d119..b3b4f46 100644 --- a/README.ru.md +++ b/README.ru.md @@ -37,7 +37,7 @@ cp -r opencart-module/* /path/to/site/root Для получения изменений и новых данных добавьте в cron следующую запись: ``` -*/5 * * * * /usr/bin/php /path/to/opencart/system/cron/history.php >> /path/to/opencart/system/logs/cronjob_history.log 2>&1 +*/5 * * * * /usr/bin/php /path/to/opencart/system/cron/history.php >> /path/to/opencart/system/storage/logs/cronjob_history.log 2>&1 ``` #### Настройка экспорта каталога @@ -45,7 +45,7 @@ cp -r opencart-module/* /path/to/site/root Для периодической выгрузки каталога добавьте в cron следующую запись: ``` -* */4 * * * /usr/bin/php /path/to/opencart/system/cron/icml.php >> /path/to/opencart/system/logs/cronjob_icml.log 2>&1 +* */4 * * * /usr/bin/php /path/to/opencart/system/cron/icml.php >> /path/to/opencart/system/storage/logs/cronjob_icml.log 2>&1 ``` В настройках CRM установите путь к файлу выгрузки diff --git a/admin/controller/extension/module/retailcrm.php b/admin/controller/extension/module/retailcrm.php index fc5500f..d8d056a 100644 --- a/admin/controller/extension/module/retailcrm.php +++ b/admin/controller/extension/module/retailcrm.php @@ -26,7 +26,11 @@ public function install() { $this->load->model('setting/setting'); $this->model_setting_setting - ->editSetting('retailcrm', array('retailcrm_status' => 1)); + ->editSetting('retailcrm', array( + 'retailcrm_status' => 1, + 'retailcrm_country' => array($this->config->get('config_country_id')) + ) + ); $this->load->model('extension/event'); @@ -82,6 +86,7 @@ public function uninstall() public function index() { + $this->load->model('localisation/country'); $this->load->model('setting/setting'); $this->load->model('extension/module'); $this->load->model('extension/retailcrm/references'); @@ -123,6 +128,16 @@ public function index() 'retailcrm_dict_delivery', 'retailcrm_dict_status', 'retailcrm_dict_payment', + 'retailcrm_countries_settings', + 'text_success_export', + 'text_success_export_order', + 'text_button_export', + 'text_button_export_order', + 'text_button_catalog', + 'text_success_catalog', + 'retailcrm_upload_order', + 'text_error_order', + 'text_error_order_id' ); $this->load->model('extension/extension'); @@ -231,7 +246,16 @@ public function index() $_data['header'] = $this->load->controller('common/header'); $_data['column_left'] = $this->load->controller('common/column_left'); $_data['footer'] = $this->load->controller('common/footer'); + $_data['countries'] = $this->model_localisation_country->getCountries(); + $_data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG; + $_data['token'] = $this->request->get['token']; + if(file_exists(DIR_SYSTEM . '/cron/export_done.txt')) { + $_data['export_file'] = false; + } else { + $_data['export_file'] = true; + } + $this->response->setOutput( $this->load->view('extension/module/retailcrm.tpl', $_data) ); @@ -294,6 +318,32 @@ public function order_create($order_id) } } + /** + * Export single order + * + * + */ + public function exportOrder() + { + $order_id = isset($this->request->get['order_id']) ? $this->request->get['order_id'] : ''; + $this->load->model('sale/order'); + + $data = $this->model_sale_order->getOrder($order_id); + $data['products'] = $this->model_sale_order->getOrderProducts($order_id); + $data['totals'] = $this->model_sale_order->getOrderTotals($order_id); + + if (!isset($data['fromApi'])) { + $this->load->model('setting/setting'); + $status = $this->model_setting_setting->getSetting('retailcrm'); + $data['order_status'] = $status['retailcrm_status'][$data['order_status_id']]; + + $this->load->model('extension/retailcrm/order'); + $result = $this->model_extension_retailcrm_order->uploadOrder($data); + } + + echo json_encode($result); + } + /** * Export orders * diff --git a/admin/language/en-gb/extension/module/retailcrm.php b/admin/language/en-gb/extension/module/retailcrm.php index f1baabb..0ca1927 100644 --- a/admin/language/en-gb/extension/module/retailcrm.php +++ b/admin/language/en-gb/extension/module/retailcrm.php @@ -10,10 +10,20 @@ $_['text_notice'] = 'Warning! Timezone in CRM & your shop must be equal, you must setup it here:'; $_['retailcrm_base_settings'] = 'Connection settings'; $_['retailcrm_dict_settings'] = 'Dictionary settings'; - +$_['retailcrm_countries_settings'] = 'Trading zones setting'; +$_['retailcrm_upload_order'] = 'Unload single order'; $_['retailcrm_url'] = 'RetailCRM URL'; $_['retailcrm_apikey'] = 'RetailCRM API Key'; +$_['text_success_export'] = 'Orders and customers successfully unloaded'; +$_['text_success_export_order'] = 'Order successfully unloaded'; +$_['text_button_export'] = 'Unload all orders and customers'; +$_['text_button_export_order'] = 'Unload order'; +$_['text_button_catalog'] = 'Unload catalog'; +$_['text_success_catalog'] = 'Catalog successfully unloaded'; +$_['text_error_order'] = 'Error! Order is not unloaded!'; +$_['text_error_order_id'] = 'Error! Enter the correct order number!'; + $_['retailcrm_dict_delivery'] = 'Shipment methods'; $_['retailcrm_dict_status'] = 'Order statuses'; $_['retailcrm_dict_payment'] = 'Payment methods'; diff --git a/admin/language/ru-ru/extension/module/retailcrm.php b/admin/language/ru-ru/extension/module/retailcrm.php index b2062ff..9672dd5 100644 --- a/admin/language/ru-ru/extension/module/retailcrm.php +++ b/admin/language/ru-ru/extension/module/retailcrm.php @@ -10,10 +10,21 @@ $_['text_notice'] = 'Внимание! Часовой пояс в CRM должен совпадать с часовым поясом в магазине, настроки часового пояса CRM можно задать по адресу:'; $_['retailcrm_base_settings'] = 'Настройки соединения'; $_['retailcrm_dict_settings'] = 'Настройки соответствия справочников'; +$_['retailcrm_countries_settings'] = 'Настройка торговых зон'; +$_['retailcrm_upload_order'] = 'Выгрузка одного заказа'; $_['retailcrm_url'] = 'Адрес RetailCRM'; $_['retailcrm_apikey'] = 'Api ключ RetailCRM'; +$_['text_success_export'] = 'Заказы и клиенты успешно выгружены'; +$_['text_success_export_order'] = 'Заказ успешно выгружен'; +$_['text_button_export'] = 'Выгрузить все заказы и клиентов'; +$_['text_button_export_order'] = 'Выгрузить заказ'; +$_['text_button_catalog'] = 'Выгрузить каталог'; +$_['text_success_catalog'] = 'Каталог успешно выгружен'; +$_['text_error_order'] = 'Ошибка! Заказ не выгружен!'; +$_['text_error_order_id'] = 'Ошибка! Введите корректный номер заказа!'; + $_['retailcrm_dict_delivery'] = 'Способы доставки'; $_['retailcrm_dict_status'] = 'Статусы'; $_['retailcrm_dict_payment'] = 'Способы оплаты'; diff --git a/admin/model/extension/retailcrm/history.php b/admin/model/extension/retailcrm/history.php index eb19ffb..5e61bad 100644 --- a/admin/model/extension/retailcrm/history.php +++ b/admin/model/extension/retailcrm/history.php @@ -227,6 +227,15 @@ protected function updateOrders($orders) $deliveryCost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : 0; + if(isset($order['discount']) && $order['discount'] > 0) { + $orderTotals = $this->model_sale_order->getOrderTotals($order['externalId']); + foreach($orderTotals as $orderTotal) { + if($orderTotal['code'] == 'coupon') { + $data['order_total'][] = $orderTotal; + } + } + } + $data['order_total'] = array( array( 'order_total_id' => '', diff --git a/admin/model/extension/retailcrm/icml.php b/admin/model/extension/retailcrm/icml.php index f2f3d47..7a28c1f 100644 --- a/admin/model/extension/retailcrm/icml.php +++ b/admin/model/extension/retailcrm/icml.php @@ -220,6 +220,7 @@ private function addOffers() ->appendChild($this->dd->createTextNode($product['name'])); } $e->appendChild($this->dd->createElement('price')) + ->appendChild($this->dd->createTextNode($productPrice + $optionsTotalCost)); ->appendChild($this->dd->createTextNode($product['price'] + $optionsTotalCost)); /** * Vendor diff --git a/admin/model/extension/retailcrm/order.php b/admin/model/extension/retailcrm/order.php index 77fa54d..2c27169 100644 --- a/admin/model/extension/retailcrm/order.php +++ b/admin/model/extension/retailcrm/order.php @@ -20,6 +20,49 @@ public function uploadToCrm($orders) { } } + public function uploadOrder($order) + { + if(isset($this->request->post['fromApi'])) return; + + $this->load->model('setting/setting'); + $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( + $settings['retailcrm_url'], + $settings['retailcrm_apikey'], + DIR_SYSTEM . 'storage/logs/retailcrm.log' + ); + + $customers = $this->retailcrm->customersList( + array( + 'name' => $order['telephone'], + 'email' => $order['email'] + ), + 1, + 100 + ); + + $order = $this->process($order); + + if($customers) { + foreach ($customers['customers'] as $customer) { + $order['customer']['id'] = $customer['id']; + } + } + + unset($customers); + + $result = $this->retailcrm->ordersCreate($order); + } + + return $result; + } + private function process($order_data) { $order = array(); diff --git a/admin/model/extension/retailcrm/references.php b/admin/model/extension/retailcrm/references.php index b764c55..79d7564 100644 --- a/admin/model/extension/retailcrm/references.php +++ b/admin/model/extension/retailcrm/references.php @@ -5,9 +5,20 @@ class ModelExtensionRetailcrmReferences extends Model { protected $retailcrm; + private $opencartApiClient; + + public function getOpercartDeliveryTypes() + { + $this->load->model('user/api'); + $this->opencartApiClient = new OpencartApiClient($this->registry); + + return $this->opencartApiClient->request('retailcrm/getDeliveryTypes', array(), array()); + } public function getDeliveryTypes() { + $this->load->model('setting/store'); + return array( 'opencart' => $this->getOpercartDeliveryTypes(), 'retailcrm' => $this->getApiDeliveryTypes() @@ -30,28 +41,6 @@ public function getPaymentTypes() ); } - public function getOpercartDeliveryTypes() - { - $deliveryMethods = array(); - $files = glob(DIR_APPLICATION . 'controller/extension/shipping/*.php'); - - if ($files) { - foreach ($files as $file) { - $extension = basename($file, '.php'); - - $this->load->language('extension/shipping/' . $extension); - - if ($this->config->get($extension . '_status')) { - $deliveryMethods[$extension.'.'.$extension] = strip_tags( - $this->language->get('heading_title') - ); - } - } - } - - return $deliveryMethods; - } - public function getOpercartOrderStatuses() { $this->load->model('localisation/order_status'); diff --git a/admin/view/stylesheet/retailcrm.css b/admin/view/stylesheet/retailcrm.css index 43cbeb6..5c3b88f 100644 --- a/admin/view/stylesheet/retailcrm.css +++ b/admin/view/stylesheet/retailcrm.css @@ -1,2 +1,3 @@ .retailcrm_unit {margin-bottom: 10px;} .retailcrm_unit input {width: 30%;} +.checkbox input{width: auto;} diff --git a/admin/view/template/extension/module/retailcrm.tpl b/admin/view/template/extension/module/retailcrm.tpl index 144f68b..9afdbdd 100644 --- a/admin/view/template/extension/module/retailcrm.tpl +++ b/admin/view/template/extension/module/retailcrm.tpl @@ -5,6 +5,10 @@ + + diff --git a/catalog/controller/api/retailcrm.php b/catalog/controller/api/retailcrm.php new file mode 100644 index 0000000..00ad266 --- /dev/null +++ b/catalog/controller/api/retailcrm.php @@ -0,0 +1,74 @@ +load->model('localisation/country'); + $this->load->model('setting/setting'); + + $countries = $this->model_setting_setting->getSetting('retailcrm')['retailcrm_country']; + $deliveryTypes = array(); + + foreach ($countries as $country) { + $deliveryTypes = array_merge($deliveryTypes, $this->getDeliveryTypesByZones($country)); + } + + if (isset($this->request->server['HTTP_ORIGIN'])) { + $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']); + $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); + $this->response->addHeader('Access-Control-Max-Age: 1000'); + $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With'); + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($deliveryTypes)); + } + + protected function getDeliveryTypesByZones($country_id) + { + $this->load->model('localisation/zone'); + $this->load->model('localisation/country'); + $this->load->model('extension/extension'); + + $shippingModules = $this->model_extension_extension->getExtensions('shipping'); + $zones = $this->model_localisation_zone->getZonesByCountryId($country_id); + $country = $this->model_localisation_country->getCountry($country_id); + $quote_data = array(); + + foreach ($zones as $zone) { + $address = array( + 'country_id' => $country_id, + 'zone_id' => $zone['zone_id'], + 'iso_code_2' => $country['iso_code_2'], + 'iso_code_3' => $country['iso_code_3'], + 'zone_code' => $zone['code'], + 'postcode' => '', + 'city' => '' + ); + + foreach ($shippingModules as $shippingModule) { + + $this->load->model('extension/shipping/' . $shippingModule['code']); + + if ($this->config->get($shippingModule['code'] . '_status')) { + if($this->{'model_extension_shipping_' . $shippingModule['code']}->getQuote($address)) { + $quote_data[] = $this->{'model_extension_shipping_' . $shippingModule['code']}->getQuote($address); + } + } + } + } + + $deliveryTypes = array(); + + foreach ($quote_data as $shipping) { + + foreach ($shipping['quote'] as $shippingMethod) { + $deliveryTypes[$shipping['code']]['title'] = $shipping['title']; + $deliveryTypes[$shipping['code']][$shippingMethod['code']] = $shippingMethod; + } + + } + + return $deliveryTypes; + } +} diff --git a/catalog/controller/extension/module/retailcrm.php b/catalog/controller/extension/module/retailcrm.php index 7b8455d..47777c3 100644 --- a/catalog/controller/extension/module/retailcrm.php +++ b/catalog/controller/extension/module/retailcrm.php @@ -26,6 +26,7 @@ public function order_create($parameter1, $parameter2 = null, $parameter3 = null $order_id = $parameter3; $data = $this->model_checkout_order->getOrder($order_id); + $data['totals'] = $this->model_account_order->getOrderTotals($order_id); $data['products'] = $this->model_account_order->getOrderProducts($order_id); foreach($data['products'] as $key => $product) { @@ -63,6 +64,7 @@ public function order_edit($parameter1, $parameter2 = null) { if($data['order_status_id'] == 0) return; $data['products'] = $this->model_account_order->getOrderProducts($order_id); + $data['totals'] = $this->model_account_order->getOrderTotals($order_id); foreach($data['products'] as $key => $product) { $productOptions = $this->model_account_order->getOrderOptions($order_id, $product['order_product_id']); @@ -79,11 +81,6 @@ public function order_edit($parameter1, $parameter2 = null) { $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('extension/retailcrm/order'); $this->model_extension_retailcrm_order->changeInCrm($data, $data['order_id']); } diff --git a/catalog/model/extension/retailcrm/order.php b/catalog/model/extension/retailcrm/order.php index 873d2ff..7860ab4 100644 --- a/catalog/model/extension/retailcrm/order.php +++ b/catalog/model/extension/retailcrm/order.php @@ -52,15 +52,21 @@ public function sendToCrm($order_data, $order_id) $deliveryCost = 0; $altTotals = isset($order_data['order_total']) ? $order_data['order_total'] : ""; $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $altTotals ; + $couponTotal = 0; if (!empty($orderTotals)) { foreach ($orderTotals as $totals) { if ($totals['code'] == 'shipping') { $deliveryCost = $totals['value']; } + + if ($totals['code'] == 'coupon') { + $couponTotal = abs($totals['value']); + } } } + $order['discount'] = $couponTotal; $order['createdAt'] = $order_data['date_added']; $payment_code = $order_data['payment_code']; @@ -205,6 +211,7 @@ public function changeInCrm($order_data, $order_id) )) ) ); + if(!empty($deliveryCost)){ $order['delivery']['cost'] = $deliveryCost; } @@ -215,7 +222,7 @@ public function changeInCrm($order_data, $order_id) foreach ($orderProducts as $product) { $offerId = ''; - if(!empty($product['option'])) { + if (!empty($product['option'])) { $options = array(); $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); diff --git a/system/cron/export.php b/system/cron/export.php index 5880920..cbf86bf 100644 --- a/system/cron/export.php +++ b/system/cron/export.php @@ -1,3 +1,4 @@ api, $method), $arguments); + $date = date('[Y-m-d H:i:s]'); if (!$response->isSuccessful()) { - error_log("[$method] " . $response->getErrorMsg() . "\n", 3, $this->log); + error_log($date . " [$method] " . $response->getErrorMsg() . "\n", 3, $this->log); if (isset($response['errors'])) { $error = implode("\n", $response['errors']); - error_log($error . "\n", 3, $this->log); + error_log($date .' '. $error . "\n", 3, $this->log); } $response = false; } return $response; } catch (CurlException $e) { - error_log("[$method] " . $e->getMessage() . "\n", 3, $this->log); + error_log($date . " [$method] " . $e->getMessage() . "\n", 3, $this->log); return false; } catch (InvalidJsonException $e) { - error_log("[$method] " . $e->getMessage() . "\n", 3, $this->log); + error_log($date . " [$method] " . $e->getMessage() . "\n", 3, $this->log); return false; } }