-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
335 lines (278 loc) · 8.19 KB
/
main.go
File metadata and controls
335 lines (278 loc) · 8.19 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// You can edit this code!
// Click here and start typing.
package main
import (
"fmt"
"slices"
"strconv"
"strings"
)
// { "B": "3", "EQ": '=', "F": "1" }
var PromotionRulesMapping = map[string]bool{
// Contoh: Beli 2 product gratis 1
"BEQF": true,
"BGTF": true,
"BGTEF": true,
"BLTF": true,
"BLTEF": true,
// Contoh: Beli 2 product gratis product lainnya
"BEQFP": true,
"BGTFP": true,
"BGTEFP": true,
"BLTFP": true,
"BLTEFP": true,
// Contoh: Beli 2 product dapat diskon 10%
"BEQDPC": true,
"BGTDPC": true,
"BGTEDPC": true,
"BLTDPC": true,
"BLTEDPC": true,
"BEQDPCP": true,
"BGTDPCP": true,
"BGTEDPCP": true,
"BLTDPCP": true,
"BLTEDPCP": true,
// Contoh: Beli 2 product dapat diskon potongan Rp 100.000
"BEQDSUFX": true,
"BGTDSUFX": true,
"BGTEDSUFX": true,
"BLTDSUFX": true,
"BLTEDSUFX": true,
"BEQDSUFXP": true,
"BGTDSUFXP": true,
"BGTEDSUFXP": true,
"BLTDSUFXP": true,
"BLTEDSUFXP": true,
// Contoh: Beli 3 produk bayar hanya 2 harga
"BEQPR": true,
"BGTPR": true,
"BGTEPR": true,
"BLTPR": true,
"BLTEPR": true,
}
const (
BEQF = "BEQF"
BGTF = "BGTF"
BGTEF = "BGTEF"
BLTF = "BLTF"
BLTEF = "BLTEF"
BEQFP = "BEQFP"
BGTFP = "BGTFP"
BGTEP = "BGTEP"
BLTP = "BLTP"
BLTEP = "BLTEP"
BEQDPC = "BEQDPC"
BGTDPC = "BGTDPC"
BGTEDPC = "BGTEDPC"
BLTDPC = "BLTDPC"
BLTEDPC = "BLTEDPC"
BEQDPCP = "BEQDPCP"
BGTDPCP = "BGTDPCP"
BGTEDPCP = "BGTEDPCP"
BLTDPCP = "BLTDPCP"
BLTEDPCP = "BLTEDPCP"
BEQDSUFX = "BEQDSUFX"
BGTDSUFX = "BGTDSUFX"
BGTEDSUFX = "BGTEDSUFX"
BLTDSUFX = "BLTDSUFX"
BLTEDSUFX = "BLTEDSUFX"
BEQDSUFXP = "BEQDSUFXP"
BGTDSUFXP = "BGTDSUFXP"
BGTEDSUFXP = "BGTEDSUFXP"
BLTDSUFXP = "BLTDSUFXP"
BLTEDSUFXP = "BLTEDSUFXP"
BEQPR = "BEQPR"
BGTPR = "BGTPR"
BGTEPR = "BGTEPR"
BLTPR = "BLTPR"
BLTEPR = "BLTEPR"
)
const (
BUY_ONE_GET_ONE = "BUY_ONE_GET_ONE"
BUY_GET_OTHER_PRODUCT = "BUY_GET_OTHER_PRODUCT"
BUY_GET_DISCOUNT_PERCENTAGE = "BUY_GET_DISCOUNT_PERCENTAGE"
BUY_GET_DISCOUNT_PERCENTAGE_WITH_PRODUCT = "BUY_GET_DISCOUNT_PERCENTAGE_WITH_PRODUCT"
BUY_GET_DISCOUNT_FIXED = "BUY_GET_DISCOUNT_FIXED"
BUY_GET_DISCOUNT_FIXED_WITH_PRODUCT = "BUY_GET_DISCOUNT_FIXED_WITH_PRODUCT"
BUY_GET_HALF_PRICE = "BUY_GET_HALF_PRICE"
)
const (
// Produk sejenis (kategori sama), type berbeda, brand harus sama
SAME_BRAND_SIMILAR_CATEGORY = "SAME_BRAND_SIMILAR_CATEGORY"
// Produk berbeda boleh apa saja, brand harus sama
SAME_BRAND_ANY_CATEGORY = "SAME_BRAND_ANY_CATEGORY"
// Produk dan brand boleh berbeda
ANY_BRAND_ANY_CATEGORY = "ANY_BRAND_ANY_CATEGORY"
// Produk harus sama persis, brand boleh berbeda
ANY_BRAND_SAME_PRODUCT = "ANY_BRAND_SAME_PRODUCT"
)
type PromotionRules struct {
Key string
Action string
Type string
Operator string
OptionOne string
OptionTwo string
OptionThree string
OptionFour string
}
type PromotionRulesDTO struct {
Key string
Value string
}
func promotionRulesCompare(src *PromotionRules) string {
xBUY_ONE_GET_ONE := []string{BEQF, BGTF, BGTEF, BLTF, BLTEF}
xBUY_GET_OTHER_PRODUCT := []string{BEQFP, BGTFP, BGTEP, BLTP, BLTEP}
xBUY_GET_DISCOUNT_PERCENTAGE := []string{BEQDPC, BGTDPC, BGTEDPC, BLTDPC, BLTEDPC}
xBUY_GET_DISCOUNT_PERCENTAGE_WITH_PRODUCT := []string{BEQDPCP, BGTDPCP, BGTEDPCP, BLTDPCP, BLTEDPCP}
xBUY_GET_DISCOUNT_FIXED := []string{BEQDSUFX, BGTDSUFX, BGTEDSUFX, BLTDSUFX, BLTEDSUFX}
xBUY_GET_DISCOUNT_FIXED_WITH_PRODUCT := []string{BEQDSUFXP, BGTDSUFXP, BGTEDSUFXP, BLTDSUFXP, BLTEDSUFXP}
xBUY_GET_HALF_PRICE := []string{BEQPR, BGTPR, BGTEPR, BLTPR, BLTEPR}
if slices.Contains(xBUY_ONE_GET_ONE, src.Key) {
return BUY_ONE_GET_ONE
} else if slices.Contains(xBUY_GET_OTHER_PRODUCT, src.Key) {
return BUY_GET_OTHER_PRODUCT
} else if slices.Contains(xBUY_GET_DISCOUNT_PERCENTAGE, src.Key) {
return BUY_GET_DISCOUNT_PERCENTAGE
} else if slices.Contains(xBUY_GET_DISCOUNT_PERCENTAGE_WITH_PRODUCT, src.Key) {
return BUY_GET_DISCOUNT_PERCENTAGE_WITH_PRODUCT
} else if slices.Contains(xBUY_GET_DISCOUNT_FIXED, src.Key) {
return BUY_GET_DISCOUNT_FIXED
} else if slices.Contains(xBUY_GET_DISCOUNT_FIXED_WITH_PRODUCT, src.Key) {
return BUY_GET_DISCOUNT_FIXED_WITH_PRODUCT
} else if slices.Contains(xBUY_GET_HALF_PRICE, src.Key) {
return BUY_GET_HALF_PRICE
}
return ""
}
func PromoRules(src []PromotionRulesDTO) *PromotionRules {
var (
value string = ""
res *PromotionRules = new(PromotionRules)
)
for _, v := range src {
res.Key += v.Key
value += fmt.Sprintf("%s ", v.Value)
}
res.Key = strings.TrimSpace(res.Key)
res.Type = promotionRulesCompare(res)
result := strings.Split(strings.TrimSpace(value), " ")
fmt.Println("RESULT", result)
if len(result) == 3 && PromotionRulesMapping[res.Key] {
res.Action = result[0]
res.Operator = result[1]
res.OptionOne = result[2]
return res
}
if len(result) == 4 && PromotionRulesMapping[res.Key] {
res.Action = result[0]
res.Operator = result[1]
res.OptionOne = result[2]
res.OptionTwo = result[3]
return res
}
if len(result) == 5 && PromotionRulesMapping[res.Key] {
res.Action = result[0]
res.Operator = result[1]
res.OptionOne = result[2]
res.OptionTwo = result[3]
res.OptionThree = result[4]
return res
}
if len(result) == 6 && PromotionRulesMapping[res.Key] {
res.Action = result[0]
res.Operator = result[1]
res.OptionOne = result[2]
res.OptionTwo = result[3]
res.OptionThree = result[4]
res.OptionFour = result[5]
return res
}
return nil
}
func DecimalToFloat(n int64) float64 {
return float64(n) / 100
}
func promotionDiscount(total int, discount int64, action string) (t, d float64) {
parseFloat, _ := strconv.ParseFloat(strconv.Itoa(int(discount)), 64)
if action == "percentage" {
d = float64(total) * (parseFloat / 100)
t = float64(total) - d
return
} else if action == "fixed" {
d = parseFloat
t = float64(total) - parseFloat
return
}
return
}
// func main() {
// total := 100000
// diskonPersen := 10.0
// diskon, totalBayar := hitungDiskon(total, diskonPersen)
// fmt.Printf("Total Belanja: Rp %d\n", total)
// fmt.Printf("Diskon %.0f%%: Rp %.0f\n", diskonPersen, diskon)
// fmt.Printf("Total Bayar: Rp %.0f\n", totalBayar)
// }
func main() {
// BUY_ONE_GET_ONE := []PromotionRulesDTO{
// {Key: "B", Value: "3"},
// {Key: "EQ", Value: "="},
// {Key: "F", Value: "1"},
// }
BUY_GET_OTHER_PRODUCT := []PromotionRulesDTO{
{Key: "B", Value: "3"},
{Key: "EQ", Value: "="},
{Key: "F", Value: "7e672597-5ea9-4b75-978c-1787630a12b0"},
{Key: "P", Value: "PRODUCT_MATCH_SAME_BRAND"},
}
// BUY_GET_DISCOUNT_PERCENTAGE := []PromotionRulesDTO{
// {Key: "B", Value: "3"},
// {Key: "GT", Value: ">"},
// {Key: "D", Value: "10"},
// {Key: "PC", Value: "%"},
// }
// BUY_GET_DISCOUNT_PERCENTAGE_WITH_PRODUCT := []PromotionRulesDTO{
// {Key: "B", Value: "3"},
// {Key: "GT", Value: ">"},
// {Key: "D", Value: "10"},
// {Key: "PC", Value: "%"},
// {Key: "P", Value: "SAME_BRAND_SIMILAR_CATEGORY"},
// }
// BUY_GET_DISCOUNT_FIXED := []PromotionRulesDTO{
// {Key: "B", Value: "3"},
// {Key: "GT", Value: ">"},
// {Key: "D", Value: "10"},
// {Key: "SU", Value: "-"},
// {Key: "FX", Value: "fx"},
// }
// BUY_GET_DISCOUNT_FIXED_WITH_PRODUCT := []PromotionRulesDTO{
// {Key: "B", Value: "3"},
// {Key: "GT", Value: ">"},
// {Key: "D", Value: "10"},
// {Key: "SU", Value: "-"},
// {Key: "FX", Value: "fx"},
// {Key: "P", Value: SAME_BRAND_SIMILAR_CATEGORY},
// }
// BUY_GET_HALF_PRICE := []PromotionRulesDTO{
// {Key: "B", Value: "3"},
// {Key: "GT", Value: ">"},
// {Key: "PR", Value: "2"},
// }
result := PromoRules(BUY_GET_OTHER_PRODUCT)
if result == nil {
fmt.Println("Tidak valid")
}
fmt.Println("KEY: ", result.Key)
fmt.Println("OPERATOR: ", result.Operator)
fmt.Println("ACTION: ", result.Action)
fmt.Println("OPTIONS 1: ", result.OptionOne)
fmt.Println("OPTIONS 2: ", result.OptionTwo)
fmt.Println("OPTIONS 3: ", result.OptionThree)
fmt.Println("OPTIONS 4: ", result.OptionFour)
fmt.Println("\n")
fmt.Println("MASUK TYPE: ", result.Type)
// totalPrice, discount := promotionDiscount(100000, 50, "percentage")
// fmt.Println(totalPrice)
// fmt.Println(discount)
}