opencart-module/catalog/model/retailcrm/order.php

272 lines
11 KiB
PHP
Raw Normal View History

2016-01-29 18:04:48 +03:00
<?php
class ModelRetailcrmOrder extends Model {
public function sendToCrm($order_data, $order_id)
{
2016-04-14 13:55:11 +03:00
if(isset($this->request->post['fromApi'])) return;
2016-01-29 18:04:48 +03:00
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm');
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
2016-07-25 17:21:15 +03:00
$this->load->model('catalog/product');
2016-01-29 18:04:48 +03:00
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
$this->retailcrm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
2017-06-07 15:28:27 +03:00
$this->setLogs()
2016-01-29 18:04:48 +03:00
);
$order = array();
$customers = $this->retailcrm->customersList(
array(
'name' => $order_data['telephone'],
'email' => $order_data['email']
),
1,
100
);
2016-07-25 17:21:15 +03:00
if($customers) {
foreach ($customers['customers'] as $customer) {
$order['customer']['id'] = $customer['id'];
}
2016-01-29 18:04:48 +03:00
}
unset($customers);
$order['externalId'] = $order_id;
$order['firstName'] = $order_data['firstname'];
$order['lastName'] = $order_data['lastname'];
$order['phone'] = $order_data['telephone'];
$order['customerComment'] = $order_data['comment'];
2016-10-14 18:13:16 +03:00
if(!empty($order_data['email'])) {
$order['email'] = $order_data['email'];
}
2016-01-29 18:04:48 +03:00
$deliveryCost = 0;
2016-03-11 14:15:24 +03:00
$altTotals = isset($order_data['order_total']) ? $order_data['order_total'] : "";
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $altTotals ;
if (!empty($orderTotals)) {
foreach ($orderTotals as $totals) {
if ($totals['code'] == 'shipping') {
$deliveryCost = $totals['value'];
}
2016-01-29 18:04:48 +03:00
}
}
2016-10-14 18:13:16 +03:00
$order['createdAt'] = $order_data['date_added'];
2016-01-29 18:04:48 +03:00
2016-04-07 15:31:04 +03:00
$payment_code = $order_data['payment_code'];
$order['paymentType'] = $settings['retailcrm_payment'][$payment_code];
2016-01-29 18:04:48 +03:00
if(!isset($order_data['shipping_iso_code_2']) && isset($order_data['shipping_country_id'])) {
$this->load->model('localisation/country');
$shipping_country = $this->model_localisation_country->getCountry($order_data['shipping_country_id']);
$order_data['shipping_iso_code_2'] = $shipping_country['iso_code_2'];
}
2016-04-07 15:31:04 +03:00
$delivery_code = $order_data['shipping_code'];
2016-01-29 18:04:48 +03:00
$order['delivery'] = array(
'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '',
2016-01-29 18:04:48 +03:00
'cost' => $deliveryCost,
'address' => array(
'index' => $order_data['shipping_postcode'],
'city' => $order_data['shipping_city'],
2016-04-07 15:31:04 +03:00
'countryIso' => $order_data['shipping_iso_code_2'],
'region' => $order_data['shipping_zone'],
2016-01-29 18:04:48 +03:00
'text' => implode(', ', array(
$order_data['shipping_postcode'],
2016-04-07 15:31:04 +03:00
(isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '',
2016-01-29 18:04:48 +03:00
$order_data['shipping_city'],
$order_data['shipping_address_1'],
$order_data['shipping_address_2']
))
)
);
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
2016-07-25 17:21:15 +03:00
$offerOptions = array('select', 'radio');
2016-01-29 18:04:48 +03:00
foreach ($orderProducts as $product) {
2016-07-25 17:21:15 +03:00
$offerId = '';
if(!empty($product['option'])) {
$options = array();
$productOptions = $this->model_catalog_product->getProductOptions($product['product_id']);
foreach($product['option'] as $option) {
if(!in_array($option['type'], $offerOptions)) continue;
foreach($productOptions as $productOption) {
if($productOption['product_option_id'] = $option['product_option_id']) {
foreach($productOption['product_option_value'] as $productOptionValue) {
if($productOptionValue['product_option_value_id'] == $option['product_option_value_id']) {
$options[$option['product_option_id']] = $productOptionValue['option_value_id'];
}
}
}
}
}
ksort($options);
$offerId = array();
foreach($options as $optionKey => $optionValue) {
$offerId[] = $optionKey.'-'.$optionValue;
}
$offerId = implode('_', $offerId);
}
2016-01-29 18:04:48 +03:00
$order['items'][] = array(
2016-08-31 17:28:11 +03:00
'offer' => array(
'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
),
2016-01-29 18:04:48 +03:00
'productName' => $product['name'],
'initialPrice' => $product['price'],
'quantity' => $product['quantity'],
);
}
2016-03-11 14:15:24 +03:00
if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) {
2016-01-29 18:04:48 +03:00
$order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
}
$this->retailcrm->ordersCreate($order);
}
}
public function changeInCrm($order_data, $order_id)
{
2016-04-14 13:55:11 +03:00
if(isset($this->request->post['fromApi'])) return;
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm');
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
2016-07-25 17:21:15 +03:00
$this->load->model('catalog/product');
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
$this->retailcrm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
2017-06-07 15:28:27 +03:00
$this->setLogs()
);
$order = array();
$payment_code = $order_data['payment_code'];
$delivery_code = $order_data['shipping_code'];
$order['externalId'] = $order_id;
$order['firstName'] = $order_data['firstname'];
$order['lastName'] = $order_data['lastname'];
$order['phone'] = $order_data['telephone'];
$order['customerComment'] = $order_data['comment'];
2016-10-14 18:13:16 +03:00
if(!empty($order_data['email'])) {
$order['email'] = $order_data['email'];
}
$deliveryCost = 0;
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ;
foreach ($orderTotals as $totals) {
if ($totals['code'] == 'shipping') {
$deliveryCost = $totals['value'];
}
}
2016-10-14 18:13:16 +03:00
$order['createdAt'] = $order_data['date_added'];
$order['paymentType'] = $settings['retailcrm_payment'][$payment_code];
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
$order['delivery'] = array(
'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '',
'cost' => $deliveryCost,
'address' => array(
'index' => $order_data['shipping_postcode'],
'city' => $order_data['shipping_city'],
'country' => $order_data['shipping_country_id'],
'region' => $order_data['shipping_zone_id'],
'text' => implode(', ', array(
$order_data['shipping_postcode'],
$country,
$order_data['shipping_city'],
$order_data['shipping_address_1'],
$order_data['shipping_address_2']
))
)
);
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
2016-07-25 17:21:15 +03:00
$offerOptions = array('select', 'radio');
foreach ($orderProducts as $product) {
2016-07-25 17:21:15 +03:00
$offerId = '';
if(!empty($product['option'])) {
$options = array();
$productOptions = $this->model_catalog_product->getProductOptions($product['product_id']);
foreach($product['option'] as $option) {
if(!in_array($option['type'], $offerOptions)) continue;
foreach($productOptions as $productOption) {
if($productOption['product_option_id'] = $option['product_option_id']) {
foreach($productOption['product_option_value'] as $productOptionValue) {
if($productOptionValue['product_option_value_id'] == $option['product_option_value_id']) {
$options[$option['product_option_id']] = $productOptionValue['option_value_id'];
}
}
}
}
}
ksort($options);
$offerId = array();
foreach($options as $optionKey => $optionValue) {
$offerId[] = $optionKey.'-'.$optionValue;
}
$offerId = implode('_', $offerId);
}
$order['items'][] = array(
2016-08-31 17:28:11 +03:00
'offer' => array(
'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
),
'productName' => $product['name'],
'initialPrice' => $product['price'],
'quantity' => $product['quantity'],
);
}
if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) {
$order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
}
$this->retailcrm->ordersEdit($order);
}
}
2017-06-07 15:28:27 +03:00
private function setLogs()
{
if (version_compare(VERSION, '2.0', '>')) {
$logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log';
} else {
$logs = DIR_SYSTEM . 'logs/ecomlogic.log';
}
return $logs;
}
2016-01-29 18:04:48 +03:00
}