bemade_fsm: fix a bug where changing the delivery address on a canceled/re-drafted SO screwed up subtasks

This commit is contained in:
Marc Durepos 2025-01-27 11:00:09 -05:00
parent 370ebae08b
commit 45c1bdf440
3 changed files with 29 additions and 15 deletions

View file

@ -51,7 +51,6 @@ class SaleOrder(models.Model):
store=True,
)
@api.depends("order_line.task_id")
def get_relevant_order_lines(self, task_id):
self.ensure_one()
linked_lines = self.order_line.filtered(

View file

@ -220,3 +220,24 @@ class Task(models.Model):
def _compute_root_ancestor(self):
for rec in self:
rec.root_ancestor = rec.parent_id and rec.parent_id.root_ancestor or self
@api.depends(
"partner_id",
"sale_line_id.order_partner_id",
"parent_id.sale_line_id",
"project_id.sale_line_id",
"milestone_id.sale_line_id",
"allow_billable",
)
def _compute_sale_line(self):
"""Override to prevent subtasks from inheriting parent's sale_line_id.
In the base implementation, if a task and its parent share the same commercial partner,
the task will inherit the parent's sale_line_id. This causes issues with our FSM tasks
where we explicitly want subtasks to NOT have a sale_line_id set.
"""
# Only run on root tasks
subtasks = self.filtered("parent_id")
(subtasks - subtasks.filtered("sale_line_id")).sale_line_id = False
super(Task, self - subtasks)._compute_sale_line()

View file

@ -384,7 +384,8 @@ class TestSalesOrder(BemadeFSMBaseTest):
# Create and confirm sale order
partner = self._generate_partner()
partner_2 = self._generate_partner(parent=partner)
partner_2 = self._generate_partner(parent=partner, company_type="person")
self.assertEqual(partner_2.commercial_partner_id, partner)
so = self._generate_sale_order(partner=partner)
sol = self._generate_sale_order_line(so, product=product)
so.action_confirm()
@ -421,19 +422,7 @@ class TestSalesOrder(BemadeFSMBaseTest):
so.with_context(disable_cancel_warning=True).action_cancel()
so.action_draft()
with Form(so) as so_form:
so_form.partner_shipping_id = partner_2
so_form.save()
so = so_form.record
so.action_confirm()
self.assertEqual(so.tasks_count, 1, "Should only have 1 task after reconfirm")
self.assertEqual(
so.tasks_ids,
main_task,
"Main task should still be the only one linked to SO",
)
so.write({"partner_shipping_id": partner_2.id})
# Get new tasks
new_main_task = sol.task_id
self.assertEqual(
@ -441,9 +430,14 @@ class TestSalesOrder(BemadeFSMBaseTest):
)
new_subtasks = new_main_task.child_ids
new_subtasks._compute_sale_line()
self.assertEqual(
len(new_subtasks), 2, "Should still have 2 subtasks after reconfirmation"
)
self.assertFalse(
new_subtasks.sale_line_id,
"Subtasks should not be linked to Sale Order Line",
)
new_task_names = (new_main_task | new_main_task._get_all_subtasks()).mapped(
"name"
)