Advanced search for delivery methods (#25)

Plugin's deliveries
ICML export
Order export
This commit is contained in:
iyzoer 2017-04-07 12:35:53 +03:00 committed by Alex Lushpai
parent 3be4ac76a9
commit c724392c36
16 changed files with 334 additions and 43 deletions

View File

@ -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

View File

@ -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 установите путь к файлу выгрузки

View File

@ -26,7 +26,11 @@ class ControllerExtensionModuleRetailcrm extends Controller
{
$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 @@ class ControllerExtensionModuleRetailcrm extends Controller
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 @@ class ControllerExtensionModuleRetailcrm extends Controller
'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,6 +246,15 @@ class ControllerExtensionModuleRetailcrm extends Controller
$_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 @@ class ControllerExtensionModuleRetailcrm extends Controller
}
}
/**
* 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
*

View File

@ -10,10 +10,20 @@ $_['text_success'] = 'Setting saved';
$_['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';

View File

@ -10,10 +10,21 @@ $_['text_success'] = 'Настройки успешно сохра
$_['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'] = 'Способы оплаты';

View File

@ -227,6 +227,15 @@ class ModelExtensionRetailcrmHistory extends Model
$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' => '',

View File

@ -220,6 +220,7 @@ class ModelExtensionRetailcrmIcml extends Model
->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

View File

@ -20,6 +20,49 @@ class ModelExtensionRetailcrmOrder extends Model {
}
}
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();

View File

@ -5,9 +5,20 @@ require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
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 @@ class ModelExtensionRetailcrmReferences extends Model
);
}
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');

View File

@ -1,2 +1,3 @@
.retailcrm_unit {margin-bottom: 10px;}
.retailcrm_unit input {width: 30%;}
.checkbox input{width: auto;}

View File

@ -5,6 +5,10 @@
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<?php if ($export_file) : ?>
<button type="button" id="export" data-toggle="tooltip" title="Выгрузить клиентов и заказы" class="btn btn-success"><i class="fa fa-download"></i></button>
<?php endif; ?>
<button type="button" id="icml" data-toggle="tooltip" title="<?php echo $text_button_catalog; ?>" class="btn btn-success"><i class="fa fa-file-text-o"></i></button>
<button type="submit" form="form-retailcrm" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
<h1><?php echo $heading_title; ?></h1>
@ -46,6 +50,25 @@
<input id="retailcrm_apikey" type="text" name="retailcrm_apikey" value="<?php if (isset($saved_settings['retailcrm_apikey'])): echo $saved_settings['retailcrm_apikey']; endif;?>">
</div>
<h3><?php echo $retailcrm_countries_settings; ?></h3>
<div class="retailcrm_unit">
<div class="well well-sm" style="height: 150px; overflow: auto; width: 30%;">
<?php foreach($countries as $country) : ?>
<div class="checkbox">
<label>
<input type="checkbox" name="<?php echo 'retailcrm_country[]'; ?>" value="<?php echo $country['country_id']; ?>" <?php if(isset($saved_settings['retailcrm_country']) && in_array($country['country_id'], $saved_settings['retailcrm_country'])): echo 'checked'; endif;?>>
<?php echo $country['name']; ?>
</label>
</div>
<?php endforeach; ?>
</div>
</div>
<h3><?php echo $retailcrm_upload_order; ?></h3>
<div class="retailcrm_unit">
<label><?php echo $text_button_export_order; ?> № </label><input type="text" name="order_id">
<button type="button" id="export_order" data-toggle="tooltip" title="<?php echo $text_button_export_order; ?>" class="btn btn-success"><i class="fa fa-download"></i></button>
</div>
<?php if (isset($saved_settings['retailcrm_apikey']) && $saved_settings['retailcrm_apikey'] != '' && isset($saved_settings['retailcrm_url']) && $saved_settings['retailcrm_url'] != ''): ?>
<?php if (!empty($retailcrm_errors)) : ?>
@ -56,16 +79,22 @@
<h3><?php echo $retailcrm_dict_settings; ?></h3>
<h4><?php echo $retailcrm_dict_delivery; ?></h4>
<?php foreach ($delivery['opencart'] as $key => $value): ?>
<div class="retailcrm_unit">
<select id="retailcrm_delivery_<?php echo $key; ?>" name="retailcrm_delivery[<?php echo $key; ?>]" >
<?php foreach($delivery['opencart'] as $value): ?>
<div class="pm"><?php echo $value['title'].':'; ?></div>
<?php unset($value['title']); ?>
<?php foreach ($value as $key => $val): ?>
<div class="retailcrm_unit">
<select id="retailcrm_delivery_<?php echo $val['code']; ?>" name="retailcrm_delivery[<?php echo $val['code']; ?>]" >
<?php foreach ($delivery['retailcrm'] as $k => $v): ?>
<option value="<?php echo $v['code'];?>" <?php if(isset($saved_settings['retailcrm_delivery'][$key]) && $v['code'] == $saved_settings['retailcrm_delivery'][$key]):?>selected="selected"<?php endif;?>>
<?php echo $v['name'];?>
</option>
<?php endforeach; ?>
<?php endforeach; ?>
</select>
<label for="retailcrm_delivery_<?php echo $key; ?>"><?php echo $value; ?></label>
<label for="retailcrm_pm_<?php echo $val['code']; ?>"><?php echo $val['title']; ?></label>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
@ -108,3 +137,71 @@
</div>
<?php echo $footer; ?>
<script type="text/javascript">
var token = '<?php echo $token; ?>';
$('#icml').on('click', function() {
$.ajax({
url: '<?php echo $catalog; ?>'+'system/cron/icml.php',
beforeSend: function() {
$('#icml').button('loading');
},
complete: function() {
$('.alert-success').remove();
$('#content > .container-fluid').prepend('<div class="alert alert-success"><i class="fa fa-exclamation-circle"></i> <?php echo $text_success_catalog; ?></div>');
$('#icml').button('reset');
},
error: function(){
alert('error');
}
});
});
$('#export').on('click', function() {
$.ajax({
url: '<?php echo $catalog; ?>'+'system/cron/export.php',
beforeSend: function() {
$('#export').button('loading');
},
complete: function() {
$('.alert-success').remove();
$('#content > .container-fluid').prepend('<div class="alert alert-success"><i class="fa fa-exclamation-circle"></i> <?php echo $text_success_export; ?></div>');
$('#export').button('reset');
},
error: function(){
alert('error');
}
});
});
$('#export_order').on('click', function() {
var order_id = $('input[name=\'order_id\']').val();
if (order_id && order_id > 0) {
$.ajax({
url: '<?php echo $catalog; ?>'+'admin/index.php?route=extension/module/retailcrm/exportOrder&token=' + token + '&order_id=' + order_id,
beforeSend: function() {
$('#export_order').button('loading');
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
},
success: function(data, textStatus, jqXHR) {
if (jqXHR['responseText'] == 'false') {
$('.alert-danger').remove();
$('#content > .container-fluid').prepend('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i><?php echo $text_error_order; ?></div>');
$('#export_order').button('reset');
} else {
$('.alert-success').remove();
$('#content > .container-fluid').prepend('<div class="alert alert-success"><i class="fa fa-exclamation-circle"></i><?php echo $text_success_export_order; ?></div>');
$('#export_order').button('reset');
$('input[name=\'order_id\']').val('');
}
}
});
} else {
$('.alert-danger').remove();
$('#content > .container-fluid').prepend('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $text_error_order_id; ?></div>');
$('#export_order').button('reset');
}
});
</script>

View File

@ -0,0 +1,74 @@
<?php
class ControllerApiRetailcrm extends Controller
{
public function getDeliveryTypes()
{
$this->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;
}
}

View File

@ -26,6 +26,7 @@ class ControllerExtensionModuleRetailcrm extends Controller
$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 @@ class ControllerExtensionModuleRetailcrm extends Controller
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 @@ class ControllerExtensionModuleRetailcrm extends Controller
$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']);
}

View File

@ -52,15 +52,21 @@ class ModelExtensionRetailcrmOrder extends Model {
$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 @@ class ModelExtensionRetailcrmOrder extends Model {
))
)
);
if(!empty($deliveryCost)){
$order['delivery']['cost'] = $deliveryCost;
}
@ -215,7 +222,7 @@ class ModelExtensionRetailcrmOrder extends Model {
foreach ($orderProducts as $product) {
$offerId = '';
if(!empty($product['option'])) {
if (!empty($product['option'])) {
$options = array();
$productOptions = $this->model_catalog_product->getProductOptions($product['product_id']);

View File

@ -1,3 +1,4 @@
<?php
$cli_action = 'extension/module/retailcrm/export';
require_once('dispatch.php');
$file = fopen(DIR_SYSTEM . '/cron/export_done.txt', "x");

View File

@ -20,22 +20,23 @@ class RetailcrmProxy
{
try {
$response = call_user_func_array(array($this->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;
}
}