@@ -14,10 +14,11 @@ def _action_launch_stock_rule(self, previous_product_uom_qty=False):
1414 return res
1515
1616 def _run_orderpoints_for_mto_products (self ):
17- orderpoints_to_procure_ids = []
1817 mto_route = self .env .ref ("stock.route_warehouse0_mto" , raise_if_not_found = False )
1918 if not mto_route :
2019 return
20+
21+ orderpoints_to_procure_ids = []
2122 for line in self :
2223 delivery_moves = line .move_ids .filtered (
2324 lambda m : m .picking_id .picking_type_code == "outgoing"
@@ -26,62 +27,73 @@ def _run_orderpoints_for_mto_products(self):
2627 for delivery_move in delivery_moves :
2728 if (
2829 not delivery_move .is_from_mto_route
29- or line .product_id ._variant_is_not_mto ()
30+ and mto_route
31+ not in delivery_move .product_id .product_tmpl_id .route_ids
3032 ):
3133 continue
3234 if not delivery_move .warehouse_id .mto_as_mts :
3335 continue
3436 orderpoint = line ._get_mto_orderpoint (delivery_move .product_id )
3537 if orderpoint .procure_recommended_qty :
3638 orderpoints_to_procure_ids .append (orderpoint .id )
37- wiz = (
38- self .env ["make.procurement.orderpoint" ]
39- .with_context (
40- ** {
41- "active_model" : "stock.warehouse.orderpoint" ,
42- "active_ids" : orderpoints_to_procure_ids ,
43- }
44- )
45- .create ({})
39+
40+ orderpoints_to_procure = self .env ["stock.warehouse.orderpoint" ].browse (
41+ orderpoints_to_procure_ids
4642 )
47- wiz .make_procurement ()
43+
44+ procurements = []
45+
46+ for item in orderpoints_to_procure :
47+ if not item .procure_recommended_qty :
48+ continue
49+ if item .product_uom .rounding <= 0 :
50+ continue
51+
52+ values = item ._prepare_procurement_values ()
53+ values ["date_planned" ] = item .procure_recommended_date
54+
55+ procurements .append (
56+ self .env ["procurement.group" ].Procurement (
57+ item .product_id ,
58+ item .procure_recommended_qty ,
59+ item .product_uom ,
60+ item .location_id ,
61+ item .name ,
62+ item .name ,
63+ item .company_id ,
64+ values ,
65+ )
66+ )
67+
68+ self .env ["procurement.group" ].run (procurements )
4869
4970 def _get_mto_orderpoint (self , product_id ):
5071 self .ensure_one ()
5172 warehouse = self .warehouse_id or self .order_id .warehouse_id
52- orderpoint = (
53- self .env ["stock.warehouse.orderpoint" ]
54- .with_context (active_test = False )
55- .search (
56- [
57- ("product_id" , "=" , product_id .id ),
58- (
59- "location_id" ,
60- "=" ,
61- warehouse ._get_locations_for_mto_orderpoints ().id ,
62- ),
63- ],
64- limit = 1 ,
65- )
73+ location = warehouse ._get_locations_for_mto_orderpoints ()
74+ orderpoint_model = self .env ["stock.warehouse.orderpoint" ].with_context (
75+ active_test = False
6676 )
67- if orderpoint and not orderpoint .active :
68- orderpoint .write (
69- {"active" : True , "product_min_qty" : 0.0 , "product_max_qty" : 0.0 }
70- )
71- elif not orderpoint :
72- orderpoint = (
73- self .env ["stock.warehouse.orderpoint" ]
74- .sudo ()
75- .create (
76- {
77- "product_id" : product_id .id ,
78- "warehouse_id" : warehouse .id ,
79- "location_id" : (
80- warehouse ._get_locations_for_mto_orderpoints ().id
81- ),
82- "product_min_qty" : 0.0 ,
83- "product_max_qty" : 0.0 ,
84- }
77+
78+ orderpoint = orderpoint_model .search_fetch (
79+ [("product_id" , "=" , product_id .id ), ("location_id" , "=" , location .id )],
80+ ["active" ],
81+ limit = 1 ,
82+ )
83+
84+ if orderpoint :
85+ if not orderpoint .active :
86+ orderpoint .write (
87+ {"active" : True , "product_min_qty" : 0.0 , "product_max_qty" : 0.0 }
8588 )
89+ else :
90+ orderpoint = orderpoint_model .sudo ().create (
91+ {
92+ "product_id" : product_id .id ,
93+ "warehouse_id" : warehouse .id ,
94+ "location_id" : location .id ,
95+ "product_min_qty" : 0.0 ,
96+ "product_max_qty" : 0.0 ,
97+ }
8698 )
8799 return orderpoint
0 commit comments