Skip to content

Commit

Permalink
Merge pull request #255 from magento-troll/MAGETWO-40061
Browse files Browse the repository at this point in the history
[Troll] MAGETWO-40061
  • Loading branch information
He, Joan(johe) committed Jan 13, 2016
2 parents e7105f2 + 466d75d commit 26a148a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,6 @@ protected function _prepareForm()
]
);

$fieldset->addField(
'sub_is_enable',
'select',
[
'name' => 'sub_is_enable',
'label' => __('Subproduct discounts'),
'title' => __('Subproduct discounts'),
'onchange' => 'hideShowSubproductOptions(this);',
'values' => [0 => __('No'), 1 => __('Yes')]
]
);

$fieldset->addField(
'sub_simple_action',
'select',
[
'label' => __('Apply'),
'name' => 'sub_simple_action',
'options' => [
'by_percent' => __('Apply as percentage of original'),
'by_fixed' => __('Apply as fixed amount'),
'to_percent' => __('Adjust final price to this percentage'),
'to_fixed' => __('Adjust final price to discount value'),
]
]
);

$fieldset->addField(
'sub_discount_amount',
'text',
[
'name' => 'sub_discount_amount',
'required' => true,
'class' => 'validate-not-negative-number',
'label' => __('Discount Amount')
]
);

$fieldset->addField(
'stop_rules_processing',
'select',
Expand Down
8 changes: 0 additions & 8 deletions app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ protected function applyRule(Rule $rule, $product)
$actionOperator = $rule->getSimpleAction();
$actionAmount = $rule->getDiscountAmount();
$actionStop = $rule->getStopRulesProcessing();
$subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
$subActionAmount = $rule->getSubDiscountAmount();

$rows = [];
try {
Expand All @@ -292,8 +290,6 @@ protected function applyRule(Rule $rule, $product)
'action_amount' => $actionAmount,
'action_stop' => $actionStop,
'sort_order' => $sortOrder,
'sub_simple_action' => $subActionOperator,
'sub_discount_amount' => $subActionAmount,
];

if (count($rows) == $this->batchCount) {
Expand Down Expand Up @@ -368,8 +364,6 @@ protected function updateRuleProductData(Rule $rule)
$sortOrder = (int)$rule->getSortOrder();
$actionOperator = $rule->getSimpleAction();
$actionAmount = $rule->getDiscountAmount();
$subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
$subActionAmount = $rule->getSubDiscountAmount();
$actionStop = $rule->getStopRulesProcessing();

$rows = [];
Expand All @@ -391,8 +385,6 @@ protected function updateRuleProductData(Rule $rule)
'action_amount' => $actionAmount,
'action_stop' => $actionStop,
'sort_order' => $sortOrder,
'sub_simple_action' => $subActionOperator,
'sub_discount_amount' => $subActionAmount,
];

if (count($rows) == $this->batchCount) {
Expand Down
15 changes: 1 addition & 14 deletions app/code/Magento/CatalogRule/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,6 @@ public function validateData(\Magento\Framework\DataObject $dataObject)
$action = $dataObject->getData('simple_action');
$discount = $dataObject->getData('discount_amount');
$result = array_merge($result, $this->validateDiscount($action, $discount));
if ($dataObject->getData('sub_is_enable') == 1) {
$action = $dataObject->getData('sub_simple_action');
$discount = $dataObject->getData('sub_discount_amount');
$result = array_merge($result, $this->validateDiscount($action, $discount));
}

return !empty($result) ? $result : true;
}
Expand Down Expand Up @@ -418,15 +413,7 @@ public function calcProductPriceRule(Product $product, $price)
if ($rulesData) {
foreach ($rulesData as $ruleData) {
if ($product->getParentId()) {
if (!empty($ruleData['sub_simple_action'])) {
$priceRules = $this->_catalogRuleData->calcPriceRule(
$ruleData['sub_simple_action'],
$ruleData['sub_discount_amount'],
$priceRules ? $priceRules : $price
);
} else {
$priceRules = $priceRules ? $priceRules : $price;
}
$priceRules = $priceRules ? $priceRules : $price;
if ($ruleData['action_stop']) {
break;
}
Expand Down
58 changes: 58 additions & 0 deletions app/code/Magento/CatalogRule/Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogRule\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
* Upgrade the CatalogRule module DB scheme
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '2.0.1', '<')) {
$this->removeSubProductDiscounts($setup);
}

$setup->endSetup();
}

/**
* Remove Sub Product Discounts
* @param SchemaSetupInterface $setup
* @return void
*/
private function removeSubProductDiscounts(SchemaSetupInterface $setup)
{
$connection = $setup->getConnection();
$data = [
'catalogrule' => [
'sub_is_enable',
'sub_simple_action',
'sub_discount_amount',
],
'catalogrule_product' => [
'sub_simple_action',
'sub_discount_amount',
],
];

foreach ($data as $table => $fields) {
foreach ($fields as $field) {
$connection->dropColumn($setup->getTable($table), $field);
}
}
}
}
28 changes: 0 additions & 28 deletions app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,52 +232,29 @@ public function validateDataDataProvider()
[
'simple_action' => 'by_fixed',
'discount_amount' => '123',
'sub_is_enable' => '0',
'sub_simple_action' => 'by_percent',
'sub_discount_amount' => '123',
],
true
],
[
[
'simple_action' => 'by_percent',
'discount_amount' => '9,99',
'sub_is_enable' => '0',
],
true
],
[
[
'simple_action' => 'by_fixed',
'discount_amount' => '123',
'sub_is_enable' => '1',
'sub_simple_action' => 'by_percent',
'sub_discount_amount' => '123',
],
[
'Percentage discount should be between 0 and 100.',
]
],
[
[
'simple_action' => 'by_percent',
'discount_amount' => '123.12',
'sub_is_enable' => '1',
'sub_simple_action' => 'to_percent',
'sub_discount_amount' => '123.001',
],
[
'Percentage discount should be between 0 and 100.',
'Percentage discount should be between 0 and 100.',
]
],
[
[
'simple_action' => 'to_percent',
'discount_amount' => '-12',
'sub_is_enable' => '1',
'sub_simple_action' => 'to_fixed',
'sub_discount_amount' => '567.8901',
],
[
'Percentage discount should be between 0 and 100.',
Expand All @@ -287,20 +264,15 @@ public function validateDataDataProvider()
[
'simple_action' => 'to_fixed',
'discount_amount' => '-1234567890',
'sub_is_enable' => '1',
'sub_simple_action' => 'by_fixed',
'sub_discount_amount' => '-5',
],
[
'Discount value should be 0 or greater.',
'Discount value should be 0 or greater.',
]
],
[
[
'simple_action' => 'invalid action',
'discount_amount' => '12',
'sub_is_enable' => '0',
],
[
'Unknown action.',
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogRule/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_CatalogRule" setup_version="2.0.0">
<module name="Magento_CatalogRule" setup_version="2.0.1">
<sequence>
<module name="Magento_Rule"/>
<module name="Magento_Catalog"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@
<script>
require(["jquery", "prototype"], function(jQuery){

function hideShowSubproductOptions()
{
if ($('rule_sub_is_enable').value == 1) {
$('rule_sub_simple_action').up('div.field').show();
$('rule_sub_discount_amount').up('div.field').show();
$('rule_sub_discount_amount').addClassName('required-entry validate-not-negative-number');
changeValidationForSubDiscountPercent();
} else {
$('rule_sub_discount_amount').
removeClassName('required-entry').
removeClassName('validate-not-negative-number').
removeClassName('validate-number-range').
removeClassName('number-range-0.00-100.00');
$('rule_sub_simple_action').up('div.field').hide();
$('rule_sub_discount_amount').up('div.field').hide();
}

return true;
}

function changeValidationForDiscountPercent()
{
if ($('rule_simple_action').value == 'by_percent' || $('rule_simple_action').value == 'to_percent') {
Expand All @@ -38,28 +18,8 @@ function changeValidationForDiscountPercent()
return true;
}

function changeValidationForSubDiscountPercent()
{
if ($('rule_sub_is_enable').value == 1) {
if ($('rule_sub_simple_action').value == 'by_percent' || $('rule_sub_simple_action').value == 'to_percent') {
$('rule_sub_discount_amount').addClassName('validate-number-range number-range-0.00-100.00');
} else {
$('rule_sub_discount_amount').
removeClassName('validate-number-range').
removeClassName('number-range-0.00-100.00');
}
}

return true;
}

jQuery(document).ready(hideShowSubproductOptions);
jQuery(document).ready(changeValidationForDiscountPercent);
jQuery(document).on('change', '#rule_sub_is_enable', hideShowSubproductOptions);
jQuery(document).on('change', '#rule_simple_action', changeValidationForDiscountPercent);
jQuery(document).on('change', '#rule_sub_simple_action', changeValidationForSubDiscountPercent);
window.hideShowSubproductOptions = hideShowSubproductOptions;
window.changeValidationForDiscountPercent = changeValidationForDiscountPercent;
window.changeValidationForSubDiscountPercent = changeValidationForSubDiscountPercent;
});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testCalcProductPriceRule()
);
$this->assertEquals($this->_object->calcProductPriceRule($product, 100), 45);
$product->setParentId(true);
$this->assertEquals($this->_object->calcProductPriceRule($product, 50), 5);
$this->assertEquals($this->_object->calcProductPriceRule($product, 50), 50);
}

/**
Expand All @@ -64,16 +64,12 @@ protected function _getCatalogRulesFixtures()
return [
[
'action_operator' => 'by_percent',
'action_amount' => '10.0000',
'sub_simple_action' => 'by_percent',
'sub_discount_amount' => '90.0000',
'action_stop' => '0',
'action_amount' => '50.0000',
'action_stop' => '0'
],
[
'action_operator' => 'by_percent',
'action_amount' => '50.0000',
'sub_simple_action' => '',
'sub_discount_amount' => '0.0000',
'action_amount' => '10.0000',
'action_stop' => '0'
]
];
Expand Down
3 changes: 0 additions & 3 deletions setup/src/Magento/Setup/Fixtures/CatalogPriceRulesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ public function execute()
],
'simple_action' => 'by_percent',
'discount_amount' => '15',
'sub_is_enable' => '0',
'sub_simple_action' => 'by_percent',
'sub_discount_amount' => '0',
'stop_rules_processing' => '0',
'page' => '1',
'limit' => '20',
Expand Down

0 comments on commit 26a148a

Please sign in to comment.