This commit is contained in:
Alex Lushpai 2016-01-29 18:01:38 +03:00
parent 4522e0233d
commit f3ac741980

View File

@ -1,174 +1,174 @@
<?php <?php
class ModelRetailcrmOrder extends Model { class ModelRetailcrmOrder extends Model {
public function sendToCrm($order_data, $order_id) public function sendToCrm($order_data, $order_id)
{ {
$this->load->model('setting/setting'); $this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm'); $settings = $this->model_setting_setting->getSetting('retailcrm');
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
$this->retailcrm = new RetailcrmProxy( $this->retailcrm = new RetailcrmProxy(
$settings['retailcrm_url'], $settings['retailcrm_url'],
$settings['retailcrm_apikey'], $settings['retailcrm_apikey'],
DIR_SYSTEM . 'logs/retailcrm.log' DIR_SYSTEM . 'logs/retailcrm.log'
); );
$order = array(); $order = array();
$payment_code = $order_data['payment_code']; $payment_code = $order_data['payment_code'];
$delivery_code = $order_data['shipping_code']; $delivery_code = $order_data['shipping_code'];
$customers = $this->retailcrm->customersList( $customers = $this->retailcrm->customersList(
array( array(
'name' => $order_data['telephone'], 'name' => $order_data['telephone'],
'email' => $order_data['email'] 'email' => $order_data['email']
), ),
1, 1,
100 100
); );
foreach($customers['customers'] as $customer) { foreach($customers['customers'] as $customer) {
$order['customer']['id'] = $customer['id']; $order['customer']['id'] = $customer['id'];
} }
unset($customers); unset($customers);
$order['externalId'] = $order_id; $order['externalId'] = $order_id;
$order['firstName'] = $order_data['firstname']; $order['firstName'] = $order_data['firstname'];
$order['lastName'] = $order_data['lastname']; $order['lastName'] = $order_data['lastname'];
$order['email'] = $order_data['email']; $order['email'] = $order_data['email'];
$order['phone'] = $order_data['telephone']; $order['phone'] = $order_data['telephone'];
$order['customerComment'] = $order_data['comment']; $order['customerComment'] = $order_data['comment'];
$deliveryCost = 0; $deliveryCost = 0;
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ; $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ;
foreach ($orderTotals as $totals) { foreach ($orderTotals as $totals) {
if ($totals['code'] == 'shipping') { if ($totals['code'] == 'shipping') {
$deliveryCost = $totals['value']; $deliveryCost = $totals['value'];
} }
} }
$order['createdAt'] = date('Y-m-d H:i:s'); $order['createdAt'] = date('Y-m-d H:i:s');
$order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; $order['paymentType'] = $settings['retailcrm_payment'][$payment_code];
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
$order['delivery'] = array( $order['delivery'] = array(
'code' => $settings['retailcrm_delivery'][$delivery_code], 'code' => $settings['retailcrm_delivery'][$delivery_code],
'cost' => $deliveryCost, 'cost' => $deliveryCost,
'address' => array( 'address' => array(
'index' => $order_data['shipping_postcode'], 'index' => $order_data['shipping_postcode'],
'city' => $order_data['shipping_city'], 'city' => $order_data['shipping_city'],
'country' => $order_data['shipping_country_id'], 'country' => $order_data['shipping_country_id'],
'region' => $order_data['shipping_zone_id'], 'region' => $order_data['shipping_zone_id'],
'text' => implode(', ', array( 'text' => implode(', ', array(
$order_data['shipping_postcode'], $order_data['shipping_postcode'],
$country, $country,
$order_data['shipping_city'], $order_data['shipping_city'],
$order_data['shipping_address_1'], $order_data['shipping_address_1'],
$order_data['shipping_address_2'] $order_data['shipping_address_2']
)) ))
) )
); );
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
foreach ($orderProducts as $product) { foreach ($orderProducts as $product) {
$order['items'][] = array( $order['items'][] = array(
'productId' => $product['product_id'], 'productId' => $product['product_id'],
'productName' => $product['name'], 'productName' => $product['name'],
'initialPrice' => $product['price'], 'initialPrice' => $product['price'],
'quantity' => $product['quantity'], 'quantity' => $product['quantity'],
); );
} }
if (isset($order_data['order_status_id'])) { if (isset($order_data['order_status_id'])) {
$order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
} }
$this->retailcrm->ordersCreate($order); $this->retailcrm->ordersCreate($order);
} }
} }
public function changeInCrm($order_data, $order_id) public function changeInCrm($order_data, $order_id)
{ {
$this->load->model('setting/setting'); $this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm'); $settings = $this->model_setting_setting->getSetting('retailcrm');
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
$this->retailcrm = new RetailcrmProxy( $this->retailcrm = new RetailcrmProxy(
$settings['retailcrm_url'], $settings['retailcrm_url'],
$settings['retailcrm_apikey'], $settings['retailcrm_apikey'],
DIR_SYSTEM . 'logs/retailcrm.log' DIR_SYSTEM . 'logs/retailcrm.log'
); );
$order = array(); $order = array();
$payment_code = $order_data['payment_code']; $payment_code = $order_data['payment_code'];
$delivery_code = $order_data['shipping_code']; $delivery_code = $order_data['shipping_code'];
$order['externalId'] = $order_id; $order['externalId'] = $order_id;
$order['firstName'] = $order_data['firstname']; $order['firstName'] = $order_data['firstname'];
$order['lastName'] = $order_data['lastname']; $order['lastName'] = $order_data['lastname'];
$order['email'] = $order_data['email']; $order['email'] = $order_data['email'];
$order['phone'] = $order_data['telephone']; $order['phone'] = $order_data['telephone'];
$order['customerComment'] = $order_data['comment']; $order['customerComment'] = $order_data['comment'];
$deliveryCost = 0; $deliveryCost = 0;
$orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ; $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ;
foreach ($orderTotals as $totals) { foreach ($orderTotals as $totals) {
if ($totals['code'] == 'shipping') { if ($totals['code'] == 'shipping') {
$deliveryCost = $totals['value']; $deliveryCost = $totals['value'];
} }
} }
$order['createdAt'] = date('Y-m-d H:i:s'); $order['createdAt'] = date('Y-m-d H:i:s');
$order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; $order['paymentType'] = $settings['retailcrm_payment'][$payment_code];
$country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ;
$order['delivery'] = array( $order['delivery'] = array(
'code' => $settings['retailcrm_delivery'][$delivery_code], 'code' => $settings['retailcrm_delivery'][$delivery_code],
'cost' => $deliveryCost, 'cost' => $deliveryCost,
'address' => array( 'address' => array(
'index' => $order_data['shipping_postcode'], 'index' => $order_data['shipping_postcode'],
'city' => $order_data['shipping_city'], 'city' => $order_data['shipping_city'],
'country' => $order_data['shipping_country_id'], 'country' => $order_data['shipping_country_id'],
'region' => $order_data['shipping_zone_id'], 'region' => $order_data['shipping_zone_id'],
'text' => implode(', ', array( 'text' => implode(', ', array(
$order_data['shipping_postcode'], $order_data['shipping_postcode'],
$country, $country,
$order_data['shipping_city'], $order_data['shipping_city'],
$order_data['shipping_address_1'], $order_data['shipping_address_1'],
$order_data['shipping_address_2'] $order_data['shipping_address_2']
)) ))
) )
); );
$orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product'];
foreach ($orderProducts as $product) { foreach ($orderProducts as $product) {
$order['items'][] = array( $order['items'][] = array(
'productId' => $product['product_id'], 'productId' => $product['product_id'],
'productName' => $product['name'], 'productName' => $product['name'],
'initialPrice' => $product['price'], 'initialPrice' => $product['price'],
'quantity' => $product['quantity'], 'quantity' => $product['quantity'],
); );
} }
if (isset($order_data['order_status_id'])) { if (isset($order_data['order_status_id'])) {
$order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
} }
$this->retailcrm->ordersEdit($order); $this->retailcrm->ordersEdit($order);
} }
} }
} }