-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_location.go
229 lines (215 loc) · 5.91 KB
/
stock_location.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
package odooconn
import (
"fmt"
"strings"
"sync"
"github.com/schollz/progressbar/v3"
)
func (o *OdooConn) StockLocation() {
mdl := "stock_location"
umdl := strings.Replace(mdl, "_", ".", -1)
fmt.Printf("\n%v StockLocation\n", umdl)
sem := make(chan int, o.JobCount)
var wg sync.WaitGroup
companyIDs := o.ResCompanyMap()
stmt := `
select distinct company,cityname,warehouse,location from (
select distinct
i.werks
,p.company
,p.cityname
,prefix||citycode warehouse
,case when i.sloc = '0001' then 'Stock' else i.sloc end as location
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '1020'
and p.werks not in ('1033','1013')
and trim(sbin) = ''
and i.unrestricted <> '0'
union
select distinct
i.werks
,p.company
,p.cityname
,prefix||citycode warehouse
,case when i.sloc = '0001' then 'Stock' else i.sloc end as location
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '1000'
and p.werks not in ('1033','1013')
and trim(sbin) = ''
and i.unrestricted <> '0'
union
select distinct
i.werks
,p.company
,p.cityname
,prefix||citycode warehouse
,case when i.sloc = '0001' then 'Stock' else i.sloc end as location
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '2000'
and p.werks not in ('1033','1013')
and trim(sbin) = ''
and i.unrestricted <> '0'
union
select distinct
i.werks
,p.company
,p.cityname
,prefix||citycode warehouse
,case when i.sloc = '0001' then 'Stock' else i.sloc end as location
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '2010'
and p.werks not in ('1033','1013')
and trim(sbin) = ''
and i.unrestricted <> '0'
) l
-- where l.company = $1
order by company,warehouse,location
`
type WarehouseLocation struct {
Company string `db:"company"`
City string `db:"cityname"`
Warehouse string `db:"warehouse"`
Location string `db:"location"`
}
var ww []WarehouseLocation
err := o.DB.Select(&ww, stmt)
o.checkErr(err)
recs := len(ww)
bar := progressbar.Default(int64(recs))
for _, v := range ww {
err := bar.Add(1)
o.checkErr(err)
cid := companyIDs[v.Company]
name := v.Location
pid, err := o.GetID(umdl, oarg{oarg{"complete_name", "=", v.Warehouse}, oarg{"company_id", "=", cid}})
o.checkErr(err)
r, err := o.GetID(umdl, oarg{oarg{"name", "=", v.Location}, oarg{"location_id", "=", pid}, oarg{"company_id", "=", cid}})
o.checkErr(err)
ur := map[string]interface{}{
"name": name,
"location_id": pid,
"company_id": cid,
}
o.Log.Info(mdl, "record", ur, "r", r, "v", v)
}
stmt = `
select distinct company,cityname,location,sbin from (
select distinct
i.werks
,p.company
,p.cityname
,case when i.sloc = '0001' then prefix||citycode||'/Stock' else prefix||citycode||'/'||i.sloc end as location
,i.sbin
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '1020'
and p.werks not in ('1033','1013')
and i.unrestricted <> '0'
and trim(i.sbin) <> ''
union
select distinct
i.werks
,p.company
,p.cityname
,case when i.sloc = '0001' then prefix||citycode||'/Stock' else prefix||citycode||'/'||i.sloc end as location
,i.sbin
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '1030'
and p.werks not in ('1033','1013')
and i.unrestricted <> '0'
and trim(i.sbin) <> ''
union
select distinct
i.werks
,p.company
,p.cityname
,case when i.sloc = '0001' then prefix||citycode||'/Stock' else prefix||citycode||'/'||i.sloc end as location
,i.sbin
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '1000'
and p.werks not in ('1033','1013')
and i.unrestricted <> '0'
and trim(i.sbin) <> ''
union
select distinct
i.werks
,p.company
,p.cityname
,case when i.sloc = '0001' then prefix||citycode||'/Stock' else prefix||citycode||'/'||i.sloc end as location
,i.sbin
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '2000'
and p.werks not in ('1033','1013')
and i.unrestricted <> '0'
and trim(i.sbin) <> ''
union
select distinct
i.werks
,p.company
,p.cityname
,case when i.sloc = '0001' then prefix||citycode||'/Stock' else prefix||citycode||'/'||i.sloc end as location
,i.sbin
from sapdata.inventorytemp i
join odoo.plantstemp p on i.werks = p.werks
where p.salesorg = '2010'
and p.werks not in ('1033','1013')
and i.unrestricted <> '0'
and trim(i.sbin) <> ''
) l
-- where l.company = $1
order by company,location,sbin
`
type StockLocation struct {
Company string `db:"company"`
City string `db:"cityname"`
Location string `db:"location"`
SBin string `db:"sbin"`
}
var rr []StockLocation
err = o.DB.Select(&rr, stmt)
o.checkErr(err)
recs = len(rr)
bar = progressbar.Default(int64(recs))
// tasker
wg.Add(recs)
for _, v := range rr {
go func(sem chan int, wg *sync.WaitGroup, bar *progressbar.ProgressBar, v StockLocation) {
defer bar.Add(1)
defer wg.Done()
sem <- 1
cid := companyIDs[v.Company]
name := v.SBin
pid, err := o.GetID(umdl, oarg{oarg{"complete_name", "=", v.Location}, oarg{"company_id", "=", cid}})
o.checkErr(err)
r, err := o.GetID(umdl, oarg{oarg{"name", "=", v.SBin}, oarg{"location_id", "=", pid}, oarg{"company_id", "=", cid}})
o.checkErr(err)
ur := map[string]interface{}{
"name": name,
"location_id": pid,
"company_id": cid,
}
o.Log.Info(mdl, "record", ur, "r", r, "v", v)
o.Record(umdl, r, ur)
<-sem
}(sem, &wg, bar, v)
}
wg.Wait()
}
func (o *OdooConn) StockLocationMap() map[string]int {
mdl := "stock_location"
umdl := strings.Replace(mdl, "_", ".", -1)
cc, err := o.SearchRead(umdl, oarg{}, 0, 0, []string{"name"})
o.checkErr(err)
cids := map[string]int{}
for _, c := range cc {
cids[c["complete_name"].(string)] = int(c["id"].(float64))
}
return cids
}