From 0e7d1dcd85953baaad3dff2875078b3da45cca7d Mon Sep 17 00:00:00 2001 From: Bostjan Marusic Date: Tue, 21 Feb 2017 14:44:10 +0100 Subject: [PATCH 1/2] support for multiple payment methods discounts --- pricerule/action-item-by-absolute.go | 1 - pricerule/calculator.go | 7 +++---- pricerule/pricerule.go | 4 ++-- pricerule/pricerule_test.go | 3 +-- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pricerule/action-item-by-absolute.go b/pricerule/action-item-by-absolute.go index 12d0110..6f890de 100644 --- a/pricerule/action-item-by-absolute.go +++ b/pricerule/action-item-by-absolute.go @@ -30,7 +30,6 @@ func calculateDiscountsItemByAbsolute(priceRuleVoucherPair RuleVoucherPair, orde func previouslyAppliedExclusionInPlace(rule *PriceRule, orderDiscountsForPosition DiscountCalculationData) bool { previouslyAppliedExclusion := false - if rule.Type == TypePromotionCustomer || rule.Type == TypePromotionProduct { if orderDiscountsForPosition.CustomerPromotionApplied || orderDiscountsForPosition.ProductPromotionApplied { previouslyAppliedExclusion = true diff --git a/pricerule/calculator.go b/pricerule/calculator.go index 8934a62..99353e9 100644 --- a/pricerule/calculator.go +++ b/pricerule/calculator.go @@ -117,7 +117,7 @@ func ClearCache() { // ApplyDiscounts applies all possible discounts on articleCollection ... if voucherCodes is "" the voucher is not applied // This is not yet used. ApplyDiscounts should at some point be able to consider previousle calculated discounts -func ApplyDiscounts(articleCollection *ArticleCollection, existingDiscounts OrderDiscounts, voucherCodes []string, paymentMethod string, roundTo float64, customProvider PriceRuleCustomProvider) (OrderDiscounts, *OrderDiscountSummary, error) { +func ApplyDiscounts(articleCollection *ArticleCollection, existingDiscounts OrderDiscounts, voucherCodes []string, paymentMethods []string, roundTo float64, customProvider PriceRuleCustomProvider) (OrderDiscounts, *OrderDiscountSummary, error) { calculationParameters := &CalculationParameters{} calculationParameters.articleCollection = articleCollection calculationParameters.roundTo = roundTo @@ -151,8 +151,8 @@ func ApplyDiscounts(articleCollection *ArticleCollection, existingDiscounts Orde // find applicable payment discounts var paymentPriceRules []PriceRule - if len(paymentMethod) > 0 { - paymentPriceRules, err = GetValidPriceRulesForPaymentMethod(paymentMethod, customProvider) + if len(paymentMethods) > 0 { + paymentPriceRules, err = GetValidPriceRulesForPaymentMethods(paymentMethods, customProvider) if err != nil { return nil, nil, err } @@ -243,7 +243,6 @@ func ApplyDiscounts(articleCollection *ArticleCollection, existingDiscounts Orde voucherDiscounts.DiscountAmount += appliedDiscount.DiscountAmountApplicable summary.VoucherDiscounts[appliedDiscount.VoucherCode] = voucherDiscounts } - } } } diff --git a/pricerule/pricerule.go b/pricerule/pricerule.go index e8f1cf1..7db07f0 100644 --- a/pricerule/pricerule.go +++ b/pricerule/pricerule.go @@ -261,8 +261,8 @@ func RemoveAllPriceRules() error { // GetValidPriceRulesForPaymentMethod - find rule for payment // check ValidFrom, ValidTo -func GetValidPriceRulesForPaymentMethod(paymentMethod string, customProvider PriceRuleCustomProvider) ([]PriceRule, error) { - query := bson.M{"type": TypePaymentMethodDiscount, "includedpaymentmethods": bson.M{"$in": []string{paymentMethod}}, "validfrom": bson.M{"$lte": time.Now()}, "validto": bson.M{"$gte": time.Now()}} +func GetValidPriceRulesForPaymentMethods(paymentMethods []string, customProvider PriceRuleCustomProvider) ([]PriceRule, error) { + query := bson.M{"type": TypePaymentMethodDiscount, "includedpaymentmethods": bson.M{"$in": paymentMethods}, "validfrom": bson.M{"$lte": time.Now()}, "validto": bson.M{"$gte": time.Now()}} return getPromotions(query, customProvider) } diff --git a/pricerule/pricerule_test.go b/pricerule/pricerule_test.go index b56be2d..088625f 100644 --- a/pricerule/pricerule_test.go +++ b/pricerule/pricerule_test.go @@ -266,8 +266,8 @@ func testScaled(t *testing.T) { positionVo.Price = 500 positionVo.Quantity = float64(2) orderVo.Articles = append(orderVo.Articles, positionVo) - // Order ------------------------------------------------------------------------------- + // Order ------------------------------------------------------------------------------- now := time.Now() discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{""}, "", 0.05, nil) timeTrack(now, "Apply scaled voucher") @@ -275,7 +275,6 @@ func testScaled(t *testing.T) { if err != nil { panic(err) } - fmt.Println("discounts for scaled percentage") spew.Dump(discountsVo) spew.Dump(*summary) From 7994b36705ba345e4f2282e054f85d5b5ecd8fee Mon Sep 17 00:00:00 2001 From: Bostjan Marusic Date: Tue, 21 Feb 2017 14:50:33 +0100 Subject: [PATCH 2/2] multiple payment methods support fix --- pricerule/checkout.go | 4 ++-- pricerule/pricerule_test.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pricerule/checkout.go b/pricerule/checkout.go index e2c8bee..e8d500b 100644 --- a/pricerule/checkout.go +++ b/pricerule/checkout.go @@ -165,8 +165,8 @@ func CommitDiscounts(orderDiscounts *OrderDiscounts, customerID string) error { // IT IS IRREVERSIBLE!!! // // alternatively use CommitDiscounts -func CommitOrderDiscounts(customerID string, articleCollection *ArticleCollection, voucherCodes []string, paymentMethod string, roundTo float64) error { - orderDiscounts, _, err := ApplyDiscounts(articleCollection, nil, voucherCodes, paymentMethod, roundTo, nil) +func CommitOrderDiscounts(customerID string, articleCollection *ArticleCollection, voucherCodes []string, paymentMethods []string, roundTo float64) error { + orderDiscounts, _, err := ApplyDiscounts(articleCollection, nil, voucherCodes, paymentMethods, roundTo, nil) if err != nil { return err } diff --git a/pricerule/pricerule_test.go b/pricerule/pricerule_test.go index 088625f..d36f2c8 100644 --- a/pricerule/pricerule_test.go +++ b/pricerule/pricerule_test.go @@ -136,7 +136,7 @@ func testShipping(t *testing.T) { positionVo.Quantity = 1 orderVo.Articles = append(orderVo.Articles, positionVo) - discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{""}, "", 0.05, nil) + discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{""}, []string{}, 0.05, nil) spew.Dump(discountsVo, summary, err) } @@ -269,7 +269,7 @@ func testScaled(t *testing.T) { // Order ------------------------------------------------------------------------------- now := time.Now() - discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{""}, "", 0.05, nil) + discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{""}, []string{}, 0.05, nil) timeTrack(now, "Apply scaled voucher") // defer removeOrder(orderVo) if err != nil { @@ -340,7 +340,7 @@ func testBuyXGetY(t *testing.T) { positionVo.Quantity = float64(2) orderVo.Articles = append(orderVo.Articles, positionVo) // Order ------------------------------------------------------------------------------- - discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{""}, "", 0.05, nil) + discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{""}, []string{}, 0.05, nil) // defer removeOrder(orderVo) if err != nil { panic(err) @@ -404,7 +404,7 @@ func testExclude(t *testing.T) { log.Println(article.ID + " " + priceRule.ID + " " + strconv.FormatBool(ok)) } - discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{}, "blah", 0.05, nil) + discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{}, []string{"blah"}, 0.05, nil) spew.Dump(discountsVo) spew.Dump(*summary) @@ -487,7 +487,7 @@ func testMaxOrder(t *testing.T) { // PRICERULES -------------------------------------------------------------------------------------- now := time.Now() - discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{}, PaymentMethodID1, 0.05, nil) + discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{}, []string{PaymentMethodID1}, 0.05, nil) timeTrack(now, "Apply multiple price rules") // defer removeOrder(orderVo) if err != nil { @@ -622,7 +622,7 @@ func testTwoStepWorkflow(t *testing.T) { // PRICERULES -------------------------------------------------------------------------------------- now := time.Now() - discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode2, VoucherCode1}, PaymentMethodID1, 0.05, nil) + discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode2, VoucherCode1}, []string{PaymentMethodID1}, 0.05, nil) timeTrack(now, "Apply multiple price rules") // defer removeOrder(orderVo) if err != nil { @@ -645,7 +645,7 @@ func testPricerulesWorkflow(t *testing.T) { panic(err) } now := time.Now() - discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode1}, PaymentMethodID1, 0.05, nil) + discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode1}, []string{PaymentMethodID1}, 0.05, nil) timeTrack(now, "Apply multiple price rules") // defer removeOrder(orderVo) if err != nil { @@ -668,7 +668,7 @@ func testCheckoutWorkflow(t *testing.T) { panic(err) } now := time.Now() - discountsVo, _, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode1}, PaymentMethodID1, 0.05, nil) + discountsVo, _, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode1}, []string{PaymentMethodID1}, 0.05, nil) timeTrack(now, "Apply multiple price rules") // defer removeOrder(orderVo) if err != nil {