@@ -49,6 +49,51 @@ def assign_reserve_area_ids_to_stock_move_query(cr):
4949 cr .execute (query )
5050
5151
52+ def create_reservation_data (cr ):
53+ cr .execute (
54+ """
55+ insert into stock_move_reserve_area_line
56+ (
57+ create_uid,
58+ create_date,
59+ move_id,
60+ product_id,
61+ reserve_area_id,
62+ reserved_availability
63+ )
64+
65+ select
66+ 1 as create_uid,
67+ current_timestamp as create_date,
68+ sm.id as move_id,
69+ sm.product_id as product_id,
70+ rel.stock_reserve_area_id as reserve_area_id,
71+ coalesce(sum(sml.product_uom_qty), 0) as reserved_availability
72+ from stock_move as sm
73+ inner join stock_move_stock_reserve_area_rel as rel on rel.stock_move_id = sm.id
74+ left join stock_move_line as sml on sml.move_id = sm.id
75+ where sm.state in ('assigned', 'partially_assigned')
76+ and sm.id not in (
77+ select move_id from stock_move_reserve_area_line group by move_id
78+ )
79+ group by sm.id, rel.stock_reserve_area_id, sm.picking_id, sm.product_id
80+ having coalesce(sum(sml.product_uom_qty), 0) > 0
81+ """
82+ )
83+
84+ cr .execute (
85+ """
86+ update stock_move as sm set area_reserved_availability = q.reserved_availability
87+ from (
88+ select move_id, min(reserved_availability) as reserved_availability
89+ from stock_move_reserve_area_line
90+ group by move_id
91+ ) as q
92+ where q.move_id = sm.id
93+ """
94+ )
95+
96+
5297def post_init_hook (cr , registry ):
5398 """
5499 This post-init-hook will create a Reserve Area for each existing WH.
@@ -67,7 +112,7 @@ def post_init_hook(cr, registry):
67112 [("id" , "child_of" , warehouse .view_location_id .id )]
68113 )
69114
70- reserve_area_obj .create (
115+ reserve_area_obj .with_context ( init_hook = True ). create (
71116 {
72117 "name" : warehouse .name ,
73118 "location_ids" : [(6 , 0 , all_locations .ids )],
@@ -78,3 +123,6 @@ def post_init_hook(cr, registry):
78123 _logger .info ("Starting assign_reserve_area_ids_to_stock_move_query" )
79124
80125 assign_reserve_area_ids_to_stock_move_query (cr )
126+
127+ _logger .info ("Create reservation data for current moves" )
128+ create_reservation_data (cr )
0 commit comments