diff --git a/pricerule/calculator.go b/pricerule/calculator.go index cea2272..393b542 100644 --- a/pricerule/calculator.go +++ b/pricerule/calculator.go @@ -419,6 +419,34 @@ func getOrderTotal(articleCollection *ArticleCollection) float64 { return total } +// find what is the articleCollection total qty of positions that belong to group +// previouslyAppliedDiscounts is for qty 1 +func getTotalQuantityForPriceRule(priceRule *PriceRule, calculationParameters *CalculationParameters) float64 { + var total float64 + + for _, article := range calculationParameters.articleCollection.Articles { + productGroupIDs := calculationParameters.productGroupIDsPerPosition[article.ID] + + // rule has no customer or product group limitations + if len(priceRule.IncludedProductGroupIDS) == 0 && + len(priceRule.ExcludedProductGroupIDS) == 0 && + len(priceRule.IncludedCustomerGroupIDS) == 0 && + len(priceRule.ExcludedCustomerGroupIDS) == 0 { + total += article.Quantity + } else { + //only sum up if limitations are matched + if IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedProductGroupIDS, productGroupIDs) && + IsNoProductOrGroupInExcludeGroups(priceRule.ExcludedProductGroupIDS, productGroupIDs) && + IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) && + IsNoProductOrGroupInExcludeGroups(priceRule.ExcludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) { + + total += article.Quantity + } + } + } + return total +} + // check if all rule group IDs are included func IsOneProductOrCustomerGroupInIncludedGroups(ruleIncludeGroups []string, productAndCustomerGroups []string) bool { if len(ruleIncludeGroups) == 0 { @@ -523,6 +551,14 @@ func validatePriceRule(priceRule PriceRule, checkedPosition *Article, calculatio } } + + //check the pricerule qty threshold + if priceRule.QtyThreshold > 0 { + ruleQtyThreshold := getTotalQuantityForPriceRule(&priceRule, calculationParameters) + if ruleQtyThreshold < priceRule.QtyThreshold { + return false, ValidationPriceRuleQuantityThresholdNotMet + } + } } now := time.Now() @@ -533,12 +569,7 @@ func validatePriceRule(priceRule PriceRule, checkedPosition *Article, calculatio if checkedPosition == nil { for _, article := range calculationParameters.articleCollection.Articles { - //the continue here is now obsolete - if checkedPosition != nil { - if checkedPosition.ID != article.ID { - continue - } - } + //at least some must match if IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedProductGroupIDS, calculationParameters.productGroupIDsPerPosition[article.ID]) { productGroupIncludeMatchOK = true } @@ -548,7 +579,6 @@ func validatePriceRule(priceRule PriceRule, checkedPosition *Article, calculatio if IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) { customerGroupIncludeMatchOK = true } - if IsNoProductOrGroupInExcludeGroups(priceRule.ExcludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) { customerGroupExcludeMatchOK = true } diff --git a/pricerule/checkout.go b/pricerule/checkout.go index d935677..16c6326 100644 --- a/pricerule/checkout.go +++ b/pricerule/checkout.go @@ -28,6 +28,7 @@ const ( ValidationPriceRuleOK TypeRuleValidationMsg = "price_rule_ok" ValidationPriceRuleCheckoutAttributesMismatch TypeRuleValidationMsg = "pricerule_checkout_attributes_missmatch" + ValidationPriceRuleQuantityThresholdNotMet = "qty_below_pricerule_quantity_threshold" ) //------------------------------------------------------------------ diff --git a/pricerule/pricerule.go b/pricerule/pricerule.go index 35499de..50b74c9 100644 --- a/pricerule/pricerule.go +++ b/pricerule/pricerule.go @@ -87,6 +87,8 @@ type PriceRule struct { WhichXYFree XYWhichType + QtyThreshold float64 // - the total qty in order for the price rule to be applicable. defaults to 0. + ScaledAmounts []ScaledAmountLevel //defines discount scale 100 -> 2%, 200 -> 3% etc - See ActionScaledPercentage & ActionScaledAbsolute ScaledAmountsPerQuantity []ScaledAmountLevel @@ -146,6 +148,7 @@ func NewPriceRule(ID string) *PriceRule { priceRule.Action = ActionItemByPercent priceRule.Amount = 0 priceRule.MinOrderAmount = 0 + priceRule.QtyThreshold = 0 priceRule.MaxUses = MaxInt priceRule.MaxUsesPerCustomer = MaxInt priceRule.Exclusive = false diff --git a/pricerule/pricerule_test.go b/pricerule/pricerule_test.go index e325bc4..095a679 100644 --- a/pricerule/pricerule_test.go +++ b/pricerule/pricerule_test.go @@ -123,6 +123,7 @@ func testVoucherRuleWithCheckoutAttributes(t *testing.T) { priceRule.IncludedProductGroupIDS = []string{group.ID} priceRule.IncludedCustomerGroupIDS = []string{} priceRule.CheckoutAttributes = []string{PaymentMethodID1} + priceRule.QtyThreshold = 2.0 priceRule.Upsert() //create pricerule @@ -142,19 +143,19 @@ func testVoucherRuleWithCheckoutAttributes(t *testing.T) { positionVo := &Article{} positionVo.ID = ProductID1SKU1 positionVo.Price = 100 - positionVo.Quantity = 2 + positionVo.Quantity = 1 orderVo.Articles = append(orderVo.Articles, positionVo) positionVo = &Article{} positionVo.ID = ProductID1SKU2 positionVo.Price = 300 - positionVo.Quantity = float64(2) + positionVo.Quantity = float64(1) orderVo.Articles = append(orderVo.Articles, positionVo) positionVo = &Article{} positionVo.ID = ProductID3SKU2 positionVo.Price = 500 - positionVo.Quantity = float64(2) + positionVo.Quantity = float64(1) orderVo.Articles = append(orderVo.Articles, positionVo) discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode1}, []string{PaymentMethodID1}, 0.05, nil)