mirror of
https://github.com/retailcrm/opencart-module.git
synced 2024-11-25 06:36:06 +03:00
send new order to crm, bug fixes
This commit is contained in:
parent
e00b16397c
commit
03ad5c9ce3
@ -43,4 +43,6 @@ Catalog export script will be here
|
||||
/index.php?route=export/intarocrm
|
||||
```
|
||||
|
||||
#### Exchange setup
|
||||
|
||||
Look into example folder
|
||||
|
@ -210,18 +210,7 @@ class ControllerModuleIntarocrm extends Controller {
|
||||
|
||||
protected function getOpercartDeliveryMethods()
|
||||
{
|
||||
$extensions = $this->model_setting_extension->getInstalled('shipping');
|
||||
|
||||
foreach ($extensions as $key => $value) {
|
||||
if (!file_exists(DIR_APPLICATION . 'controller/shipping/' . $value . '.php')) {
|
||||
$this->model_setting_extension->uninstall('shipping', $value);
|
||||
|
||||
unset($extensions[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$deliveryMethods = array();
|
||||
|
||||
$files = glob(DIR_APPLICATION . 'controller/shipping/*.php');
|
||||
|
||||
if ($files) {
|
||||
@ -231,7 +220,7 @@ class ControllerModuleIntarocrm extends Controller {
|
||||
$this->load->language('shipping/' . $extension);
|
||||
|
||||
if ($this->config->get($extension . '_status')) {
|
||||
$deliveryMethods[] = strip_tags($this->language->get('heading_title'));
|
||||
$deliveryMethods[$extension.'.'.$extension] = strip_tags($this->language->get('heading_title'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -247,18 +236,7 @@ class ControllerModuleIntarocrm extends Controller {
|
||||
|
||||
protected function getOpercartPaymentTypes()
|
||||
{
|
||||
$extensions = $this->model_setting_extension->getInstalled('payment');
|
||||
|
||||
foreach ($extensions as $key => $value) {
|
||||
if (!file_exists(DIR_APPLICATION . 'controller/payment/' . $value . '.php')) {
|
||||
$this->model_setting_extension->uninstall('payment', $value);
|
||||
|
||||
unset($extensions[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$paymentTypes = array();
|
||||
|
||||
$files = glob(DIR_APPLICATION . 'controller/payment/*.php');
|
||||
|
||||
if ($files) {
|
||||
@ -268,7 +246,7 @@ class ControllerModuleIntarocrm extends Controller {
|
||||
$this->load->language('payment/' . $extension);
|
||||
|
||||
if ($this->config->get($extension . '_status')) {
|
||||
$paymentTypes[] = strip_tags($this->language->get('heading_title'));
|
||||
$paymentTypes[$extension] = strip_tags($this->language->get('heading_title'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ class ControllerExportIntarocrm extends Controller {
|
||||
}
|
||||
|
||||
$e->appendChild($this->dd->createElement('name'))->appendChild($this->dd->createTextNode($offer['name']));
|
||||
$e->appendChild($this->dd->createElement('productName'))->appendChild($this->dd->createTextNode($offer['name']));
|
||||
$e->appendChild($this->dd->createElement('productName'))->appendChild($this->dd->createTextNode($offer['name'] .' '. $offer['model']));
|
||||
$e->appendChild($this->dd->createElement('vendor'))->appendChild($this->dd->createTextNode($offer['manufacturer']));
|
||||
$e->appendChild($this->dd->createElement('price', $offer['price']));
|
||||
|
||||
|
82
example/catalog/model/checkout/order.php
Normal file
82
example/catalog/model/checkout/order.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This is stripped-down copy of /catalog/model/checkout/order.php
|
||||
* For exchange you must modify original file.
|
||||
*/
|
||||
|
||||
class ModelCheckoutOrder extends Model
|
||||
{
|
||||
public function addOrder($data) {
|
||||
|
||||
/*
|
||||
* This is original code
|
||||
*/
|
||||
$this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', store_id = '" . (int)$data['store_id'] . "', store_name = '" . $this->db->escape($data['store_name']) . "', store_url = '" . $this->db->escape($data['store_url']) . "', customer_id = '" . (int)$data['customer_id'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', payment_company = '" . $this->db->escape($data['payment_company']) . "', payment_company_id = '" . $this->db->escape($data['payment_company_id']) . "', payment_tax_id = '" . $this->db->escape($data['payment_tax_id']) . "', payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', payment_city = '" . $this->db->escape($data['payment_city']) . "', payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', payment_country = '" . $this->db->escape($data['payment_country']) . "', payment_country_id = '" . (int)$data['payment_country_id'] . "', payment_zone = '" . $this->db->escape($data['payment_zone']) . "', payment_zone_id = '" . (int)$data['payment_zone_id'] . "', payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', payment_method = '" . $this->db->escape($data['payment_method']) . "', payment_code = '" . $this->db->escape($data['payment_code']) . "', shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', shipping_company = '" . $this->db->escape($data['shipping_company']) . "', shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($data['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($data['shipping_country']) . "', shipping_country_id = '" . (int)$data['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', shipping_method = '" . $this->db->escape($data['shipping_method']) . "', shipping_code = '" . $this->db->escape($data['shipping_code']) . "', comment = '" . $this->db->escape($data['comment']) . "', total = '" . (float)$data['total'] . "', affiliate_id = '" . (int)$data['affiliate_id'] . "', commission = '" . (float)$data['commission'] . "', language_id = '" . (int)$data['language_id'] . "', currency_id = '" . (int)$data['currency_id'] . "', currency_code = '" . $this->db->escape($data['currency_code']) . "', currency_value = '" . (float)$data['currency_value'] . "', ip = '" . $this->db->escape($data['ip']) . "', forwarded_ip = '" . $this->db->escape($data['forwarded_ip']) . "', user_agent = '" . $this->db->escape($data['user_agent']) . "', accept_language = '" . $this->db->escape($data['accept_language']) . "', date_added = NOW(), date_modified = NOW()");
|
||||
|
||||
$order_id = $this->db->getLastId();
|
||||
|
||||
foreach ($data['products'] as $product) {
|
||||
$this->db->query("INSERT INTO " . DB_PREFIX . "order_product SET order_id = '" . (int)$order_id . "', product_id = '" . (int)$product['product_id'] . "', name = '" . $this->db->escape($product['name']) . "', model = '" . $this->db->escape($product['model']) . "', quantity = '" . (int)$product['quantity'] . "', price = '" . (float)$product['price'] . "', total = '" . (float)$product['total'] . "', tax = '" . (float)$product['tax'] . "', reward = '" . (int)$product['reward'] . "'");
|
||||
|
||||
$order_product_id = $this->db->getLastId();
|
||||
|
||||
foreach ($product['option'] as $option) {
|
||||
$this->db->query("INSERT INTO " . DB_PREFIX . "order_option SET order_id = '" . (int)$order_id . "', order_product_id = '" . (int)$order_product_id . "', product_option_id = '" . (int)$option['product_option_id'] . "', product_option_value_id = '" . (int)$option['product_option_value_id'] . "', name = '" . $this->db->escape($option['name']) . "', `value` = '" . $this->db->escape($option['value']) . "', `type` = '" . $this->db->escape($option['type']) . "'");
|
||||
}
|
||||
|
||||
foreach ($product['download'] as $download) {
|
||||
$this->db->query("INSERT INTO " . DB_PREFIX . "order_download SET order_id = '" . (int)$order_id . "', order_product_id = '" . (int)$order_product_id . "', name = '" . $this->db->escape($download['name']) . "', filename = '" . $this->db->escape($download['filename']) . "', mask = '" . $this->db->escape($download['mask']) . "', remaining = '" . (int)($download['remaining'] * $product['quantity']) . "'");
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data['vouchers'] as $voucher) {
|
||||
$this->db->query("INSERT INTO " . DB_PREFIX . "order_voucher SET order_id = '" . (int)$order_id . "', description = '" . $this->db->escape($voucher['description']) . "', code = '" . $this->db->escape($voucher['code']) . "', from_name = '" . $this->db->escape($voucher['from_name']) . "', from_email = '" . $this->db->escape($voucher['from_email']) . "', to_name = '" . $this->db->escape($voucher['to_name']) . "', to_email = '" . $this->db->escape($voucher['to_email']) . "', voucher_theme_id = '" . (int)$voucher['voucher_theme_id'] . "', message = '" . $this->db->escape($voucher['message']) . "', amount = '" . (float)$voucher['amount'] . "'");
|
||||
}
|
||||
|
||||
foreach ($data['totals'] as $total) {
|
||||
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
|
||||
}
|
||||
|
||||
/*
|
||||
* This is integration code
|
||||
*/
|
||||
$data['order_id'] = $order_id;
|
||||
$this->crmOrderAction($data, 'create');
|
||||
|
||||
/*
|
||||
* This is original code again
|
||||
*/
|
||||
return $order_id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Crm order action method. Add it into the end of class code.
|
||||
*
|
||||
* @param $action string values: edit, create
|
||||
*/
|
||||
protected function crmOrderAction($order, $action=null)
|
||||
{
|
||||
$this->load->model('setting/setting');
|
||||
$settings = $this->model_setting_setting->getSetting('intarocrm');
|
||||
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
|
||||
|
||||
if(isset($settings['intarocrm_url']) && $settings['intarocrm_url'] != '' && isset($settings['intarocrm_apikey']) && $settings['intarocrm_apikey'] != '') {
|
||||
include_once __DIR__ . '/../../../system/library/intarocrm/apihelper.php';
|
||||
|
||||
$crm = new ApiHelper($settings);
|
||||
|
||||
if ($action == null) {
|
||||
$crm->dumperData($order);
|
||||
} else {
|
||||
$method = 'order' . ucfirst($action);
|
||||
$crm->$method($order);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
202
system/library/intarocrm/apihelper.php
Normal file
202
system/library/intarocrm/apihelper.php
Normal file
@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
class ApiHelper
|
||||
{
|
||||
private $dir, $fileDate;
|
||||
protected $intaroApi, $log, $settings;
|
||||
|
||||
public function __construct($settings) {
|
||||
$this->dir = __DIR__ . '/../logs/';
|
||||
$this->fileDate = $this->dir . 'intarocrm_history.log';
|
||||
$this->settings = $settings;
|
||||
$this->domain = $settings['domain'];
|
||||
|
||||
$this->intaroApi = new IntaroCrm\RestApi(
|
||||
$settings['intarocrm_url'],
|
||||
$settings['intarocrm_apikey']
|
||||
);
|
||||
|
||||
$this->initLogger();
|
||||
}
|
||||
|
||||
public function dumperData($data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function orderCreate($data) {
|
||||
|
||||
$order = array();
|
||||
$customer = array();
|
||||
|
||||
$payment_code = $data['payment_code'];
|
||||
$delivery_code = $data['shipping_code'];
|
||||
$settings = $this->settings;
|
||||
|
||||
try {
|
||||
$customers = $this->intaroApi->customers($data['telephone'], $data['email'], $data['lastname'], 200, 0);
|
||||
} catch (ApiException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::customers:' . $e->getMessage());
|
||||
$this->log->addError('['.$this->domain.'] RestApi::customers:' . json_encode($data));
|
||||
} catch (CurlException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::customers::Curl:' . $e->getMessage());
|
||||
}
|
||||
|
||||
if(count($customers) > 0 && isset($customers[0]['externalId'])) {
|
||||
$order['customerId'] = $customers[0]['externalId'];
|
||||
} else {
|
||||
$order['customerId'] = $data['customer_id'];
|
||||
$customer['externalId'] = $data['customer_id'];
|
||||
$customer['firstName'] = $data['firstname'];
|
||||
$customer['lastName'] = $data['lastname'];
|
||||
$customer['email'] = $data['email'];
|
||||
$customer['phones']['number'] = $data['telephone'];
|
||||
|
||||
$customer['address']['text'] = implode(', ', array(
|
||||
$data['payment_postcode'],
|
||||
$data['payment_country'],
|
||||
$data['payment_city'],
|
||||
$data['payment_address_1'],
|
||||
$data['payment_address_2']
|
||||
));
|
||||
|
||||
try {
|
||||
$this->intaroApi->customerCreate($customer);
|
||||
} catch (ApiException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
|
||||
} catch (CurlException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$order['externalId'] = $data['order_id'];
|
||||
$order['firstName'] = $data['firstname'];
|
||||
$order['lastName'] = $data['lastname'];
|
||||
$order['email'] = $data['email'];
|
||||
$order['phone'] = $data['telephone'];
|
||||
$order['customerComment'] = $data['comment'];
|
||||
|
||||
$order['deliveryCost'] = 0;
|
||||
foreach ($data['totals'] as $totals) {
|
||||
if ($totals['code'] == 'shipping') {
|
||||
$order['deliveryCost'] = $totals['value'];
|
||||
}
|
||||
}
|
||||
|
||||
$order['deliveryAddress']['text'] = implode(', ', array(
|
||||
$data['shipping_postcode'],
|
||||
$data['shipping_country'],
|
||||
$data['shipping_city'],
|
||||
$data['shipping_address_1'],
|
||||
$data['shipping_address_2']
|
||||
));
|
||||
|
||||
$order['createdAt'] = date('Y-m-d H:i:s');
|
||||
$order['paymentType'] = $settings['intarocrm_payment'][$payment_code];
|
||||
|
||||
$order['delivery'] = array(
|
||||
'code' => $settings['intarocrm_delivery'][$delivery_code],
|
||||
'cost' => $order['deliveryCost']
|
||||
);
|
||||
|
||||
|
||||
foreach ($data['products'] as $product) {
|
||||
$order['items'][] = array(
|
||||
'productId' => $product['product_id'],
|
||||
'productName' => $product['name'] . ' '. $product['model'],
|
||||
'initialPrice' => $product['price'],
|
||||
'quantity' => $product['quantity'],
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->intaroApi->orderCreate($order);
|
||||
} catch (ApiException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
|
||||
} catch (CurlException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function orderHistory() {
|
||||
|
||||
try {
|
||||
$orders = $this->intaroApi->orderHistory($this->getDate());
|
||||
$this->saveDate($this->intaroApi->getGeneratedAt()->format('Y-m-d H:i:s'));
|
||||
} catch (ApiException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . $e->getMessage());
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . json_encode($orders));
|
||||
|
||||
return false;
|
||||
} catch (CurlException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderHistory::Curl:' . $e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach($orders as $order) {
|
||||
|
||||
if (!isset($order['deleted'])) {
|
||||
try {
|
||||
$o = $this->intaroApi->orderGet($order['id'], 'id');
|
||||
} catch (ApiException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderGet:' . $e->getMessage());
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderGet:' . json_encode($order));
|
||||
|
||||
return false;
|
||||
} catch (CurlException $e) {
|
||||
$this->log->addError('['.$this->domain.'] RestApi::orderGet::Curl:' . $e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if(isset($o['orderMethod']) && $o['orderMethod'] == $this->params['intarocrm_api']['orderMethod']) {
|
||||
$this->log->addNotice('send order to PAP: ' . $o['id'] . ' - ' . $o['status']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function saveDate($date) {
|
||||
file_put_contents($this->fileDate, $date, LOCK_EX);
|
||||
}
|
||||
|
||||
private function getDate() {
|
||||
if (file_exists($this->fileDate)) {
|
||||
$result = file_get_contents($this->fileDate);
|
||||
} else {
|
||||
$result = date('Y-m-d H:i:s', strtotime('-2 days', strtotime(date('Y-m-d H:i:s'))));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function explodeFIO($str) {
|
||||
if(!$str)
|
||||
return array();
|
||||
|
||||
$array = explode(" ", $str, 3);
|
||||
$newArray = array();
|
||||
|
||||
foreach($array as $ar) {
|
||||
if(!$ar)
|
||||
continue;
|
||||
|
||||
$newArray[] = $ar;
|
||||
}
|
||||
|
||||
return $newArray;
|
||||
}
|
||||
|
||||
protected function initLogger() {
|
||||
$this->log = new Monolog\Logger('intarocrm');
|
||||
$this->log->pushHandler(new Monolog\Handler\StreamHandler($this->dir . 'intarocrm.log', Monolog\Logger::INFO));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user