/
a.go
574 lines (508 loc) · 14 KB
/
a.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package main
import (
"eaciit/gdrj/model"
"eaciit/gdrj/modules"
"os"
"math"
"strings"
"time"
"github.com/eaciit/dbox"
"github.com/eaciit/orm/v1"
"github.com/eaciit/toolkit"
)
var conn dbox.IConnection
var count int
var ratioTableName string
var (
t0 time.Time
fiscalyear, iscount, scount int
data map[string]float64
masters = toolkit.M{}
)
type promoratio struct {
SPG, Promo float64
}
var promoElementRatios = map[string]promoratio{
"2014-2015": promoratio{0.19152315, 0.80847685},
"2015-2016": promoratio{0.22213851, 0.77786149},
}
func setinitialconnection() {
var err error
conn, err = modules.GetDboxIConnection("db_godrej")
if err != nil {
toolkit.Println("Initial connection found : ", err)
os.Exit(1)
}
err = gdrj.SetDb(conn)
if err != nil {
toolkit.Println("Initial connection found : ", err)
os.Exit(1)
}
}
type plalloc struct {
Key string
Current float64
Expect float64
Absorbed float64
}
var plallocs = map[string]*plalloc{}
var salestotals = map[string]*plalloc{}
func main() {
t0 = time.Now()
setinitialconnection()
defer gdrj.CloseDb()
prepmastercalc()
toolkit.Println("Start data query...")
tablenames := []string{
"salespls-summary"}
for _, tn := range tablenames {
e := buildRatio(tn)
if e != nil {
toolkit.Printfn("Build ratio error: %s - %s", tn, e.Error())
return
}
e = processTable(tn)
if e != nil {
toolkit.Printfn("Process table error: %s - %s", tn, e.Error())
return
}
}
}
func buildRatio(tn string) error {
cursor, _ := conn.NewQuery().From(tn).
//Where(dbox.Eq("key.trxsrc", "VDIST"), dbox.Eq("key.customer_reportchannel", "RD")).
//Where(dbox.Eq("key.date_fiscal", "2014-2015")).
Select().Cursor(nil)
defer cursor.Close()
tstart := time.Now()
count := cursor.Count()
current := 0
i := 0
for {
mr := toolkit.M{}
if e := cursor.Fetch(&mr, 1, false); e != nil {
break
}
key := mr.Get("key", toolkit.M{}).(toolkit.M)
//yr := key.GetInt("date_year")
//mth := key.GetInt("date_month")
fiscal := key.GetString("date_fiscal")
kc := key.GetString("customer_customergroup")
if kc != "" {
keysales := toolkit.Sprintf("%s_%s", fiscal, kc)
salesvalue := mr.GetFloat64("PL8A")
salestotal := salestotals[keysales]
if salestotal == nil {
salestotal = new(plalloc)
salestotal.Key = keysales
}
salestotal.Current += salesvalue
salestotals[keysales] = salestotal
}
i++
current = makeProgressLog("Build Sales Ratio", i, count, 5, current, tstart)
}
//rawdatapl_promospg11072016_target
ctarget, _ := conn.NewQuery().From("rawdatapl_promospg11072016_targetratio").
Select().Cursor(nil)
defer ctarget.Close()
tstart = time.Now()
count = ctarget.Count()
current = 0
i = 0
for {
mr := toolkit.M{}
if e := ctarget.Fetch(&mr, 1, false); e != nil {
break
}
yr := mr.GetInt("year")
//mth := mr.GetInt("period")
//dt := gdrj.NewDate(yr, mth, 1)
/*
dt := time.Date(yr, time.Month(mth), 1, 0, 0, 0, 0, time.UTC).AddDate(0, 3, 0)
yr = dt.Year()
mth = int(dt.Month())
*/
//fiscal := dt.Fiscal
fiscal := toolkit.Sprintf("%d-%d", yr, yr+1)
//promoratios := promoElementRatios[fiscal]
kc := mr.GetString("keyaccountcode")
if kc != "" {
gl := mr.GetString("account")
promovalue := mr.GetFloat64("amountinidr_newtarget")
keypromo := toolkit.Sprintf("%s_%s_%s", fiscal, kc, gl)
alloc := plallocs[keypromo]
if alloc == nil {
alloc = new(plalloc)
alloc.Key = keypromo
}
alloc.Expect += promovalue
plallocs[keypromo] = alloc
}
i++
current = makeProgressLog("Build Promotion Ratio", i, count, 5, current, tstart)
}
return nil
}
func makeProgressLog(reference string, i, count, step, current int, tstart time.Time) int {
perstep := count * step / 100
if current == 0 {
current = perstep
}
pct := i * 100 / count
if i >= current {
toolkit.Printfn("Processing %s, %d of %d [%d pct] in %s",
reference, i, count, pct, time.Since(tstart).String())
current += perstep
}
return current
}
func processTable(tn string) error {
cursor, _ := conn.NewQuery().From(tn).
//Where(dbox.Eq("key.trxsrc", "VDIST"), dbox.Eq("key.customer_reportchannel", "RD")).
//Where(dbox.Eq("key.date_fiscal", "2015-2016"), dbox.Eq("key.customer_customergroup", "CR")).
//Where(dbox.Eq("key.date_fiscal", "2014-2015")).
Select().Cursor(nil)
defer cursor.Close()
plmodels := masters["plmodel"].(map[string]*gdrj.PLModel)
count := cursor.Count()
i := 0
step := count / 20
mstone := step
absorsedsales := float64(0)
for {
mr := toolkit.M{}
ef := cursor.Fetch(&mr, 1, false)
if ef != nil {
break
}
i++
if i >= mstone {
pct := i * 100 / count
toolkit.Printfn("Processing %s, %d of %d [%d pct] in %s",
tn, i, count, pct, time.Since(t0).String())
mstone += step
}
key := mr.Get("key", toolkit.M{}).(toolkit.M)
kc := key.GetString("customer_customergroup")
//year := key.GetInt("date_year")
//month := key.GetInt("date_month")
fiscal := key.GetString("date_fiscal")
sales := mr.GetFloat64("PL8A")
absorsedsales += sales
//keysales := toolkit.Sprintf("%d_%d_%s", year, month, kc)
keysales := toolkit.Sprintf("%s_%s", fiscal, kc)
salestotal := salestotals[keysales]
//plmodelcheck := ""
for k, plmodel := range plmodels {
//plmodel := plmodels[k]
if plmodel.GLReff != "" &&
plmodel.PLHeader1 == "Advt & Promo Expenses" {
newv := float64(0)
//keypromo := toolkit.Sprintf("%d_%d_%s_%s", year, month, kc, plmodel.GLReff)
keypromo := toolkit.Sprintf("%s_%s_%s", fiscal, kc, plmodel.GLReff)
alloc := plallocs[keypromo]
if alloc != nil && salestotal != nil {
newv = -toolkit.Div(alloc.Expect*sales, salestotal.Current)
alloc.Absorbed += newv
plallocs[keypromo] = alloc
}
mr.Set(k, float64(newv))
}
}
/*
if mr.GetFloat64(plmodelcheck) != 0 {
toolkit.Printfn("%s previously has value", mr.GetString("_id"))
}
*/
CalcSum(mr)
/*
if mr.GetFloat64(plmodelcheck) == 0 {
toolkit.Printfn("%s after calc has no value", mr.GetString("_id"))
}
plmodelcheck = ""
*/
esave := conn.NewQuery().From(tn).Save().Exec(toolkit.M{}.Set("data", mr))
if esave != nil {
return esave
}
}
toolkit.Printfn("Done. Sales absorsed: %f", absorsedsales)
for k, v := range plallocs {
if math.Abs(v.Expect+v.Absorbed) > 10 {
toolkit.Printfn("%s: Expected: %f Absorbed: %f", k, v.Expect, v.Absorbed)
}
}
return nil
}
func CalcSum(tkm toolkit.M) {
var netsales, cogs, grossmargin, sellingexpense,
sga, opincome, directexpense, indirectexpense,
royaltiestrademark, advtpromoexpense, operatingexpense,
freightexpense, nonoprincome, ebt, taxexpense,
percentpbt, eat, totdepreexp, damagegoods, ebitda, ebitdaroyalties, ebitsga,
grosssales, discount, advexp, promoexp, spgexp float64
exclude := []string{"PL8A", "PL14A", "PL74A", "PL26A", "PL32A", "PL39A", "PL41A", "PL44A",
"PL74B", "PL74C", "PL32B", "PL94B", "PL94C", "PL39B", "PL41B", "PL41C", "PL44B", "PL44C", "PL44D", "PL44E",
"PL44F", "PL6A", "PL0", "PL28", "PL29A", "PL31", "PL94A"}
plmodels := masters.Get("plmodel").(map[string]*gdrj.PLModel)
inexclude := func(f string) bool {
for _, v := range exclude {
if v == f {
return true
}
}
return false
}
for k, v := range tkm {
if k == "_id" || k == "key" {
continue
}
ar01k := strings.Split(k, "_")
if inexclude(ar01k[0]) {
continue
}
plmodel, exist := plmodels[ar01k[0]]
if !exist {
//toolkit.Println(k)
continue
}
Amount := toolkit.ToFloat64(v, 6, toolkit.RoundingAuto)
switch plmodel.PLHeader1 {
case "Net Sales":
netsales += Amount
case "Direct Expense":
directexpense += Amount
case "Indirect Expense":
indirectexpense += Amount
case "Freight Expense":
freightexpense += Amount
case "Royalties & Trademark Exp":
royaltiestrademark += Amount
case "Advt & Promo Expenses":
advtpromoexpense += Amount
case "G&A Expenses":
sga += Amount
case "Non Operating (Income) / Exp":
nonoprincome += Amount
case "Tax Expense":
taxexpense += Amount
case "Total Depreciation Exp":
if plmodel.PLHeader2 == "Damaged Goods" {
damagegoods += Amount
} else {
totdepreexp += Amount
}
}
// switch v.Group2 {
switch plmodel.PLHeader2 {
case "Gross Sales":
grosssales += Amount
case "Discount":
discount += Amount
case "Advertising Expenses":
advexp += Amount
case "Promotions Expenses":
promoexp += Amount
case "SPG Exp / Export Cost":
spgexp += Amount
}
}
cogs = directexpense + indirectexpense
grossmargin = netsales + cogs
sellingexpense = freightexpense + royaltiestrademark + advtpromoexpense
operatingexpense = sellingexpense + sga
opincome = grossmargin + operatingexpense
ebt = opincome + nonoprincome //asume nonopriceincome already minus
percentpbt = 0
if ebt != 0 {
percentpbt = taxexpense / ebt * 100
}
eat = ebt + taxexpense
ebitda = totdepreexp + damagegoods + opincome
ebitdaroyalties = ebitda - royaltiestrademark
ebitsga = opincome - sga
ebitsgaroyalty := ebitsga - royaltiestrademark
tkm.Set("PL0", grosssales)
tkm.Set("PL6A", discount)
tkm.Set("PL8A", netsales)
tkm.Set("PL14A", directexpense)
tkm.Set("PL74A", indirectexpense)
tkm.Set("PL26A", royaltiestrademark)
tkm.Set("PL32A", advtpromoexpense)
tkm.Set("PL94A", sga)
tkm.Set("PL39A", nonoprincome)
tkm.Set("PL41A", taxexpense)
tkm.Set("PL44A", totdepreexp)
tkm.Set("PL28", advexp)
tkm.Set("PL29A", promoexp)
tkm.Set("PL31", spgexp)
tkm.Set("PL74B", cogs)
tkm.Set("PL74C", grossmargin)
tkm.Set("PL32B", sellingexpense)
tkm.Set("PL94B", operatingexpense)
tkm.Set("PL94C", opincome)
tkm.Set("PL39B", ebt)
tkm.Set("PL41B", percentpbt)
tkm.Set("PL41C", eat)
tkm.Set("PL44B", opincome)
tkm.Set("PL44C", ebitda)
tkm.Set("PL44D", ebitdaroyalties)
tkm.Set("PL44E", ebitsga)
tkm.Set("PL44F", ebitsgaroyalty)
}
func CalcSum_(tkm toolkit.M) toolkit.M {
var netsales, cogs, grossmargin, sellingexpense,
sga, opincome, directexpense, indirectexpense,
royaltiestrademark, advtpromoexpense, operatingexpense,
freightexpense, nonoprincome, ebt, taxexpense,
percentpbt, eat, totdepreexp, damagegoods, ebitda, ebitdaroyalties, ebitsga,
grosssales, discount, advexp, promoexp, spgexp float64
exclude := []string{"PL8A", "PL14A", "PL74A", "PL26A", "PL32A", "PL39A", "PL41A", "PL44A",
"PL74B", "PL74C", "PL32B", "PL94B", "PL94C", "PL39B", "PL41B", "PL41C", "PL44B", "PL44C", "PL44D", "PL44E",
"PL44F", "PL6A", "PL0", "PL28", "PL29A", "PL31"}
//"PL94A",
plmodels := masters.Get("plmodel").(map[string]*gdrj.PLModel)
inexclude := func(f string) bool {
for _, v := range exclude {
if v == f {
return true
}
}
return false
}
for k, v := range tkm {
if k == "_id" {
continue
}
if inexclude(k) {
continue
}
// arrk := strings.Split(k, "_")
plmodel, exist := plmodels[k]
if !exist {
//toolkit.Println(k)
continue
}
Amount := toolkit.ToFloat64(v, 6, toolkit.RoundingAuto)
// PLHeader1
// PLHeader2
// PLHeader3
// switch v.Group1 {
switch plmodel.PLHeader1 {
case "Net Sales":
netsales += Amount
case "Direct Expense":
directexpense += Amount
case "Indirect Expense":
indirectexpense += Amount
case "Freight Expense":
freightexpense += Amount
case "Royalties & Trademark Exp":
royaltiestrademark += Amount
case "Advt & Promo Expenses":
advtpromoexpense += Amount
case "G&A Expenses":
sga += Amount
case "Non Operating (Income) / Exp":
nonoprincome += Amount
case "Tax Expense":
taxexpense += Amount
case "Total Depreciation Exp":
if plmodel.PLHeader2 == "Damaged Goods" {
damagegoods += Amount
} else {
totdepreexp += Amount
}
}
// switch v.Group2 {
switch plmodel.PLHeader2 {
case "Gross Sales":
grosssales += Amount
case "Discount":
discount += Amount
case "Advertising Expenses":
advexp += Amount
case "Promotions Expenses":
promoexp += Amount
case "SPG Exp / Export Cost":
spgexp += Amount
}
}
cogs = directexpense + indirectexpense
grossmargin = netsales + cogs
sellingexpense = freightexpense + royaltiestrademark + advtpromoexpense
operatingexpense = sellingexpense + sga
opincome = grossmargin + operatingexpense
ebt = opincome + nonoprincome //asume nonopriceincome already minus
percentpbt = 0
if ebt != 0 {
percentpbt = taxexpense / ebt * 100
}
eat = ebt + taxexpense
ebitda = totdepreexp + damagegoods + opincome
ebitdaroyalties = ebitda - royaltiestrademark
ebitsga = opincome - sga
ebitsgaroyalty := ebitsga - royaltiestrademark
tkm.Set("PL0", grosssales)
tkm.Set("PL6A", discount)
tkm.Set("PL8A", netsales)
tkm.Set("PL14A", directexpense)
tkm.Set("PL74A", indirectexpense)
tkm.Set("PL26A", royaltiestrademark)
tkm.Set("PL32A", advtpromoexpense)
tkm.Set("PL94A", sga)
tkm.Set("PL39A", nonoprincome)
tkm.Set("PL41A", taxexpense)
tkm.Set("PL44A", totdepreexp)
tkm.Set("PL28", advexp)
tkm.Set("PL29A", promoexp)
tkm.Set("PL31", spgexp)
tkm.Set("PL74B", cogs)
tkm.Set("PL74C", grossmargin)
tkm.Set("PL32B", sellingexpense)
tkm.Set("PL94B", operatingexpense)
tkm.Set("PL94C", opincome)
tkm.Set("PL39B", ebt)
tkm.Set("PL41B", percentpbt)
tkm.Set("PL41C", eat)
tkm.Set("PL44B", opincome)
tkm.Set("PL44C", ebitda)
tkm.Set("PL44D", ebitdaroyalties)
tkm.Set("PL44E", ebitsga)
tkm.Set("PL44F", ebitsgaroyalty)
return tkm
}
func buildmap(holder interface{},
fnModel func() orm.IModel,
filter *dbox.Filter,
fnIter func(holder interface{}, obj interface{})) interface{} {
crx, ecrx := gdrj.Find(fnModel(), filter, nil)
if ecrx != nil {
toolkit.Printfn("Cursor Error: %s", ecrx.Error())
os.Exit(100)
}
defer crx.Close()
for {
s := fnModel()
e := crx.Fetch(s, 1, false)
if e != nil {
break
}
fnIter(holder, s)
}
return holder
}
func prepmastercalc() {
toolkit.Println("--> PL MODEL")
masters.Set("plmodel", buildmap(map[string]*gdrj.PLModel{},
func() orm.IModel {
return new(gdrj.PLModel)
},
nil,
func(holder, obj interface{}) {
h := holder.(map[string]*gdrj.PLModel)
o := obj.(*gdrj.PLModel)
h[o.ID] = o
}).(map[string]*gdrj.PLModel))
}