1
1
import logging
2
- from datetime import datetime
3
2
4
3
from odoo import api , fields , models
5
4
@@ -80,34 +79,33 @@ def action_run(self):
80
79
81
80
self .procurement_group_id = self .env ["procurement.group" ].create (
82
81
{
83
- "name" : (
84
- f"Adjustment Request { self .location_id .name } / { self .planned_consumed_date } "
85
- ),
82
+ "name" : (f"Resupply Order { self .location_id .name } " ),
86
83
"move_type" : "one" ,
87
84
}
88
85
)
89
86
90
- lines_with_stock_quants = self ._get_existing_quants ()
87
+ quant_groups = self ._get_existing_quants ()
91
88
92
89
procurements = []
93
90
94
- for (
95
- line ,
96
- stock_quant ,
97
- ) in lines_with_stock_quants : # self.stock_picking_adjustment_request_lines:
91
+ for line in self .stock_resupply_order_lines :
98
92
# service products have no tracking/lot and cannot be run in procurement
99
93
if line .product_id .product_tmpl_id .type == "service" :
100
94
continue
101
95
102
- if stock_quant .quantity < line .quantity :
96
+ available_quantity = self ._get_available_quantity_for_product (
97
+ quant_groups , line
98
+ )
99
+
100
+ if available_quantity < line .quantity :
103
101
procurements .append (
104
102
self .env ["procurement.group" ].Procurement (
105
103
product_id = line .product_id ,
106
- product_qty = line .quantity - stock_quant . quantity ,
104
+ product_qty = line .quantity - available_quantity ,
107
105
product_uom = line .product_id .product_tmpl_id .uom_id ,
108
106
location_id = self .location_id ,
109
- name = f"Adjustment request { self .planned_consumed_date } " ,
110
- origin = f"Adjustment request { self .planned_consumed_date } " ,
107
+ name = f"Resupply Order to { self .location_id . name } " ,
108
+ origin = f"Resupply Order to { self .location_id . name } " ,
111
109
company_id = self .company_id ,
112
110
values = self ._get_procurement_values (),
113
111
)
@@ -120,23 +118,37 @@ def action_run(self):
120
118
121
119
return self .procurement_group_id
122
120
121
+ def _get_available_quantity_for_product (
122
+ self , quant_groups , stock_resupply_order_line
123
+ ):
124
+ try :
125
+ # I could not find a way to merge stock_resupply_order_lines
126
+ # with the quant_groups query, so it is retrieved with a product
127
+ # id search here. Not ideal, but it works.
128
+ group = next (
129
+ group
130
+ for group in quant_groups
131
+ if group ["product_id" ][0 ] == stock_resupply_order_line .product_id .id
132
+ )
133
+
134
+ return group ["quantity" ] - group ["reserved_quantity" ]
135
+ except StopIteration :
136
+ return 0
137
+
123
138
def _get_procurement_values (self ):
124
139
"""
125
140
Values to pass to the procurement once the order is run.
126
141
"""
127
- return (
128
- {
129
- "group_id" : self .procurement_group_id ,
130
- "service_level_id" : self .service_level .id ,
131
- "planned_consumed_date" : self .planned_consumed_date ,
132
- },
133
- )
142
+ return {
143
+ "group_id" : self .procurement_group_id ,
144
+ }
134
145
135
146
def _get_existing_quants (self ):
136
147
"""
137
148
Get stock quants at the targeted location. Override if you need to
138
149
apply specific constraints.
139
150
"""
151
+
140
152
return (
141
153
self .env ["stock.quant" ]
142
154
.sudo ()
@@ -146,37 +158,17 @@ def _get_existing_quants(self):
146
158
(
147
159
"product_id" ,
148
160
"in" ,
149
- list ( self .stock_picking_adjustment_request_lines .product_id .id ) ,
161
+ self .stock_resupply_order_lines .product_id .ids ,
150
162
),
151
- # ("expiration_date", ">", self.planned_consumed_date),
152
- ],
153
- fields = [
154
- "product_id" ,
155
- "quantity:sum(quantity)" ,
156
- "reserved_quantity:sum(reserved_quantity)" ,
157
163
],
164
+ # Cant aggregate available_quantity here.
165
+ fields = ["product_id" , "quantity:sum" , "reserved_quantity:sum" ],
158
166
groupby = ["product_id" ],
159
- orderby = "location_id, product_id, lot_id " ,
167
+ orderby = "product_id" ,
160
168
lazy = False ,
161
169
)
162
170
)
163
171
164
- # stock_quant = self.env["stock.quant"].search(
165
- # [
166
- # ("location_id", "=", self.location_id.id),
167
- # ("product_id", "=", line.product_id.id),
168
- # ("expiration_date", ">", line.planned_consumed_date),
169
- # ]
170
- # )
171
-
172
- @api .model
173
- def action_run_at (self , date ):
174
- return self .search ([("date" , "=" , date )]).action_run ()
175
-
176
- @api .model
177
- def action_run_today (self ):
178
- return self .action_run_at (datetime .now ())
179
-
180
172
def action_view_transfers (self ):
181
173
pickings = self .procurement_group_id .stock_move_ids .picking_id
182
174
0 commit comments