Skip to content

Commit

Permalink
[FIXUP] allows to update the product_uom_qty into blanket order
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Dec 18, 2024
1 parent fb44614 commit 42184c7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
29 changes: 28 additions & 1 deletion sale_order_blanket_order/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ def _check_blanket_product_not_overlapping(self):
)
)

@api.depends("call_off_line_ids", "order_type", "call_off_line_ids.state")
@api.depends(
"call_off_line_ids", "order_type", "call_off_line_ids.state", "product_uom_qty"
)
def _compute_call_off_remaining_qty(self):
"""Compute the quantity remaining to deliver for call-off order lines.
Expand Down Expand Up @@ -620,3 +622,28 @@ def _prepare_call_of_vals_to_deliver_blanket_remaining_qty(self):
"product_uom": self.product_uom.id,
"product_packaging_id": self.product_packaging_id.id,
}

def _blanket_check_update_product_uom_qty(self, values):
if "product_uom_qty" not in values:
return
new_qty = values.get("product_uom_qty")
for line in self:
if line.order_type != "blanket" or line.state != "sale" or line.is_expense:
continue
called_qty = line.product_uom_qty - line.call_off_remaining_qty
if (
float_compare(
new_qty, called_qty, precision_rounding=line.product_uom.rounding
)
< 0
):
raise ValidationError(
_(
"The purchased quantity cannot be less than the quantity "
"already called by call-off orders."
)
)

def write(self, values):
self._blanket_check_update_product_uom_qty(values)
return super(SaleOrderLine, self).write(values)
10 changes: 10 additions & 0 deletions sale_order_blanket_order_stock_prebook/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class SaleOrder(models.Model):
ondelete={"at_confirm": "cascade"},
)

def _get_or_create_reserve_procurement_group(self):
"""Get or create the procurement group for the reservation."""
self.ensure_one()
picking_reservations = self._get_reservation_pickings().filtered(
lambda p: p.state in ("assigned", "confirmed")
)
if picking_reservations:
return picking_reservations[0].group_id
return self._create_reserve_procurement_group()

def _blanket_order_reserve_call_off_remaining_qty(self):
"""Reserve the stock for the blanket order."""
to_reserve, other_orders = self._split_recrodset_for_reservation_strategy(
Expand Down
24 changes: 21 additions & 3 deletions sale_order_blanket_order_stock_prebook/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ def _prepare_reserve_procurements(self, group):
procurements = [proc]
return procurements

def _prebook_stock_for_call_off_remaining_qty(self):
def _prebook_stock_for_call_off_remaining_qty(self, previous_product_uom_qty=None):
"""Prebook the stock for qty remaining to call off."""
previous_product_uom_qty = previous_product_uom_qty or {}
self = self.with_context(sale_stock_prebook_stop_proc_run=True)
procurements = []
lines_by_order = defaultdict(self.browse)
for line in self:
lines_by_order[line.order_id] |= line
for order, lines in lines_by_order.items():
group = order._create_reserve_procurement_group()
group = order._get_or_create_reserve_procurement_group()
for line in lines:
if line.id in previous_product_uom_qty:
line._release_reservation()
remaining_qty = line.call_off_remaining_qty
if (
float_compare(
Expand All @@ -69,7 +72,7 @@ def _launch_stock_rule_for_call_off_line_qty(
# the call off order and the current line is part of this qty, it
# represents the real remaining qty to consume and therefore the qty to
# reserve on the blanket order.
self._prebook_stock_for_call_off_remaining_qty()
self._prebook_stock_for_call_off_remaining_qty(previous_product_uom_qty)

# run normal delivery rule on the blanket order. This will create the
# move on the call off order for the qty not reserved IOW the qty to
Expand All @@ -81,3 +84,18 @@ def _launch_stock_rule_for_call_off_line_qty(
super()._launch_stock_rule_for_call_off_line_qty(
qty_to_deliver, previous_product_uom_qty
)

def _action_launch_stock_rule(self, previous_product_uom_qty=None):
previous_product_uom_qty = previous_product_uom_qty or {}
lines_to_update_reservation = self.filtered(
lambda l: l.order_type == "blanket"
and l.id in previous_product_uom_qty
and l.order_id.blanket_reservation_strategy == "at_confirm"
)
lines_to_update_reservation._prebook_stock_for_call_off_remaining_qty(
previous_product_uom_qty
)
others_lines = self - lines_to_update_reservation
return super(SaleOrderLine, others_lines)._action_launch_stock_rule(
previous_product_uom_qty=previous_product_uom_qty
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ def test_change_reservation_mode(self):
moves = self._get_current_moves(self.blanket_so, only_reservation=True)
self.assertTrue(moves)

def test_update_reservation(self):
self.blanket_so.action_confirm()
so_line_product_2 = self.blanket_so.order_line.filtered(
lambda l: l.product_id == self.product_2
)
moves = self._get_current_moves(
self.blanket_so, self.product_2, only_reservation=True
)
reserved_qty = sum(moves.mapped("product_uom_qty"))
self.assertEqual(reserved_qty, 10)
so_line_product_2.product_uom_qty = 5.0
moves = self._get_current_moves(
self.blanket_so, self.product_2, only_reservation=True
)
reserved_qty = sum(moves.mapped("product_uom_qty"))
self.assertEqual(reserved_qty, 5)

so_line_product_2.product_uom_qty = 15.0
moves = self._get_current_moves(
self.blanket_so, self.product_2, only_reservation=True
)
reserved_qty = sum(moves.mapped("product_uom_qty"))
self.assertEqual(reserved_qty, 15)

@freezegun.freeze_time("2025-02-01")
def test_change_reservation_mode_partially_processed(self):
# in this test we create a call-off to partially deliver
Expand Down

0 comments on commit 42184c7

Please sign in to comment.