diff --git a/pricerule/action-item-by-percent.go b/pricerule/action-item-by-percent.go index 09a66ee..3e7fcdf 100644 --- a/pricerule/action-item-by-percent.go +++ b/pricerule/action-item-by-percent.go @@ -31,7 +31,9 @@ func calculateDiscountsItemByPercent(articleCollection *ArticleCollection, price func getInitializedDiscountApplied(priceRuleVoucherPair RuleVoucherPair, orderDiscounts OrderDiscounts, itemID string) *DiscountApplied { discountApplied := &DiscountApplied{} discountApplied.PriceRuleID = priceRuleVoucherPair.Rule.ID - discountApplied.MappingID = priceRuleVoucherPair.Rule.MappingID + //discountApplied.MappingID = priceRuleVoucherPair.Rule.MappingID + discountApplied.Custom = priceRuleVoucherPair.Rule.Custom + if priceRuleVoucherPair.Voucher != nil { discountApplied.VoucherID = priceRuleVoucherPair.Voucher.ID discountApplied.VoucherCode = priceRuleVoucherPair.Voucher.VoucherCode diff --git a/pricerule/calculator.go b/pricerule/calculator.go index 8d8a52e..8ec45c2 100644 --- a/pricerule/calculator.go +++ b/pricerule/calculator.go @@ -41,6 +41,8 @@ type DiscountApplied struct { Quantity float64 Price float64 //price without reductions CalculationBasePrice float64 //price used for the calculation of the discount + + Custom interface{} } // DiscountCalculationData - of an item @@ -94,7 +96,7 @@ type RuleVoucherPair struct { //------------------------------------------------------------------ // ApplyDiscounts applies all possible discounts on articleCollection ... if voucherCodes is "" the voucher is not applied -func ApplyDiscounts(articleCollection *ArticleCollection, voucherCodes []string, paymentMethod string, roundTo float64) (OrderDiscounts, *OrderDiscountSummary, error) { +func ApplyDiscounts(articleCollection *ArticleCollection, voucherCodes []string, paymentMethod string, roundTo float64, customProvider PriceRuleCustomProvider) (OrderDiscounts, *OrderDiscountSummary, error) { var ruleVoucherPairs []RuleVoucherPair now := time.Now() @@ -108,7 +110,7 @@ func ApplyDiscounts(articleCollection *ArticleCollection, voucherCodes []string, } // find applicable pricerules - auto promotions - promotionPriceRules, err := GetValidPriceRulesForPromotions([]Type{TypePromotionCustomer, TypePromotionProduct, TypePromotionOrder}) + promotionPriceRules, err := GetValidPriceRulesForPromotions([]Type{TypePromotionCustomer, TypePromotionProduct, TypePromotionOrder}, customProvider) if err != nil { return nil, nil, err @@ -163,7 +165,7 @@ func ApplyDiscounts(articleCollection *ArticleCollection, voucherCodes []string, var ruleVoucherPairsStep2 []RuleVoucherPair for _, voucherCode := range voucherCodes { if len(voucherCode) > 0 { - voucherVo, voucherPriceRule, err := GetVoucherAndPriceRule(voucherCode) + voucherVo, voucherPriceRule, err := GetVoucherAndPriceRule(voucherCode, customProvider) if voucherVo == nil { log.Println("voucher not found for code: " + voucherCode + " in " + "priceRule.ApplyDiscounts") } diff --git a/pricerule/checkout.go b/pricerule/checkout.go index 26c5721..be15b3d 100644 --- a/pricerule/checkout.go +++ b/pricerule/checkout.go @@ -70,7 +70,7 @@ func ValidateVoucher(voucherCode string, articleCollection *ArticleCollection) ( //get voucher customerID := articleCollection.CustomerID - voucher, voucherPriceRule, err := GetVoucherAndPriceRule(voucherCode) + voucher, voucherPriceRule, err := GetVoucherAndPriceRule(voucherCode, nil) //check if exists if err != nil || voucher.VoucherCode != voucherCode { @@ -168,7 +168,7 @@ func CommitDiscounts(orderDiscounts *OrderDiscounts, customerID string) error { // // alternatively use CommitDiscounts func CommitOrderDiscounts(customerID string, articleCollection *ArticleCollection, voucherCodes []string, paymentMethod string, roundTo float64) error { - orderDiscounts, _, err := ApplyDiscounts(articleCollection, voucherCodes, paymentMethod, roundTo) + orderDiscounts, _, err := ApplyDiscounts(articleCollection, voucherCodes, paymentMethod, roundTo, nil) if err != nil { return err } @@ -191,7 +191,7 @@ func redeemVoucherByCode(voucherCode string, customerID string) error { // Returns false, ValidationPreviouslyAppliedRuleBlock if a previous rule blocks application func checkPreviouslyAppliedRules(voucherPriceRule *PriceRule, voucher *Voucher, articleCollection *ArticleCollection, groupIDsForCustomer []string, productGroupIDsPerPosition map[string][]string) (ok bool, reason TypeRuleValidationMsg) { // find applicable pricerules - auto promotions - promotionPriceRules, err := GetValidPriceRulesForPromotions([]Type{TypePromotionOrder, TypePromotionCustomer, TypePromotionProduct}) + promotionPriceRules, err := GetValidPriceRulesForPromotions([]Type{TypePromotionOrder, TypePromotionCustomer, TypePromotionProduct}, nil) if err != nil { panic(err) } diff --git a/pricerule/persistor.go b/pricerule/persistor.go index a638c62..4d2d4af 100644 --- a/pricerule/persistor.go +++ b/pricerule/persistor.go @@ -213,7 +213,7 @@ func mapDecodeObj(obj interface{}, customProvider PriceRuleCustomProvider) (type } return typedObject, nil case *PriceRule: - typedObject := obj.(*Voucher) + typedObject := obj.(*PriceRule) objCustom := customProvider.NewPriceRuleCustom() if objCustom != nil && typedObject.Custom != nil { err = mapstructure.Decode(typedObject.Custom, objCustom) diff --git a/pricerule/pricerule.go b/pricerule/pricerule.go index e292fc6..a1a4d6e 100644 --- a/pricerule/pricerule.go +++ b/pricerule/pricerule.go @@ -275,18 +275,41 @@ func GetValidPriceRulesForPaymentMethod(paymentMethod string) ([]PriceRule, erro // GetValidPriceRulesForPromotions - find rule for payment // check ValidFrom, ValidTo -func GetValidPriceRulesForPromotions(priceRuleTypes []Type) ([]PriceRule, error) { +func GetValidPriceRulesForPromotions(priceRuleTypes []Type, customProvider PriceRuleCustomProvider) ([]PriceRule, error) { p := GetPersistorForObject(new(PriceRule)) query := bson.M{"type": bson.M{"$in": priceRuleTypes}, "validfrom": bson.M{"$lte": time.Now()}, "validto": bson.M{"$gte": time.Now()}} - var result []PriceRule + var result []*PriceRule err := p.GetCollection().Find(query).Select(nil).Sort("priority").All(&result) if err != nil { // handle error return nil, err } - return result, nil + + if customProvider == nil { + priceRulesMapped := []PriceRule{} + for _, r := range result { + priceRulesMapped = append(priceRulesMapped, *r) + } + return priceRulesMapped, nil + } + + priceRulesMapped := []PriceRule{} + for _, r := range result { + + if customProvider != nil { + var err error + typedObject, err := mapDecodeObj(r, customProvider) + if err != nil { + return nil, err + } + r = typedObject.(*PriceRule) + priceRulesMapped = append(priceRulesMapped, *r) + } + } + + return priceRulesMapped, nil } //------------------------------------------------------------------ diff --git a/pricerule/voucher.go b/pricerule/voucher.go index 01aa14a..037c5f5 100644 --- a/pricerule/voucher.go +++ b/pricerule/voucher.go @@ -52,7 +52,7 @@ func NewVoucher(ID string, voucherCode string, priceRule *PriceRule, customerID voucher := new(Voucher) voucher.ID = ID voucher.PriceRuleID = priceRule.ID - voucher.MappingID = priceRule.MappingID + // voucher.MappingID = priceRule.MappingID voucher.VoucherCode = voucherCode if len(customerID) > 0 { voucher.VoucherType = VoucherTypePersonalized @@ -155,15 +155,15 @@ func RemoveAllVouchers() error { } // GetVoucherAndPriceRule - -func GetVoucherAndPriceRule(voucherCode string) (*Voucher, *PriceRule, error) { - voucher, err := GetVoucherByCode(voucherCode, nil) +func GetVoucherAndPriceRule(voucherCode string, customProvider PriceRuleCustomProvider) (*Voucher, *PriceRule, error) { + voucher, err := GetVoucherByCode(voucherCode, customProvider) if err != nil { return nil, nil, err } if voucher != nil && len(voucher.PriceRuleID) > 0 { //get the pricerule - priceRule, err := GetPriceRuleByID(voucher.PriceRuleID, nil) + priceRule, err := GetPriceRuleByID(voucher.PriceRuleID, customProvider) if err != nil { return voucher, nil, err }