refactoring, reduce core files modification, additional models for hooks emulation & tools, update docs

This commit is contained in:
Alex Lushpai 2014-08-28 02:13:37 +04:00
parent fe2473f7f1
commit 285885de46
8 changed files with 371 additions and 283 deletions

View File

@ -4,7 +4,7 @@ require_once __DIR__ . '/../../../system/library/intarocrm/vendor/autoload.php';
class ControllerModuleIntarocrm extends Controller { class ControllerModuleIntarocrm extends Controller {
private $error = array(); private $error = array();
protected $dd, $eCategories, $eOffers; protected $log, $statuses, $payments, $deliveryTypes;
public function install() { public function install() {
$this->load->model('setting/setting'); $this->load->model('setting/setting');
@ -19,10 +19,13 @@ class ControllerModuleIntarocrm extends Controller {
public function index() { public function index() {
$this->log = new Monolog\Logger('opencart-module'); $this->log = new Monolog\Logger('opencart-module');
$this->log->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/../../../system/logs/intarocrm_module.log', Monolog\Logger::INFO)); $this->log->pushHandler(
new Monolog\Handler\StreamHandler(DIR_LOGS . 'intarocrm_module.log', Monolog\Logger::INFO)
);
$this->load->model('setting/setting'); $this->load->model('setting/setting');
$this->load->model('setting/extension'); $this->load->model('setting/extension');
$this->load->model('intarocrm/tools');
$this->load->language('module/intarocrm'); $this->load->language('module/intarocrm');
$this->document->setTitle($this->language->get('heading_title')); $this->document->setTitle($this->language->get('heading_title'));
$this->document->addStyle('/admin/view/stylesheet/intarocrm.css'); $this->document->addStyle('/admin/view/stylesheet/intarocrm.css');
@ -56,7 +59,9 @@ class ControllerModuleIntarocrm extends Controller {
$this->data['intarocrm_errors'] = array(); $this->data['intarocrm_errors'] = array();
$this->data['saved_settings'] = $this->model_setting_setting->getSetting('intarocrm'); $this->data['saved_settings'] = $this->model_setting_setting->getSetting('intarocrm');
if ($this->data['saved_settings']['intarocrm_url'] != '' && $this->data['saved_settings']['intarocrm_apikey'] != '') { if ($this->data['saved_settings']['intarocrm_url'] != '' &&
$this->data['saved_settings']['intarocrm_apikey'] != ''
) {
$this->intarocrm = new \IntaroCrm\RestApi( $this->intarocrm = new \IntaroCrm\RestApi(
$this->data['saved_settings']['intarocrm_url'], $this->data['saved_settings']['intarocrm_url'],
@ -70,19 +75,26 @@ class ControllerModuleIntarocrm extends Controller {
try { try {
$this->deliveryTypes = $this->intarocrm->deliveryTypesList(); $this->deliveryTypes = $this->intarocrm->deliveryTypesList();
} }
catch (ApiException $e) catch (IntaroCrm\Exception\ApiException $e)
{ {
$this->data['intarocrm_error'][] = $e->getMessage(); $this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError('['.$this->config->get('store_name').'] RestApi::deliveryTypesList::Api:' . $e->getMessage()); $this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::deliveryTypesList::Api:' . $e->getMessage()
);
} }
catch (CurlException $e) catch (IntaroCrm\Exception\CurlException $e)
{ {
$this->data['intarocrm_error'][] = $e->getMessage(); $this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError('['.$this->config->get('store_name').'] RestApi::deliveryTypesList::Curl:' . $e->getMessage()); $this->log->addError(
'[' . $this->config->get('store_name') .
'] RestApi::deliveryTypesList::Curl:' . $e->getMessage()
);
} }
$this->data['delivery'] = array( $this->data['delivery'] = array(
'opencart' => $this->getOpercartDeliveryMethods(), 'opencart' => $this->model_intarocrm_tools->getOpercartDeliveryMethods(),
'intarocrm' => $this->deliveryTypes 'intarocrm' => $this->deliveryTypes
); );
@ -92,19 +104,27 @@ class ControllerModuleIntarocrm extends Controller {
try { try {
$this->statuses = $this->intarocrm->orderStatusesList(); $this->statuses = $this->intarocrm->orderStatusesList();
} }
catch (ApiException $e) catch (IntaroCrm\Exception\ApiException $e)
{ {
$this->data['intarocrm_error'][] = $e->getMessage(); $this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError('['.$this->config->get('store_name').'] RestApi::orderStatusesList::Api:' . $e->getMessage()); $this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::orderStatusesList::Api:' . $e->getMessage()
);
} }
catch (CurlException $e) catch (IntaroCrm\Exception\CurlException $e)
{ {
$this->data['intarocrm_error'][] = $e->getMessage(); $this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError('['.$this->config->get('store_name').'] RestApi::orderStatusesList::Curl:' . $e->getMessage()); $this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::orderStatusesList::Curl:' . $e->getMessage()
);
} }
$this->data['statuses'] = array( $this->data['statuses'] = array(
'opencart' => $this->getOpercartOrderStatuses(), 'opencart' => $this->model_intarocrm_tools->getOpercartOrderStatuses(),
'intarocrm' => $this->statuses 'intarocrm' => $this->statuses
); );
@ -115,19 +135,27 @@ class ControllerModuleIntarocrm extends Controller {
try { try {
$this->payments = $this->intarocrm->paymentTypesList(); $this->payments = $this->intarocrm->paymentTypesList();
} }
catch (ApiException $e) catch (IntaroCrm\Exception\ApiException $e)
{ {
$this->data['intarocrm_error'][] = $e->getMessage(); $this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError('['.$this->config->get('store_name').'] RestApi::paymentTypesList::Api:' . $e->getMessage()); $this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::paymentTypesList::Api:' . $e->getMessage()
);
} }
catch (CurlException $e) catch (IntaroCrm\Exception\CurlException $e)
{ {
$this->data['intarocrm_error'][] = $e->getMessage(); $this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError('['.$this->config->get('store_name').'] RestApi::paymentTypesList::Curl:' . $e->getMessage()); $this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::paymentTypesList::Curl:' . $e->getMessage()
);
} }
$this->data['payments'] = array( $this->data['payments'] = array(
'opencart' => $this->getOpercartPaymentTypes(), 'opencart' => $this->model_intarocrm_tools->getOpercartPaymentTypes(),
'intarocrm' => $this->payments 'intarocrm' => $this->payments
); );
@ -200,19 +228,24 @@ class ControllerModuleIntarocrm extends Controller {
public function order_history() public function order_history()
{ {
$this->log = new Monolog\Logger('opencart-module'); $this->log = new Monolog\Logger('opencart-module');
$this->log->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/../../../system/logs/intarocrm_module.log', Monolog\Logger::INFO)); $this->log->pushHandler(
new Monolog\Handler\StreamHandler(DIR_LOGS . 'intarocrm_module.log', Monolog\Logger::INFO)
);
$this->load->model('setting/setting'); $this->load->model('setting/setting');
$this->load->model('setting/store'); $this->load->model('setting/store');
$this->load->model('sale/order'); $this->load->model('sale/order');
$this->load->model('sale/customer'); $this->load->model('sale/customer');
$this->load->model('intarocrm/tools');
$settings = $this->model_setting_setting->getSetting('intarocrm'); $settings = $this->model_setting_setting->getSetting('intarocrm');
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST); $settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
if (isset($settings['intarocrm_url']) && $settings['intarocrm_url'] != '' && isset($settings['intarocrm_apikey']) && $settings['intarocrm_apikey'] != '') { if (isset($settings['intarocrm_url']) &&
$settings['intarocrm_url'] != '' &&
isset($settings['intarocrm_apikey']) &&
$settings['intarocrm_apikey'] != ''
) {
include_once __DIR__ . '/../../../system/library/intarocrm/apihelper.php'; include_once __DIR__ . '/../../../system/library/intarocrm/apihelper.php';
$crm = new ApiHelper($settings); $crm = new ApiHelper($settings);
$orders = $crm->orderHistory(); $orders = $crm->orderHistory();
@ -237,15 +270,24 @@ class ControllerModuleIntarocrm extends Controller {
$cData = array( $cData = array(
'customer_group_id' => '1', 'customer_group_id' => '1',
'firstname' => $order['customer']['firstName'], 'firstname' => $order['customer']['firstName'],
'lastname' => (isset($order['customer']['lastName'])) ? $order['customer']['lastName'] : ' ', 'lastname' => (isset($order['customer']['lastName']))
? $order['customer']['lastName']
: ' '
,
'email' => $order['customer']['email'], 'email' => $order['customer']['email'],
'telephone' => (isset($order['customer']['phones'][0]['number'])) ? $order['customer']['phones'][0]['number'] : ' ', 'telephone' => (isset($order['customer']['phones'][0]['number']))
? $order['customer']['phones'][0]['number']
: ' '
,
'newsletter' => 0, 'newsletter' => 0,
'password' => 'tmppass', 'password' => 'tmppass',
'status' => 1, 'status' => 1,
'address' => array( 'address' => array(
'firstname' => $order['customer']['firstName'], 'firstname' => $order['customer']['firstName'],
'lastname' => (isset($order['customer']['lastName'])) ? $order['customer']['lastName'] : ' ', 'lastname' => (isset($order['customer']['lastName']))
? $order['customer']['lastName']
: ' '
,
'address_1' => $order['customer']['address']['text'], 'address_1' => $order['customer']['address']['text'],
'city' => $order['customer']['address']['city'], 'city' => $order['customer']['address']['city'],
'postcode' => $order['customer']['address']['index'] 'postcode' => $order['customer']['address']['index']
@ -258,7 +300,9 @@ class ControllerModuleIntarocrm extends Controller {
$tryToFind = $this->model_sale_customer->getCustomerByEmail($order['customer']['email']); $tryToFind = $this->model_sale_customer->getCustomerByEmail($order['customer']['email']);
$customer_id = $tryToFind['customer_id']; $customer_id = $tryToFind['customer_id'];
} else { } else {
$last = $this->model_sale_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1)); $last = $this->model_sale_customer->getCustomers(
$data = array('order' => 'DESC', 'limit' => 1)
);
$customer_id = $last[0]['customer_id']; $customer_id = $last[0]['customer_id'];
} }
@ -269,16 +313,22 @@ class ControllerModuleIntarocrm extends Controller {
$payment = array_flip($settings['intarocrm_payment']); $payment = array_flip($settings['intarocrm_payment']);
$status = array_flip($settings['intarocrm_status']); $status = array_flip($settings['intarocrm_status']);
$ocPayment = $this->getOpercartPaymentTypes(); $ocPayment = $this->model_intarocrm_tools->getOpercartPaymentTypes();
$ocDelivery = $this->getOpercartDeliveryMethods(); $ocDelivery = $this->model_intarocrm_tools->getOpercartDeliveryMethods();
$data['store_id'] = ($this->config->get('config_store_id') == null) ? 0 : $this->config->get('config_store_id'); $data['store_id'] = ($this->config->get('config_store_id') == null)
? 0
: $this->config->get('config_store_id')
;
$data['customer'] = $order['customer']['firstName']; $data['customer'] = $order['customer']['firstName'];
$data['customer_id'] = $customer_id; $data['customer_id'] = $customer_id;
$data['firstname'] = $order['customer']['firstName']; $data['firstname'] = $order['customer']['firstName'];
$data['lastname'] = (isset($order['customer']['lastName'])) ? $order['customer']['lastName'] : ' '; $data['lastname'] = (isset($order['customer']['lastName'])) ? $order['customer']['lastName'] : ' ';
$data['email'] = $order['customer']['email']; $data['email'] = $order['customer']['email'];
$data['telephone'] = (isset($order['customer']['phones'][0]['number'])) ? $order['customer']['phones'][0]['number'] : ' '; $data['telephone'] = (isset($order['customer']['phones'][0]['number']))
? $order['customer']['phones'][0]['number']
: ' '
;
$data['comment'] = $order['customerComment']; $data['comment'] = $order['customerComment'];
$data['payment_address'] = '0'; $data['payment_address'] = '0';
@ -298,7 +348,10 @@ class ControllerModuleIntarocrm extends Controller {
$data['shipping_address'] = '0'; $data['shipping_address'] = '0';
$data['shipping_firstname'] = $order['customer']['firstName']; $data['shipping_firstname'] = $order['customer']['firstName'];
$data['shipping_lastname'] = (isset($order['customer']['lastName'])) ? $order['customer']['lastName'] : ' '; $data['shipping_lastname'] = (isset($order['customer']['lastName']))
? $order['customer']['lastName']
: ' '
;
$data['shipping_address_1'] = $order['delivery']['address']['text']; $data['shipping_address_1'] = $order['delivery']['address']['text'];
$data['shipping_city'] = $order['delivery']['address']['city']; $data['shipping_city'] = $order['delivery']['address']['city'];
$data['shipping_postcode'] = $order['delivery']['address']['index']; $data['shipping_postcode'] = $order['delivery']['address']['index'];
@ -341,7 +394,10 @@ class ControllerModuleIntarocrm extends Controller {
array( array(
'order_total_id' => '', 'order_total_id' => '',
'code' => 'total', 'code' => 'total',
'value' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, 'value' => isset($order['totalSumm'])
? $order['totalSumm']
: $order['summ'] + $deliveryCost
,
'sort_order' => $totalSettings['total_sort_order'] 'sort_order' => $totalSettings['total_sort_order']
) )
); );
@ -385,44 +441,18 @@ class ControllerModuleIntarocrm extends Controller {
} }
} else { } else {
$this->log->addNotice('['.$this->config->get('store_name').'] RestApi::orderHistory: you need to configure Intarocrm module first.'); $this->log->addNotice(
'['.
$this->config->get('store_name').
'] RestApi::orderHistory: you need to configure Intarocrm module first.'
);
} }
} }
public function exportXml() public function export_icml()
{ {
$string = '<?xml version="1.0" encoding="UTF-8"?> $this->load->model('intarocrm/tools');
<yml_catalog date="'.date('Y-m-d H:i:s').'"> $this->model_intarocrm_tools->generateICML();
<shop>
<name>'.$this->config->get('config_name').'</name>
<categories/>
<offers/>
</shop>
</yml_catalog>
';
$xml = new SimpleXMLElement($string, LIBXML_NOENT |LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE);
$this->dd = new DOMDocument();
$this->dd->preserveWhiteSpace = false;
$this->dd->formatOutput = true;
$this->dd->loadXML($xml->asXML());
$this->eCategories = $this->dd->getElementsByTagName('categories')->item(0);
$this->eOffers = $this->dd->getElementsByTagName('offers')->item(0);
$this->addCategories();
$this->addOffers();
$this->dd->saveXML();
$downloadPath = __DIR__ . '/../../../download/';
if (!file_exists($downloadPath)) {
mkdir($downloadPath, 0755);
}
$this->dd->save($downloadPath . 'intarocrm.xml');
} }
private function validate() { private function validate() {
@ -437,147 +467,5 @@ class ControllerModuleIntarocrm extends Controller {
} }
} }
protected function getOpercartDeliveryMethods()
{
$deliveryMethods = array();
$files = glob(DIR_APPLICATION . 'controller/shipping/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('shipping/' . $extension);
if ($this->config->get($extension . '_status')) {
$deliveryMethods[$extension.'.'.$extension] = strip_tags($this->language->get('heading_title'));
}
}
}
return $deliveryMethods;
}
protected function getOpercartOrderStatuses()
{
$this->load->model('localisation/order_status');
return $this->model_localisation_order_status->getOrderStatuses(array());
}
protected function getOpercartPaymentTypes()
{
$paymentTypes = array();
$files = glob(DIR_APPLICATION . 'controller/payment/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('payment/' . $extension);
if ($this->config->get($extension . '_status')) {
$paymentTypes[$extension] = strip_tags($this->language->get('heading_title'));
}
}
}
return $paymentTypes;
}
private function addCategories()
{
$this->load->model('catalog/category');
foreach ($this->model_catalog_category->getCategories(array()) as $category) {
$e = $this->eCategories->appendChild($this->dd->createElement('category', $category['name']));
$e->setAttribute('id',$category['category_id']);
}
} }
private function addOffers()
{
$this->load->model('catalog/product');
$this->load->model('catalog/manufacturer');
$this->load->model('tool/image');
$offerManufacturers = array();
$manufacturers = $this->model_catalog_manufacturer->getManufacturers(array());
foreach ($manufacturers as $manufacturer) {
$offerManufacturers[$manufacturer['manufacturer_id']] = $manufacturer['name'];
}
foreach ($this->model_catalog_product->getProducts(array()) as $offer) {
$e = $this->eOffers->appendChild($this->dd->createElement('offer'));
$e->setAttribute('id', $offer['product_id']);
$e->setAttribute('productId', $offer['product_id']);
$e->setAttribute('quantity', $offer['quantity']);
$e->setAttribute('available', $offer['status'] ? 'true' : 'false');
/*
* DIRTY HACK, NEED TO REFACTOR
*/
$sql = "SELECT * FROM `" . DB_PREFIX . "product_to_category` WHERE `product_id` = " .$offer['product_id']. ";";
$result = $this->db->query($sql);
foreach ($result->rows as $row) {
$e->appendChild($this->dd->createElement('categoryId', $row['category_id']));
}
$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('price', $offer['price']));
if ($offer['manufacturer_id'] != 0) {
$e->appendChild($this->dd->createElement('vendor'))->appendChild($this->dd->createTextNode($offerManufacturers[$offer['manufacturer_id']]));
}
if ($offer['image']) {
$e->appendChild(
$this->dd->createElement(
'picture',
$this->model_tool_image->resize($offer['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'))
)
);
}
$this->url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
$e->appendChild($this->dd->createElement('url'))->appendChild(
$this->dd->createTextNode(
$this->url->link('product/product&product_id=' . $offer['product_id'])
)
);
if ($offer['sku'] != '') {
$sku = $this->dd->createElement('param');
$sku->setAttribute('name', 'article');
$sku->appendChild($this->dd->createTextNode($offer['sku']));
$e->appendChild($sku);
}
if ($offer['weight'] != '') {
$weight = $this->dd->createElement('param');
$weight->setAttribute('name', 'weight');
$weightValue = (isset($offer['weight_class'])) ? round($offer['weight'], 3) . ' ' . $offer['weight_class'] : round($offer['weight'], 3);
$weight->appendChild($this->dd->createTextNode($weightValue));
$e->appendChild($weight);
}
if ($offer['length'] != '' && $offer['width'] != '' && $offer['height'] != '') {
$size = $this->dd->createElement('param');
$size->setAttribute('name', 'size');
$size->appendChild(
$this->dd->createTextNode(
round($offer['length'], 2) .'x'.
round($offer['width'], 2) .'x'.
round($offer['height'], 2)
)
);
$e->appendChild($size);
}
}
}
}
?>

View File

@ -0,0 +1,19 @@
<?php
class ModelIntarocrmOrder extends Model {
public function send($order, $order_id)
{
$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';
$order['order_id'] = $order_id;
$crm = new ApiHelper($settings);
$crm->processOrder($order);
}
}
}
?>

View File

@ -0,0 +1,197 @@
<?php
class ModelIntarocrmTools extends Model {
protected $dd, $eCategories, $eOffers;
public function getOpercartDeliveryMethods()
{
$deliveryMethods = array();
$files = glob(DIR_APPLICATION . 'controller/shipping/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('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');
return $this->model_localisation_order_status->getOrderStatuses(array());
}
public function getOpercartPaymentTypes()
{
$paymentTypes = array();
$files = glob(DIR_APPLICATION . 'controller/payment/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('payment/' . $extension);
if ($this->config->get($extension . '_status')) {
$paymentTypes[$extension] = strip_tags($this->language->get('heading_title'));
}
}
}
return $paymentTypes;
}
public function generateICML()
{
$string = '<?xml version="1.0" encoding="UTF-8"?>
<yml_catalog date="'.date('Y-m-d H:i:s').'">
<shop>
<name>'.$this->config->get('config_name').'</name>
<categories/>
<offers/>
</shop>
</yml_catalog>
';
$xml = new SimpleXMLElement($string, LIBXML_NOENT |LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE);
$this->dd = new DOMDocument();
$this->dd->preserveWhiteSpace = false;
$this->dd->formatOutput = true;
$this->dd->loadXML($xml->asXML());
$this->eCategories = $this->dd->getElementsByTagName('categories')->item(0);
$this->eOffers = $this->dd->getElementsByTagName('offers')->item(0);
$this->addCategories();
$this->addOffers();
$this->dd->saveXML();
$downloadPath = DIR_SYSTEM . '/../download/';
if (!file_exists($downloadPath)) {
mkdir($downloadPath, 0755);
}
$this->dd->save($downloadPath . 'intarocrm.xml');
}
private function addCategories()
{
$this->load->model('catalog/category');
foreach ($this->model_catalog_category->getCategories(array()) as $category) {
$e = $this->eCategories->appendChild($this->dd->createElement('category', $category['name']));
$e->setAttribute('id',$category['category_id']);
}
}
private function addOffers()
{
$this->load->model('catalog/product');
$this->load->model('catalog/manufacturer');
$this->load->model('tool/image');
$offerManufacturers = array();
$manufacturers = $this->model_catalog_manufacturer->getManufacturers(array());
foreach ($manufacturers as $manufacturer) {
$offerManufacturers[$manufacturer['manufacturer_id']] = $manufacturer['name'];
}
foreach ($this->model_catalog_product->getProducts(array()) as $offer) {
$e = $this->eOffers->appendChild($this->dd->createElement('offer'));
$e->setAttribute('id', $offer['product_id']);
$e->setAttribute('productId', $offer['product_id']);
$e->setAttribute('quantity', $offer['quantity']);
$e->setAttribute('available', $offer['status'] ? 'true' : 'false');
/*
* DIRTY HACK, NEED TO REFACTOR
*/
$sql = "SELECT * FROM `" .
DB_PREFIX .
"product_to_category` WHERE `product_id` = " .$offer['product_id']. ";"
;
$result = $this->db->query($sql);
foreach ($result->rows as $row) {
$e->appendChild($this->dd->createElement('categoryId', $row['category_id']));
}
$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('price', $offer['price']));
if ($offer['manufacturer_id'] != 0) {
$e->appendChild($this->dd->createElement('vendor'))
->appendChild($this->dd->createTextNode($offerManufacturers[$offer['manufacturer_id']]));
}
if ($offer['image']) {
$e->appendChild(
$this->dd->createElement(
'picture',
$this->model_tool_image->resize(
$offer['image'],
$this->config->get('config_image_product_width'),
$this->config->get('config_image_product_height')
)
)
);
}
$this->url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
$e->appendChild($this->dd->createElement('url'))->appendChild(
$this->dd->createTextNode(
$this->url->link('product/product&product_id=' . $offer['product_id'])
)
);
if ($offer['sku'] != '') {
$sku = $this->dd->createElement('param');
$sku->setAttribute('name', 'article');
$sku->appendChild($this->dd->createTextNode($offer['sku']));
$e->appendChild($sku);
}
if ($offer['weight'] != '') {
$weight = $this->dd->createElement('param');
$weight->setAttribute('name', 'weight');
$weightValue = (isset($offer['weight_class']))
? round($offer['weight'], 3) . ' ' . $offer['weight_class']
: round($offer['weight'], 3)
;
$weight->appendChild($this->dd->createTextNode($weightValue));
$e->appendChild($weight);
}
if ($offer['length'] != '' && $offer['width'] != '' && $offer['height'] != '') {
$size = $this->dd->createElement('param');
$size->setAttribute('name', 'size');
$size->appendChild(
$this->dd->createTextNode(
round($offer['length'], 2) .'x'.
round($offer['width'], 2) .'x'.
round($offer['height'], 2)
)
);
$e->appendChild($size);
}
}
}
}

View File

@ -0,0 +1,19 @@
<?php
class ModelIntarocrmOrder extends Model {
public function send($order, $order_id)
{
$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';
$order['order_id'] = $order_id;
$crm = new ApiHelper($settings);
$crm->processOrder($order);
}
}
}
?>

View File

@ -1,3 +1,3 @@
<?php <?php
$cli_action = 'module/intarocrm/exportXml'; $cli_action = 'module/intarocrm/export_icml';
require_once('cli_dispatch.php'); require_once('cli_dispatch.php');

View File

@ -41,35 +41,16 @@ Into your CRM settings set path to exported file
#### Export new order from shop to CRM #### Export new order from shop to CRM
Open /catalog/model/checkout/order.php script. Into addOrder method add this line before return statement:
``` ```
$this->crmOrderAction($data, $order_id, 'create'); $this->load->model('intarocrm/order');
$this->model_intarocrm_order->send($data, $order_id);
``` ```
In the end of this file add method: Add this lines into /catalog/model/checkout/order.php script, into addOrder method before return statement and
into /admin/model/sale/order.php script, into addOrder & editOrder methods at the end of these methods
```
protected function crmOrderAction($order, $order_id, $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';
$order['order_id'] = $order_id;
$crm = new ApiHelper($settings);
if ($action != null) {
$method = 'order' . ucfirst($action);
$crm->$method($order);
}
}
}
```
#### Export new order from CRM to shop #### Export new order from CRM to shop

View File

@ -1,6 +1,6 @@
TODO TODO
==== ====
* Edit order hook
* Export old customers & orders * Export old customers & orders
* New customers export * New customers export
* Make sources PSR-2 compatible

View File

@ -13,23 +13,22 @@ class ApiHelper
$this->settings = $settings; $this->settings = $settings;
$this->domain = $settings['domain']; $this->domain = $settings['domain'];
$this->log = new Monolog\Logger('intarocrm');
$this->log->pushHandler(
new Monolog\Handler\StreamHandler($this->dir . 'intarocrm_module.log', Monolog\Logger::INFO)
);
$this->intaroApi = new IntaroCrm\RestApi( $this->intaroApi = new IntaroCrm\RestApi(
$settings['intarocrm_url'], $settings['intarocrm_url'],
$settings['intarocrm_apikey'] $settings['intarocrm_apikey']
); );
$this->initLogger();
} }
public function dumperData($data) public function processOrder($data) {
{
return false;
}
public function orderCreate($data) {
$order = array(); $order = array();
$customer = array(); $customer = array();
$customers = array();
$payment_code = $data['payment_code']; $payment_code = $data['payment_code'];
$delivery_code = $data['shipping_code']; $delivery_code = $data['shipping_code'];
@ -37,13 +36,14 @@ class ApiHelper
try { try {
$customers = $this->intaroApi->customers($data['telephone'], $data['email'], $data['lastname'], 200, 0); $customers = $this->intaroApi->customers($data['telephone'], $data['email'], $data['lastname'], 200, 0);
} catch (ApiException $e) { } catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customers:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::customers:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::customers:' . json_encode($data)); $this->log->addError('['.$this->domain.'] RestApi::customers:' . json_encode($data));
} catch (CurlException $e) { } catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customers::Curl:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::customers::Curl:' . $e->getMessage());
} }
if(count($customers) > 0 && isset($customers[0]['externalId'])) { if(count($customers) > 0 && isset($customers[0]['externalId'])) {
$order['customerId'] = $customers[0]['externalId']; $order['customerId'] = $customers[0]['externalId'];
} else { } else {
@ -64,14 +64,16 @@ class ApiHelper
try { try {
$this->intaroApi->customerEdit($customer); $this->intaroApi->customerEdit($customer);
} catch (ApiException $e) { } catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order)); $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
} catch (CurlException $e) { } catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
} }
} }
unset($customers);
$order['externalId'] = $data['order_id']; $order['externalId'] = $data['order_id'];
$order['firstName'] = $data['firstname']; $order['firstName'] = $data['firstname'];
$order['lastName'] = $data['lastname']; $order['lastName'] = $data['lastname'];
@ -113,26 +115,28 @@ class ApiHelper
} }
try { try {
$this->intaroApi->orderCreate($order); $this->intaroApi->orderEdit($order);
} catch (ApiException $e) { } catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order)); $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
} catch (CurlException $e) { } catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
} }
} }
public function orderHistory() { public function orderHistory() {
$orders = array();
try { try {
$orders = $this->intaroApi->orderHistory($this->getDate()); $orders = $this->intaroApi->orderHistory($this->getDate());
$this->saveDate($this->intaroApi->getGeneratedAt()->format('Y-m-d H:i:s')); $this->saveDate($this->intaroApi->getGeneratedAt()->format('Y-m-d H:i:s'));
} catch (ApiException $e) { } catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . json_encode($orders)); $this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . json_encode($orders));
return false; return false;
} catch (CurlException $e) { } catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderHistory::Curl:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderHistory::Curl:' . $e->getMessage());
return false; return false;
@ -144,13 +148,13 @@ class ApiHelper
public function orderFixExternalIds($data) public function orderFixExternalIds($data)
{ {
try { try {
$this->intaroApi->orderFixExternalIds($data); return $this->intaroApi->orderFixExternalIds($data);
} catch (ApiException $e) { } catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($data)); $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($data));
return false; return false;
} catch (CurlException $e) { } catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage());
return false; return false;
@ -160,13 +164,13 @@ class ApiHelper
public function customerFixExternalIds($data) public function customerFixExternalIds($data)
{ {
try { try {
$this->intaroApi->customerFixExternalIds($data); return $this->intaroApi->customerFixExternalIds($data);
} catch (ApiException $e) { } catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . json_encode($data)); $this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . json_encode($data));
return false; return false;
} catch (CurlException $e) { } catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds::Curl:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds::Curl:' . $e->getMessage());
return false; return false;
@ -175,16 +179,17 @@ class ApiHelper
public function getOrderItems($order_id) public function getOrderItems($order_id)
{ {
$order = '';
try { try {
$order = $this->intaroApi->orderGet($order_id); $order = $this->intaroApi->orderGet($order_id);
return $order['items']; return $order['items'];
} catch (ApiException $e) { } catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($data)); $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($order));
return false; return false;
} catch (CurlException $e) { } catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage()); $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage());
return false; return false;
@ -206,25 +211,4 @@ class ApiHelper
return $result; 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_module.log', Monolog\Logger::INFO));
}
} }