fixed handling of custom interfaces

This commit is contained in:
Florian Schlegel
2017-01-24 14:07:43 +01:00
parent f9ddf7d82e
commit 2397ed1534
6 changed files with 42 additions and 15 deletions
+3 -1
View File
@@ -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
+5 -3
View File
@@ -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")
}
+3 -3
View File
@@ -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)
}
+1 -1
View File
@@ -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)
+26 -3
View File
@@ -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
}
//------------------------------------------------------------------
+4 -4
View File
@@ -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
}