From 00f273ff3c096d62b23813a87ad4c9421fdc779f Mon Sep 17 00:00:00 2001 From: iyzoer Date: Mon, 15 May 2017 17:58:45 +0400 Subject: [PATCH 1/2] Product properties in the order (#27) --- .../controller/extension/module/retailcrm.php | 8 +--- admin/model/extension/retailcrm/history.php | 5 +-- admin/model/extension/retailcrm/order.php | 1 + .../template/extension/module/retailcrm.tpl | 22 +++++----- catalog/controller/api/retailcrm.php | 1 - catalog/model/extension/retailcrm/order.php | 43 ++++++++++++++++--- system/cron/export.php | 1 - 7 files changed, 53 insertions(+), 28 deletions(-) diff --git a/admin/controller/extension/module/retailcrm.php b/admin/controller/extension/module/retailcrm.php index d8d056a..8a47e69 100644 --- a/admin/controller/extension/module/retailcrm.php +++ b/admin/controller/extension/module/retailcrm.php @@ -54,13 +54,6 @@ class ControllerExtensionModuleRetailcrm extends Controller 'catalog/model/account/customer/addCustomer/after', 'extension/module/retailcrm/customer_create' ); - - $this->model_extension_event - ->addEvent( - 'retailcrm', - 'catalog/model/checkout/order/editOrder/after', - 'extension/module/retailcrm/order_edit' - ); } /** @@ -376,6 +369,7 @@ class ControllerExtensionModuleRetailcrm extends Controller $this->load->model('extension/retailcrm/order'); $this->model_extension_retailcrm_order->uploadToCrm($fullOrders); + $file = fopen(DIR_SYSTEM . '/cron/export_done.txt', "x"); } /** diff --git a/admin/model/extension/retailcrm/history.php b/admin/model/extension/retailcrm/history.php index 5e61bad..c963532 100644 --- a/admin/model/extension/retailcrm/history.php +++ b/admin/model/extension/retailcrm/history.php @@ -64,9 +64,8 @@ class ModelExtensionRetailcrmHistory extends Model $this->ocPayment = $this->model_extension_retailcrm_references ->getOpercartPaymentTypes(); - $this->ocDelivery = $this->model_extension_retailcrm_references - ->getOpercartDeliveryTypes(); - + $this->ocDelivery = $settings['retailcrm_delivery']; + $this->zones = $this->model_localisation_zone->getZones(); $updatedOrders = array(); diff --git a/admin/model/extension/retailcrm/order.php b/admin/model/extension/retailcrm/order.php index 2c27169..5edd1de 100644 --- a/admin/model/extension/retailcrm/order.php +++ b/admin/model/extension/retailcrm/order.php @@ -1,6 +1,7 @@ load->model('catalog/product'); diff --git a/admin/view/template/extension/module/retailcrm.tpl b/admin/view/template/extension/module/retailcrm.tpl index 9afdbdd..8703791 100644 --- a/admin/view/template/extension/module/retailcrm.tpl +++ b/admin/view/template/extension/module/retailcrm.tpl @@ -6,7 +6,7 @@
- + @@ -63,12 +63,7 @@
-

-
- - -
- + @@ -76,10 +71,14 @@
+

+
+ + +

-
@@ -142,7 +141,7 @@ var token = ''; $('#icml').on('click', function() { $.ajax({ - url: ''+'system/cron/icml.php', + url: '' + 'admin/index.php?route=extension/module/retailcrm/icml&token=' + token, beforeSend: function() { $('#icml').button('loading'); }, @@ -159,7 +158,7 @@ $('#export').on('click', function() { $.ajax({ - url: ''+'system/cron/export.php', + url: '' + 'admin/index.php?route=extension/module/retailcrm/export&token=' + token, beforeSend: function() { $('#export').button('loading'); }, @@ -178,7 +177,7 @@ var order_id = $('input[name=\'order_id\']').val(); if (order_id && order_id > 0) { $.ajax({ - url: ''+'admin/index.php?route=extension/module/retailcrm/exportOrder&token=' + token + '&order_id=' + order_id, + url: '' + 'admin/index.php?route=extension/module/retailcrm/exportOrder&token=' + token + '&order_id=' + order_id, beforeSend: function() { $('#export_order').button('loading'); }, @@ -205,3 +204,4 @@ } }); + diff --git a/catalog/controller/api/retailcrm.php b/catalog/controller/api/retailcrm.php index 00ad266..823258c 100644 --- a/catalog/controller/api/retailcrm.php +++ b/catalog/controller/api/retailcrm.php @@ -47,7 +47,6 @@ class ControllerApiRetailcrm extends Controller ); foreach ($shippingModules as $shippingModule) { - $this->load->model('extension/shipping/' . $shippingModule['code']); if ($this->config->get($shippingModule['code'] . '_status')) { diff --git a/catalog/model/extension/retailcrm/order.php b/catalog/model/extension/retailcrm/order.php index 7860ab4..5892a67 100644 --- a/catalog/model/extension/retailcrm/order.php +++ b/catalog/model/extension/retailcrm/order.php @@ -78,7 +78,12 @@ class ModelExtensionRetailcrmOrder extends Model { $order_data['shipping_iso_code_2'] = $shipping_country['iso_code_2']; } - $delivery_code = $order_data['shipping_code']; + if(isset($settings['retailcrm_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['retailcrm_delivery'][$delivery_code] : '', 'cost' => $deliveryCost, @@ -109,6 +114,14 @@ class ModelExtensionRetailcrmOrder extends Model { $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); foreach($product['option'] as $option) { + if ($option['type'] == 'select' || $option['type'] == 'radio' || $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']) { @@ -130,7 +143,7 @@ class ModelExtensionRetailcrmOrder extends Model { $offerId = implode('_', $offerId); } - $order['items'][] = array( + $item = array( 'offer' => array( 'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'] ), @@ -138,6 +151,10 @@ class ModelExtensionRetailcrmOrder extends Model { 'initialPrice' => $product['price'], 'quantity' => $product['quantity'], ); + + if (isset($properties)) $item['properties'] = $properties; + + $order['items'][] = $item; } if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { @@ -169,8 +186,6 @@ class ModelExtensionRetailcrmOrder extends Model { $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']; @@ -195,6 +210,12 @@ class ModelExtensionRetailcrmOrder extends Model { $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; + if(isset($settings['retailcrm_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['retailcrm_delivery'][$delivery_code] : '', 'address' => array( @@ -228,6 +249,14 @@ class ModelExtensionRetailcrmOrder extends Model { $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); foreach($product['option'] as $option) { + if ($option['type'] == 'select' || $option['type'] == 'radio' || $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']) { @@ -249,7 +278,7 @@ class ModelExtensionRetailcrmOrder extends Model { $offerId = implode('_', $offerId); } - $order['items'][] = array( + $item = array( 'offer' => array( 'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'] ), @@ -257,6 +286,10 @@ class ModelExtensionRetailcrmOrder extends Model { 'initialPrice' => $product['price'], 'quantity' => $product['quantity'], ); + + if (isset($properties)) $item['properties'] = $properties; + + $order['items'][] = $item; } if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { diff --git a/system/cron/export.php b/system/cron/export.php index cbf86bf..5880920 100644 --- a/system/cron/export.php +++ b/system/cron/export.php @@ -1,4 +1,3 @@ Date: Thu, 18 May 2017 14:11:12 +0400 Subject: [PATCH 2/2] Update a customer's from history (#28) --- admin/model/extension/retailcrm/history.php | 58 +++++++++++++++++-- catalog/model/extension/retailcrm/order.php | 5 ++ .../retailcrm/RetailcrmHistoryHelper.php | 16 +++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/admin/model/extension/retailcrm/history.php b/admin/model/extension/retailcrm/history.php index c963532..35755e0 100644 --- a/admin/model/extension/retailcrm/history.php +++ b/admin/model/extension/retailcrm/history.php @@ -44,14 +44,19 @@ class ModelExtensionRetailcrmHistory extends Model ? new DateTime($history['retailcrm_history']) : new DateTime(date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))))); - $packs = $crm->ordersHistory(array( + $packsOrders = $crm->ordersHistory(array( 'startDate' => $lastRun->format('Y-m-d H:i:s'), ), 1, 100); - if(!$packs->isSuccessful() && count($packs->history) <= 0) + $packsCustomers = $crm->customersHistory(array( + 'startDate' => $lastRun->format('Y-m-d H:i:s'), + ), 1, 100); + if(!$packsOrders->isSuccessful() && count($packsOrders->history) <= 0 && !$packsCustomers->isSuccessful() && count($Customers->history) <= 0) return false; - $orders = RetailcrmHistoryHelper::assemblyOrder($packs->history); + + $orders = RetailcrmHistoryHelper::assemblyOrder($packsOrders->history); + $customers = RetailcrmHistoryHelper::assemblyCustomer($packsCustomers->history); - $generatedAt = $packs['generatedAt']; + $generatedAt = $packsOrders['generatedAt']; $this->subtotalSettings = $this->model_setting_setting->getSetting('sub_total'); $this->totalSettings = $this->model_setting_setting->getSetting('total'); @@ -84,6 +89,19 @@ class ModelExtensionRetailcrmHistory extends Model unset($orders); + $updateCustomers = array(); + + foreach ($customers as $customer) { + + if (isset($customer['deleted'])) continue; + + if (isset($customer['externalId'])) { + $updateCustomers[] = $customer['id']; + } + } + + unset($customers); + if (!empty($newOrders)) { $orders = $crm->ordersList($filter = array('ids' => $newOrders)); if ($orders) { @@ -98,6 +116,13 @@ class ModelExtensionRetailcrmHistory extends Model } } + if (!empty($updateCustomers)) { + $customers = $crm->customersList($filter = array('ids' => $updateCustomers)); + if ($customers) { + $this->updateCustomers($customers['customers']); + } + } + $this->model_setting_setting->editSetting('retailcrm_history', array('retailcrm_history' => $generatedAt)); if (!empty($this->createResult['customers'])) { @@ -460,4 +485,29 @@ class ModelExtensionRetailcrmHistory extends Model return array('customers' => $customersIdsFix, 'orders' => $ordersIdsFix); } + + protected function updateCustomers($customers) + { + foreach ($customers as $customer) { + + $customer_id = $customer['externalId']; + $customerData = $this->model_customer_customer->getCustomer($customer_id); + + $customerData['firstname'] = $customer['firstName']; + $customerData['lastname'] = $customer['lastName']; + $customerData['email'] = $customer['email']; + $customerData['telephone'] = $customer['phones'][0]['number']; + + $customerAddress = $this->model_customer_customer->getAddress($customerData['address_id']); + + $customerAddress['firstname'] = $customer['firstName']; + $customerAddress['lastname'] = $customer['lastName']; + $customerAddress['address_1'] = $customer['address']['text']; + $customerAddress['city'] = $customer['address']['city']; + $customerAddress['postcode'] = $customer['address']['index']; + $customerData['address'] = array($customerAddress); + + $this->model_customer_customer->editCustomer($customer_id, $customerData); + } + } } diff --git a/catalog/model/extension/retailcrm/order.php b/catalog/model/extension/retailcrm/order.php index 5892a67..55f5ddb 100644 --- a/catalog/model/extension/retailcrm/order.php +++ b/catalog/model/extension/retailcrm/order.php @@ -171,6 +171,7 @@ class ModelExtensionRetailcrmOrder extends Model { $this->load->model('setting/setting'); $settings = $this->model_setting_setting->getSetting('retailcrm'); + $settingPaid = $this->model_setting_setting->getSetting($order_data['payment_code']); if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { $this->load->model('catalog/product'); @@ -296,6 +297,10 @@ class ModelExtensionRetailcrmOrder extends Model { $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; } + if ($order_data['order_status_id'] == $settingPaid[$order_data['payment_code'] . '_order_status_id']) { + $order['paymentStatus'] = 'paid'; + } + $this->retailcrm->ordersEdit($order); } } diff --git a/system/library/retailcrm/RetailcrmHistoryHelper.php b/system/library/retailcrm/RetailcrmHistoryHelper.php index d21b327..604cf3e 100644 --- a/system/library/retailcrm/RetailcrmHistoryHelper.php +++ b/system/library/retailcrm/RetailcrmHistoryHelper.php @@ -80,6 +80,22 @@ class RetailcrmHistoryHelper { return $orders; } + public static function assemblyCustomer($customerHistory) + { + $customers = array(); + foreach ($customerHistory as $change) { + $change['order'] = self::removeEmpty($change['customer']); + + if(!empty($customers[$change['customer']['id']]) && $customers[$change['customer']['id']]) { + $customers[$change['customer']['id']] = array_merge($customers[$change['customer']['id']], $change['customer']); + } else { + $customers[$change['customer']['id']] = $change['customer']; + } + } + + return $customers; + } + public static function newValue($value) { if(isset($value['code'])) {