Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][FIX] sale_timesheet_rounded: avoid recomputing rounded amounts when not needed #590

Open
wants to merge 3 commits into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ odoo_test_flavor: Both
odoo_version: 15.0
org_name: Odoo Community Association (OCA)
org_slug: OCA
rebel_module_groups: []
rebel_module_groups:
- sale_timesheet_rounded
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the conflict in particular?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I don't remember the exact fault, but AFAIR this was a good idea. Is this a blocking comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, if not justified. The issue is that when we increase the number of tests suites that will run.
So, what is the conflict? Can't we find a better way to prevent that? Eg: disable partly of fully this feature when not needed?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonhd91 worked on a fix for that in the context of the migration of this module to 17.0; he will backport it and follow-up here

Copy link

@sonhd91 sonhd91 Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repo_description: 'TODO: add repo description.'
repo_name: timesheet
repo_slug: timesheet
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ jobs:
matrix:
include:
- container: ghcr.io/oca/oca-ci/py3.8-odoo15.0:latest
include: "sale_timesheet_rounded"
makepot: "true"
name: test with Odoo
- container: ghcr.io/oca/oca-ci/py3.8-ocb15.0:latest
include: "sale_timesheet_rounded"
name: test with OCB
- container: ghcr.io/oca/oca-ci/py3.8-odoo15.0:latest
exclude: "sale_timesheet_rounded"
makepot: "true"
name: test with Odoo
- container: ghcr.io/oca/oca-ci/py3.8-ocb15.0:latest
exclude: "sale_timesheet_rounded"
name: test with OCB
makepot: "true"
leemannd marked this conversation as resolved.
Show resolved Hide resolved
services:
Expand All @@ -49,6 +59,9 @@ jobs:
POSTGRES_DB: odoo
ports:
- 5432:5432
env:
INCLUDE: "${{ matrix.include }}"
EXCLUDE: "${{ matrix.exclude }}"
steps:
- uses: actions/checkout@v3
with:
Expand Down
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
2 changes: 1 addition & 1 deletion sale_timesheet_rounded/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ def pre_init_hook(cr):
cr.execute( # pylint: disable=E8103
sql.SQL(
"UPDATE {table} SET {column} = unit_amount WHERE {column} IS NULL"
).format(table=table, column=column),
).format(table=table, column=column)
)
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self._context.get("timesheet_no_recompute", False):
if self.env.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(

Check warning on line 19 in sale_timesheet_rounded/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

sale_timesheet_rounded/models/account_move.py#L19

Added line #L19 was not covered by tests
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a comment


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(

Check warning on line 19 in sale_timesheet_rounded/wizard/sale_make_invoice_advance.py

View check run for this annotation

Codecov / codecov/patch

sale_timesheet_rounded/wizard/sale_make_invoice_advance.py#L19

Added line #L19 was not covered by tests
SaleAdvancePaymentInv, self.with_context(timesheet_no_recompute=True)
).create_invoices()