2014-08-15 13:35:22 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../../system/library/intarocrm/vendor/autoload.php';
|
|
|
|
|
|
|
|
class ControllerModuleIntarocrm extends Controller {
|
|
|
|
private $error = array();
|
2014-08-28 02:13:37 +04:00
|
|
|
protected $log, $statuses, $payments, $deliveryTypes;
|
2014-08-15 13:35:22 +04:00
|
|
|
|
|
|
|
public function install() {
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->model_setting_setting->editSetting('intarocrm', array('intarocrm_status'=>1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uninstall() {
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->model_setting_setting->editSetting('intarocrm', array('intarocrm_status'=>0));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
|
|
|
$this->log = new Monolog\Logger('opencart-module');
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->pushHandler(
|
|
|
|
new Monolog\Handler\StreamHandler(DIR_LOGS . 'intarocrm_module.log', Monolog\Logger::INFO)
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->load->model('setting/extension');
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->load->model('intarocrm/tools');
|
2014-08-15 13:35:22 +04:00
|
|
|
$this->load->language('module/intarocrm');
|
|
|
|
$this->document->setTitle($this->language->get('heading_title'));
|
|
|
|
$this->document->addStyle('/admin/view/stylesheet/intarocrm.css');
|
|
|
|
|
|
|
|
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
|
|
|
|
$this->model_setting_setting->editSetting('intarocrm', $this->request->post);
|
|
|
|
$this->session->data['success'] = $this->language->get('text_success');
|
|
|
|
$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$text_strings = array(
|
|
|
|
'heading_title',
|
|
|
|
'text_enabled',
|
|
|
|
'text_disabled',
|
|
|
|
'button_save',
|
|
|
|
'button_cancel',
|
|
|
|
'text_notice',
|
|
|
|
'intarocrm_url',
|
|
|
|
'intarocrm_apikey',
|
|
|
|
'intarocrm_base_settings',
|
|
|
|
'intarocrm_dict_settings',
|
|
|
|
'intarocrm_dict_delivery',
|
|
|
|
'intarocrm_dict_status',
|
|
|
|
'intarocrm_dict_payment',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($text_strings as $text) {
|
|
|
|
$this->data[$text] = $this->language->get($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['intarocrm_errors'] = array();
|
|
|
|
$this->data['saved_settings'] = $this->model_setting_setting->getSetting('intarocrm');
|
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
if ($this->data['saved_settings']['intarocrm_url'] != '' &&
|
|
|
|
$this->data['saved_settings']['intarocrm_apikey'] != ''
|
|
|
|
) {
|
2014-08-15 13:35:22 +04:00
|
|
|
|
|
|
|
$this->intarocrm = new \IntaroCrm\RestApi(
|
|
|
|
$this->data['saved_settings']['intarocrm_url'],
|
|
|
|
$this->data['saved_settings']['intarocrm_apikey']
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delivery
|
|
|
|
*/
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->deliveryTypes = $this->intarocrm->deliveryTypesList();
|
|
|
|
}
|
2014-08-28 02:13:37 +04:00
|
|
|
catch (IntaroCrm\Exception\ApiException $e)
|
2014-08-15 13:35:22 +04:00
|
|
|
{
|
|
|
|
$this->data['intarocrm_error'][] = $e->getMessage();
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addError(
|
|
|
|
'[' .
|
|
|
|
$this->config->get('store_name') .
|
|
|
|
'] RestApi::deliveryTypesList::Api:' . $e->getMessage()
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
2014-08-28 02:13:37 +04:00
|
|
|
catch (IntaroCrm\Exception\CurlException $e)
|
2014-08-15 13:35:22 +04:00
|
|
|
{
|
|
|
|
$this->data['intarocrm_error'][] = $e->getMessage();
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addError(
|
|
|
|
'[' . $this->config->get('store_name') .
|
|
|
|
'] RestApi::deliveryTypesList::Curl:' . $e->getMessage()
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['delivery'] = array(
|
2014-08-28 02:13:37 +04:00
|
|
|
'opencart' => $this->model_intarocrm_tools->getOpercartDeliveryMethods(),
|
2014-08-15 13:35:22 +04:00
|
|
|
'intarocrm' => $this->deliveryTypes
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Statuses
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
$this->statuses = $this->intarocrm->orderStatusesList();
|
|
|
|
}
|
2014-08-28 02:13:37 +04:00
|
|
|
catch (IntaroCrm\Exception\ApiException $e)
|
2014-08-15 13:35:22 +04:00
|
|
|
{
|
|
|
|
$this->data['intarocrm_error'][] = $e->getMessage();
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addError(
|
|
|
|
'[' .
|
|
|
|
$this->config->get('store_name') .
|
|
|
|
'] RestApi::orderStatusesList::Api:' . $e->getMessage()
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
2014-08-28 02:13:37 +04:00
|
|
|
catch (IntaroCrm\Exception\CurlException $e)
|
2014-08-15 13:35:22 +04:00
|
|
|
{
|
|
|
|
$this->data['intarocrm_error'][] = $e->getMessage();
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addError(
|
|
|
|
'[' .
|
|
|
|
$this->config->get('store_name') .
|
|
|
|
'] RestApi::orderStatusesList::Curl:' . $e->getMessage()
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['statuses'] = array(
|
2014-08-28 02:13:37 +04:00
|
|
|
'opencart' => $this->model_intarocrm_tools->getOpercartOrderStatuses(),
|
2014-08-15 13:35:22 +04:00
|
|
|
'intarocrm' => $this->statuses
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Payment
|
|
|
|
*/
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->payments = $this->intarocrm->paymentTypesList();
|
|
|
|
}
|
2014-08-28 02:13:37 +04:00
|
|
|
catch (IntaroCrm\Exception\ApiException $e)
|
2014-08-15 13:35:22 +04:00
|
|
|
{
|
|
|
|
$this->data['intarocrm_error'][] = $e->getMessage();
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addError(
|
|
|
|
'[' .
|
|
|
|
$this->config->get('store_name') .
|
|
|
|
'] RestApi::paymentTypesList::Api:' . $e->getMessage()
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
2014-08-28 02:13:37 +04:00
|
|
|
catch (IntaroCrm\Exception\CurlException $e)
|
2014-08-15 13:35:22 +04:00
|
|
|
{
|
|
|
|
$this->data['intarocrm_error'][] = $e->getMessage();
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addError(
|
|
|
|
'[' .
|
|
|
|
$this->config->get('store_name') .
|
|
|
|
'] RestApi::paymentTypesList::Curl:' . $e->getMessage()
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['payments'] = array(
|
2014-08-28 02:13:37 +04:00
|
|
|
'opencart' => $this->model_intarocrm_tools->getOpercartPaymentTypes(),
|
2014-08-15 13:35:22 +04:00
|
|
|
'intarocrm' => $this->payments
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$config_data = array(
|
|
|
|
'intarocrm_status'
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($config_data as $conf) {
|
|
|
|
if (isset($this->request->post[$conf])) {
|
|
|
|
$this->data[$conf] = $this->request->post[$conf];
|
|
|
|
} else {
|
|
|
|
$this->data[$conf] = $this->config->get($conf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->error['warning'])) {
|
|
|
|
$this->data['error_warning'] = $this->error['warning'];
|
|
|
|
} else {
|
|
|
|
$this->data['error_warning'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['breadcrumbs'] = array();
|
|
|
|
|
|
|
|
$this->data['breadcrumbs'][] = array(
|
|
|
|
'text' => $this->language->get('text_home'),
|
|
|
|
'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
|
|
|
|
'separator' => false
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->data['breadcrumbs'][] = array(
|
|
|
|
'text' => $this->language->get('text_module'),
|
|
|
|
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
|
|
|
|
'separator' => ' :: '
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->data['breadcrumbs'][] = array(
|
|
|
|
'text' => $this->language->get('heading_title'),
|
|
|
|
'href' => $this->url->link('module/intarocrm', 'token=' . $this->session->data['token'], 'SSL'),
|
|
|
|
'separator' => ' :: '
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->data['action'] = $this->url->link('module/intarocrm', 'token=' . $this->session->data['token'], 'SSL');
|
|
|
|
|
|
|
|
$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
|
|
|
|
|
|
|
|
|
|
|
|
$this->data['modules'] = array();
|
|
|
|
|
|
|
|
if (isset($this->request->post['intarocrm_module'])) {
|
|
|
|
$this->data['modules'] = $this->request->post['intarocrm_module'];
|
|
|
|
} elseif ($this->config->get('intarocrm_module')) {
|
|
|
|
$this->data['modules'] = $this->config->get('intarocrm_module');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->model('design/layout');
|
|
|
|
|
|
|
|
$this->data['layouts'] = $this->model_design_layout->getLayouts();
|
|
|
|
|
|
|
|
$this->template = 'module/intarocrm.tpl';
|
|
|
|
$this->children = array(
|
|
|
|
'common/header',
|
|
|
|
'common/footer',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->response->setOutput($this->render());
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:14:04 +04:00
|
|
|
public function order_history()
|
|
|
|
{
|
|
|
|
$this->log = new Monolog\Logger('opencart-module');
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->pushHandler(
|
|
|
|
new Monolog\Handler\StreamHandler(DIR_LOGS . 'intarocrm_module.log', Monolog\Logger::INFO)
|
|
|
|
);
|
2014-08-18 18:14:04 +04:00
|
|
|
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->load->model('setting/store');
|
|
|
|
$this->load->model('sale/order');
|
2014-08-19 07:56:17 +04:00
|
|
|
$this->load->model('sale/customer');
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->load->model('intarocrm/tools');
|
2014-08-29 19:32:23 +04:00
|
|
|
$this->load->model('catalog/product');
|
2014-09-03 13:03:25 +04:00
|
|
|
$this->load->model('localisation/zone');
|
2014-08-29 19:32:23 +04:00
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$this->load->language('module/intarocrm');
|
2014-08-19 07:56:17 +04:00
|
|
|
|
2014-08-18 18:14:04 +04:00
|
|
|
$settings = $this->model_setting_setting->getSetting('intarocrm');
|
|
|
|
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
|
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
if (isset($settings['intarocrm_url']) &&
|
|
|
|
$settings['intarocrm_url'] != '' &&
|
|
|
|
isset($settings['intarocrm_apikey']) &&
|
|
|
|
$settings['intarocrm_apikey'] != ''
|
|
|
|
) {
|
2014-08-18 18:14:04 +04:00
|
|
|
include_once __DIR__ . '/../../../system/library/intarocrm/apihelper.php';
|
|
|
|
$crm = new ApiHelper($settings);
|
|
|
|
$orders = $crm->orderHistory();
|
2014-08-19 07:56:17 +04:00
|
|
|
$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');
|
2014-08-18 18:14:04 +04:00
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$delivery = array_flip($settings['intarocrm_delivery']);
|
|
|
|
$payment = array_flip($settings['intarocrm_payment']);
|
|
|
|
$status = array_flip($settings['intarocrm_status']);
|
|
|
|
|
|
|
|
$ocPayment = $this->model_intarocrm_tools->getOpercartPaymentTypes();
|
|
|
|
$ocDelivery = $this->model_intarocrm_tools->getOpercartDeliveryMethods();
|
|
|
|
|
|
|
|
$zones = $this->model_localisation_zone->getZones();
|
|
|
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
2014-08-19 07:56:17 +04:00
|
|
|
if (!isset($order['deleted']) || !$order['deleted']) {
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
2014-08-29 19:32:23 +04:00
|
|
|
$customer_id = (isset($order['customer']['externalId']) && $order['customer']['externalId'] != 0)
|
2014-08-19 07:56:17 +04:00
|
|
|
? $order['customer']['externalId']
|
|
|
|
: ''
|
|
|
|
;
|
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
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',
|
2014-08-19 07:56:17 +04:00
|
|
|
'firstname' => $order['customer']['firstName'],
|
2014-08-28 02:13:37 +04:00
|
|
|
'lastname' => (isset($order['customer']['lastName']))
|
|
|
|
? $order['customer']['lastName']
|
|
|
|
: ' '
|
|
|
|
,
|
2014-09-03 13:03:25 +04:00
|
|
|
'email' => $order['customer']['email'],
|
|
|
|
'telephone' => (isset($order['customer']['phones'][0]['number']))
|
|
|
|
? $order['customer']['phones'][0]['number']
|
|
|
|
: ' '
|
2014-08-29 19:32:23 +04:00
|
|
|
,
|
2014-09-03 13:03:25 +04:00
|
|
|
'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' => '',
|
|
|
|
);
|
2014-08-19 07:56:17 +04:00
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$this->model_sale_customer->addCustomer($cData);
|
2014-08-19 07:56:17 +04:00
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
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'];
|
|
|
|
}
|
2014-08-19 07:56:17 +04:00
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$customersIdsFix[] = array('id' => $order['customer']['id'], 'externalId' => (int)$customer_id);
|
|
|
|
}
|
2014-08-19 07:56:17 +04:00
|
|
|
}
|
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
/*
|
|
|
|
* Build order data
|
|
|
|
*/
|
2014-08-19 07:56:17 +04:00
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
$data['store_id'] = ($this->config->get('config_store_id') == null)
|
|
|
|
? 0
|
|
|
|
: $this->config->get('config_store_id')
|
|
|
|
;
|
2014-08-19 07:56:17 +04:00
|
|
|
$data['customer'] = $order['customer']['firstName'];
|
|
|
|
$data['customer_id'] = $customer_id;
|
2014-08-29 19:32:23 +04:00
|
|
|
$data['customer_group_id'] = 1;
|
|
|
|
$data['firstname'] = $order['firstName'];
|
|
|
|
$data['lastname'] = (isset($order['lastName'])) ? $order['lastName'] : ' ';
|
2014-08-19 07:56:17 +04:00
|
|
|
$data['email'] = $order['customer']['email'];
|
2014-08-28 02:13:37 +04:00
|
|
|
$data['telephone'] = (isset($order['customer']['phones'][0]['number']))
|
|
|
|
? $order['customer']['phones'][0]['number']
|
|
|
|
: ' '
|
|
|
|
;
|
2014-08-29 19:32:23 +04:00
|
|
|
|
|
|
|
$data['comment'] = isset($order['customerComment']) ? $order['customerComment'] : '';
|
|
|
|
$data['fax'] = '';
|
2014-08-19 07:56:17 +04:00
|
|
|
|
|
|
|
$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'];
|
2014-08-29 19:32:23 +04:00
|
|
|
$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']
|
|
|
|
;
|
2014-08-19 07:56:17 +04:00
|
|
|
|
|
|
|
/*
|
2014-09-03 13:03:25 +04:00
|
|
|
* Country & zone id detection
|
2014-08-19 07:56:17 +04:00
|
|
|
*/
|
2014-08-29 19:32:23 +04:00
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$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;
|
2014-08-19 07:56:17 +04:00
|
|
|
|
|
|
|
$data['shipping_address'] = '0';
|
|
|
|
$data['shipping_firstname'] = $order['customer']['firstName'];
|
2014-08-28 02:13:37 +04:00
|
|
|
$data['shipping_lastname'] = (isset($order['customer']['lastName']))
|
|
|
|
? $order['customer']['lastName']
|
|
|
|
: ' '
|
|
|
|
;
|
2014-08-19 07:56:17 +04:00
|
|
|
$data['shipping_address_1'] = $order['delivery']['address']['text'];
|
2014-08-29 19:32:23 +04:00
|
|
|
$data['shipping_address_2'] = '';
|
|
|
|
$data['shipping_company'] = '';
|
|
|
|
$data['shipping_company_id'] = '';
|
2014-08-19 07:56:17 +04:00
|
|
|
$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']];
|
2014-08-29 19:32:23 +04:00
|
|
|
|
|
|
|
// 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'] = '';
|
|
|
|
|
2014-08-19 07:56:17 +04:00
|
|
|
$data['order_product'] = array();
|
|
|
|
|
|
|
|
foreach($order['items'] as $item) {
|
2014-08-29 19:32:23 +04:00
|
|
|
$p = $this->model_catalog_product->getProduct($item['offer']['externalId']);
|
2014-08-19 07:56:17 +04:00
|
|
|
$data['order_product'][] = array(
|
|
|
|
'product_id' => $item['offer']['externalId'],
|
|
|
|
'name' => $item['offer']['name'],
|
|
|
|
'quantity' => $item['quantity'],
|
|
|
|
'price' => $item['initialPrice'],
|
|
|
|
'total' => $item['initialPrice'] * $item['quantity'],
|
2014-08-29 19:32:23 +04:00
|
|
|
'model' => $p['model'],
|
|
|
|
|
|
|
|
// this data will not retrive from crm
|
|
|
|
'order_product_id' => '',
|
|
|
|
'tax' => 0,
|
|
|
|
'reward' => 0
|
2014-08-19 07:56:17 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$deliveryCost = isset($order['delivery']['cost']) ? $order['delivery']['cost'] : 0;
|
|
|
|
|
|
|
|
$data['order_total'] = array(
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'sub_total',
|
2014-09-03 13:03:25 +04:00
|
|
|
'title' => $this->language->get('product_summ'),
|
2014-08-19 07:56:17 +04:00
|
|
|
'value' => $order['summ'],
|
2014-08-29 19:32:23 +04:00
|
|
|
'text' => $order['summ'],
|
2014-08-19 07:56:17 +04:00
|
|
|
'sort_order' => $subtotalSettings['sub_total_sort_order']
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'shipping',
|
2014-08-29 19:32:23 +04:00
|
|
|
'title' => $ocDelivery[$data['shipping_code']],
|
2014-08-19 07:56:17 +04:00
|
|
|
'value' => $deliveryCost,
|
2014-08-29 19:32:23 +04:00
|
|
|
'text' => $deliveryCost,
|
2014-08-19 07:56:17 +04:00
|
|
|
'sort_order' => $shippingSettings['shipping_sort_order']
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'total',
|
2014-09-03 13:03:25 +04:00
|
|
|
'title' => $this->language->get('column_total'),
|
2014-08-28 02:13:37 +04:00
|
|
|
'value' => isset($order['totalSumm'])
|
|
|
|
? $order['totalSumm']
|
|
|
|
: $order['summ'] + $deliveryCost
|
|
|
|
,
|
2014-08-29 19:32:23 +04:00
|
|
|
'text' => isset($order['totalSumm'])
|
|
|
|
? $order['totalSumm']
|
|
|
|
: $order['summ'] + $deliveryCost
|
|
|
|
,
|
2014-08-19 07:56:17 +04:00
|
|
|
'sort_order' => $totalSettings['total_sort_order']
|
|
|
|
)
|
2014-08-18 18:14:04 +04:00
|
|
|
);
|
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$data['fromApi'] = true;
|
2014-08-29 19:32:23 +04:00
|
|
|
|
2014-08-19 07:56:17 +04:00
|
|
|
if (isset($order['externalId'])) {
|
2014-09-03 13:03:25 +04:00
|
|
|
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'];
|
2014-08-19 07:56:17 +04:00
|
|
|
}
|
2014-09-03 13:03:25 +04:00
|
|
|
|
2014-08-19 07:56:17 +04:00
|
|
|
$this->model_sale_order->editOrder($order['externalId'], $data);
|
|
|
|
} else {
|
2014-09-03 13:03:25 +04:00
|
|
|
$data['order_status_id'] = 1;
|
2014-08-19 07:56:17 +04:00
|
|
|
$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']);
|
|
|
|
}
|
2014-09-03 13:03:25 +04:00
|
|
|
|
2014-08-18 18:14:04 +04:00
|
|
|
}
|
2014-08-19 07:56:17 +04:00
|
|
|
}
|
2014-08-18 18:14:04 +04:00
|
|
|
|
2014-08-19 07:56:17 +04:00
|
|
|
if (!empty($customersIdsFix)) {
|
|
|
|
$crm->customerFixExternalIds($customersIdsFix);
|
2014-08-18 18:14:04 +04:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:56:17 +04:00
|
|
|
if (!empty($ordersIdsFix)) {
|
|
|
|
$crm->orderFixExternalIds($ordersIdsFix);
|
2014-08-18 18:14:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addNotice(
|
|
|
|
'['.
|
|
|
|
$this->config->get('store_name').
|
|
|
|
'] RestApi::orderHistory: you need to configure Intarocrm module first.'
|
|
|
|
);
|
2014-08-18 18:14:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
public function export_icml()
|
2014-08-27 18:06:19 +04:00
|
|
|
{
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->load->model('intarocrm/tools');
|
|
|
|
$this->model_intarocrm_tools->generateICML();
|
2014-08-19 07:56:17 +04:00
|
|
|
}
|
|
|
|
|
2014-08-15 13:35:22 +04:00
|
|
|
private function validate() {
|
|
|
|
if (!$this->user->hasPermission('modify', 'module/intarocrm')) {
|
|
|
|
$this->error['warning'] = $this->language->get('error_permission');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->error) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
}
|