Skip to content

Commit

Permalink
imp: added init reserve data for initial areas by sql
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisOForgeFlow committed Oct 2, 2023
1 parent b1f1228 commit 37a01b6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 49 additions & 1 deletion stock_reserve_area/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,51 @@ def assign_reserve_area_ids_to_stock_move_query(cr):
cr.execute(query)


def create_reservation_data(cr):
cr.execute(
"""
insert into stock_move_reserve_area_line
(
create_uid,
create_date,
move_id,
product_id,
reserve_area_id,
reserved_availability
)
select
1 as create_uid,
current_timestamp as create_date,
sm.id as move_id,
sm.product_id as product_id,
rel.stock_reserve_area_id as reserve_area_id,
coalesce(sum(sml.product_uom_qty), 0) as reserved_availability
from stock_move as sm
inner join stock_move_stock_reserve_area_rel as rel on rel.stock_move_id = sm.id
left join stock_move_line as sml on sml.move_id = sm.id
where sm.state in ('assigned', 'partially_assigned')
and sm.id not in (
select move_id from stock_move_reserve_area_line group by move_id
)
group by sm.id, rel.stock_reserve_area_id, sm.picking_id, sm.product_id
having coalesce(sum(sml.product_uom_qty), 0) > 0
"""
)

cr.execute(
"""
update stock_move as sm set area_reserved_availability = q.reserved_availability
from (
select move_id, min(reserved_availability) as reserved_availability
from stock_move_reserve_area_line
group by move_id
) as q
where q.move_id = sm.id
"""
)


def post_init_hook(cr, registry):
"""
This post-init-hook will create a Reserve Area for each existing WH.
Expand All @@ -67,7 +112,7 @@ def post_init_hook(cr, registry):
[("id", "child_of", warehouse.view_location_id.id)]
)

reserve_area_obj.create(
reserve_area_obj.with_context(init_hook=True).create(
{
"name": warehouse.name,
"location_ids": [(6, 0, all_locations.ids)],
Expand All @@ -78,3 +123,6 @@ def post_init_hook(cr, registry):
_logger.info("Starting assign_reserve_area_ids_to_stock_move_query")

assign_reserve_area_ids_to_stock_move_query(cr)

_logger.info("Create reservation data for current moves")
create_reservation_data(cr)
2 changes: 2 additions & 0 deletions stock_reserve_area/models/stock_reserve_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def _onchange_location_ids(self):
)

def update_reserve_area_lines(self, moves, locs_to_add, locs_to_delete):
if self.env.context.get("init_hook", False):
return
for move in moves:
reserve_area_line = self.env["stock.move.reserve.area.line"].search(
[("move_id", "=", move.id), ("reserve_area_id", "=", self.id)]
Expand Down

0 comments on commit 37a01b6

Please sign in to comment.