Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Nov 29, 2024
1 parent aef0093 commit 967750c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 38 deletions.
5 changes: 5 additions & 0 deletions sale_framework/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ def release_reservation(self):
to_unreserve = self.filtered(lambda order: order.order_type != "blanket")
return super(SaleOrder, to_unreserve).release_reservation()

def _link_lines_to_blanket_order_line(self):
"""Link the order lines to the blanket order lines."""
self.order_line._link_to_blanket_order_line()

def _on_blanket_order_confirm(self):
"""This method is called when a blanket order is confirmed.
Expand Down Expand Up @@ -243,6 +247,7 @@ def _on_call_off_order_confirm(self):
raise ValidationError(
_("Only call-off orders can be confirmed as call-off orders.")
)
self._link_lines_to_blanket_order_line()

def _blanket_order_reserve_stock(self):
"""Reserve the stock for the blanket order."""
Expand Down
117 changes: 79 additions & 38 deletions sale_framework/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _compute_call_off_remaining_qty(self):
)
call_off_delivered_qty = {}
for r in res:
call_off_delivered_qty[r["blanket_line_id"][0]] = r["product_uom_qty_sum"]
call_off_delivered_qty[r["blanket_line_id"][0]] = r["product_uom_qty"]
for line in self:
new_call_off_remaining_qty = call_off_delivered_qty.get(line.id, 0.0)
if line in blanket_lines:
Expand Down Expand Up @@ -345,9 +345,75 @@ def _action_launch_stock_rule(self, previous_product_uom_qty=False):
res = super(SaleOrderLine, other_lines)._action_launch_stock_rule(
previous_product_uom_qty
)
call_off_lines._launch_stock_rule_on_blanket_order(previous_product_uom_qty)
if not self.env.context.get("call_off_split_process"):
# When splitting a call-off line, we don't want to launch the stock rule
# since it will be done after the split process
call_off_lines._launch_stock_rule_on_blanket_order(previous_product_uom_qty)
return res

def _link_to_blanket_order_line(self):
"""Link the call-off order lines to the corresponding blanket order lines.
This method is called at the confirmation time of call-off orders. It will
link each call-off line to the corresponding blanket line. If the quantity on
the call-off line is greater than the quantity on the blanket line, the
call-off line will be split to ensure taht the quantity on the call-off line
is less than or equal to the quantity on the referenced blanket line. The split
process is special case which only occurs when multiple lines into for a same
product and package exists in the blanket order.
"""
matching_dict = self._get_blanket_lines_for_call_off_lines_dict()
for call_off_lines, blanket_lines in matching_dict.values():
for call_off_line in call_off_lines:
if call_off_line.blanket_line_id:
continue
qty_to_deliver = call_off_line.product_uom_qty
for blanket_line in blanket_lines:
# All the call-off quantities can be delivered on this blanket line
if (
float_compare(
qty_to_deliver,
blanket_line.call_off_remaining_qty,
precision_rounding=blanket_line.product_uom.rounding,
)
<= 0
):
call_off_line.blanket_line_id = blanket_line
qty_to_deliver = 0
break
# The quantity to deliver is greater than the remaining quantity
# on this blanket line. We split the call-off line into a new line
# which will consume the remaining quantity on this blanket line.
# The remaining quantity will be consumed by the next blanket line.
qty_deliverable = blanket_line.call_off_remaining_qty
if not float_is_zero(
qty_deliverable,
precision_rounding=call_off_line.product_uom.rounding,
):
call_off_line = call_off_line.with_context(
call_off_split_process=True
)
qty_to_deliver -= qty_deliverable
call_off_line.product_uom_qty -= qty_deliverable
# we force the state to draft to avoid the launch of the stock rule at copy
new_call_off_line = call_off_line.copy(
default={
"product_uom_qty": qty_deliverable,
"order_id": call_off_line.order_id.id,
}
)
new_call_off_line.blanket_line_id = blanket_line
new_call_off_line.state = call_off_line.state
if not float_is_zero(
qty_to_deliver,
precision_rounding=call_off_line.product_uom.rounding,
):
raise ValueError(
"The quantity to deliver on the call-off order line "
"is greater than the quantity remaining to deliver on "
"the blanket order line."
)

def _launch_stock_rule_on_blanket_order(self, previous_product_uom_qty):
for line in self:
line = line.with_context(call_off_sale_line_id=line.id)
Expand Down Expand Up @@ -401,40 +467,15 @@ def _stock_rule_on_blanket_with_reservation_at_call_off(
"""
self.ensure_one()
qty_to_deliver = self.product_uom_qty
blanket_lines = self.order_id.blanket_order_id.order_line.filtered(
lambda l: l.product_id == self.product_id
blanket_line = self.blanket_line_id
wizard = (
self.env["manual.delivery"]
.with_context(
active_id=blanket_line.id,
active_model="sale.order.line",
active_ids=blanket_line.ids,
)
.create({})
)
for blanket_line in blanket_lines:
if (
float_compare(
qty_to_deliver,
blanket_line.call_off_remaining_qty,
precision_rounding=blanket_line.product_uom.rounding,
)
<= 0
):
wizard = (
self.env["manual.delivery"]
.with_context(
active_id=blanket_line.id,
active_model="sale.order.line",
active_ids=blanket_line.ids,
)
.create({})
)
wizard.line_ids.quantity = qty_to_deliver
wizard.confirm()
break
else:
wizard = (
self.env["manual.delivery"]
.with_context(
active_id=blanket_line.id,
active_model="sale.order.line",
active_ids=blanket_line.ids,
)
.create({})
)
wizard.line_ids.quantity = blanket_line.call_off_remaining_qty
qty_to_deliver -= blanket_line.call_off_remaining_qty
wizard.confirm()
wizard.line_ids.quantity = qty_to_deliver
wizard.confirm()

0 comments on commit 967750c

Please sign in to comment.