mirror of
https://github.com/retailcrm/opencart-module.git
synced 2025-02-16 22:43:18 +03:00
Метод получения и обработки данных истории вынесен в отдельную модель, рефакторинг кода
This commit is contained in:
parent
f8a5cd0f5f
commit
5001adfc3d
@ -33,7 +33,7 @@ cp -r opencart-module/* /path/to/opecart/instance
|
|||||||
Для периодической выгрузки каталога добавьте в cron следующую запись:
|
Для периодической выгрузки каталога добавьте в cron следующую запись:
|
||||||
|
|
||||||
```
|
```
|
||||||
* */4 * * * /usr/bin/php /path/to/opencart/admin/model/retailcrm/export.php >> /path/to/opencart/system/logs/cronjob_export.log 2>&1
|
* */4 * * * /usr/bin/php /path/to/opencart/system/cron/export.php >> /path/to/opencart/system/logs/cronjob_export.log 2>&1
|
||||||
```
|
```
|
||||||
|
|
||||||
В настройках CRM установите путь к файлу выгрузки
|
В настройках CRM установите путь к файлу выгрузки
|
||||||
@ -67,10 +67,6 @@ $this->model_retailcrm_order->send($data, $order_id);
|
|||||||
|
|
||||||
```
|
```
|
||||||
if (!isset($data['fromApi'])) {
|
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('retailcrm/order');
|
$this->load->model('retailcrm/order');
|
||||||
$this->model_retailcrm_order->send($data, $order_id);
|
$this->model_retailcrm_order->send($data, $order_id);
|
||||||
}
|
}
|
||||||
@ -81,6 +77,6 @@ if (!isset($data['fromApi'])) {
|
|||||||
Для получения изменений и новых данных добавьте в cron следующую запись:
|
Для получения изменений и новых данных добавьте в cron следующую запись:
|
||||||
|
|
||||||
```
|
```
|
||||||
*/5 * * * * /usr/bin/php /path/to/opencart/admin/model/retailcrm/history.php >> /path/to/opencart/system/logs/cronjob_history.log 2>&1
|
*/5 * * * * /usr/bin/php /path/to/opencart/system/cron/retailcrm/history.php >> /path/to/opencart/system/logs/cronjob_history.log 2>&1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once DIR_SYSTEM . 'library/retailcrm/Retailcrm.php';
|
require_once DIR_SYSTEM . 'library/retailcrm.php';
|
||||||
|
|
||||||
class ControllerModuleRetailcrm extends Controller {
|
class ControllerModuleRetailcrm extends Controller {
|
||||||
private $error = array();
|
private $error = array();
|
||||||
protected $log, $statuses, $payments, $deliveryTypes;
|
protected $log, $statuses, $payments, $deliveryTypes, $retailcrm;
|
||||||
|
public $children, $template;
|
||||||
|
|
||||||
public function install() {
|
public function install() {
|
||||||
$this->load->model('setting/setting');
|
$this->load->model('setting/setting');
|
||||||
@ -74,26 +75,16 @@ class ControllerModuleRetailcrm extends Controller {
|
|||||||
/*
|
/*
|
||||||
* Delivery
|
* Delivery
|
||||||
*/
|
*/
|
||||||
|
$this->deliveryTypes = array();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->deliveryTypes = $this->retailcrm->deliveryTypesList();
|
$this->deliveryTypes = $this->retailcrm->deliveryTypesList();
|
||||||
}
|
} catch (CurlException $e) {
|
||||||
catch (CurlException $e)
|
|
||||||
{
|
|
||||||
$this->data['retailcrm_error'][] = $e->getMessage();
|
$this->data['retailcrm_error'][] = $e->getMessage();
|
||||||
$this->log->addError(
|
$this->log->addError('RestApi::deliveryTypesList::Curl:' . $e->getMessage());
|
||||||
'[' .
|
} catch (InvalidJsonException $e) {
|
||||||
$this->config->get('store_name') .
|
|
||||||
'] RestApi::deliveryTypesList::Api:' . $e->getMessage()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (InvalidJsonException $e)
|
|
||||||
{
|
|
||||||
$this->data['retailcrm_error'][] = $e->getMessage();
|
$this->data['retailcrm_error'][] = $e->getMessage();
|
||||||
$this->log->addError(
|
$this->log->addError('RestApi::deliveryTypesList::JSON:' . $e->getMessage());
|
||||||
'[' . $this->config->get('store_name') .
|
|
||||||
'] RestApi::deliveryTypesList::Curl:' . $e->getMessage()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data['delivery'] = array(
|
$this->data['delivery'] = array(
|
||||||
@ -104,26 +95,16 @@ class ControllerModuleRetailcrm extends Controller {
|
|||||||
/*
|
/*
|
||||||
* Statuses
|
* Statuses
|
||||||
*/
|
*/
|
||||||
|
$this->statuses = array();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->statuses = $this->retailcrm->orderStatusesList();
|
$this->statuses = $this->retailcrm->orderStatusesList();
|
||||||
}
|
} catch (CurlException $e) {
|
||||||
catch (CurlException $e)
|
|
||||||
{
|
|
||||||
$this->data['retailcrm_error'][] = $e->getMessage();
|
$this->data['retailcrm_error'][] = $e->getMessage();
|
||||||
$this->log->addError(
|
$this->log->addError('RestApi::orderStatusesList::Curl:' . $e->getMessage());
|
||||||
'[' .
|
} catch (InvalidJsonException $e) {
|
||||||
$this->config->get('store_name') .
|
|
||||||
'] RestApi::orderStatusesList::Api:' . $e->getMessage()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (InvalidJsonException $e)
|
|
||||||
{
|
|
||||||
$this->data['retailcrm_error'][] = $e->getMessage();
|
$this->data['retailcrm_error'][] = $e->getMessage();
|
||||||
$this->log->addError(
|
$this->log->addError('RestApi::orderStatusesList::JSON:' . $e->getMessage());
|
||||||
'[' .
|
|
||||||
$this->config->get('store_name') .
|
|
||||||
'] RestApi::orderStatusesList::Curl:' . $e->getMessage()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data['statuses'] = array(
|
$this->data['statuses'] = array(
|
||||||
@ -134,27 +115,16 @@ class ControllerModuleRetailcrm extends Controller {
|
|||||||
/*
|
/*
|
||||||
* Payment
|
* Payment
|
||||||
*/
|
*/
|
||||||
|
$this->payments = array();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->payments = $this->retailcrm->paymentTypesList();
|
$this->payments = $this->retailcrm->paymentTypesList();
|
||||||
}
|
} catch (CurlException $e) {
|
||||||
catch (CurlException $e)
|
|
||||||
{
|
|
||||||
$this->data['retailcrm_error'][] = $e->getMessage();
|
$this->data['retailcrm_error'][] = $e->getMessage();
|
||||||
$this->log->addError(
|
$this->log->addError('RestApi::paymentTypesList::Curl:' . $e->getMessage());
|
||||||
'[' .
|
} catch (InvalidJsonException $e) {
|
||||||
$this->config->get('store_name') .
|
|
||||||
'] RestApi::paymentTypesList::Api:' . $e->getMessage()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (InvalidJsonException $e)
|
|
||||||
{
|
|
||||||
$this->data['retailcrm_error'][] = $e->getMessage();
|
$this->data['retailcrm_error'][] = $e->getMessage();
|
||||||
$this->log->addError(
|
$this->log->addError('RestApi::paymentTypesList::JSON:' . $e->getMessage());
|
||||||
'[' .
|
|
||||||
$this->config->get('store_name') .
|
|
||||||
'] RestApi::paymentTypesList::Curl:' . $e->getMessage()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data['payments'] = array(
|
$this->data['payments'] = array(
|
||||||
@ -230,309 +200,8 @@ class ControllerModuleRetailcrm extends Controller {
|
|||||||
|
|
||||||
public function history()
|
public function history()
|
||||||
{
|
{
|
||||||
$this->load->model('setting/setting');
|
$this->load->model('retailcrm/history');
|
||||||
$this->load->model('setting/store');
|
$this->model_retailcrm_history->request();
|
||||||
$this->load->model('sale/order');
|
|
||||||
$this->load->model('sale/customer');
|
|
||||||
$this->load->model('retailcrm/tools');
|
|
||||||
$this->load->model('catalog/product');
|
|
||||||
$this->load->model('localisation/zone');
|
|
||||||
|
|
||||||
$this->load->language('module/retailcrm');
|
|
||||||
|
|
||||||
$settings = $this->model_setting_setting->getSetting('retailcrm');
|
|
||||||
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
|
|
||||||
|
|
||||||
if (isset($settings['retailcrm_url']) &&
|
|
||||||
$settings['retailcrm_url'] != '' &&
|
|
||||||
isset($settings['retailcrm_apikey']) &&
|
|
||||||
$settings['retailcrm_apikey'] != ''
|
|
||||||
) {
|
|
||||||
DIR_SYSTEM . 'library/retailcrm/Retailcrm.php';
|
|
||||||
$crm = new ApiHelper($settings);
|
|
||||||
$orders = $crm->ordersHistory();
|
|
||||||
$ordersIdsFix = array();
|
|
||||||
$customersIdsFix = array();
|
|
||||||
$subtotalSettings = $this->model_setting_setting->getSetting('sub_total');
|
|
||||||
$totalSettings = $this->model_setting_setting->getSetting('total');
|
|
||||||
$shippingSettings = $this->model_setting_setting->getSetting('shipping');
|
|
||||||
|
|
||||||
$delivery = array_flip($settings['retailcrm_delivery']);
|
|
||||||
$payment = array_flip($settings['retailcrm_payment']);
|
|
||||||
$status = array_flip($settings['retailcrm_status']);
|
|
||||||
|
|
||||||
$ocPayment = $this->model_retailcrm_tools->getOpercartPaymentTypes();
|
|
||||||
$ocDelivery = $this->model_retailcrm_tools->getOpercartDeliveryMethods();
|
|
||||||
|
|
||||||
$zones = $this->model_localisation_zone->getZones();
|
|
||||||
|
|
||||||
foreach ($orders as $order) {
|
|
||||||
|
|
||||||
if (!isset($order['deleted']) || !$order['deleted']) {
|
|
||||||
|
|
||||||
$data = array();
|
|
||||||
|
|
||||||
$customer_id = (isset($order['customer']['externalId']) && $order['customer']['externalId'] != 0)
|
|
||||||
? $order['customer']['externalId']
|
|
||||||
: ''
|
|
||||||
;
|
|
||||||
|
|
||||||
if (isset($order['externalId'])) {
|
|
||||||
/*
|
|
||||||
* opercart developers believe that to remove all
|
|
||||||
* products from the order during the editing is a good
|
|
||||||
* idea...
|
|
||||||
*
|
|
||||||
* so we have to get order data from crm
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
$order = $crm->getOrder($order['externalId']);
|
|
||||||
} else {
|
|
||||||
if ($customer_id == '') {
|
|
||||||
$cData = array(
|
|
||||||
'customer_group_id' => '1',
|
|
||||||
'firstname' => $order['customer']['firstName'],
|
|
||||||
'lastname' => (isset($order['customer']['lastName']))
|
|
||||||
? $order['customer']['lastName']
|
|
||||||
: ' '
|
|
||||||
,
|
|
||||||
'email' => $order['customer']['email'],
|
|
||||||
'telephone' => (isset($order['customer']['phones'][0]['number']))
|
|
||||||
? $order['customer']['phones'][0]['number']
|
|
||||||
: ' '
|
|
||||||
,
|
|
||||||
'newsletter' => 0,
|
|
||||||
'password' => 'tmppass',
|
|
||||||
'status' => 1,
|
|
||||||
'address' => array(
|
|
||||||
'firstname' => $order['customer']['firstName'],
|
|
||||||
'lastname' => (isset($order['customer']['lastName']))
|
|
||||||
? $order['customer']['lastName']
|
|
||||||
: ' '
|
|
||||||
,
|
|
||||||
'address_1' => $order['customer']['address']['text'],
|
|
||||||
'city' => isset($order['customer']['address']['city'])
|
|
||||||
? $order['customer']['address']['city']
|
|
||||||
: $order['delivery']['address']['city']
|
|
||||||
,
|
|
||||||
'postcode' => isset($order['customer']['address']['index'])
|
|
||||||
? $order['customer']['address']['index']
|
|
||||||
: $order['delivery']['address']['index']
|
|
||||||
,
|
|
||||||
),
|
|
||||||
'tax_id' => '',
|
|
||||||
'zone_id' => '',
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->model_sale_customer->addCustomer($cData);
|
|
||||||
|
|
||||||
if (isset($order['customer']['email']) && $order['customer']['email'] != '') {
|
|
||||||
$tryToFind = $this->model_sale_customer->getCustomerByEmail($order['customer']['email']);
|
|
||||||
$customer_id = $tryToFind['customer_id'];
|
|
||||||
} else {
|
|
||||||
$last = $this->model_sale_customer->getCustomers(
|
|
||||||
$data = array('order' => 'DESC', 'limit' => 1)
|
|
||||||
);
|
|
||||||
$customer_id = $last[0]['customer_id'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$customersIdsFix[] = array('id' => $order['customer']['id'], 'externalId' => (int)$customer_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Build order data
|
|
||||||
*/
|
|
||||||
|
|
||||||
$data['store_id'] = ($this->config->get('config_store_id') == null)
|
|
||||||
? 0
|
|
||||||
: $this->config->get('config_store_id')
|
|
||||||
;
|
|
||||||
$data['customer'] = $order['customer']['firstName'];
|
|
||||||
$data['customer_id'] = $customer_id;
|
|
||||||
$data['customer_group_id'] = 1;
|
|
||||||
$data['firstname'] = $order['firstName'];
|
|
||||||
$data['lastname'] = (isset($order['lastName'])) ? $order['lastName'] : ' ';
|
|
||||||
$data['email'] = $order['customer']['email'];
|
|
||||||
$data['telephone'] = (isset($order['customer']['phones'][0]['number']))
|
|
||||||
? $order['customer']['phones'][0]['number']
|
|
||||||
: ' '
|
|
||||||
;
|
|
||||||
|
|
||||||
$data['comment'] = isset($order['customerComment']) ? $order['customerComment'] : '';
|
|
||||||
$data['fax'] = '';
|
|
||||||
|
|
||||||
$data['payment_address'] = '0';
|
|
||||||
$data['payment_firstname'] = $order['firstName'];
|
|
||||||
$data['payment_lastname'] = (isset($order['lastName'])) ? $order['lastName'] : ' ';
|
|
||||||
$data['payment_address_1'] = $order['customer']['address']['text'];
|
|
||||||
$data['payment_address_2'] = '';
|
|
||||||
$data['payment_company'] = '';
|
|
||||||
$data['payment_company_id'] = '';
|
|
||||||
$data['payment_city'] = isset($order['customer']['address']['city'])
|
|
||||||
? $order['customer']['address']['city']
|
|
||||||
: $order['delivery']['address']['city']
|
|
||||||
;
|
|
||||||
$data['payment_postcode'] = isset($order['customer']['address']['index'])
|
|
||||||
? $order['customer']['address']['index']
|
|
||||||
: $order['delivery']['address']['index']
|
|
||||||
;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Country & zone id detection
|
|
||||||
*/
|
|
||||||
|
|
||||||
$country = 0;
|
|
||||||
$region = '';
|
|
||||||
|
|
||||||
if(is_int($order['delivery']['address']['region'])) {
|
|
||||||
$region = $order['delivery']['address']['region'];
|
|
||||||
} else {
|
|
||||||
foreach($zones as $zone) {
|
|
||||||
if($order['delivery']['address']['region'] == $zone['name']) {
|
|
||||||
$region = $zone['zone_id'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
$data['payment_country_id'] = isset($order['customer']['address']['country'])
|
|
||||||
? $order['customer']['address']['country']
|
|
||||||
: $order['delivery']['address']['country']
|
|
||||||
;
|
|
||||||
$data['payment_zone_id'] = isset($order['customer']['address']['region'])
|
|
||||||
? $order['customer']['address']['region']
|
|
||||||
: $region
|
|
||||||
;
|
|
||||||
|
|
||||||
$data['shipping_country_id'] = $order['delivery']['address']['country'];
|
|
||||||
$data['shipping_zone_id'] = $region;
|
|
||||||
|
|
||||||
$data['shipping_address'] = '0';
|
|
||||||
$data['shipping_firstname'] = $order['customer']['firstName'];
|
|
||||||
$data['shipping_lastname'] = (isset($order['customer']['lastName']))
|
|
||||||
? $order['customer']['lastName']
|
|
||||||
: ' '
|
|
||||||
;
|
|
||||||
$data['shipping_address_1'] = $order['delivery']['address']['text'];
|
|
||||||
$data['shipping_address_2'] = '';
|
|
||||||
$data['shipping_company'] = '';
|
|
||||||
$data['shipping_company_id'] = '';
|
|
||||||
$data['shipping_city'] = $order['delivery']['address']['city'];
|
|
||||||
$data['shipping_postcode'] = $order['delivery']['address']['index'];
|
|
||||||
|
|
||||||
$data['shipping'] = $delivery[$order['delivery']['code']];
|
|
||||||
$data['shipping_method'] = $ocDelivery[$data['shipping']];
|
|
||||||
$data['shipping_code'] = $delivery[$order['delivery']['code']];
|
|
||||||
$data['payment'] = $payment[$order['paymentType']];
|
|
||||||
$data['payment_method'] = $ocPayment[$data['payment']];
|
|
||||||
$data['payment_code'] = $payment[$order['paymentType']];
|
|
||||||
|
|
||||||
// this data will not retrive from crm for now
|
|
||||||
$data['tax'] = '';
|
|
||||||
$data['tax_id'] = '';
|
|
||||||
$data['product'] = '';
|
|
||||||
$data['product_id'] = '';
|
|
||||||
$data['reward'] = '';
|
|
||||||
$data['affiliate'] = '';
|
|
||||||
$data['affiliate_id'] = '';
|
|
||||||
$data['payment_tax_id'] = '';
|
|
||||||
$data['order_product_id'] = '';
|
|
||||||
$data['payment_company'] = '';
|
|
||||||
$data['payment_company_id'] = '';
|
|
||||||
$data['company'] = '';
|
|
||||||
$data['company_id'] = '';
|
|
||||||
|
|
||||||
$data['order_product'] = array();
|
|
||||||
|
|
||||||
foreach($order['items'] as $item) {
|
|
||||||
$p = $this->model_catalog_product->getProduct($item['offer']['externalId']);
|
|
||||||
$data['order_product'][] = array(
|
|
||||||
'product_id' => $item['offer']['externalId'],
|
|
||||||
'name' => $item['offer']['name'],
|
|
||||||
'quantity' => $item['quantity'],
|
|
||||||
'price' => $item['initialPrice'],
|
|
||||||
'total' => $item['initialPrice'] * $item['quantity'],
|
|
||||||
'model' => $p['model'],
|
|
||||||
|
|
||||||
// this data will not retrive from crm
|
|
||||||
'order_product_id' => '',
|
|
||||||
'tax' => 0,
|
|
||||||
'reward' => 0
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$deliveryCost = isset($order['delivery']['cost']) ? $order['delivery']['cost'] : 0;
|
|
||||||
|
|
||||||
$data['order_total'] = array(
|
|
||||||
array(
|
|
||||||
'order_total_id' => '',
|
|
||||||
'code' => 'sub_total',
|
|
||||||
'title' => $this->language->get('product_summ'),
|
|
||||||
'value' => $order['summ'],
|
|
||||||
'text' => $order['summ'],
|
|
||||||
'sort_order' => $subtotalSettings['sub_total_sort_order']
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'order_total_id' => '',
|
|
||||||
'code' => 'shipping',
|
|
||||||
'title' => $ocDelivery[$data['shipping_code']],
|
|
||||||
'value' => $deliveryCost,
|
|
||||||
'text' => $deliveryCost,
|
|
||||||
'sort_order' => $shippingSettings['shipping_sort_order']
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'order_total_id' => '',
|
|
||||||
'code' => 'total',
|
|
||||||
'title' => $this->language->get('column_total'),
|
|
||||||
'value' => isset($order['totalSumm'])
|
|
||||||
? $order['totalSumm']
|
|
||||||
: $order['summ'] + $deliveryCost
|
|
||||||
,
|
|
||||||
'text' => isset($order['totalSumm'])
|
|
||||||
? $order['totalSumm']
|
|
||||||
: $order['summ'] + $deliveryCost
|
|
||||||
,
|
|
||||||
'sort_order' => $totalSettings['total_sort_order']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$data['fromApi'] = true;
|
|
||||||
|
|
||||||
if (isset($order['externalId'])) {
|
|
||||||
if(array_key_exists($order['status'], $status)) {
|
|
||||||
$data['order_status_id'] = $status[$order['status']];
|
|
||||||
} else {
|
|
||||||
$tmpOrder = $this->model_sale_order->getOrder($order['externalId']);
|
|
||||||
$data['order_status_id'] = $tmpOrder['order_status_id'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->model_sale_order->editOrder($order['externalId'], $data);
|
|
||||||
} else {
|
|
||||||
$data['order_status_id'] = 1;
|
|
||||||
$this->model_sale_order->addOrder($data);
|
|
||||||
$last = $this->model_sale_order->getOrders($data = array('order' => 'DESC', 'limit' => 1));
|
|
||||||
$ordersIdsFix[] = array('id' => $order['id'], 'externalId' => (int) $last[0]['order_id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($customersIdsFix)) {
|
|
||||||
$crm->customerFixExternalIds($customersIdsFix);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($ordersIdsFix)) {
|
|
||||||
$crm->orderFixExternalIds($ordersIdsFix);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$this->log->addNotice(
|
|
||||||
'['.
|
|
||||||
$this->config->get('store_name').
|
|
||||||
'] RestApi::orderHistory: you need to configure retailcrm module first.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function icml()
|
public function icml()
|
||||||
|
@ -27,4 +27,4 @@ $_['size'] = 'Size';
|
|||||||
|
|
||||||
// Errors
|
// Errors
|
||||||
$_['error_permission'] = 'Warning! You do not have permission to modify module';
|
$_['error_permission'] = 'Warning! You do not have permission to modify module';
|
||||||
?>
|
|
||||||
|
@ -27,4 +27,4 @@ $_['size'] = 'Размер';
|
|||||||
|
|
||||||
// Errors
|
// Errors
|
||||||
$_['error_permission'] = 'У вас недостаточно прав на изменение настроек модуля';
|
$_['error_permission'] = 'У вас недостаточно прав на изменение настроек модуля';
|
||||||
?>
|
|
||||||
|
@ -1,3 +1,306 @@
|
|||||||
<?php
|
<?php
|
||||||
$cli_action = 'module/retailcrm/history';
|
|
||||||
require_once('dispatch.php');
|
class ModelRetailcrmHistory extends Model
|
||||||
|
{
|
||||||
|
public function request()
|
||||||
|
{
|
||||||
|
$this->load->model('setting/setting');
|
||||||
|
$this->load->model('setting/store');
|
||||||
|
$this->load->model('sale/order');
|
||||||
|
$this->load->model('sale/customer');
|
||||||
|
$this->load->model('retailcrm/tools');
|
||||||
|
$this->load->model('catalog/product');
|
||||||
|
$this->load->model('localisation/zone');
|
||||||
|
|
||||||
|
$this->load->language('module/retailcrm');
|
||||||
|
|
||||||
|
$settings = $this->model_setting_setting->getSetting('retailcrm');
|
||||||
|
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
|
||||||
|
|
||||||
|
if (!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
|
||||||
|
$crm = new ApiHelper($settings);
|
||||||
|
$orders = $crm->ordersHistory();
|
||||||
|
$ordersIdsFix = array();
|
||||||
|
$customersIdsFix = array();
|
||||||
|
$subtotalSettings = $this->model_setting_setting->getSetting('sub_total');
|
||||||
|
$totalSettings = $this->model_setting_setting->getSetting('total');
|
||||||
|
$shippingSettings = $this->model_setting_setting->getSetting('shipping');
|
||||||
|
|
||||||
|
$delivery = array_flip($settings['retailcrm_delivery']);
|
||||||
|
$payment = array_flip($settings['retailcrm_payment']);
|
||||||
|
$status = array_flip($settings['retailcrm_status']);
|
||||||
|
|
||||||
|
$ocPayment = $this->model_retailcrm_tools->getOpercartPaymentTypes();
|
||||||
|
$ocDelivery = $this->model_retailcrm_tools->getOpercartDeliveryMethods();
|
||||||
|
|
||||||
|
$zones = $this->model_localisation_zone->getZones();
|
||||||
|
|
||||||
|
foreach ($orders as $order) {
|
||||||
|
|
||||||
|
if (!isset($order['deleted']) || !$order['deleted']) {
|
||||||
|
|
||||||
|
$data = array();
|
||||||
|
|
||||||
|
$customer_id = (isset($order['customer']['externalId']) && $order['customer']['externalId'] != 0)
|
||||||
|
? $order['customer']['externalId']
|
||||||
|
: ''
|
||||||
|
;
|
||||||
|
|
||||||
|
if (isset($order['externalId'])) {
|
||||||
|
/*
|
||||||
|
* opercart developers believe that to remove all
|
||||||
|
* products from the order during the editing is a good
|
||||||
|
* idea...
|
||||||
|
*
|
||||||
|
* so we have to get order data from crm
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
$order = $crm->getOrder($order['externalId']);
|
||||||
|
} else {
|
||||||
|
if ($customer_id == '') {
|
||||||
|
$cData = array(
|
||||||
|
'customer_group_id' => '1',
|
||||||
|
'firstname' => $order['customer']['firstName'],
|
||||||
|
'lastname' => (isset($order['customer']['lastName']))
|
||||||
|
? $order['customer']['lastName']
|
||||||
|
: ' '
|
||||||
|
,
|
||||||
|
'email' => $order['customer']['email'],
|
||||||
|
'telephone' => (isset($order['customer']['phones'][0]['number']))
|
||||||
|
? $order['customer']['phones'][0]['number']
|
||||||
|
: ' '
|
||||||
|
,
|
||||||
|
'newsletter' => 0,
|
||||||
|
'password' => 'tmppass',
|
||||||
|
'status' => 1,
|
||||||
|
'address' => array(
|
||||||
|
'firstname' => $order['customer']['firstName'],
|
||||||
|
'lastname' => (isset($order['customer']['lastName']))
|
||||||
|
? $order['customer']['lastName']
|
||||||
|
: ' '
|
||||||
|
,
|
||||||
|
'address_1' => $order['customer']['address']['text'],
|
||||||
|
'city' => isset($order['customer']['address']['city'])
|
||||||
|
? $order['customer']['address']['city']
|
||||||
|
: $order['delivery']['address']['city']
|
||||||
|
,
|
||||||
|
'postcode' => isset($order['customer']['address']['index'])
|
||||||
|
? $order['customer']['address']['index']
|
||||||
|
: $order['delivery']['address']['index']
|
||||||
|
,
|
||||||
|
),
|
||||||
|
'tax_id' => '',
|
||||||
|
'zone_id' => '',
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->model_sale_customer->addCustomer($cData);
|
||||||
|
|
||||||
|
if (isset($order['customer']['email']) && $order['customer']['email'] != '') {
|
||||||
|
$tryToFind = $this->model_sale_customer->getCustomerByEmail($order['customer']['email']);
|
||||||
|
$customer_id = $tryToFind['customer_id'];
|
||||||
|
} else {
|
||||||
|
$last = $this->model_sale_customer->getCustomers(
|
||||||
|
$data = array('order' => 'DESC', 'limit' => 1)
|
||||||
|
);
|
||||||
|
$customer_id = $last[0]['customer_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$customersIdsFix[] = array('id' => $order['customer']['id'], 'externalId' => (int)$customer_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Build order data
|
||||||
|
*/
|
||||||
|
|
||||||
|
$data['store_id'] = ($this->config->get('config_store_id') == null)
|
||||||
|
? 0
|
||||||
|
: $this->config->get('config_store_id')
|
||||||
|
;
|
||||||
|
$data['customer'] = $order['customer']['firstName'];
|
||||||
|
$data['customer_id'] = $customer_id;
|
||||||
|
$data['customer_group_id'] = 1;
|
||||||
|
$data['firstname'] = $order['firstName'];
|
||||||
|
$data['lastname'] = (isset($order['lastName'])) ? $order['lastName'] : ' ';
|
||||||
|
$data['email'] = $order['customer']['email'];
|
||||||
|
$data['telephone'] = (isset($order['customer']['phones'][0]['number']))
|
||||||
|
? $order['customer']['phones'][0]['number']
|
||||||
|
: ' '
|
||||||
|
;
|
||||||
|
|
||||||
|
$data['comment'] = isset($order['customerComment']) ? $order['customerComment'] : '';
|
||||||
|
$data['fax'] = '';
|
||||||
|
|
||||||
|
$data['payment_address'] = '0';
|
||||||
|
$data['payment_firstname'] = $order['firstName'];
|
||||||
|
$data['payment_lastname'] = (isset($order['lastName'])) ? $order['lastName'] : ' ';
|
||||||
|
$data['payment_address_1'] = $order['customer']['address']['text'];
|
||||||
|
$data['payment_address_2'] = '';
|
||||||
|
$data['payment_company'] = '';
|
||||||
|
$data['payment_company_id'] = '';
|
||||||
|
$data['payment_city'] = isset($order['customer']['address']['city'])
|
||||||
|
? $order['customer']['address']['city']
|
||||||
|
: $order['delivery']['address']['city']
|
||||||
|
;
|
||||||
|
$data['payment_postcode'] = isset($order['customer']['address']['index'])
|
||||||
|
? $order['customer']['address']['index']
|
||||||
|
: $order['delivery']['address']['index']
|
||||||
|
;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Country & zone id detection
|
||||||
|
*/
|
||||||
|
|
||||||
|
$country = 0;
|
||||||
|
$region = '';
|
||||||
|
|
||||||
|
if(is_int($order['delivery']['address']['region'])) {
|
||||||
|
$region = $order['delivery']['address']['region'];
|
||||||
|
} else {
|
||||||
|
foreach($zones as $zone) {
|
||||||
|
if($order['delivery']['address']['region'] == $zone['name']) {
|
||||||
|
$region = $zone['zone_id'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$data['payment_country_id'] = isset($order['customer']['address']['country'])
|
||||||
|
? $order['customer']['address']['country']
|
||||||
|
: $order['delivery']['address']['country']
|
||||||
|
;
|
||||||
|
$data['payment_zone_id'] = isset($order['customer']['address']['region'])
|
||||||
|
? $order['customer']['address']['region']
|
||||||
|
: $region
|
||||||
|
;
|
||||||
|
|
||||||
|
$data['shipping_country_id'] = $order['delivery']['address']['country'];
|
||||||
|
$data['shipping_zone_id'] = $region;
|
||||||
|
|
||||||
|
$data['shipping_address'] = '0';
|
||||||
|
$data['shipping_firstname'] = $order['customer']['firstName'];
|
||||||
|
$data['shipping_lastname'] = (isset($order['customer']['lastName']))
|
||||||
|
? $order['customer']['lastName']
|
||||||
|
: ' '
|
||||||
|
;
|
||||||
|
$data['shipping_address_1'] = $order['delivery']['address']['text'];
|
||||||
|
$data['shipping_address_2'] = '';
|
||||||
|
$data['shipping_company'] = '';
|
||||||
|
$data['shipping_company_id'] = '';
|
||||||
|
$data['shipping_city'] = $order['delivery']['address']['city'];
|
||||||
|
$data['shipping_postcode'] = $order['delivery']['address']['index'];
|
||||||
|
|
||||||
|
$data['shipping'] = $delivery[$order['delivery']['code']];
|
||||||
|
$data['shipping_method'] = $ocDelivery[$data['shipping']];
|
||||||
|
$data['shipping_code'] = $delivery[$order['delivery']['code']];
|
||||||
|
$data['payment'] = $payment[$order['paymentType']];
|
||||||
|
$data['payment_method'] = $ocPayment[$data['payment']];
|
||||||
|
$data['payment_code'] = $payment[$order['paymentType']];
|
||||||
|
|
||||||
|
// this data will not retrive from crm for now
|
||||||
|
$data['tax'] = '';
|
||||||
|
$data['tax_id'] = '';
|
||||||
|
$data['product'] = '';
|
||||||
|
$data['product_id'] = '';
|
||||||
|
$data['reward'] = '';
|
||||||
|
$data['affiliate'] = '';
|
||||||
|
$data['affiliate_id'] = '';
|
||||||
|
$data['payment_tax_id'] = '';
|
||||||
|
$data['order_product_id'] = '';
|
||||||
|
$data['payment_company'] = '';
|
||||||
|
$data['payment_company_id'] = '';
|
||||||
|
$data['company'] = '';
|
||||||
|
$data['company_id'] = '';
|
||||||
|
|
||||||
|
$data['order_product'] = array();
|
||||||
|
|
||||||
|
foreach($order['items'] as $item) {
|
||||||
|
$p = $this->model_catalog_product->getProduct($item['offer']['externalId']);
|
||||||
|
$data['order_product'][] = array(
|
||||||
|
'product_id' => $item['offer']['externalId'],
|
||||||
|
'name' => $item['offer']['name'],
|
||||||
|
'quantity' => $item['quantity'],
|
||||||
|
'price' => $item['initialPrice'],
|
||||||
|
'total' => $item['initialPrice'] * $item['quantity'],
|
||||||
|
'model' => $p['model'],
|
||||||
|
|
||||||
|
// this data will not retrive from crm
|
||||||
|
'order_product_id' => '',
|
||||||
|
'tax' => 0,
|
||||||
|
'reward' => 0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$deliveryCost = isset($order['delivery']['cost']) ? $order['delivery']['cost'] : 0;
|
||||||
|
|
||||||
|
$data['order_total'] = array(
|
||||||
|
array(
|
||||||
|
'order_total_id' => '',
|
||||||
|
'code' => 'sub_total',
|
||||||
|
'title' => $this->language->get('product_summ'),
|
||||||
|
'value' => $order['summ'],
|
||||||
|
'text' => $order['summ'],
|
||||||
|
'sort_order' => $subtotalSettings['sub_total_sort_order']
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'order_total_id' => '',
|
||||||
|
'code' => 'shipping',
|
||||||
|
'title' => $ocDelivery[$data['shipping_code']],
|
||||||
|
'value' => $deliveryCost,
|
||||||
|
'text' => $deliveryCost,
|
||||||
|
'sort_order' => $shippingSettings['shipping_sort_order']
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'order_total_id' => '',
|
||||||
|
'code' => 'total',
|
||||||
|
'title' => $this->language->get('column_total'),
|
||||||
|
'value' => isset($order['totalSumm'])
|
||||||
|
? $order['totalSumm']
|
||||||
|
: $order['summ'] + $deliveryCost
|
||||||
|
,
|
||||||
|
'text' => isset($order['totalSumm'])
|
||||||
|
? $order['totalSumm']
|
||||||
|
: $order['summ'] + $deliveryCost
|
||||||
|
,
|
||||||
|
'sort_order' => $totalSettings['total_sort_order']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$data['fromApi'] = true;
|
||||||
|
|
||||||
|
if (isset($order['externalId'])) {
|
||||||
|
if(array_key_exists($order['status'], $status)) {
|
||||||
|
$data['order_status_id'] = $status[$order['status']];
|
||||||
|
} else {
|
||||||
|
$tmpOrder = $this->model_sale_order->getOrder($order['externalId']);
|
||||||
|
$data['order_status_id'] = $tmpOrder['order_status_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->model_sale_order->editOrder($order['externalId'], $data);
|
||||||
|
} else {
|
||||||
|
$data['order_status_id'] = 1;
|
||||||
|
$this->model_sale_order->addOrder($data);
|
||||||
|
$last = $this->model_sale_order->getOrders($data = array('order' => 'DESC', 'limit' => 1));
|
||||||
|
$ordersIdsFix[] = array('id' => $order['id'], 'externalId' => (int) $last[0]['order_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($customersIdsFix)) {
|
||||||
|
$crm->customerFixExternalIds($customersIdsFix);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($ordersIdsFix)) {
|
||||||
|
$crm->orderFixExternalIds($ordersIdsFix);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$this->log->addNotice(
|
||||||
|
'['.
|
||||||
|
$this->config->get('store_name').
|
||||||
|
'] RestApi::orderHistory: you need to configure retailcrm module first.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class ModelRetailcrmIcml extends Model {
|
class ModelRetailcrmIcml extends Model
|
||||||
|
{
|
||||||
protected $shop;
|
protected $shop;
|
||||||
protected $file;
|
protected $file;
|
||||||
protected $properties;
|
protected $properties;
|
||||||
@ -55,6 +56,9 @@ class ModelRetailcrmIcml extends Model {
|
|||||||
$this->dd->save($downloadPath . 'retailcrm.xml');
|
$this->dd->save($downloadPath . 'retailcrm.xml');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
private function addCategories()
|
private function addCategories()
|
||||||
{
|
{
|
||||||
$categories = $this->model_catalog_category->getCategories(array());
|
$categories = $this->model_catalog_category->getCategories(array());
|
||||||
|
@ -7,11 +7,10 @@ class ModelRetailcrmOrder extends Model {
|
|||||||
$settings = $this->model_setting_setting->getSetting('retailcrm');
|
$settings = $this->model_setting_setting->getSetting('retailcrm');
|
||||||
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
|
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
|
||||||
|
|
||||||
if(
|
$status = $this->model_setting_setting->getSetting('retailcrm');
|
||||||
!empty($settings['retailcrm_url'])
|
$order['order_status'] = $status['retailcrm_status'][$order['order_status_id']];
|
||||||
&&
|
|
||||||
!empty($settings['retailcrm_apikey'])
|
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
|
||||||
) {
|
|
||||||
require_once DIR_SYSTEM . 'library/retailcrm.php';
|
require_once DIR_SYSTEM . 'library/retailcrm.php';
|
||||||
$order['order_id'] = $order_id;
|
$order['order_id'] = $order_id;
|
||||||
$crm = new ApiHelper($settings);
|
$crm = new ApiHelper($settings);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class ModelRetailcrmReferences extends Model {
|
class ModelRetailcrmReferences extends Model
|
||||||
|
{
|
||||||
|
|
||||||
public function getOpercartDeliveryMethods()
|
public function getOpercartDeliveryMethods()
|
||||||
{
|
{
|
||||||
|
@ -12,7 +12,7 @@ if (!isset($cli_action)) {
|
|||||||
define('VERSION', '1.5.6');
|
define('VERSION', '1.5.6');
|
||||||
|
|
||||||
// Configuration (note we're using the admin config)
|
// Configuration (note we're using the admin config)
|
||||||
require_once(realpath(dirname(__FILE__)) . '/../../config.php');
|
require_once(realpath(dirname(__FILE__)) . '/../../admin/config.php');
|
||||||
|
|
||||||
// Configuration check
|
// Configuration check
|
||||||
if (!defined('DIR_APPLICATION')) {
|
if (!defined('DIR_APPLICATION')) {
|
3
system/cron/history.php
Normal file
3
system/cron/history.php
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
$cli_action = 'module/retailcrm/history';
|
||||||
|
require_once('dispatch.php');
|
Loading…
x
Reference in New Issue
Block a user