/
price-selection.go
281 lines (230 loc) · 8.46 KB
/
price-selection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package strategies
import (
"context"
"fmt"
"reflect"
"github.com/driver005/gateway/interfaces"
"github.com/driver005/gateway/models"
"github.com/driver005/gateway/utils"
"github.com/google/uuid"
"github.com/samber/lo"
)
type PriceSelectionStrategy struct {
ctx context.Context
r Registry
}
func NewPriceSelectionStrategy(
r Registry,
) *PriceSelectionStrategy {
return &PriceSelectionStrategy{
context.Background(),
r,
}
}
func (s *PriceSelectionStrategy) SetContext(context context.Context) *PriceSelectionStrategy {
s.ctx = context
return s
}
func (s *PriceSelectionStrategy) CalculateVariantPrice(data []interfaces.Pricing, context *interfaces.PricingContext) (map[uuid.UUID]interfaces.PriceSelectionResult, *utils.ApplictaionError) {
dataMap := make(map[uuid.UUID]interfaces.Pricing)
for _, d := range data {
dataMap[d.VariantId] = d
}
cacheKeysMap := make(map[uuid.UUID]string)
for _, d := range data {
cacheKeysMap[d.VariantId] = s.GetCacheKey(d.VariantId, context, d.Quantity)
}
nonCachedData := make([]interfaces.Pricing, 0)
variantPricesMap := make(map[uuid.UUID]interfaces.PriceSelectionResult)
if !context.IgnoreCache {
var cacheHits []interfaces.PriceSelectionResult
for _, cacheKey := range cacheKeysMap {
cacheHit, err := s.r.CacheService().Get(cacheKey)
if err != nil {
return nil, utils.NewApplictaionError(
utils.INVALID_DATA,
err.Error(),
)
}
cacheHits = append(cacheHits, cacheHit.(interfaces.PriceSelectionResult))
}
if len(cacheHits) == 0 {
for _, d := range dataMap {
nonCachedData = append(nonCachedData, d)
}
}
for index, cacheHit := range cacheHits {
variantId := data[index].VariantId
if !reflect.DeepEqual(cacheHit, interfaces.PriceSelectionResult{}) {
variantPricesMap[variantId] = cacheHit
continue
}
nonCachedData = append(nonCachedData, dataMap[variantId])
}
} else {
for _, d := range dataMap {
nonCachedData = append(nonCachedData, d)
}
}
// results := make(map[uuid.UUID]interfaces.PriceSelectionResult)
results, err := s.CalculateVariantPriceNew(nonCachedData, context)
if err != nil {
return nil, err
}
for variantId, prices := range results {
variantPricesMap[variantId] = prices
if !context.IgnoreCache {
err := s.r.CacheService().Set(cacheKeysMap[variantId], prices, nil)
if err != nil {
return nil, utils.NewApplictaionError(
utils.INVALID_DATA,
err.Error(),
)
}
}
}
return variantPricesMap, nil
}
func (s *PriceSelectionStrategy) CalculateVariantPriceNew(data []interfaces.Pricing, context *interfaces.PricingContext) (map[uuid.UUID]interfaces.PriceSelectionResult, *utils.ApplictaionError) {
var variantIDs uuid.UUIDs
for _, d := range data {
variantIDs = append(variantIDs, d.VariantId)
}
variantsPrices, _, err := s.r.MoneyAmountRepository().FindManyForVariantsInRegion(variantIDs, context.RegionId, context.CurrencyCode, context.CustomerId, context.IncludeDiscountPrices, true)
if err != nil {
return nil, err
}
variantPricesMap := make(map[uuid.UUID]interfaces.PriceSelectionResult)
for variantId, prices := range variantsPrices {
dataItem, _ := lo.Find(data, func(item interfaces.Pricing) bool {
return item.VariantId == variantId
})
result := interfaces.PriceSelectionResult{
Prices: prices,
}
if len(prices) == 0 || context != nil {
variantPricesMap[variantId] = result
}
taxRate := 0.0
for _, nextTaxRate := range context.TaxRates {
taxRate += nextTaxRate.Rate / 100
}
for _, ma := range prices {
isTaxInclusive := ma.Currency.IncludesTax
if ma.PriceList != nil && ma.PriceList.IncludesTax {
isTaxInclusive = ma.PriceList.IncludesTax
} else if ma.Region != nil && ma.Region.IncludesTax {
isTaxInclusive = ma.Region.IncludesTax
}
if ma.RegionId.UUID == context.RegionId && ma.PriceListId.UUID == uuid.Nil && reflect.ValueOf(ma.MinQuantity).IsZero() && reflect.ValueOf(ma.MaxQuantity).IsZero() {
result.OriginalPriceIncludesTax = isTaxInclusive
result.OriginalPrice = ma.Amount
}
if ma.CurrencyCode == context.CurrencyCode && ma.PriceListId.UUID == uuid.Nil && reflect.ValueOf(ma.MinQuantity).IsZero() && reflect.ValueOf(ma.MaxQuantity).IsZero() && reflect.ValueOf(result.OriginalPrice).IsZero() {
result.OriginalPriceIncludesTax = isTaxInclusive
result.OriginalPrice = ma.Amount
}
if IsValidQuantity(ma, dataItem.Quantity) && IsValidAmount(ma.Amount, result, isTaxInclusive, taxRate) && (ma.CurrencyCode == context.CurrencyCode || ma.RegionId.UUID == context.RegionId) {
result.CalculatedPrice = ma.Amount
result.CalculatedPriceType = string(ma.PriceList.Type)
result.CalculatedPriceIncludesTax = isTaxInclusive
}
}
variantPricesMap[variantId] = result
}
return variantPricesMap, nil
}
func (s *PriceSelectionStrategy) CalculateVariantPriceOld(data []interfaces.Pricing, context *interfaces.PricingContext) (map[uuid.UUID]interfaces.PriceSelectionResult, *utils.ApplictaionError) {
var variantIDs uuid.UUIDs
for _, d := range data {
variantIDs = append(variantIDs, d.VariantId)
}
variantsPrices, _, err := s.r.MoneyAmountRepository().FindManyForVariantsInRegion(variantIDs, context.RegionId, context.CurrencyCode, context.CustomerId, context.IncludeDiscountPrices, true)
if err != nil {
return nil, err
}
variantPricesMap := make(map[uuid.UUID]interfaces.PriceSelectionResult)
for variantId, prices := range variantsPrices {
dataItem, _ := lo.Find(data, func(item interfaces.Pricing) bool {
return item.VariantId == variantId
})
result := interfaces.PriceSelectionResult{
Prices: prices,
}
if len(prices) == 0 || context != nil {
variantPricesMap[variantId] = result
}
for _, ma := range prices {
if ma.RegionId.UUID == context.RegionId && ma.PriceListId.UUID == uuid.Nil && reflect.ValueOf(ma.MinQuantity).IsZero() && reflect.ValueOf(ma.MaxQuantity).IsZero() {
result.OriginalPrice = ma.Amount
}
if ma.CurrencyCode == context.CurrencyCode && ma.PriceListId.UUID == uuid.Nil && reflect.ValueOf(ma.MinQuantity).IsZero() && reflect.ValueOf(ma.MaxQuantity).IsZero() && reflect.ValueOf(result.OriginalPrice).IsZero() {
result.OriginalPrice = ma.Amount
}
if IsValidQuantity(ma, dataItem.Quantity) && (reflect.ValueOf(result.CalculatedPrice).IsZero() || ma.Amount < result.CalculatedPrice) && (ma.CurrencyCode == context.CurrencyCode || ma.RegionId.UUID == context.RegionId) {
result.CalculatedPrice = ma.Amount
result.CalculatedPriceType = string(ma.PriceList.Type)
}
}
variantPricesMap[variantId] = result
}
return variantPricesMap, nil
}
func (s *PriceSelectionStrategy) OnVariantsPricesUpdate(variantIds uuid.UUIDs) error {
for _, id := range variantIds {
err := s.r.CacheService().Invalidate(fmt.Sprintf("ps:%s:*", id))
if err != nil {
return err
}
}
return nil
}
func (s *PriceSelectionStrategy) GetCacheKey(variantID uuid.UUID, context *interfaces.PricingContext, quantity int) string {
taxRate := 0.0
for _, nextTaxRate := range context.TaxRates {
taxRate += nextTaxRate.Rate / 100
}
return fmt.Sprintf("ps:%s:%s:%s:%s:%d:%t:%f", variantID, context.RegionId, context.CurrencyCode, context.CustomerId, quantity, context.IncludeDiscountPrices, taxRate)
}
func IsValidAmount(amount float64, result interfaces.PriceSelectionResult, isTaxInclusive bool, taxRate float64) bool {
if reflect.ValueOf(result.CalculatedPrice).IsZero() {
return true
}
if isTaxInclusive == result.CalculatedPriceIncludesTax {
return amount < result.CalculatedPrice
}
if taxRate != 0.0 {
if isTaxInclusive {
return amount < (1+taxRate)*(result.CalculatedPrice)
} else {
return (1+taxRate)*amount < result.CalculatedPrice
}
}
return false
}
func IsValidQuantity(price models.MoneyAmount, quantity int) bool {
if quantity != 0 && IsValidPriceWithQuantity(price, quantity) {
return true
}
if quantity == 0 && IsValidPriceWithoutQuantity(price) {
return true
}
return false
}
func IsValidPriceWithoutQuantity(price models.MoneyAmount) bool {
if reflect.ValueOf(price.MinQuantity).IsZero() && reflect.ValueOf(price.MaxQuantity).IsZero() {
return true
}
if reflect.ValueOf(price.MinQuantity).IsZero() || price.MinQuantity == 0 {
return !reflect.ValueOf(price.MaxQuantity).IsZero()
}
return false
}
func IsValidPriceWithQuantity(price models.MoneyAmount, quantity int) bool {
if !reflect.ValueOf(price.MinQuantity).IsZero() || price.MinQuantity <= quantity {
if !reflect.ValueOf(price.MaxQuantity).IsZero() || price.MaxQuantity >= quantity {
return true
}
}
return false
}