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

117 lines
4.3 KiB
PHP
Raw Normal View History

2016-01-29 18:04:48 +03:00
<?php
class ModelRetailcrmOrder extends Model {
2016-08-31 17:28:11 +03:00
public function uploadToCrm($orders) {
$this->load->model('catalog/product');
2016-01-29 18:04:48 +03:00
$this->load->model('setting/setting');
2016-08-31 17:28:11 +03:00
$this->settings = $this->model_setting_setting->getSetting('retailcrm');
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
$ordersToCrm = array();
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
foreach($orders as $order) {
$ordersToCrm[] = $this->process($order);
}
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
$chunkedOrders = array_chunk($ordersToCrm, 50);
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
foreach($chunkedOrders as $ordersPart) {
$this->retailcrmApi->ordersUpload($ordersPart);
}
}
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
private function process($order_data) {
$order = array();
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
$payment_code = $order_data['payment_code'];
$delivery_code = $order_data['shipping_code'];
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
$order['externalId'] = $order_data['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
2016-10-14 18:13:16 +03:00
if(!empty($order_data['email'])) {
$order['email'] = $order_data['email'];
}
2016-08-31 17:28:11 +03:00
$order['customer']['externalId'] = $order_data['customer_id'];
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
$deliveryCost = 0;
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ;
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
foreach ($orderTotals as $totals) {
if ($totals['code'] == 'shipping') {
$deliveryCost = $totals['value'];
2016-01-29 18:04:48 +03:00
}
}
2016-08-31 17:28:11 +03:00
$order['createdAt'] = $order_data['date_added'];
$order['paymentType'] = $this->settings['retailcrm_payment'][$payment_code];
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
$order['delivery'] = array(
'code' => !empty($delivery_code) ? $this->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'];
$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(!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'];
}
}
}
}
}
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
ksort($options);
2016-01-29 18:04:48 +03:00
2016-08-31 17:28:11 +03:00
$offerId = array();
foreach($options as $optionKey => $optionValue) {
$offerId[] = $optionKey.'-'.$optionValue;
2016-01-29 18:04:48 +03:00
}
2016-08-31 17:28:11 +03:00
$offerId = implode('_', $offerId);
2016-01-29 18:04:48 +03:00
}
2016-08-31 17:28:11 +03:00
$order['items'][] = array(
'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'],
'productName' => $product['name'],
'initialPrice' => $product['price'],
'quantity' => $product['quantity'],
2016-01-29 18:04:48 +03:00
);
}
2016-08-31 17:28:11 +03:00
return $order;
2016-01-29 18:04:48 +03:00
}
}