diff --git a/CHANGELOG.md b/CHANGELOG.md index 76a68b3..8fe0294 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v.3.1.3 +* Добавлена возможность передачи акционных цен для нескольких групп пользователей +* Добавлена передача нулевой цены для неустановленных акционных цен +* Убрана базовая цена retailcrm из настроек соответствия типов цен + ## v.3.1.2 * Добавлен перевод на испанский язык * Переделан перевод на английский язык diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index e72bcd3..da23081 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -95,6 +95,7 @@ public function index() $this->load->model('localisation/country'); $this->load->model('setting/setting'); $this->load->model('extension/retailcrm/references'); + $this->load->model('customer/customer_group'); $this->load->language('extension/module/retailcrm'); $this->document->setTitle($this->language->get('heading_title')); $this->document->addStyle('/admin/view/stylesheet/retailcrm.css'); @@ -235,6 +236,9 @@ public function index() $key = isset($_data['saved_settings'][\Retailcrm\Retailcrm::MODULE . '_apikey']) ? $_data['saved_settings'][\Retailcrm\Retailcrm::MODULE . '_apikey'] : null; + $apiVersion = isset($_data['saved_settings'][\Retailcrm\Retailcrm::MODULE . '_apiversion']) + ? $_data['saved_settings'][\Retailcrm\Retailcrm::MODULE . '_apiversion'] + : null; if (!empty($url) && !empty($key)) { $_data['delivery'] = $this->model_extension_retailcrm_references @@ -245,6 +249,9 @@ public function index() ->getPaymentTypes($retailcrm_api_client); $_data['customFields'] = $this->model_extension_retailcrm_references ->getCustomFields($retailcrm_api_client); + $_data['priceTypes'] = $this->model_extension_retailcrm_references + ->getPriceTypes(); + $_data['customerGroups'] = $this->model_customer_customer_group->getCustomerGroups(); } $config_data = array( diff --git a/src/upload/admin/model/extension/retailcrm/prices.php b/src/upload/admin/model/extension/retailcrm/prices.php index c784184..fe598c5 100644 --- a/src/upload/admin/model/extension/retailcrm/prices.php +++ b/src/upload/admin/model/extension/retailcrm/prices.php @@ -20,6 +20,7 @@ public function uploadPrices($products, $retailcrm_api_client, $retailcrm) { $this->load->model('catalog/option'); $this->load->model('setting/setting'); + $this->load->model('customer/customer_group'); $prices = $this->getPrices($products, $retailcrm_api_client, $retailcrm); @@ -46,76 +47,109 @@ public function uploadPrices($products, $retailcrm_api_client, $retailcrm) */ protected function getPrices($products, $retailcrm_api_client, $retailcrm) { - $settings = $this->model_setting_setting->getSetting(\retailcrm\Retailcrm::MODULE); - $prices = array(); $site = $this->getSite($retailcrm_api_client); - if (!isset($settings[\Retailcrm\Retailcrm::MODULE . '_special']) - || $settings[\Retailcrm\Retailcrm::MODULE . '_apiversion'] == 'v3' - ) { - return false; - } - foreach ($products as $product) { $specials = $this->model_catalog_product->getProductSpecials($product['product_id']); if (!$specials) { + $productPrice = $this->getEmptyPrice(); + $prices[] = $this->getPriceRequest($product, $site, $productPrice, $retailcrm); continue; } + $productPrice = array(); + if (is_array($specials) && count($specials)) { $productPrice = $this->getSpecialPrice($specials); + } + + $prices[] = $this->getPriceRequest($product, $site, $productPrice, $retailcrm); + } + + return $prices; + } - if (!$productPrice) { - continue; + /** + * Get prices for request + * + * @param $product + * @param $site + * @param $productPrice + * @param \Retailcrm\Retailcrm $retailcrm + * + * @return array + */ + private function getPriceRequest($product, $site, $productPrice, $retailcrm) + { + $settings = $this->model_setting_setting->getSetting(\retailcrm\Retailcrm::MODULE); + $offers = $retailcrm->getOffers($product); + $pricesProduct = array(); + + foreach ($offers as $optionsString => $optionsValues) { + $optionsString = explode('_', $optionsString); + $options = array(); + + foreach($optionsString as $optionString) { + $option = explode('-', $optionString); + $optionIds = explode(':', $option[0]); + + if ($optionString != '0:0-0') { + $optionData = $this->getOptionData($optionIds[1], $option[1]); + $options[$optionIds[0]] = array( + 'name' => $optionData['optionName'], + 'value' => $optionData['optionValue'], + 'value_id' => $option[1] + ); } } - $offers = $retailcrm->getOffers($product); + ksort($options); - foreach ($offers as $optionsString => $optionsValues) { - $optionsString = explode('_', $optionsString); - $options = array(); + $offerId = array(); - foreach($optionsString as $optionString) { - $option = explode('-', $optionString); - $optionIds = explode(':', $option[0]); + foreach($options as $optionKey => $optionData) { + $offerId[] = $optionKey.'-'.$optionData['value_id']; + } - if ($optionString != '0:0-0') { - $optionData = $this->getOptionData($optionIds[1], $option[1]); - $options[$optionIds[0]] = array( - 'name' => $optionData['optionName'], - 'value' => $optionData['optionValue'], - 'value_id' => $option[1] - ); - } + $offerId = implode('_', $offerId); + $price = array(); + + foreach($productPrice as $k => $v) { + if (isset($settings[\Retailcrm\Retailcrm::MODULE . '_special_' . $k])) { + $price[] = array( + 'code' => $settings[\Retailcrm\Retailcrm::MODULE . '_special_' . $k], + 'price' => $v == 0 ? $v : $v + $optionsValues['price'] + ); } + } - ksort($options); + $pricesProduct = array( + 'externalId' => $offerId ? $product['product_id'] . '#' . $offerId : $product['product_id'], + 'site' => $site, + 'prices' => $price + ); + } - $offerId = array(); + return $pricesProduct; + } - foreach($options as $optionKey => $optionData) { - $offerId[] = $optionKey.'-'.$optionData['value_id']; - } + /** + * Get price for no special + * + * @return array $productPrice + */ + private function getEmptyPrice() + { + $customerGroups = $this->model_customer_customer_group->getCustomerGroups(); + $productPrice = array(); - $offerId = implode('_', $offerId); - - $prices[] = array( - 'externalId' => $offerId ? $product['product_id'] . '#' . $offerId : $product['product_id'], - 'site' => $site, - 'prices' => array( - array( - 'code' => $settings[\Retailcrm\Retailcrm::MODULE . '_special'], - 'price' => $productPrice + $optionsValues['price'] - ) - ) - ); - } + foreach ($customerGroups as $customerGroup) { + $productPrice[$customerGroup['customer_group_id']] = 0; } - return $prices; + return $productPrice; } /** @@ -129,21 +163,35 @@ private function getSpecialPrice($specials) { $date = date('Y-m-d'); $always = '0000-00-00'; - $productPrice = 0; + $productPrice = array(); foreach ($specials as $special) { if (($special['date_start'] == $always && $special['date_end'] == $always) || ($special['date_start'] <= $date && $special['date_end'] >= $date) ) { - if ((isset($priority) && $priority > $special['priority']) - || !isset($priority) - ) { - $productPrice = $special['price']; - $priority = $special['priority']; + if ((isset($groupId) && $groupId == $special['customer_group_id']) || !isset($groupId)) { + if ((isset($priority) && $priority > $special['priority']) + || !isset($priority) + ) { + $productPrice[$special['customer_group_id']] = $special['price']; + $priority = $special['priority']; + $groupId = $special['customer_group_id']; + } + } else { + $productPrice[$special['customer_group_id']] = $special['price']; + $groupId = $special['customer_group_id']; } } } + $customerGroups = $this->model_customer_customer_group->getCustomerGroups(); + + foreach ($customerGroups as $customerGroup) { + if (!isset($productPrice[$customerGroup['customer_group_id']])){ + $productPrice[$customerGroup['customer_group_id']] = 0; + } + } + return $productPrice; } diff --git a/src/upload/admin/view/template/extension/module/retailcrm.twig b/src/upload/admin/view/template/extension/module/retailcrm.twig index 75561c4..53695ec 100644 --- a/src/upload/admin/view/template/extension/module/retailcrm.twig +++ b/src/upload/admin/view/template/extension/module/retailcrm.twig @@ -114,23 +114,28 @@ {% if saved_settings.module_retailcrm_apiversion is defined and saved_settings.module_retailcrm_apiversion != 'v3' %} -
- {{ special_price_settings }} -
- -
- +
+ {{ special_price_settings }} +
+ {% for customerGroup in customerGroups %} + {% set cud = customerGroup.customer_group_id %} +
+ +
+ +
+
+ {% endfor %}
-
-
+ {% endif %}
{{ order_number }} diff --git a/tests/admin/ModelRetailcrmPricesAdminTest.php b/tests/admin/ModelRetailcrmPricesAdminTest.php index 073c539..58790bc 100644 --- a/tests/admin/ModelRetailcrmPricesAdminTest.php +++ b/tests/admin/ModelRetailcrmPricesAdminTest.php @@ -30,7 +30,9 @@ public function setUp() \Retailcrm\Retailcrm::MODULE, array( \Retailcrm\Retailcrm::MODULE . '_apiversion' => 'v5', - \Retailcrm\Retailcrm::MODULE . '_special' => 'special' + \Retailcrm\Retailcrm::MODULE . '_special_1' => 'special1', + \Retailcrm\Retailcrm::MODULE . '_special_2' => 'special2', + \Retailcrm\Retailcrm::MODULE . '_special_3' => 'special3' ) ); } @@ -50,9 +52,13 @@ public function testUploadPrices() $this->assertInternalType('array', $price); $this->assertArrayHasKey('externalId', $price); $this->assertArrayHasKey('site', $price); - $this->assertEquals('test_site', $price['site']); + $this->assertSame('test_site', $price['site']); $this->assertArrayHasKey('prices', $price); $this->assertInternalType('array', $price['prices']); + $this->assertSame('special1', $price['prices'][0]['code']); + $this->assertSame('special2', $price['prices'][1]['code']); + $this->assertSame('special3', $price['prices'][2]['code']); + $this->assertSame(0, $price['prices'][2]['price']); } private function getSites() diff --git a/tests/opencart_sample_data.sql b/tests/opencart_sample_data.sql index 20718b6..ef202bd 100644 --- a/tests/opencart_sample_data.sql +++ b/tests/opencart_sample_data.sql @@ -4,9 +4,13 @@ INSERT INTO `oc_customer` (`customer_id`, `customer_group_id`, `store_id`, `lang TRUNCATE TABLE `oc_customer_activity`; TRUNCATE TABLE `oc_customer_group`; INSERT INTO `oc_customer_group` (`customer_group_id`, `approval`, `sort_order`) VALUES ('1', '0', '1'); +INSERT INTO `oc_customer_group` (`customer_group_id`, `approval`, `sort_order`) VALUES ('2', '0', '1'); +INSERT INTO `oc_customer_group` (`customer_group_id`, `approval`, `sort_order`) VALUES ('3', '0', '0'); TRUNCATE TABLE `oc_customer_group_description`; INSERT INTO `oc_customer_group_description` (`customer_group_id`, `language_id`, `name`, `description`) VALUES ('1', '1', 'Default', 'test'); +INSERT INTO `oc_customer_group_description` (`customer_group_id`, `language_id`, `name`, `description`) VALUES ('2', '1', 'Test2', 'test2'); +INSERT INTO `oc_customer_group_description` (`customer_group_id`, `language_id`, `name`, `description`) VALUES ('3', '1', 'test3', 'test3'); TRUNCATE TABLE `oc_customer_history`; TRUNCATE TABLE `oc_customer_ip`; @@ -44,5 +48,6 @@ INSERT INTO `oc_order_total` (`order_total_id`, `order_id`, `code`, `title`, `va INSERT INTO `oc_order_total` (`order_total_id`, `order_id`, `code`, `title`, `value`, `sort_order`) VALUES ('165', '2', 'shipping', 'Flat Rate', '5.0000', '3'); INSERT INTO `oc_order_total` (`order_total_id`, `order_id`, `code`, `title`, `value`, `sort_order`) VALUES ('163', '1', 'total', 'Total', '106.0000', '9'); INSERT INTO `oc_order_total` (`order_total_id`, `order_id`, `code`, `title`, `value`, `sort_order`) VALUES ('166', '2', 'total', 'Total', '85.0000', '9'); +INSERT INTO `oc_product_special` (`product_id`, `customer_group_id`, `priority`, `price`,`date_start`, `date_end`) values ('42', '2', '1', '110.000', CURDATE(), ADDDATE(CURDATE(),INTERVAL 10 DAY)); TRUNCATE TABLE `oc_order_voucher`; \ No newline at end of file