/
actd.go
294 lines (257 loc) · 6.45 KB
/
actd.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
package main
import (
"eaciit/gdrj/model"
"eaciit/gdrj/modules"
"os"
"github.com/eaciit/dbox"
"github.com/eaciit/orm/v1"
"github.com/eaciit/toolkit"
// "strings"
"time"
)
var conn dbox.IConnection
var count int
var (
t0 time.Time
fiscalyear, iscount, scount int
data map[string]float64
masters = toolkit.M{}
)
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
Total float64
}
var plallocs = map[string]*plalloc{}
func main() {
t0 = time.Now()
setinitialconnection()
defer gdrj.CloseDb()
prepmastercalc()
toolkit.Println("Start data query...")
tablenames := []string{
"salespls-summary"}
var e error
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
}
}
}
var pcts = map[string]float64{
"MT": -0.05633490,
"GT": -0.05930065,
"RD": -0.05746188,
}
func processTable(tn string) error {
cursor, _ := conn.NewQuery().From(tn).
//Where(dbox.Eq("key.trxsrc", "VDIST"), dbox.Eq("key.customer_reportchannel", "RD")).
Select().Cursor(nil)
defer cursor.Close()
count := cursor.Count()
i := 0
step := count / 20
mstone := step
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)
channel := key.GetString("customer_channelname")
actpct := pcts[channel]
sales := mr.GetFloat64("PL0")
disc := actpct * sales
mr.Set("PL7A", disc)
mr = CalcSum(mr)
esave := conn.NewQuery().From(tn).Save().Exec(toolkit.M{}.Set("data", mr))
if esave != nil {
return esave
}
}
return nil
}
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))
}