From a8afffb7d8b38de3cb51fbbbf302600a91556822 Mon Sep 17 00:00:00 2001 From: Bostjan Marusic Date: Wed, 8 Mar 2017 10:25:55 +0100 Subject: [PATCH] IsAmountIndependentOfQty --- pricerule/action-item-by-absolute.go | 7 ++++++- pricerule/action-item-by-percent.go | 1 - pricerule/action-item-set-by-absolute.go | 2 +- pricerule/action-scaled.go | 1 - pricerule/pricerule.go | 4 ++++ pricerule/pricerule_test.go | 10 +++++----- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pricerule/action-item-by-absolute.go b/pricerule/action-item-by-absolute.go index a31b2c6..1d40147 100644 --- a/pricerule/action-item-by-absolute.go +++ b/pricerule/action-item-by-absolute.go @@ -15,7 +15,12 @@ func calculateDiscountsItemByAbsolute(priceRuleVoucherPair RuleVoucherPair, orde discountApplied := getInitializedDiscountApplied(priceRuleVoucherPair, orderDiscounts, article.ID) //calculate the actual discount - discountApplied.DiscountAmount = roundToStep((orderDiscounts[article.ID].Quantity * priceRuleVoucherPair.Rule.Amount), calculationParameters.roundTo) + if !priceRuleVoucherPair.Rule.IsAmountIndependentOfQty { + discountApplied.DiscountAmount = roundToStep((orderDiscounts[article.ID].Quantity * priceRuleVoucherPair.Rule.Amount), calculationParameters.roundTo) + } else { + discountApplied.DiscountAmount = priceRuleVoucherPair.Rule.Amount + } + discountApplied.DiscountSingle = priceRuleVoucherPair.Rule.Amount discountApplied.Quantity = orderDiscounts[article.ID].Quantity diff --git a/pricerule/action-item-by-percent.go b/pricerule/action-item-by-percent.go index 331d4b0..a0aef81 100644 --- a/pricerule/action-item-by-percent.go +++ b/pricerule/action-item-by-percent.go @@ -2,7 +2,6 @@ package pricerule // CalculateDiscountsItemByPercent - func calculateDiscountsItemByPercent(priceRuleVoucherPair RuleVoucherPair, orderDiscounts OrderDiscounts, calculationParameters *CalculationParameters) OrderDiscounts { - if priceRuleVoucherPair.Rule.Action != ActionItemByPercent { panic("CalculateDiscountsItemByPercent called with pricerule of action " + priceRuleVoucherPair.Rule.Action) } diff --git a/pricerule/action-item-set-by-absolute.go b/pricerule/action-item-set-by-absolute.go index 900d544..fed405d 100644 --- a/pricerule/action-item-set-by-absolute.go +++ b/pricerule/action-item-set-by-absolute.go @@ -22,7 +22,7 @@ func calculateItemSetAbsoluteDiscount(priceRuleVoucherPair RuleVoucherPair, orde tempPriceRule := *priceRuleVoucherPair.Rule tempPriceRule.Amount = cartDiscountAmount tempPriceRule.Action = ActionCartByAbsolute - //tempPriceRule.IncludedProductGroupIDS = append(tempPriceRule.IncludedProductGroupIDS, orderItemsThatBelongToSet...) + tempPriceRule.IsAmountIndependentOfQty = priceRuleVoucherPair.Rule.IsAmountIndependentOfQty log.Println("rule action ActionItemSetAbsolute internally converted to ActionCartAbsolute") priceRuleVoucherPair.Rule = &tempPriceRule return calculateDiscountsCartByAbsolute(priceRuleVoucherPair, orderDiscounts, calculationParameters) diff --git a/pricerule/action-scaled.go b/pricerule/action-scaled.go index 72a3840..3124200 100644 --- a/pricerule/action-scaled.go +++ b/pricerule/action-scaled.go @@ -52,6 +52,5 @@ func getTotalQuantityForRule(priceRule PriceRule, calculationParameters *Calcula totalQty += article.Quantity } } - return totalQty } diff --git a/pricerule/pricerule.go b/pricerule/pricerule.go index 754c664..372b9e1 100644 --- a/pricerule/pricerule.go +++ b/pricerule/pricerule.go @@ -63,6 +63,8 @@ type PriceRule struct { Amount float64 //the value depending on action + IsAmountIndependentOfQty bool // do we apply the discount as amount * qty // false by default + Priority int // the articleCollection in which rule is applied ValidFrom time.Time // valid from timestamp @@ -153,6 +155,7 @@ func NewPriceRule(ID string) *PriceRule { priceRule.Description = map[string]string{} priceRule.Action = ActionItemByPercent priceRule.Amount = 0 + priceRule.IsAmountIndependentOfQty = false priceRule.MinOrderAmount = 0 priceRule.QtyThreshold = 0 priceRule.MaxUses = MaxInt @@ -168,6 +171,7 @@ func NewPriceRule(ID string) *PriceRule { priceRule.ValidTo = time.Date(9999, time.January, 1, 0, 0, 0, 0, time.UTC) // far in the future priceRule.WhichXYFree = XYCheapestFree priceRule.ItemSets = [][]string{} + return priceRule } diff --git a/pricerule/pricerule_test.go b/pricerule/pricerule_test.go index 5c44eeb..47736de 100644 --- a/pricerule/pricerule_test.go +++ b/pricerule/pricerule_test.go @@ -92,7 +92,7 @@ func Init(t *testing.T) { checkVouchersExists(t) } -func TestBestOption(t *testing.T) { +func testBestOption(t *testing.T) { RemoveAllGroups() RemoveAllPriceRules() RemoveAllVouchers() @@ -221,7 +221,7 @@ func TestBestOption(t *testing.T) { } -func testDiscountFoItemSets(t *testing.T) { +func TestDiscountFoItemSets(t *testing.T) { RemoveAllGroups() RemoveAllPriceRules() RemoveAllVouchers() @@ -242,7 +242,7 @@ func testDiscountFoItemSets(t *testing.T) { "fr": "itemset-discount", "it": "itemset-discount", } - priceRule.Type = TypePromotionProduct + priceRule.Type = TypePromotionOrder priceRule.Description = priceRule.Name priceRule.Action = ActionItemSetAbsolute priceRule.Amount = 10 @@ -267,7 +267,7 @@ func testDiscountFoItemSets(t *testing.T) { positionVo := &Article{} positionVo.ID = ProductID1SKU1 positionVo.Price = 100 - positionVo.Quantity = 2 + positionVo.Quantity = 4 orderVo.Articles = append(orderVo.Articles, positionVo) positionVo = &Article{} @@ -279,7 +279,7 @@ func testDiscountFoItemSets(t *testing.T) { positionVo = &Article{} positionVo.ID = ProductID3SKU2 positionVo.Price = 500 - positionVo.Quantity = 33 + positionVo.Quantity = 5 orderVo.Articles = append(orderVo.Articles, positionVo) discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{}, []string{PaymentMethodID1}, 0.05, nil)