Skip to content

Commit

Permalink
[15.0][FIX] sale_timesheet_rounded: avoid recomputing rounded amounts…
Browse files Browse the repository at this point in the history
… when posting an invoice
  • Loading branch information
StephaneMangin committed Jun 8, 2023
1 parent 8da4328 commit 8ab89bf
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions sale_timesheet_rounded/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import models
from .hooks import pre_init_hook
from . import wizard
1 change: 1 addition & 0 deletions sale_timesheet_rounded/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import account_analytic_line
from . import project_project
from . import sale
from . import account_move
7 changes: 7 additions & 0 deletions sale_timesheet_rounded/models/account_analytic_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class AccountAnalyticLine(models.Model):
copy=False,
)

@api.depends("timesheet_invoice_id.state")
def _compute_project_id(self):
field_rounded = self._fields["unit_amount_rounded"]
if self._context.get("timesheet_no_recompute", False):
self.env.remove_to_compute(field_rounded, self)
return super()._compute_project_id()

@api.depends("project_id", "unit_amount")
def _compute_unit_rounded(self):
for record in self:
Expand Down
31 changes: 31 additions & 0 deletions sale_timesheet_rounded/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

from odoo import models


class AccountMove(models.Model):

_inherit = "account.move"

def _post(self, soft=True):
# We must avoid the recomputation of the unit amount rounded called by
# the compute_project_id (especially when project has not been changed)
return super(AccountMove, self.with_context(timesheet_no_recompute=True))._post(
soft=soft
)

def unlink(self):
return super(
AccountMove, self.with_context(timesheet_no_recompute=True)
).unlink()

def button_cancel(self):
return super(
AccountMove, self.with_context(timesheet_no_recompute=True)
).button_cancel()

def button_draft(self):
return super(
AccountMove, self.with_context(timesheet_no_recompute=True)
).button_draft()
57 changes: 57 additions & 0 deletions sale_timesheet_rounded/tests/test_rounded.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,60 @@ def test_calc_rounded_amount_method(self):
self.assertEqual(
aal._calc_rounded_amount(rounding_unit, rounding_method, factor, amount), 2
)

def test_post_invoice_with_rounded_amount_unchanged(self):
"""Posting an invoice MUST NOT recompute rounded amount unit.
- invoicing the SO should not recompute and update the
unit_amount_rounded
- the invoiced qty should be the same as the aal.unit_amount_rounded
"""
unit_amount_rounded = 111
analytic_line = self.create_analytic_line(unit_amount=10)
analytic_line.unit_amount_rounded = unit_amount_rounded
account_move = self.sale_order._create_invoices()
prd_ts_id = self.product_delivery_timesheet2
account_move._post()
# the unit_amount_rounded is not changed
self.assertEqual(analytic_line.unit_amount_rounded, unit_amount_rounded)
# the invoiced qty remains the same
inv_line = account_move.line_ids.filtered(lambda l: l.product_id == prd_ts_id)
self.assertEqual(inv_line.quantity, unit_amount_rounded)

def test_draft_invoice_with_rounded_amount_unchanged(self):
"""Drafting an invoice MUST NOT recompute rounded amount unit.
- invoicing the SO should not recompute and update the
unit_amount_rounded
- the invoiced qty should be the same as the aal.unit_amount_rounded
"""
unit_amount_rounded = 0.12
analytic_line = self.create_analytic_line(unit_amount=10)
analytic_line.unit_amount_rounded = unit_amount_rounded
account_move = self.sale_order._create_invoices()
prd_ts_id = self.product_delivery_timesheet2
account_move.button_draft()
# the unit_amount_rounded is not changed
self.assertEqual(analytic_line.unit_amount_rounded, unit_amount_rounded)
# the invoiced qty remains the same
inv_line = account_move.line_ids.filtered(lambda l: l.product_id == prd_ts_id)
self.assertEqual(inv_line.quantity, unit_amount_rounded)

def test_cancel_invoice_with_rounded_amount_unchanged(self):
"""Cancelling an invoice MUST NOT recompute rounded amount unit.
- invoicing the SO should not recompute and update the
unit_amount_rounded
- the invoiced qty should be the same as the aal.unit_amount_rounded
"""
unit_amount_rounded_total = 15
analytic_line_1 = self.create_analytic_line(unit_amount=10)
analytic_line_2 = self.create_analytic_line(unit_amount=10)
analytic_line_1.unit_amount_rounded = unit_amount_rounded_total
analytic_line_2.unit_amount_rounded = 0
account_move = self.sale_order._create_invoices()
prd_ts_id = self.product_delivery_timesheet2
account_move.button_cancel()
# the unit_amount_rounded is not changed
self.assertEqual(analytic_line_1.unit_amount_rounded, unit_amount_rounded_total)
self.assertEqual(analytic_line_2.unit_amount_rounded, 0)
# the invoiced qty remains the same
inv_line = account_move.line_ids.filtered(lambda l: l.product_id == prd_ts_id)
self.assertEqual(inv_line.quantity, unit_amount_rounded_total)
1 change: 1 addition & 0 deletions sale_timesheet_rounded/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_make_invoice_advance
21 changes: 21 additions & 0 deletions sale_timesheet_rounded/wizard/sale_make_invoice_advance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2023 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

from odoo import models


class SaleAdvancePaymentInv(models.TransientModel):

_inherit = "sale.advance.payment.inv"

def create_invoices(self):
"""Override method from sale/wizard/sale_make_invoice_advance.py
When the user want to invoice the timesheets to the SO
up to a specific period then we need to recompute the
qty_to_invoice for each product_id in sale.order.line,
before creating the invoice.
"""
return super(
SaleAdvancePaymentInv, self.with_context(timesheet_no_recompute=True)
).create_invoices()

0 comments on commit 8ab89bf

Please sign in to comment.