Skip to content

Commit

Permalink
[WIP] fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mariadforgeflow committed Oct 3, 2023
1 parent 552b889 commit 8477486
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 4 additions & 0 deletions stock_reserve_area/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def create_reservation_data(cr):
and sm.id not in (
select move_id from stock_move_reserve_area_line group by move_id
)
and sm.location_dest_id not in (
select location_id from stock_reserve_area_stock_location_rel
where reserve_area_id = rel.stock_reserve_area_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
"""
Expand Down
6 changes: 4 additions & 2 deletions stock_reserve_area/models/stock_move_reserve_area_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StockMoveReserveAreaLine(models.Model):

_name = "stock.move.reserve.area.line"

move_id = fields.Many2one("stock.move")
move_id = fields.Many2one("stock.move", index=True)

picking_id = fields.Many2one("stock.picking", related="move_id.picking_id")

Expand All @@ -23,7 +23,9 @@ class StockMoveReserveAreaLine(models.Model):
" Area of the source location.",
)

reserve_area_id = fields.Many2one("stock.reserve.area", ondelete="cascade")
reserve_area_id = fields.Many2one(
"stock.reserve.area", ondelete="cascade", index=True
)

product_id = fields.Many2one(
"product.product", related="move_id.product_id", store=True
Expand Down
50 changes: 29 additions & 21 deletions stock_reserve_area/models/stock_reserve_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ def check_location_ids(self):
raise UserError(_("All Areas must be concentric"))

Check warning on line 48 in stock_reserve_area/models/stock_reserve_area.py

View check run for this annotation

Codecov / codecov/patch

stock_reserve_area/models/stock_reserve_area.py#L48

Added line #L48 was not covered by tests

def is_location_in_area(self, location):
location_in = self.env["stock.location"].search(
[("id", "in", self.location_ids.ids), ("id", "=", location.id)]
)
return bool(location_in)
return bool(location in self.location_ids)

@api.onchange("location_ids")
def _onchange_location_ids(self):
Expand Down Expand Up @@ -79,34 +76,37 @@ def update_reserve_area_lines(self, moves, locs_to_add, locs_to_delete):
# and now is out move the move didn't impact this area but now the
# source location is inside of it and it's out move
move.reserve_area_ids |= self

Check warning on line 78 in stock_reserve_area/models/stock_reserve_area.py

View check run for this annotation

Codecov / codecov/patch

stock_reserve_area/models/stock_reserve_area.py#L78

Added line #L78 was not covered by tests
move._do_unreserve()
move._action_assign() # will create new reserve_area_line and reserve
elif (
not move._is_out_area(self)
and reserve_area_line
and (
move.location_dest_id in locs_to_add
or move.location_id in locs_to_delete
)
if move.state not in ("confirmed", "waiting"):
# TODO don't unreserve and reserve, reserve in area the locally reserved qty
move._do_unreserve()
move._action_assign() # will create new reserve_area_line and reserve

Check warning on line 82 in stock_reserve_area/models/stock_reserve_area.py

View check run for this annotation

Codecov / codecov/patch

stock_reserve_area/models/stock_reserve_area.py#L81-L82

Added lines #L81 - L82 were not covered by tests
elif not move._is_out_area(self) and (
move.location_dest_id in locs_to_add
or move.location_id in locs_to_delete
):
# 1. the move was out of the area but we dest loc is added in area so it
# is not out move anymore.
# 2. the move was out of the area but we remove source location from
# area so this area doesn't apply for reservation.
move.reserve_area_ids -= self

Check warning on line 91 in stock_reserve_area/models/stock_reserve_area.py

View check run for this annotation

Codecov / codecov/patch

stock_reserve_area/models/stock_reserve_area.py#L91

Added line #L91 was not covered by tests
reserve_area_line.unlink()
move._do_unreserve()
move._action_assign()
if reserve_area_line:
# TODO don't unreserve and reserve, recalculate area_reserved_availability
reserve_area_line.unlink()
move._do_unreserve()
move._action_assign()

Check warning on line 96 in stock_reserve_area/models/stock_reserve_area.py

View check run for this annotation

Codecov / codecov/patch

stock_reserve_area/models/stock_reserve_area.py#L94-L96

Added lines #L94 - L96 were not covered by tests

@api.model
def create(self, vals):
res = super().create(vals)
moves_impacted = self.env["stock.move"].search(
[
"|",
("location_id", "in", res.location_ids.ids),
("location_dest_id", "in", res.location_ids.ids),
("state", "in", ("confirmed", "waiting", "partially_available")),
("location_dest_id", "not in", res.location_ids.ids),
(
"state",
"in",
("confirmed", "waiting", "assigned", "partially_available"),
),
]
)
res.update_reserve_area_lines(moves_impacted, res.location_ids, [])
Expand All @@ -126,7 +126,11 @@ def write(self, vals):
"|",
("location_id", "in", to_add.ids + to_delete.ids),
("location_dest_id", "in", to_add.ids + to_delete.ids),
("state", "in", ("confirmed", "waiting", "partially_available")),
(
"state",
"in",
("confirmed", "waiting", "partially_available", "assigned"),
),
]
)
self.update_reserve_area_lines(moves_impacted, to_add, to_delete)
Expand All @@ -138,7 +142,11 @@ def unlink(self):
moves_impacted = self.env["stock.move"].search(

Check warning on line 142 in stock_reserve_area/models/stock_reserve_area.py

View check run for this annotation

Codecov / codecov/patch

stock_reserve_area/models/stock_reserve_area.py#L140-L142

Added lines #L140 - L142 were not covered by tests
[
("location_id", "in", locations.ids),
("state", "in", ("confirmed", "waiting", "partially_available")),
(
"state",
"in",
("confirmed", "waiting", "partially_available", "assigned"),
),
]
)
moves_impacted._compute_area_reserved_availability()

Check warning on line 152 in stock_reserve_area/models/stock_reserve_area.py

View check run for this annotation

Codecov / codecov/patch

stock_reserve_area/models/stock_reserve_area.py#L152

Added line #L152 was not covered by tests
Expand Down

0 comments on commit 8477486

Please sign in to comment.