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

318 lines
11 KiB
PHP

<?php
class ModelExtensionRetailcrmOrder extends Model {
public function sendToCrm($order_data, $order_id)
{
if(isset($this->request->post['fromApi'])) return;
$this->initApi();
$order = array();
$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);
$order = $this->processOrder($order_data, $order_id);
$response = $this->retailcrm->ordersCreate($order);
if ($settings[$moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) {
$this->createPayment($order_data, $order_id);
}
}
public function changeInCrm($order_data, $order_id)
{
if(isset($this->request->post['fromApi'])) return;
$this->initApi();
$order = array();
$order = $this->processOrder($order_data, $order_id);
$response = $this->retailcrm->ordersEdit($order);
if ($settings[$moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) {
$this->editPayment($order_data, $order_id);
}
}
protected function processOrder($order_data, $order_id)
{
$moduleTitle = $this->getModuleTitle();
$this->load->model('setting/setting');
$this->load->model('catalog/product');
$settings = $this->model_setting_setting->getSetting($moduleTitle);
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']);
}
$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'];
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'];
}
if ($totals['code'] == 'coupon') {
$couponTotal = abs($totals['value']);
}
}
if (isset($couponTotal)) $order['discount'] = $couponTotal;
$order['createdAt'] = $order_data['date_added'];
if ($settings[$moduleTitle . '_apiversion'] != 'v5') {
$order['paymentType'] = $settings[$moduleTitle . '_payment'][$payment_code];
}
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
if(isset($settings[$moduleTitle . '_delivery'][$order_data['shipping_code']])) {
$delivery_code = $order_data['shipping_code'];
} else {
$delivery_code = stristr($order_data['shipping_code'], '.', TRUE);
}
$order['delivery'] = array(
'code' => !empty($delivery_code) ? $settings[$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');
foreach ($orderProducts as $product) {
$offerId = '';
if (!empty($product['option'])) {
$options = array();
$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'];
}
}
}
}
}
ksort($options);
$offerId = array();
foreach($options as $optionKey => $optionValue) {
$offerId[] = $optionKey.'-'.$optionValue;
}
$offerId = implode('_', $offerId);
}
if ($settings[$moduleTitle . '_apiversion'] != 'v3') {
$item = array(
'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']
);
}
if (isset($properties)) $item['properties'] = $properties;
$order['items'][] = $item;
if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) {
$order['status'] = $settings[$moduleTitle . '_status'][$order_data['order_status_id']];
}
if ($settings[$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';
}
}
}
}
return $order;
}
protected function createPayment($order, $order_id)
{
$moduleTitle = $this->getModuleTitle();
$settings = $this->model_setting_setting->getSetting($moduleTitle);
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']);
}
$payment_code = $order['payment_code'];
foreach ($order['totals'] as $total) {
if ($total['code'] == 'total') $amount = $total['value'];
}
$payment = array(
'externalId' => $order_id,
'type' => $settings[$moduleTitle . '_payment'][$payment_code],
'amount' => $amount
);
if (version_compare(VERSION, '3.0', '<')) {
if ($order['order_status_id'] == $settingPaid[$order['payment_code'] . '_order_status_id']) {
$payment['status'] = 'paid';
}
} else {
if ($order['order_status_id'] == $settingPaid['payment_' . $order['payment_code'] . '_order_status_id']) {
$payment['status'] = 'paid';
}
}
$payment['order'] = array(
'externalId' => $order_id
);
$this->retailcrm->ordersPaymentCreate($payment);
}
protected function editPayment($order, $order_id)
{
$moduleTitle = $this->getModuleTitle();
$settings = $this->model_setting_setting->getSetting($moduleTitle);
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']);
}
$payment_code = $order['payment_code'];
foreach ($order['totals'] as $total) {
if ($total['code'] == 'total') $amount = $total['value'];
}
$payment = array(
'externalId' => $order_id,
'type' => $settings[$moduleTitle . '_payment'][$payment_code],
'amount' => $amount
);
if (version_compare(VERSION, '3.0', '<')) {
if ($order['order_status_id'] == $settingPaid[$order['payment_code'] . '_order_status_id']) {
$payment['status'] = 'paid';
}
} else {
if ($order['order_status_id'] == $settingPaid['payment_' . $order['payment_code'] . '_order_status_id']) {
$payment['status'] = 'paid';
}
}
$this->retailcrm->ordersPaymentEdit($payment);
}
private function initApi()
{
$moduleTitle = $this->getModuleTitle();
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting($moduleTitle);
if(!empty($settings[$moduleTitle . '_url']) && !empty($settings[$moduleTitle . '_apikey'])) {
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
$this->retailcrm = new RetailcrmProxy(
$settings[$moduleTitle . '_url'],
$settings[$moduleTitle . '_apikey'],
DIR_SYSTEM . 'storage/logs/retailcrm.log',
$settings[$moduleTitle . '_apiversion']
);
}
}
private function getModuleTitle()
{
if (version_compare(VERSION, '3.0', '<')) {
$title = 'retailcrm';
} else {
$title = 'module_retailcrm';
}
return $title;
}
}