Skip to content

Commit

Permalink
Merge pull request #136 from sergeygw1990/v3.1
Browse files Browse the repository at this point in the history
v3.1.3
  • Loading branch information
gwinn authored Mar 1, 2019
2 parents dbb353b + 1ec5e14 commit 17140b1
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 68 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v.3.1.3
* Добавлена возможность передачи акционных цен для нескольких групп пользователей
* Добавлена передача нулевой цены для неустановленных акционных цен
* Убрана базовая цена retailcrm из настроек соответствия типов цен

## v.3.1.2
* Добавлен перевод на испанский язык
* Переделан перевод на английский язык
Expand Down
7 changes: 7 additions & 0 deletions src/upload/admin/controller/extension/module/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
148 changes: 98 additions & 50 deletions src/upload/admin/model/extension/retailcrm/prices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down
37 changes: 21 additions & 16 deletions src/upload/admin/view/template/extension/module/retailcrm.twig
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,28 @@
</div>
</fieldset>
{% if saved_settings.module_retailcrm_apiversion is defined and saved_settings.module_retailcrm_apiversion != 'v3' %}
<fieldset>
<legend>{{ special_price_settings }}</legend>
<div class="row form-group retailcrm_unit">
<label class="col-sm-2 col-form-label" for="module_retailcrm_special">{{ special_price_settings }}</label>
<div class="col-md-4 col-sm-10">
<select id="module_retailcrm_special" name="module_retailcrm_special" class="form-control">
{% for priceType in priceTypes %}
{% if priceType.active == true %}
<option value="{{priceType.code }}" {% if saved_settings.module_retailcrm_special is defined and saved_settings.module_retailcrm_special == priceType.code %} selected="selected" {% endif %}>
{{ priceType.name }}
</option>
{% endif %}
{% endfor %}
</select>
<fieldset>
<legend>{{ special_price_settings }}</legend>
<div class="form-group retailcrm_unit">
{% for customerGroup in customerGroups %}
{% set cud = customerGroup.customer_group_id %}
<div class="row retailcrm_unit">
<label class="col-sm-2 control-label" for="opencart_customer_group_{{ cud }}">{{ customerGroup.name }}</label>
<div class="col-md-4 col-sm-10">
<select id="module_retailcrm_special_{{ cud }}" name="module_retailcrm_special_{{ cud }}" class="form-control">
{% for priceType in priceTypes %}
{% if priceType.active == true and priceType.default == false %}
<option value ="{{ priceType.code }}" {% if saved_settings['module_retailcrm_special_'~cud] is defined and priceType.code == saved_settings['module_retailcrm_special_'~cud] %} selected="selected" {% endif %}>
{{ priceType.name }}
</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
{% endfor %}
</div>
</div>
</fieldset>
</fieldset>
{% endif %}
<fieldset>
<legend>{{ order_number }}</legend>
Expand Down
10 changes: 8 additions & 2 deletions tests/admin/ModelRetailcrmPricesAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
);
}
Expand All @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions tests/opencart_sample_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down Expand Up @@ -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`;

0 comments on commit 17140b1

Please sign in to comment.