Skip to content

Commit

Permalink
[FIX] when invoicing timesheet updating an existing invoice, automati…
Browse files Browse the repository at this point in the history
…cally compute the invoice parent task option, for consistency
  • Loading branch information
florian-dacosta committed Apr 26, 2024
1 parent 961d7be commit a117e02
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
25 changes: 25 additions & 0 deletions project_invoicing_subcontractor/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,28 @@ def _compute_to_pay(self):
"paid",
):
invoice.to_pay = True

def _is_invoiced_with_parent_task_option(self):
"""
Res is True if invoice has been invoiced with parent task option
False if invoice has been invoiced without parent task option
None if it was not applicable. (no child task in the invoice)
"""
self.ensure_one()
res = None
# check if invoice parent task was used during invoicing
has_child_task = False
for inv_line in self.invoice_line_ids:
timesheet_tasks = inv_line.subcontractor_work_ids.timesheet_line_ids.task_id
if not timesheet_tasks:
continue
if not has_child_task and inv_line.task_id.parent_id:
has_child_task = True
if len(timesheet_tasks) > 1 or (
len(timesheet_tasks) == 1 and timesheet_tasks.id != inv_line.task_id
):
res = True
break
if not res and has_child_task:
res = False
return res
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def _compute_to_invoice_partner_id(self):
invoice_id = fields.Many2one(
"account.move", compute="_compute_invoice", readonly=False, store=True
)
invoice_parent_task = fields.Boolean()
invoice_parent_task = fields.Boolean(
compute="_compute_invoice_parent_task", store=True, readonly=False
)
has_parent_task = fields.Boolean(compute="_compute_has_parent_task")

invoicing_typology_id = fields.Many2one(
Expand All @@ -72,6 +74,27 @@ def _compute_to_invoice_partner_id(self):
default=lambda self: self._get_default_timesheet_lines(),
)

@api.depends("invoice_id", "create_invoice")
def _compute_invoice_parent_task(self):
for rec in self:
# In case an invoice is beeing modified, if invoice_parent_task option
# was used when it was first created, it has to be used also during
# modification. For consistency, of course, but also because the method
# we use to update an invoice is to delete an invoice line and re-create
# it. If we are not in the same mode, we may delete an invoice line
# linked to multiple task and try to recreate multiple lines...It is
# not what we want, and it is not implemented either, so it fails.
if rec.invoice_id and not rec.create_invoice:
is_invoiced_with_parent_task = (
rec.invoice_id._is_invoiced_with_parent_task_option()
)
# if result is None, there were no child task timesheet on the invoice
# in that case, do not change the option choosen by user.
if is_invoiced_with_parent_task is True:
rec.invoice_parent_task = True
elif is_invoiced_with_parent_task is False:
rec.invoice_parent_task = False

@api.depends("timesheet_line_ids")
def _compute_has_parent_task(self):
for record in self:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<field name='has_parent_task' invisible="1" />
<field
name="invoice_parent_task"
attrs="{'invisible': [('has_parent_task', '=', False)]}"
attrs="{'invisible': [('has_parent_task', '=', False)], 'readonly': [('invoice_id', '!=', False)]}"
/>

<field name="to_invoice_partner_id" invisible="1" />
Expand Down

0 comments on commit a117e02

Please sign in to comment.