diff --git a/Changelog.md b/Changelog.md index abfbc63..f56d422 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,20 +1,21 @@ -Changelog -========= +## v.2.0.3 +* Улучшена механика выгрузки изменений из RetailCRM +* Улучшена механика выборки и настройки способов доставки -####v.1.0.1 +## v.1.0.1 * Добавлена передача скидки по купону -####v.1.0 +## v.1.0 * Улучшена модель получения способов доставки * Устранены ошибки пакетной выгрузки -####v0.3.0 +## v0.3.0 * Расширена библиотека клиента * Добавлена возможность кастомизации моделей заказа через vqmod * Устранены мелкие баги, проведен рефакторинг кода. -####v0.2.0 +## v0.2.0 Общие изменения @@ -32,12 +33,12 @@ Changelog * Скорректировано указание активности офера * Убрана генерация размера офера вследствие кастомизации этого параметра в разных магазинах -####v0.1.1 +## v0.1.1 * Устранена ошибка редактирования, при которой терялась часть данных при получении истории из CRM * Оптимизирован код получения и обработки истории заказов * Актуализированы переводы -####v.0.1 +## v.0.1 * Реализован интерфейс настроек модуля * Реализована отправка данных о заказе/клиенте в CRM * Реализована выгрузка каталога (cron only) diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 178e2fc..66eac2e 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -346,7 +346,7 @@ class ControllerModuleRetailcrm extends Controller private function setLogs() { - if (version_compare(VERSION, '2.0', '>')) { + if (version_compare(VERSION, '2.1', '>')) { $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { $logs = DIR_SYSTEM . 'logs/retailcrm.log'; diff --git a/admin/model/retailcrm/base_history.php b/admin/model/retailcrm/base_history.php new file mode 100644 index 0000000..24c5fc1 --- /dev/null +++ b/admin/model/retailcrm/base_history.php @@ -0,0 +1,195 @@ +db->query("INSERT INTO `" . DB_PREFIX . "order` SET store_url = '" . $this->db->escape($order['store_url']) . "', store_id = '" . (int)$order['store_id'] . "', store_name = '" . $order['store_name'] . "', customer_id = '" . (int)$order['customer_id'] . "', customer_group_id = '" . (int)$order['customer_group_id'] . "', firstname = '" . $this->db->escape($order['firstname']) . "', lastname = '" . $this->db->escape($order['lastname']) . "', email = '" . $this->db->escape($order['email']) . "', telephone = '" . $this->db->escape($order['telephone']) . "', custom_field = '" . $this->db->escape(isset($order['custom_field']) ? json_encode($order['custom_field']) : '') . "', payment_firstname = '" . $this->db->escape($order['payment_firstname']) . "', payment_lastname = '" . $this->db->escape($order['payment_lastname']) . "', payment_address_1 = '" . $this->db->escape($order['payment_address_1']) . "', payment_city = '" . $this->db->escape($order['payment_city']) . "', payment_postcode = '" . $this->db->escape($order['payment_postcode']) . "', payment_country = '" . $this->db->escape($order['payment_country']) . "', payment_country_id = '" . (int)$order['payment_country_id'] . "', payment_zone = '" . $this->db->escape($order['payment_zone']) . "', payment_zone_id = '" . (int)$order['payment_zone_id'] . "', payment_method = '" . $this->db->escape($order['payment_method']) . "', payment_code = '" . $this->db->escape($order['payment_code']) . "', shipping_firstname = '" . $this->db->escape($order['shipping_firstname']) . "', shipping_lastname = '" . $this->db->escape($order['shipping_lastname']) . "', shipping_address_1 = '" . $this->db->escape($order['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($order['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($order['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($order['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($order['shipping_country']) . "', shipping_country_id = '" . (int)$order['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($order['shipping_zone']) . "', shipping_zone_id = '" . (int)$order['shipping_zone_id'] . "', shipping_method = '" . $this->db->escape($order['shipping_method']) . "', shipping_code = '" . $this->db->escape($order['shipping_code']) . "', comment = '" . $this->db->escape($order['comment']) . "', total = '" . (float)$order['total'] . "', affiliate_id = '" . (int)$order['affiliate_id'] . "', language_id = '" . (int)$order['language_id'] . "', currency_id = '" . (int)$order['currency_id'] . "', currency_code = '" . $this->db->escape($order['currency_code']) . "', currency_value = '" . (float)$order['currency_value'] . "', order_status_id = '" . (int)$order['order_status_id'] . "', date_added = NOW(), date_modified = NOW()"); + + $order_id = $this->db->getLastId(); + + // Products + if (isset($order['order_product']) && $order['order_product']) { + $this->addOrderProducts($order_id, $order['order_product']); + } + + // Totals + if (isset($order['order_total'])) { + $this->addOrderTotals($order_id, $order['order_total']); + } + + return $order_id; + } + + /** + * Edit order in OC + * + * @param int $order_id + * @param array $order + * + * @return void + */ + public function editOrder($order_id, $order) + { + $this->db->query("UPDATE `" . DB_PREFIX . "order` SET customer_id = '" . (int)$order['customer_id'] . "', customer_group_id = '" . (int)$order['customer_group_id'] . "', firstname = '" . $this->db->escape($order['firstname']) . "', lastname = '" . $this->db->escape($order['lastname']) . "', email = '" . $this->db->escape($order['email']) . "', telephone = '" . $this->db->escape($order['telephone']) . "', custom_field = '" . $this->db->escape(json_encode($order['custom_field'])) . "', payment_firstname = '" . $this->db->escape($order['payment_firstname']) . "', payment_lastname = '" . $this->db->escape($order['payment_lastname']) . "', payment_address_1 = '" . $this->db->escape($order['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($order['payment_address_2']) . "', payment_city = '" . $this->db->escape($order['payment_city']) . "', payment_postcode = '" . $this->db->escape($order['payment_postcode']) . "', payment_country = '" . $this->db->escape($order['payment_country']) . "', payment_country_id = '" . (int)$order['payment_country_id'] . "', payment_zone = '" . $this->db->escape($order['payment_zone']) . "', payment_zone_id = '" . (int)$order['payment_zone_id'] . "', payment_method = '" . $this->db->escape($order['payment_method']) . "', payment_code = '" . $this->db->escape($order['payment_code']) . "', shipping_firstname = '" . $this->db->escape($order['shipping_firstname']) . "', shipping_lastname = '" . $this->db->escape($order['shipping_lastname']) . "', shipping_address_1 = '" . $this->db->escape($order['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($order['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($order['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($order['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($order['shipping_country']) . "', shipping_country_id = '" . (int)$order['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($order['shipping_zone']) . "', shipping_zone_id = '" . (int)$order['shipping_zone_id'] . "', shipping_method = '" . $this->db->escape($order['shipping_method']) . "', shipping_code = '" . $this->db->escape($order['shipping_code']) . "', comment = '" . $this->db->escape($order['comment']) . "', total = '" . (float)$order['total'] . "', order_status_id = '" . (int)$order['order_status_id'] . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'"); + + $this->db->query("DELETE FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'"); + $this->db->query("DELETE FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "'"); + + // Products + if (isset($order['order_product']) && $order['order_product']) { + $this->addOrderProducts($order_id, $order['order_product']); + } + + // Totals + $this->db->query("DELETE FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'"); + + if (isset($order['order_total'])) { + $this->addOrderTotals($order_id, $order['order_total']); + } + } + + /** + * Add order products + * + * @param int $order_id + * @param array $products + * + * @return void + */ + public function addOrderProducts($order_id, $products) + { + foreach ($products as $product) { + $this->db->query("INSERT INTO " . DB_PREFIX . "order_product SET order_id = '" . (int)$order_id . "', product_id = '" . (int)$product['product_id'] . "', name = '" . $this->db->escape($product['name']) . "', model = '" . $this->db->escape($product['model']) . "', quantity = '" . (int)$product['quantity'] . "', price = '" . (float)$product['price'] . "', total = '" . (float)$product['total'] . "'"); + + $order_product_id = $this->db->getLastId(); + + foreach ($product['option'] as $option) { + $this->db->query("INSERT INTO " . DB_PREFIX . "order_option SET order_id = '" . (int)$order_id . "', order_product_id = '" . (int)$order_product_id . "', product_option_id = '" . (int)$option['product_option_id'] . "', product_option_value_id = '" . (int)$option['product_option_value_id'] . "', name = '" . $this->db->escape($option['name']) . "', `value` = '" . $this->db->escape($option['value']) . "', `type` = '" . $this->db->escape($option['type']) . "'"); + } + } + } + + /** + * Add order totals + * + * @param int $order_id + * @param array $totals + * + * @return void + */ + public function addOrderTotals($order_id, $totals) + { + foreach ($totals as $total) { + $this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'"); + } + } + + /** + * Get total titles + * + * @return string $title + */ + protected function totalTitles() + { + if (version_compare(VERSION, '3.0', '<')) { + $title = ''; + } else { + $title = 'total_'; + } + + return $title; + } + + /** + * Get country by iso code 2 + * + * @param string $isoCode + * + * @return array + */ + public function getCountryByIsoCode($isoCode) + { + $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE iso_code_2 = '" . $isoCode . "'"); + + return $query->row; + } + + /** + * Get zone by name + * + * @param string $name + * + * @return array + */ + public function getZoneByName($name) + { + $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE name = '" . $name . "'"); + + return $query->row; + } + + /** + * Get currency + * + * @param string $code + * @param string $field (default = '') + * + * @return mixed array | string + */ + public function getCurrencyByCode($code, $field = '') + { + $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "currency` WHERE code = '" . $code . "'"); + + if (!$field) { + return $query->row; + } + + return $query->row[$field]; + } + + /** + * Get language + * + * @param string $code + * @param string $field (default = '') + * + * @return mixed array | string + */ + public function getLanguageByCode($code, $field = '') + { + $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "language` WHERE code = '" . $code . "'"); + + if (!$field) { + return $query->row; + } + + return $query->row[$field]; + } + + /** + * Get product option value + * + * @param int $option_value_id + * @param string $field + * + * @return mixed array | string + */ + public function getOptionValue($option_value_id, $field = '') + { + $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_value_description` WHERE option_value_id = '" . $option_value_id . "'"); + + if (!$field) { + return $query->row; + } + + return $query->row[$field]; + } +} diff --git a/admin/model/retailcrm/customer.php b/admin/model/retailcrm/customer.php index 84b8488..ce3ed4b 100644 --- a/admin/model/retailcrm/customer.php +++ b/admin/model/retailcrm/customer.php @@ -51,7 +51,7 @@ class ModelRetailcrmCustomer extends Model { private function setLogs() { - if (version_compare(VERSION, '2.0', '>')) { + if (version_compare(VERSION, '2.1', '>')) { $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { $logs = DIR_SYSTEM . 'logs/retailcrm.log'; diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 1d5068b..6d9cc97 100644 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -1,6 +1,8 @@ ordersHistory(array( 'startDate' => $lastRun->format('Y-m-d H:i:s'), ), 1, 100); - if(!$packs->isSuccessful() && count($packs->history) <= 0) + if (!$packs->isSuccessful() && count($packs->history) <= 0) { + return false; + } + $orders = RetailcrmHistoryHelper::assemblyOrder($packs->history); $generatedAt = $packs['generatedAt']; @@ -64,7 +69,7 @@ class ModelRetailcrmHistory extends Model $this->delivery = array_flip($settings['retailcrm_delivery']); $this->payment = array_flip($settings['retailcrm_payment']); $this->status = array_flip($settings['retailcrm_status']); - + $this->settings = $settings; $this->ocPayment = $this->model_retailcrm_references ->getOpercartPaymentTypes(); @@ -78,7 +83,9 @@ class ModelRetailcrmHistory extends Model foreach ($orders as $order) { - if (isset($order['deleted'])) continue; + if (isset($order['deleted'])) { + continue; + } if (isset($order['externalId'])) { $updatedOrders[] = $order['id']; @@ -117,52 +124,84 @@ class ModelRetailcrmHistory extends Model protected function updateOrders($orders) { foreach ($orders as $order) { - $store = $this->config->get('config_store_id'); + $ocOrder = $this->model_sale_order->getOrder($order['externalId']); + + if (isset($order['paymentType'])) { + $payment['type'] = $order['paymentType']; + } $data = array(); - $data['store_id'] = $store == null ? 0 : $store; + $mail = isset($order['email']) ? $order['email'] : $order['customer']['email']; + $phone = isset($order['phone']) ? $order['phone'] : ''; + + if (!$phone) { + $data['telephone'] = $order['customer']['phones'] ? $order['customer']['phones'][0]['number'] : '80000000000'; + } else { + $data['telephone'] = $phone; + } + $data['customer'] = $order['firstName']; $data['customer_id'] = (!empty($order['customer']['externalId'])) ? $order['customer']['externalId'] : 0; $data['customer_group_id'] = 1; $data['firstname'] = $order['firstName']; - $data['lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' '; - $data['email'] = $order['email']; - $data['telephone'] = (!empty($order['phone'])) ? $order['phone'] : ''; + $data['lastname'] = isset($order['lastName']) ? $order['lastName'] : $order['firstName']; + $data['email'] = $mail ? $mail : uniqid() . '@retailrcm.ru'; $data['comment'] = !empty($order['customerComment']) ? $order['customerComment'] : ''; - $data['fax'] = ''; - $data['payment_address'] = '0'; $data['payment_firstname'] = $order['firstName']; - $data['payment_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' '; + $data['payment_lastname'] = isset($order['lastName']) ? $order['lastName'] : $order['firstName']; $data['payment_address_1'] = isset($order['customer']['address']) ? $order['customer']['address']['text'] : ''; $data['payment_address_2'] = ''; - $data['payment_company'] = ''; - $data['payment_company_id'] = ''; $data['payment_city'] = !empty($order['customer']['address']['city']) ? $order['customer']['address']['city'] : $order['delivery']['address']['city']; $data['payment_postcode'] = !empty( $order['customer']['address']['index'] ) ? $order['customer']['address']['index'] : $order['delivery']['address']['index']; - $region = ''; + $shippingZone = ''; if (is_int($order['delivery']['address']['region'])) { - $region = $order['delivery']['address']['region']; + $shippingZone = $order['delivery']['address']['region']; } else { - foreach ($this->zones as $zone) { - if ($order['delivery']['address']['region'] == $zone['name']) { - $region = $zone['zone_id']; - } + $shippingZone = $this->getZoneByName($order['delivery']['address']['region']); + + if ($shippingZone) { + $shipping_zone_id = $shippingZone['zone_id']; + } else { + $shipping_zone_id = 0; } } - $data['payment_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0; - $data['payment_zone_id'] = !empty($order['delivery']['address']['region']) ? $order['delivery']['address']['region'] : $region; + if (isset($order['customer']['address']['region'])) { + $paymentZone = $this->getZoneByName($order['customer']['address']['region']); - $data['shipping_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0; - $data['shipping_zone_id'] = $region; + if ($paymentZone) { + $payment_zone_id = $paymentZone['zone_id']; + } else { + $payment_zone_id = 0; + } + } + if (isset($order['countryIso']) && !empty($order['countryIso'])) { + $shippingCountry = $this->getCountryByIsoCode($order['countryIso']); + } + + if (isset($order['customer']['address']['countryIso']) && !empty($order['customer']['address']['countryIso'])) { + $paymentCountry = $this->getCountryByIsoCode($order['customer']['address']['countryIso']); + } else { + $paymentCountry = $this->getCountryByIsoCode($order['countryIso']); + } + + $delivery = isset($order['delivery']['code']) ? $order['delivery']['code'] : null; + $data['payment_country_id'] = $paymentCountry ? $paymentCountry['country_id'] : $ocOrder['payment_country_id']; + $data['payment_country'] = isset($paymentCountry) ? $paymentCountry['name'] : $ocOrder['payment_country']; + $data['payment_zone_id'] = $payment_zone_id ? $payment_zone_id : $ocOrder['payment_zone_id']; + $data['payment_zone'] = isset($order['customer']['address']['region']) ? $order['customer']['address']['region'] : $ocOrder['payment_zone']; + $data['shipping_country_id'] = $shippingCountry ? $shippingCountry['country_id'] : $ocOrder['shipping_country_id']; + $data['shipping_country'] = $shippingCountry ? $shippingCountry['name'] : $ocOrder['shipping_country']; + $data['shipping_zone_id'] = $shipping_zone_id ? $shipping_zone_id : $ocOrder['shipping_zone_id']; + $data['shipping_zone'] = $shippingZone ? $shippingZone['name'] : $ocOrder['shipping_zone']; $data['shipping_address'] = '0'; $data['shipping_firstname'] = $order['firstName']; - $data['shipping_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' '; + $data['shipping_lastname'] = isset($order['lastName']) ? $order['lastName'] : $order['firstName']; $data['shipping_address_1'] = $order['delivery']['address']['text']; $data['shipping_address_2'] = ''; $data['shipping_company'] = ''; @@ -170,13 +209,40 @@ class ModelRetailcrmHistory extends Model $data['shipping_city'] = $order['delivery']['address']['city']; $data['shipping_postcode'] = $order['delivery']['address']['index']; - $data['shipping'] = $this->delivery[$order['delivery']['code']]; - $data['shipping_method'] = $this->ocDelivery[$data['shipping']]; - $data['shipping_code'] = $this->delivery[$order['delivery']['code']]; + if ($delivery !== null) { + if (isset($this->settings['retailcrm_delivery'][$ocOrder['shipping_code']]) + && isset($this->delivery[$delivery]) + ) { + $data['shipping'] = $this->delivery[$delivery]; - $data['payment'] = $this->payment[$order['paymentType']]; - $data['payment_method'] = $this->ocPayment[$data['payment']]; - $data['payment_code'] = $this->payment[$order['paymentType']]; + $shipping = explode('.', $data['shipping']); + $shippingModule = $shipping[0]; + + if (isset($this->ocDelivery[$shippingModule][$data['shipping']]['title'])) { + $data['shipping_method'] = $this->ocDelivery[$shippingModule][$data['shipping']]['title']; + } else { + $data['shipping_method'] =$this->ocDelivery[$shippingModule]['title']; + } + + $data['shipping_code'] = $data['shipping']; + } + } else { + if (!isset($this->settings[$ocOrder['shipping_code']]) + || !isset($this->delivery[$delivery]) + ) { + $data['shipping_method'] = $ocOrder['shipping_method']; + $data['shipping_code'] = $ocOrder['shipping_code']; + } + } + + if (isset($payment)) { + $data['payment'] = $this->payment[$payment['type']]; + $data['payment_method'] = isset($this->ocPayment[$data['payment']]) ? $this->ocPayment[$data['payment']] : $ocOrder['payment_method']; + $data['payment_code'] = isset($this->payment[$payment['type']]) ? $this->payment[$payment['type']] : $ocOrder['payment_code']; + } else { + $data['payment_method'] = $ocOrder['payment_method']; + $data['payment_code'] = $ocOrder['payment_code']; + } // this data will not retrive from crm for now $data['tax'] = ''; @@ -196,15 +262,15 @@ class ModelRetailcrmHistory extends Model $data['order_product'] = array(); foreach ($order['items'] as $item) { - //$product = $this->model_catalog_product->getProduct($item['offer']['externalId']); $productId = $item['offer']['externalId']; $options = array(); - if(mb_strpos($item['offer']['externalId'], '#') > 1) { + + if (mb_strpos($item['offer']['externalId'], '#') > 1) { $offer = explode('#', $item['offer']['externalId']); $productId = $offer[0]; $optionsFromCRM = explode('_', $offer[1]); - foreach($optionsFromCRM as $optionFromCRM) { + foreach ($optionsFromCRM as $optionFromCRM) { $optionData = explode('-', $optionFromCRM); $productOptionId = $optionData[0]; $optionValueId = $optionData[1]; @@ -215,14 +281,27 @@ class ModelRetailcrmHistory extends Model if($productOptionId == $productOption['product_option_id']) { foreach($productOption['product_option_value'] as $productOptionValue) { if($productOptionValue['option_value_id'] == $optionValueId) { - $options[$productOptionId] = $productOptionValue['product_option_value_id']; + $options[] = array( + 'product_option_id' => $productOptionId, + 'product_option_value_id' => $productOptionValue['product_option_value_id'], + 'value' => $this->getOptionValue($productOptionValue['option_value_id'], 'name'), + 'type' => $productOption['type'], + 'name' => $productOption['name'], + ); } } } } } } + + $product = $this->model_catalog_product->getProduct($productId); + $data['order_product'][] = array( + 'name' => $product['name'], + 'model' => $product['model'], + 'price' => $item['initialPrice'], + 'total' => (float)($item['initialPrice'] * $item['quantity']), 'product_id' => $productId, 'quantity' => $item['quantity'], 'option' => $options @@ -231,7 +310,7 @@ class ModelRetailcrmHistory extends Model $deliveryCost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : 0; - if(isset($order['discount']) && $order['discount'] > 0) { + if (isset($order['discount']) && $order['discount'] > 0) { $orderTotals = $this->model_sale_order->getOrderTotals($order['externalId']); foreach($orderTotals as $orderTotal) { if($orderTotal['code'] == 'coupon') { @@ -240,6 +319,7 @@ class ModelRetailcrmHistory extends Model } } + $data['total'] = $order['totalSumm']; $data['order_total'] = array( array( 'order_total_id' => '', @@ -252,10 +332,10 @@ class ModelRetailcrmHistory extends Model array( 'order_total_id' => '', 'code' => 'shipping', - 'title' => $this->ocDelivery[$data['shipping_code']], + 'title' => $data['shipping_method'], 'value' => $deliveryCost, 'text' => $deliveryCost, - 'sort_order' => $this->shippingSettings['shipping_sort_order'] + 'sort_order' => $this->shippingSettings[$this->totalTitle . 'shipping_sort_order'] ), array( 'order_total_id' => '', @@ -263,7 +343,7 @@ class ModelRetailcrmHistory extends Model 'title' => $this->language->get('column_total'), 'value' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, 'text' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, - 'sort_order' => $this->totalSettings['total_sort_order'] + 'sort_order' => $this->totalSettings[$this->totalTitle . 'total_sort_order'] ) ); @@ -276,7 +356,8 @@ class ModelRetailcrmHistory extends Model $data['order_status_id'] = $tmpOrder['order_status_id']; } - $this->opencartApiClient->editOrder($order['externalId'], $data); + $this->editOrder($order['externalId'], $data); + $this->opencartApiClient->addHistory($order['externalId'], $data['order_status_id']); } } @@ -288,6 +369,10 @@ class ModelRetailcrmHistory extends Model foreach ($orders as $order) { $store = $this->config->get('config_store_id'); + if (isset($order['paymentType'])) { + $payment['type'] = $order['paymentType']; + } + $customer_id = (!empty($order['customer']['externalId'])) ? $order['customer']['externalId'] : 0; @@ -306,6 +391,8 @@ class ModelRetailcrmHistory extends Model 'newsletter' => 0, 'password' => 'tmppass', 'status' => 1, + 'approved' => 1, + 'safe' => 0, 'address' => array( array( 'firstname' => $order['firstName'], @@ -348,19 +435,33 @@ class ModelRetailcrmHistory extends Model $customersIdsFix[] = array('id' => $order['customer']['id'], 'externalId' => (int)$customer_id); } + $mail = isset($order['email']) ? $order['email'] : $order['customer']['email']; + $phone = isset($order['phone']) ? $order['phone'] : ''; + + if (!$phone) { + $data['telephone'] = $order['customer']['phones'] ? $order['customer']['phones'][0]['number'] : '80000000000'; + } else { + $data['telephone'] = $phone; + } + + $data['store_url'] = $this->config->get('config_url'); + $data['currency_code'] = $this->config->get('config_currency'); + $data['currency_value'] = $this->getCurrencyByCode($data['currency_code'], 'value'); + $data['currency_id'] = $this->getCurrencyByCode($data['currency_code'], 'currency_id'); + $data['language_id'] = $this->getLanguageByCode($this->config->get('config_language'), 'language_id'); $data['store_id'] = $store == null ? 0 : $store; + $data['store_name'] = $this->config->get('config_name'); $data['customer'] = $order['firstName']; $data['customer_id'] = $customer_id; $data['customer_group_id'] = 1; $data['firstname'] = $order['firstName']; - $data['lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' '; - $data['email'] = $order['email']; - $data['telephone'] = (!empty($order['customer']['phones'][0]['number'])) ? $order['customer']['phones'][0]['number'] : ' '; + $data['lastname'] = (isset($order['lastName'])) ? $order['lastName'] : $order['firstName']; + $data['email'] = $mail ? $mail : uniqid() . '@retailrcm.ru'; $data['comment'] = !empty($order['customerComment']) ? $order['customerComment'] : ''; $data['fax'] = ''; $data['payment_address'] = '0'; $data['payment_firstname'] = $order['firstName']; - $data['payment_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' '; + $data['payment_lastname'] = (isset($order['lastName'])) ? $order['lastName'] : $order['firstName']; $data['payment_address_1'] = $order['customer']['address']['text']; $data['payment_address_2'] = ''; $data['payment_company'] = ''; @@ -368,38 +469,77 @@ class ModelRetailcrmHistory extends Model $data['payment_city'] = !empty($order['customer']['address']['city']) ? $order['customer']['address']['city'] : $order['delivery']['address']['city']; $data['payment_postcode'] = !empty($order['customer']['address']['index']) ? $order['customer']['address']['index'] : $order['delivery']['address']['index']; - $region = ''; + $shippingZone = ''; if (!empty($order['delivery']['address']['region']) && is_int($order['delivery']['address']['region'])) { - $region = $order['delivery']['address']['region']; + $shippingZone = $order['delivery']['address']['region']; } else { - foreach ($this->zones as $zone) { - if ($order['delivery']['address']['region'] == $zone['name']) { - $region = $zone['zone_id']; - } + $shippingZone = $this->getZoneByName($order['delivery']['address']['region']); + + if ($shippingZone) { + $shipping_zone_id = $shippingZone['zone_id']; + } else { + $shipping_zone_id = 0; } } - $data['payment_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0; - $data['payment_zone_id'] = !empty($order['delivery']['address']['region']) ? $order['delivery']['address']['region'] : $region; - $data['shipping_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0; - $data['shipping_zone_id'] = $region; + if (isset($order['customer']['address']['region'])) { + $paymentZone = $this->getZoneByName($order['customer']['address']['region']); + + if ($paymentZone) { + $payment_zone_id = $paymentZone['zone_id']; + } else { + $payment_zone_id = 0; + } + } + + if (isset($order['delivery']['address']['countryIso'])) { + $shippingCountry = $this->getCountryByIsoCode($order['delivery']['address']['countryIso']); + } + + if (isset($order['customer']['address']['countryIso'])) { + $paymentCountry = $this->getCountryByIsoCode($order['customer']['address']['countryIso']); + } + + $delivery = isset($order['delivery']['code']) ? $order['delivery']['code'] : null; + $data['payment_country_id'] = $paymentCountry ? $paymentCountry['country_id'] : 0; + $data['payment_country'] = isset($paymentCountry) ? $paymentCountry['name'] : ''; + $data['payment_zone_id'] = $payment_zone_id; + $data['payment_zone'] = isset($order['customer']['address']['region']) ? $order['customer']['address']['region'] : ''; + $data['shipping_country_id'] = $shippingCountry ? $shippingCountry['country_id'] : 0; + $data['shipping_country'] = $shippingCountry ? $shippingCountry['name'] : ''; + $data['shipping_zone_id'] = $shipping_zone_id; + $data['shipping_zone'] = $shippingZone ? $shippingZone['name'] : $data['payment_zone']; $data['shipping_address'] = '0'; $data['shipping_firstname'] = $order['firstName']; - $data['shipping_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' '; + $data['shipping_lastname'] = (isset($order['lastName'])) ? $order['lastName'] : $order['firstName']; $data['shipping_address_1'] = $order['delivery']['address']['text']; $data['shipping_address_2'] = ''; $data['shipping_company'] = ''; $data['shipping_company_id'] = ''; $data['shipping_city'] = $order['delivery']['address']['city']; $data['shipping_postcode'] = $order['delivery']['address']['index']; + $data['shipping'] = $delivery != null ? $this->delivery[$delivery] : ''; + $data['shipping_code'] = $delivery != null ? $this->delivery[$delivery] : ''; - $data['shipping'] = $this->delivery[$order['delivery']['code']]; - $data['shipping_method'] = $this->ocDelivery[$data['shipping']]; - $data['shipping_code'] = $this->delivery[$order['delivery']['code']]; - $data['payment'] = $this->payment[$order['paymentType']]; - $data['payment_method'] = $this->ocPayment[$data['payment']]; - $data['payment_code'] = $this->payment[$order['paymentType']]; + $shipping = explode('.', $data['shipping']); + $shippingModule = $shipping[0]; + + if (isset($this->ocDelivery[$shippingModule][$data['shipping']]['title'])) { + $data['shipping_method'] = $this->ocDelivery[$shippingModule][$data['shipping']]['title']; + } else { + $data['shipping_method'] =$this->ocDelivery[$shippingModule]['title']; + } + + if (isset($payment)) { + $data['payment'] = $this->payment[$payment['type']]; + $data['payment_method'] = $this->ocPayment[$data['payment']]; + $data['payment_code'] = $this->payment[$payment['type']]; + } else { + $data['payment'] = 'free_checkout'; + $data['payment_method'] = $this->ocPayment[$data['payment']]; + $data['payment_code'] = 'free_checkout'; + } // this data will not retrive from crm for now $data['tax'] = ''; @@ -408,7 +548,7 @@ class ModelRetailcrmHistory extends Model $data['product_id'] = ''; $data['reward'] = ''; $data['affiliate'] = ''; - $data['affiliate_id'] = ''; + $data['affiliate_id'] = 0; $data['payment_tax_id'] = ''; $data['order_product_id'] = ''; $data['payment_company'] = ''; @@ -419,24 +559,55 @@ class ModelRetailcrmHistory extends Model $data['order_product'] = array(); foreach ($order['items'] as $item) { - $product = $this->model_catalog_product->getProduct($item['offer']['externalId']); - $data['order_product'][] = array( - 'product_id' => $item['offer']['externalId'], - 'name' => $item['offer']['name'], - 'quantity' => $item['quantity'], - 'price' => $item['initialPrice'], - 'total' => $item['initialPrice'] * $item['quantity'], - 'model' => $product['model'], + $productId = $item['offer']['externalId']; + $options = array(); - // this data will not retrive from crm - 'order_product_id' => '', - 'tax' => 0, - 'reward' => 0 + if(mb_strpos($item['offer']['externalId'], '#') > 1) { + $offer = explode('#', $item['offer']['externalId']); + $productId = $offer[0]; + $optionsFromCRM = explode('_', $offer[1]); + + foreach ($optionsFromCRM as $optionFromCRM) { + $optionData = explode('-', $optionFromCRM); + $productOptionId = $optionData[0]; + $optionValueId = $optionData[1]; + + $productOptions = $this->model_catalog_product->getProductOptions($productId); + + foreach($productOptions as $productOption) { + if($productOptionId == $productOption['product_option_id']) { + foreach($productOption['product_option_value'] as $productOptionValue) { + if($productOptionValue['option_value_id'] == $optionValueId) { + $options[] = array( + 'product_option_id' => $productOptionId, + 'product_option_value_id' => $productOptionValue['product_option_value_id'], + 'value' => $this->getOptionValue($productOptionValue['option_value_id'], 'name'), + 'type' => $productOption['type'], + 'name' => $productOption['name'], + ); + } + } + } + } + } + } + + $product = $this->model_catalog_product->getProduct($productId); + + $data['order_product'][] = array( + 'name' => $product['name'], + 'model' => $product['model'], + 'price' => $item['initialPrice'], + 'total' => (float)($item['initialPrice'] * $item['quantity']), + 'product_id' => $productId, + 'quantity' => $item['quantity'], + 'option' => $options ); } $deliveryCost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : 0; + $data['total'] = $order['totalSumm']; $data['order_total'] = array( array( 'order_total_id' => '', @@ -449,10 +620,10 @@ class ModelRetailcrmHistory extends Model array( 'order_total_id' => '', 'code' => 'shipping', - 'title' => $this->ocDelivery[$data['shipping_code']], + 'title' => $data['shipping_method'], 'value' => $deliveryCost, 'text' => $deliveryCost, - 'sort_order' => $this->shippingSettings['shipping_sort_order'] + 'sort_order' => $this->shippingSettings[$this->totalTitle . 'shipping_sort_order'] ), array( 'order_total_id' => '', @@ -460,18 +631,16 @@ class ModelRetailcrmHistory extends Model 'title' => $this->language->get('column_total'), 'value' => !empty($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, 'text' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, - 'sort_order' => $this->totalSettings['total_sort_order'] + 'sort_order' => $this->totalSettings[$this->totalTitle . 'total_sort_order'] ) ); $data['fromApi'] = true; $data['order_status_id'] = 1; - $this->opencartApiClient->addOrder($data); + $order_id = $this->addOrder($data); - $last = $this->model_sale_order->getOrders($data = array('order' => 'DESC', 'limit' => 1, 'start' => 0)); - - $ordersIdsFix[] = array('id' => $order['id'], 'externalId' => (int) $last[0]['order_id']); + $ordersIdsFix[] = array('id' => $order['id'], 'externalId' => (int) $order_id); } return array('customers' => $customersIdsFix, 'orders' => $ordersIdsFix); @@ -479,7 +648,7 @@ class ModelRetailcrmHistory extends Model private function setLogs() { - if (version_compare(VERSION, '2.0', '>')) { + if (version_compare(VERSION, '2.1', '>')) { $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { $logs = DIR_SYSTEM . 'logs/retailcrm.log'; diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index 19d835b..e0f7374 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -135,7 +135,7 @@ class ModelRetailcrmOrder extends Model { private function setLogs() { - if (version_compare(VERSION, '2.0', '>')) { + if (version_compare(VERSION, '2.1', '>')) { $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { $logs = DIR_SYSTEM . 'logs/retailcrm.log'; diff --git a/admin/model/retailcrm/references.php b/admin/model/retailcrm/references.php index 8c4a553..a56c5d6 100644 --- a/admin/model/retailcrm/references.php +++ b/admin/model/retailcrm/references.php @@ -127,7 +127,7 @@ class ModelRetailcrmReferences extends Model private function setLogs() { - if (version_compare(VERSION, '2.0', '>')) { + if (version_compare(VERSION, '2.1', '>')) { $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { $logs = DIR_SYSTEM . 'logs/retailcrm.log'; diff --git a/admin/view/stylesheet/retailcrm.css b/admin/view/stylesheet/retailcrm.css index 43cbeb6..fd49d08 100644 --- a/admin/view/stylesheet/retailcrm.css +++ b/admin/view/stylesheet/retailcrm.css @@ -1,2 +1,3 @@ .retailcrm_unit {margin-bottom: 10px;} .retailcrm_unit input {width: 30%;} +.retailcrm_unit select {max-width: 500px;} \ No newline at end of file diff --git a/catalog/controller/api/retailcrm.php b/catalog/controller/api/retailcrm.php index 13d8ba9..202670a 100644 --- a/catalog/controller/api/retailcrm.php +++ b/catalog/controller/api/retailcrm.php @@ -4,13 +4,19 @@ class ControllerApiRetailcrm extends Controller { public function getDeliveryTypes() { - $this->load->model('localisation/country'); - $this->load->model('setting/setting'); - $countries = $this->model_setting_setting->getSetting('retailcrm')['retailcrm_country']; - $deliveryTypes = array(); + $api = $this->auth(); - foreach ($countries as $country) { - $deliveryTypes = array_merge($deliveryTypes, $this->getDeliveryTypesByZones($country)); + if (isset($api['error'])) { + $response = $api; + } else { + $this->load->model('localisation/country'); + $this->load->model('setting/setting'); + $countries = $this->model_setting_setting->getSetting('retailcrm')['retailcrm_country']; + $response = array(); + + foreach ($countries as $country) { + $response = array_merge($response, $this->getDeliveryTypesByZones($country)); + } } if (isset($this->request->server['HTTP_ORIGIN'])) { @@ -21,7 +27,32 @@ class ControllerApiRetailcrm extends Controller } $this->response->addHeader('Content-Type: application/json'); - $this->response->setOutput(json_encode($deliveryTypes)); + $this->response->setOutput(json_encode($response)); + } + + public function addOrderHistory() + { + $api = $this->auth(); + + if (isset($api['error'])) { + $response = $api; + } elseif (!isset($this->request->post['order_id']) || !isset($this->request->post['order_status_id'])) { + $response = array('error' => 'Not found data'); + } else { + $this->load->model('checkout/order'); + $this->model_checkout_order->addOrderHistory($this->request->post['order_id'], $this->request->post['order_status_id']); + $response = array('success' => true); + } + + if (isset($this->request->server['HTTP_ORIGIN'])) { + $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']); + $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); + $this->response->addHeader('Access-Control-Max-Age: 1000'); + $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With'); + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($response)); } protected function getDeliveryTypesByZones($country_id) @@ -60,6 +91,19 @@ class ControllerApiRetailcrm extends Controller if($this->{'model_shipping_' . $shippingModule['code']}->getQuote($address)) { $quote_data[] = $this->{'model_shipping_' . $shippingModule['code']}->getQuote($address); + } else { + $this->load->language('shipping/' . $shippingModule['code']); + + $quote_data[] = array( + 'code' => $shippingModule['code'], + 'title' => $this->language->get('text_title'), + 'quote' => array( + array( + 'code' => $shippingModule['code'], + 'title' => $this->language->get('text_title') + ) + ) + ); } } } @@ -68,14 +112,59 @@ class ControllerApiRetailcrm extends Controller $deliveryTypes = array(); foreach ($quote_data as $shipping) { - foreach ($shipping['quote'] as $shippingMethod) { $deliveryTypes[$shipping['code']]['title'] = $shipping['title']; $deliveryTypes[$shipping['code']][$shippingMethod['code']] = $shippingMethod; } - } return $deliveryTypes; } + + private function auth() + { + if (version_compare(VERSION, '2.1.0', '>=')) { + if (!isset($this->request->get['token']) + || !$this->request->get['token'] + ) { + return array('error' => 'Not found api key'); + } + + if (isset($this->request->get['token']) + && !empty($this->request->get['token']) + ) { + $this->load->model('account/api'); + $api = $this->model_account_api->getApiByKey($this->request->get['token']); + + if (!empty($api)) { + return $api; + } + + return array('error' => 'Invalid api key'); + } + } else { + if (!isset($this->request->get['username']) + || !$this->request->get['username'] + || !isset($this->request->get['password']) + || !$this->request->get['password'] + ) { + return array('error' => 'Not found api user'); + } + + if (isset($this->request->get['username']) + && !empty($this->request->get['username']) + && isset($this->request->get['password']) + && !empty($this->request->get['password']) + ) { + $this->load->model('account/api'); + $api = $this->model_account_api->login($this->request->get['username'], $this->request->get['password']); + + if (!empty($api)) { + return $api; + } + + return array('error' => 'Invalid api user'); + } + } + } } diff --git a/catalog/model/retailcrm/customer.php b/catalog/model/retailcrm/customer.php index 80f9f7b..9421f8f 100644 --- a/catalog/model/retailcrm/customer.php +++ b/catalog/model/retailcrm/customer.php @@ -42,7 +42,7 @@ class ModelRetailcrmCustomer extends Model { private function setLogs() { - if (version_compare(VERSION, '2.0', '>')) { + if (version_compare(VERSION, '2.1', '>')) { $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { $logs = DIR_SYSTEM . 'logs/retailcrm.log'; diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index 8704538..965b17f 100644 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -274,7 +274,7 @@ class ModelRetailcrmOrder extends Model { private function setLogs() { - if (version_compare(VERSION, '2.0', '>')) { + if (version_compare(VERSION, '2.1', '>')) { $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { $logs = DIR_SYSTEM . 'logs/retailcrm.log'; diff --git a/system/library/retailcrm/OpencartApiClient.php b/system/library/retailcrm/OpencartApiClient.php index e21b1da..f1a8a30 100644 --- a/system/library/retailcrm/OpencartApiClient.php +++ b/system/library/retailcrm/OpencartApiClient.php @@ -40,11 +40,13 @@ class OpencartApiClient { return false; } - public function request($method, $getParams, $postParams) { + private function request($method, $getParams, $postParams) { $opencartStoreInfo = $this->model_setting_store->getStore($this->opencartStoreId); - if(version_compare(VERSION, '2.1.0', '>=') && !empty($this->apiToken)) { + if (version_compare(VERSION, '2.1.0', '>=') && !empty($this->apiToken)) { $getParams['token'] = $this->apiToken; + } elseif (is_array($this->apiToken) && isset($this->apiToken['username'])) { + $getParams = $this->apiToken; } $postParams['fromApi'] = true; @@ -89,7 +91,7 @@ class OpencartApiClient { $api = array(); foreach ($apiUsers as $apiUser) { if($apiUser['status'] == 1) { - if(version_compare(VERSION, '2.1.0', '>=')) { + if (version_compare(VERSION, '2.1.0', '>=')) { $api = array( 'api_id' => $apiUser['api_id'], 'key' => $apiUser['key'] @@ -106,191 +108,33 @@ class OpencartApiClient { } } - if(!isset($api['api_id'])) + if(!isset($api['api_id'])) { return false; - - if(version_compare(VERSION, '2.1.0', '>=')) { - $alreadyBinded = false; - - $innerIp = $this->getInnerIpAddr(); - $apiIps = $this->model_user_api->getApiIps($api['api_id']); - foreach($apiIps as $apiIp) { - if($apiIp['ip'] == $innerIp) - $alreadyBinded = true; - } - - if(!$alreadyBinded) { - $this->model_user_api->addApiIp($api['api_id'], $innerIp); - } } - $apiAnswer = $this->request('login', array(), $apiUser); - - if(version_compare(VERSION, '2.1.0', '>=')) { - $this->apiToken = $apiAnswer['token']; + if (isset($api['key'])) { + $this->apiToken = $api['key']; + } elseif (isset($api['username'])) { + $this->apiToken = $api; + } else { + $this->apiToken = false; } - - return $apiAnswer; } - public function editOrder($order_id, $data) { - $data['telephone'] = trim($data['telephone']); - $customer = array( - 'currency' => isset($data['currency']) ? $data['currency'] : '', - 'customer' => $data['customer'], - 'customer_id' => $data['customer_id'], - 'customer_group_id' => $data['customer_group_id'], - 'firstname' => $data['firstname'], - 'lastname' => $data['lastname'], - 'email' => $data['email'], - 'telephone' => !empty($data['telephone']) ? $data['telephone'] : '0000', - 'fax' => $data['fax'], - ); - $this->request('customer', array(), $customer); - - $products = array(); - foreach ($data['order_product'] as $order_product) { - $products[] = array( - 'product_id' => $order_product['product_id'], - 'quantity' => $order_product['quantity'], - 'option' => $order_product['option'] - ); - } - $this->request('cart/add', array(), array('product' => $products)); - - $payment_address = array( - 'payment_address' => $data['payment_address'], - 'firstname' => $data['payment_firstname'], - 'lastname' => $data['payment_lastname'], - 'company' => $data['payment_company'], - 'address_1'=> $data['payment_address_1'], - 'address_2' => $data['payment_address_2'], - 'city' => !empty($data['payment_city']) ? $data['payment_city'] : 'none', - 'postcode' => $data['payment_postcode'], - 'country_id' => $data['payment_country_id'], - 'zone_id' => !empty($data['payment_zone_id']) ? $data['payment_zone_id'] : 0, - ); - $this->request('payment/address', array(), $payment_address); - - $this->request('payment/methods', array(), array()); - $payment_method = array( - 'payment_method' => $data['payment_code'] - ); - $this->request('payment/method', array(), $payment_method); - - $shipping_address = array( - 'shipping_address' => $data['shipping_address'], - 'firstname' => $data['shipping_firstname'], - 'lastname' => $data['shipping_lastname'], - 'company' => $data['shipping_company'], - 'address_1' => $data['shipping_address_1'], - 'address_2' => $data['shipping_address_2'], - 'city' => !empty($data['shipping_city']) ? $data['shipping_city'] : 'none', - 'postcode' => $data['shipping_postcode'], - 'country_id' => $data['shipping_country_id'], - 'zone_id' => !empty($data['shipping_zone_id']) ? $data['shipping_zone_id'] : 0, - ); - $this->request('shipping/address', array(), $shipping_address); - - $this->request('shipping/methods', array(), array()); - $shipping_method = array( - 'shipping_method' => $data['shipping_code'] - ); - $this->request('shipping/method', array(), $shipping_method); - - $order = array( - 'shipping_method' => $data['shipping_code'], - 'payment_method' => $data['payment_code'], - 'order_status_id' => $data['order_status_id'], - 'comment' => $data['comment'], - 'affiliate_id' => $data['affiliate_id'], - ); - $this->request('order/edit', array('order_id' => $order_id), $order); - } - - public function addOrder($data) { - $currency = $this->getCookieValue('currency'); - if($currency) { - $a = $this->request('currency', array(), array('currency' => $currency)); - } - - $customer = array( - 'store_id' => $data['store_id'], - 'currency' => $currency != false ? $currency : '', - 'customer' => $data['customer'], - 'customer_id' => $data['customer_id'], - 'customer_group_id' => $data['customer_group_id'], - 'firstname' => $data['firstname'], - 'lastname' => $data['lastname'], - 'email' => $data['email'], - 'telephone' => $data['telephone'], - 'fax' => $data['fax'], - ); - $this->request('customer', array(), $customer); - - $products = array(); - foreach($data['order_product'] as $product) { - $product = array( - 'product_id' => $product['product_id'], - 'quantity' => $product['quantity'], - ); - $products[] = $product; - } - $this->request('cart/add', array(), array('product' => $products)); - - $payment_address = array( - 'payment_address' => $data['payment_address'], - 'firstname' => $data['payment_firstname'], - 'lastname' => $data['payment_lastname'], - 'company' => $data['payment_company'], - 'address_1' => $data['payment_address_1'], - 'address_2' => $data['payment_address_2'], - 'city' => $data['payment_city'], - 'postcode' => $data['payment_postcode'], - 'country_id' => $data['payment_country_id'], - 'zone_id' => $data['payment_zone_id'], - ); - $this->request('payment/address', array(), $payment_address); - - $shipping_address = array( - 'shipping_address' => $data['shipping_address'], - 'firstname' => $data['shipping_firstname'], - 'lastname' => $data['shipping_lastname'], - 'company' => $data['shipping_company'], - 'address_1' => $data['shipping_address_1'], - 'address_2' => $data['shipping_address_2'], - 'city' => $data['shipping_city'], - 'postcode' => $data['shipping_postcode'], - 'country_id' => $data['shipping_country_id'], - 'zone_id' => !empty($data['shipping_zone_id']) ? $data['shipping_zone_id'] : 0, - ); - $this->request('shipping/address', array(), $shipping_address); - - $this->request('shipping/methods', array(), array()); - $shipping_method = array( - 'shipping_method' => $data['shipping_code'] - ); - $this->request('shipping/method', array(), $shipping_method); - - $this->request('payment/methods', array(), array()); - $payment_method = array( - 'payment_method' => $data['payment_code'] - ); - $this->request('payment/method', array(), $payment_method); - - $order = array( - 'shipping_method' => $data['shipping_code'], - 'payment_method' => $data['payment_code'], - 'order_status_id' => $data['order_status_id'], - 'comment' => $data['comment'], - 'affiliate_id' => $data['affiliate_id'], - ); - $this->request('order/add', array(), $order); + /** + * Add history order + * + * @param int $order_id + * @param int $order_status_id + * + * @return void + */ + public function addHistory($order_id, $order_status_id) + { + $this->request('retailcrm/addOrderHistory', array(), array('order_id' => $order_id, 'order_status_id' => $order_status_id)); } public function getDeliveryTypes() { - $this->apiToken = $this->session->data['token']; - return $this->request('retailcrm/getDeliveryTypes', array(), array()); } diff --git a/system/library/retailcrm/RetailcrmHistoryHelper.php b/system/library/retailcrm/RetailcrmHistoryHelper.php index bc5a718..7368d16 100644 --- a/system/library/retailcrm/RetailcrmHistoryHelper.php +++ b/system/library/retailcrm/RetailcrmHistoryHelper.php @@ -10,10 +10,13 @@ class RetailcrmHistoryHelper { } } $orders = array(); + foreach ($orderHistory as $change) { $change['order'] = self::removeEmpty($change['order']); - if($change['order']['items']) { + + if(isset($change['order']['items']) && $change['order']['items']) { $items = array(); + foreach($change['order']['items'] as $item) { if(isset($change['created'])) { $item['create'] = 1; @@ -23,55 +26,61 @@ class RetailcrmHistoryHelper { $change['order']['items'] = $items; } - if($change['order']['contragent']['contragentType']) { + if (isset($change['order']['contragent']['contragentType']) + && $change['order']['contragent']['contragentType'] + ) { $change['order']['contragentType'] = $change['order']['contragent']['contragentType']; unset($change['order']['contragent']); } - if($orders[$change['order']['id']]) { + if (isset($orders[$change['order']['id']]) && $orders[$change['order']['id']]) { $orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']); } else { $orders[$change['order']['id']] = $change['order']; } - if($change['item']) { - if($orders[$change['order']['id']]['items'][$change['item']['id']]) { + if (isset($change['item']) && $change['item']) { + if (isset($orders[$change['order']['id']]['items'][$change['item']['id']]) + && $orders[$change['order']['id']]['items'][$change['item']['id']] + ) { $orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']); } else { $orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item']; } - if(empty($change['oldValue']) && $change['field'] == 'order_product') { + if (empty($change['oldValue']) && $change['field'] == 'order_product') { $orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = true; } - if(empty($change['newValue']) && $change['field'] == 'order_product') { + if (empty($change['newValue']) && $change['field'] == 'order_product') { $orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = true; } - if(!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) { + if (!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) { $orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue']; } } else { - if($fields['delivery'][$change['field']] == 'service') { + if (isset($fields['delivery'][$change['field']]) + && $fields['delivery'][$change['field']] == 'service' + ) { $orders[$change['order']['id']]['delivery']['service']['code'] = self::newValue($change['newValue']); - } elseif($fields['delivery'][$change['field']]) { + } elseif (isset($fields['delivery'][$change['field']]) && $fields['delivery'][$change['field']]) { $orders[$change['order']['id']]['delivery'][$fields['delivery'][$change['field']]] = self::newValue($change['newValue']); - } elseif($fields['orderAddress'][$change['field']]) { + } elseif (isset($fields['orderAddress'][$change['field']]) && $fields['orderAddress'][$change['field']]) { $orders[$change['order']['id']]['delivery']['address'][$fields['orderAddress'][$change['field']]] = $change['newValue']; - } elseif($fields['integrationDelivery'][$change['field']]) { + } elseif (isset($fields['integrationDelivery'][$change['field']]) && $fields['integrationDelivery'][$change['field']]) { $orders[$change['order']['id']]['delivery']['service'][$fields['integrationDelivery'][$change['field']]] = self::newValue($change['newValue']); - } elseif($fields['customerContragent'][$change['field']]) { + } elseif (isset($fields['customerContragent'][$change['field']]) && $fields['customerContragent'][$change['field']]) { $orders[$change['order']['id']][$fields['customerContragent'][$change['field']]] = self::newValue($change['newValue']); - } elseif(strripos($change['field'], 'custom_') !== false) { + } elseif (strripos($change['field'], 'custom_') !== false) { $orders[$change['order']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']); - } elseif($fields['order'][$change['field']]) { + } elseif (isset($fields['order'][$change['field']]) && $fields['order'][$change['field']]) { $orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']); } - if(isset($change['created'])) { + if (isset($change['created'])) { $orders[$change['order']['id']]['create'] = 1; } - if(isset($change['deleted'])) { + if (isset($change['deleted'])) { $orders[$change['order']['id']]['deleted'] = 1; } } diff --git a/system/library/retailcrm/RetailcrmHttpClient.php b/system/library/retailcrm/RetailcrmHttpClient.php index 6ec0e5c..7f4d4e0 100644 --- a/system/library/retailcrm/RetailcrmHttpClient.php +++ b/system/library/retailcrm/RetailcrmHttpClient.php @@ -81,7 +81,6 @@ class RetailcrmHttpClient $curlHandler = curl_init(); curl_setopt($curlHandler, CURLOPT_URL, $url); curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curlHandler, CURLOPT_FAILONERROR, false); curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false);