-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_warehouse_orderpoint.go
198 lines (184 loc) · 7.18 KB
/
stock_warehouse_orderpoint.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
package odooconn
import (
"fmt"
"strings"
"sync"
"github.com/schollz/progressbar/v3"
)
func (o *OdooConn) StockReorderPoint() {
mdl := "stock_warehouse_orderpoint"
umdl := strings.Replace(mdl, "_", ".", -1)
fmt.Printf("\n%v StockReorderPoint\n", umdl)
sem := make(chan int, o.JobCount)
var wg sync.WaitGroup
stmt := `
select default_code,company,warehouse,location,stock_min,stock_max,lot_size_min,uom,reorder_rule,reorder_rule_description,mtart
from (
select distinct
right(m.matnr,7) as default_code
,p.company
,p.cityname||' '||p.ccode as warehouse
,case when trim(i.sbin) = '' then trim(p.prefix||p.citycode||'/Stock')
when i.sbin is null then trim(p.prefix||p.citycode||'/Stock')
else trim(p.prefix||p.citycode||'/Stock/'||i.sbin) end as location
,i.stock_min,i.stock_max,i.lot_size_min
,uu."name" as uom
,i.reorder_rule,i.reorder_rule_description
,m.mtart
from sapdata.materials m
join (
select
mandt,matnr,werks,'1030' salesorg,name1,sloc,sbin,unrestricted,returnqty,inqualinsp,"blocked",suom,suom_per_x,suom_per_y,perunit
,unitcost,inventorycost,stock_min,stock_max,stock_rounding,lot_size_min,mrp_type,mrp_type_description,reorder_rule,reorder_rule_description
from sapdata.inventorytemp i where salesorg = '1000'
) i on m.mandt = i.mandt and m.matnr = i.matnr
join odoo.uom_uom uu on m.meins = uu.uom
left join odoo.mat_dnu dnu on m.matnr = dnu.matnr
left join odoo.plantstemp p on i.salesorg = p.salesorg and i.werks = p.werks
where dnu.matnr is null
and i.werks not in ('1006','1013','1024')
and p.salesorg = '1030'
and i.stock_min <> 0
and m.mtart in ('ROH','HALB')
union
select distinct
right(m.matnr,7) as default_code
,p.company
,p.cityname||' '||p.ccode as warehouse
,case when trim(i.sbin) = '' then trim(p.prefix||p.citycode||'/Stock')
when i.sbin is null then trim(p.prefix||p.citycode||'/Stock')
else trim(p.prefix||p.citycode||'/Stock/'||i.sbin) end as location
,i.stock_min,i.stock_max,i.lot_size_min
,uu."name" as uom
,i.reorder_rule,i.reorder_rule_description
,m.mtart
from sapdata.materials m
join (
select
mandt,matnr,werks,salesorg,name1,sloc,sbin,unrestricted,returnqty,inqualinsp,"blocked",suom,suom_per_x,suom_per_y,perunit
,unitcost,inventorycost,stock_min,stock_max,stock_rounding,lot_size_min,mrp_type,mrp_type_description,reorder_rule,reorder_rule_description
from sapdata.inventorytemp i where salesorg = '1000'
) i on m.mandt = i.mandt and m.matnr = i.matnr
join odoo.uom_uom uu on m.meins = uu.uom
left join odoo.mat_dnu dnu on m.matnr = dnu.matnr
left join odoo.plantstemp p on i.salesorg = p.salesorg and i.werks = p.werks
where dnu.matnr is null
and i.werks not in ('1006','1013','1024')
and p.salesorg = '1000'
and i.stock_min <> 0
and m.mtart not in ('DIEN','ROH','HALB')
union
select distinct
right(m.matnr,7) as default_code
,p.company
,p.cityname||' '||p.ccode as warehouse
,case when trim(i.sbin) = '' then trim(p.prefix||p.citycode||'/Stock')
when i.sbin is null then trim(p.prefix||p.citycode||'/Stock')
else trim(p.prefix||p.citycode||'/Stock/'||i.sbin) end as location
,i.stock_min,i.stock_max,i.lot_size_min
,uu."name" as uom
,i.reorder_rule,i.reorder_rule_description
,m.mtart
from sapdata.materials m
join (
select
mandt,matnr,werks,salesorg,name1,sloc,sbin,unrestricted,returnqty,inqualinsp,"blocked",suom,suom_per_x,suom_per_y,perunit
,unitcost,inventorycost,stock_min,stock_max,stock_rounding,lot_size_min,mrp_type,mrp_type_description,reorder_rule,reorder_rule_description
from sapdata.inventorytemp i where salesorg = '1020'
) i on m.mandt = i.mandt and m.matnr = i.matnr
join odoo.uom_uom uu on m.meins = uu.uom
left join odoo.mat_dnu dnu on m.matnr = dnu.matnr
left join odoo.plantstemp p on i.salesorg = p.salesorg and i.werks = p.werks
where dnu.matnr is null
and i.werks in ('1024')
and p.salesorg = '1020'
and i.stock_min <> 0
and m.mtart in ('FERT')
) sor
order by default_code,company
`
type Reorderpoint struct {
DefaultCode string `db:"default_code"`
Company string `db:"company"`
Warehouse string `db:"warehouse"`
Location string `db:"location"`
StockMin float64 `db:"stock_min"`
StockMax float64 `db:"stock_max"`
LotSize float64 `db:"lot_size_min"`
UOM string `db:"uom"`
ReorderRule string `db:"reorder_rule"`
ReorderRuleDescription string `db:"reorder_rule_description"`
Mtart string `db:"mtart"`
}
var rr []Reorderpoint
err := o.DB.Select(&rr, stmt)
o.checkErr(err)
recs := len(rr)
bar := progressbar.Default(int64(recs))
companyIDs := o.ResCompanyMap()
routeID, err := o.GetID("stock.location.route", oarg{oarg{"name", "=", "Buy"}})
o.checkErr(err)
wg.Add(recs)
for _, v := range rr {
go func(sem chan int, wg *sync.WaitGroup, bar *progressbar.ProgressBar, v Reorderpoint) {
defer bar.Add(1)
defer wg.Done()
sem <- 1
companyID := companyIDs[v.Company]
warehouseID, err := o.GetID("stock.warehouse", oarg{oarg{"name", "=", v.Warehouse}, oarg{"company_id", "=", companyID}})
o.checkErr(err)
locationID, err := o.GetID("stock.location", oarg{oarg{"complete_name", "=", v.Location}, oarg{"company_id", "=", companyID}})
o.checkErr(err)
productTmplID, err := o.GetID("product.template", oarg{oarg{"default_code", "=", v.DefaultCode}})
o.checkErr(err)
productID, err := o.GetID("product.product", oarg{oarg{"product_tmpl_id", "=", productTmplID}})
o.checkErr(err)
r, err := o.GetID(umdl, oarg{
oarg{"warehouse_id", "=", warehouseID},
oarg{"location_id", "=", locationID},
oarg{"product_id", "=", productID},
oarg{"company_id", "=", companyID},
})
o.checkErr(err)
ur := map[string]interface{}{
"product_id": productID,
"warehouse_id": warehouseID,
"location_id": locationID,
"company_id": companyID,
"route_id": routeID,
"product_min_qty": v.StockMin,
"product_max_qty": v.StockMax,
"qty_multiple": v.LotSize,
"trigger": "manual",
}
o.Log.Info(umdl, "record", ur, "r", r)
o.Record(umdl, r, ur)
<-sem
}(sem, &wg, bar, v)
}
wg.Wait()
}
// StockWarehouseOrderpoint function
func (o *OdooConn) StockWarehouseOrderpoint() {
mdl := "stock_warehouse_orderpoint"
umdl := strings.Replace(mdl, "_", ".", -1)
stmt := `
select
"name",product_id,location_id
,product_min_qty,product_max_qty
from odoo.artg_stock_warehouse_orderpointtemp
where product_min_qty <> 0 and product_max_qty <> 0
order by location_id,product_id`
rr := []struct {
Name string `db:"name"`
ProductID string `db:"product_id"`
LocationID string `db:"location_id"`
ProductMinQty float64 `db:"product_min_qty"`
ProductMaxQty float64 `db:"product_max_qty"`
}{}
err := o.DB.Select(&rr, stmt)
o.checkErr(err)
recs := len(rr)
o.Log.Info(mdl, "model", umdl, "record", recs)
// id | name | trigger | active | snoozed_until | warehouse_id | location_id | product_id | product_category_id | product_min_qty | product_max_qty | qty_multiple | group_id | company_id | route_id | qty_to_order | create_uid | create_date | write_uid | write_date | bom_id | supplier_id
}