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

323 lines
12 KiB
PHP
Raw Normal View History

2017-03-22 11:52:11 +03:00
<?php
class ModelExtensionRetailcrmOrder extends Model {
public function sendToCrm($order_data, $order_id)
{
if(isset($this->request->post['fromApi'])) return;
$this->initApi();
2017-07-21 10:27:08 +03:00
$order = $this->processOrder($order_data, $order_id);
$customers = $this->retailcrm->customersList(
array(
'name' => $order_data['telephone'],
'email' => $order_data['email']
),
1,
100
);
if($customers) {
foreach ($customers['customers'] as $customer) {
$order['customer']['id'] = $customer['id'];
}
}
2017-03-22 11:52:11 +03:00
unset($customers);
2017-03-22 11:52:11 +03:00
$response = $this->retailcrm->ordersCreate($order);
2017-03-22 11:52:11 +03:00
2017-07-21 10:27:08 +03:00
if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) {
$this->createPayment($order_data, $order_id);
}
}
2017-03-22 11:52:11 +03:00
public function changeInCrm($order_data, $order_id)
{
if(isset($this->request->post['fromApi'])) return;
2017-03-22 11:52:11 +03:00
$this->initApi();
2017-03-22 11:52:11 +03:00
$order = $this->processOrder($order_data, $order_id);
2017-03-22 11:52:11 +03:00
2017-07-21 10:27:08 +03:00
$customers = $this->retailcrm->customersList(
array(
'name' => $order_data['telephone'],
'email' => $order_data['email']
),
1,
100
);
if($customers) {
foreach ($customers['customers'] as $customer) {
$order['customer']['id'] = $customer['id'];
}
}
unset($customers);
$response = $this->retailcrm->ordersEdit($order);
2017-03-22 11:52:11 +03:00
2017-07-21 10:27:08 +03:00
if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) {
2017-07-27 14:41:58 +03:00
$response_order = $this->retailcrm->ordersGet($order_id);
if ($response_order->isSuccessful()) $order_info = $response_order['order'];
foreach ($order_info['payments'] as $payment_data) {
if ($payment_data['externalId'] == $order_id) {
$payment = $payment_data;
}
}
if (isset($payment) && $payment['type'] != $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']]) {
$response = $this->retailcrm->ordersPaymentDelete($payment['id']);
if ($response->isSuccessful()) {
$this->createPayment($order_data, $order_id);
}
}
}
}
2017-03-22 11:52:11 +03:00
protected function processOrder($order_data, $order_id)
{
2017-07-21 10:27:08 +03:00
$this->moduleTitle = $this->getModuleTitle();
$this->load->model('setting/setting');
$this->load->model('catalog/product');
2017-07-21 10:27:08 +03:00
$this->settings = $this->model_setting_setting->getSetting($this->moduleTitle);
2017-07-28 10:45:06 +03:00
if (version_compare(VERSION, '3.0', '<')) {
$settingPaid = $this->model_setting_setting->getSetting($order_data['payment_code']);
} else {
$settingPaid = $this->model_setting_setting->getSetting('payment_' . $order_data['payment_code']);
}
2017-04-07 12:35:06 +03:00
$payment_code = $order_data['payment_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-01-29 18:04:48 +03:00
if(!empty($order_data['email'])) {
$order['email'] = $order_data['email'];
}
2017-07-28 10:45:06 +03:00
$deliveryCost = 0;
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ;
2017-03-22 11:52:11 +03:00
foreach ($orderTotals as $totals) {
if ($totals['code'] == 'shipping') {
$deliveryCost = $totals['value'];
2017-03-22 11:52:11 +03:00
}
if ($totals['code'] == 'coupon') {
$couponTotal = abs($totals['value']);
}
}
$order['createdAt'] = $order_data['date_added'];
2017-03-22 11:52:11 +03:00
2017-07-21 10:27:08 +03:00
if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') {
$order['paymentType'] = $this->settings[$this->moduleTitle . '_payment'][$payment_code];
2017-07-27 14:41:58 +03:00
if (isset($couponTotal)) $order['discount'] = $couponTotal;
} else {
if (isset($couponTotal)) $order['discountManualAmount'] = $couponTotal;
}
2017-03-22 11:52:11 +03:00
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
2017-03-22 11:52:11 +03:00
2017-07-21 10:27:08 +03:00
if(isset($this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']])) {
$delivery_code = $order_data['shipping_code'];
} else {
$delivery_code = stristr($order_data['shipping_code'], '.', TRUE);
}
$order['delivery'] = array(
2017-07-21 10:27:08 +03:00
'code' => !empty($delivery_code) ? $this->settings[$this->moduleTitle . '_delivery'][$delivery_code] : '',
'address' => array(
'index' => $order_data['shipping_postcode'],
'city' => $order_data['shipping_city'],
'countryIso' => $order_data['shipping_iso_code_2'],
'region' => $order_data['shipping_zone'],
'text' => implode(', ', array(
$order_data['shipping_postcode'],
$country,
$order_data['shipping_city'],
$order_data['shipping_address_1'],
$order_data['shipping_address_2']
))
)
);
if(!empty($deliveryCost)){
$order['delivery']['cost'] = $deliveryCost;
}
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
$offerOptions = array('select', 'radio');
2017-03-22 11:52:11 +03:00
foreach ($orderProducts as $product) {
$offerId = '';
2017-03-22 11:52:11 +03:00
if (!empty($product['option'])) {
$options = array();
2017-05-05 14:52:01 +03:00
$productOptions = $this->model_catalog_product->getProductOptions($product['product_id']);
foreach($product['option'] as $option) {
if ($option['type'] == 'checkbox') {
$properties[] = array(
'code' => $option['product_option_value_id'],
'name' => $option['name'],
'value' => $option['value']
);
}
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'];
2017-03-22 11:52:11 +03:00
}
}
}
}
}
2017-03-22 11:52:11 +03:00
ksort($options);
2017-03-22 11:52:11 +03:00
$offerId = array();
foreach($options as $optionKey => $optionValue) {
$offerId[] = $optionKey.'-'.$optionValue;
2017-03-22 11:52:11 +03:00
}
$offerId = implode('_', $offerId);
}
2017-03-22 11:52:11 +03:00
2017-07-21 10:27:08 +03:00
if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v3') {
2017-05-05 14:52:01 +03:00
$item = array(
2017-03-22 11:52:11 +03:00
'offer' => array(
'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
),
'productName' => $product['name'],
'initialPrice' => $product['price'],
'quantity' => $product['quantity'],
);
} else {
$item = array(
'productName' => $product['name'],
'initialPrice' => $product['price'],
'quantity' => $product['quantity'],
'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id']
);
}
2017-05-05 14:52:01 +03:00
if (isset($properties)) $item['properties'] = $properties;
2017-05-05 14:52:01 +03:00
$order['items'][] = $item;
2017-03-22 11:52:11 +03:00
if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) {
2017-07-21 10:27:08 +03:00
$order['status'] = $this->settings[$this->moduleTitle . '_status'][$order_data['order_status_id']];
2017-03-22 11:52:11 +03:00
}
2017-07-21 10:27:08 +03:00
if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') {
if (version_compare(VERSION, '3.0', '<')) {
if ($order_data['order_status_id'] == $settingPaid[$order_data['payment_code'] . '_order_status_id']) {
$order['paymentStatus'] = 'paid';
}
} else {
if ($order_data['order_status_id'] == $settingPaid['payment_' . $order_data['payment_code'] . '_order_status_id']) {
$order['paymentStatus'] = 'paid';
}
}
}
2017-03-22 11:52:11 +03:00
}
return $order;
}
2017-03-22 11:52:11 +03:00
protected function createPayment($order, $order_id)
2017-07-21 10:27:08 +03:00
{
if (version_compare(VERSION, '3.0', '<')) {
$settingPaid = $this->model_setting_setting->getSetting($order['payment_code']);
} else {
$settingPaid = $this->model_setting_setting->getSetting('payment_' . $order['payment_code']);
}
2017-03-22 11:52:11 +03:00
$payment_code = $order['payment_code'];
2017-03-22 11:52:11 +03:00
foreach ($order['totals'] as $total) {
if ($total['code'] == 'total') $amount = $total['value'];
}
2017-03-22 11:52:11 +03:00
$payment = array(
'externalId' => $order_id,
2017-07-21 10:27:08 +03:00
'type' => $this->settings[$this->moduleTitle . '_payment'][$payment_code],
'amount' => $amount
);
$payment['order'] = array(
'externalId' => $order_id
);
$this->retailcrm->ordersPaymentCreate($payment);
}
2017-03-22 11:52:11 +03:00
protected function editPayment($order, $order_id)
{
if (version_compare(VERSION, '3.0', '<')) {
$settingPaid = $this->model_setting_setting->getSetting($order['payment_code']);
} else {
$settingPaid = $this->model_setting_setting->getSetting('payment_' . $order['payment_code']);
}
2017-03-22 11:52:11 +03:00
$payment_code = $order['payment_code'];
2017-03-22 11:52:11 +03:00
foreach ($order['totals'] as $total) {
if ($total['code'] == 'total') $amount = $total['value'];
}
2017-03-22 11:52:11 +03:00
$payment = array(
'externalId' => $order_id,
2017-07-21 10:27:08 +03:00
'type' => $this->settings[$this->moduleTitle . '_payment'][$payment_code],
'amount' => $amount
);
2017-03-22 11:52:11 +03:00
$this->retailcrm->ordersPaymentEdit($payment);
}
2017-03-22 11:52:11 +03:00
private function initApi()
{
$moduleTitle = $this->getModuleTitle();
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting($moduleTitle);
2017-05-05 14:52:01 +03:00
if(!empty($settings[$moduleTitle . '_url']) && !empty($settings[$moduleTitle . '_apikey'])) {
2017-05-05 14:52:01 +03:00
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
2017-03-22 11:52:11 +03:00
$this->retailcrm = new RetailcrmProxy(
$settings[$moduleTitle . '_url'],
$settings[$moduleTitle . '_apikey'],
DIR_SYSTEM . 'storage/logs/retailcrm.log',
$settings[$moduleTitle . '_apiversion']
);
}
}
2017-03-22 11:52:11 +03:00
private function getModuleTitle()
{
if (version_compare(VERSION, '3.0', '<')) {
$title = 'retailcrm';
} else {
$title = 'module_retailcrm';
2017-03-22 11:52:11 +03:00
}
return $title;
2017-03-22 11:52:11 +03:00
}
2017-03-22 11:52:11 +03:00
}