From ec6e8433aac96c13bc0af0e62d9317fa52ae1dda Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Mon, 23 Sep 2024 11:21:46 +0200 Subject: [PATCH 1/2] [IMP] account_move_cutoff: add forward product on defferred revenue/expenses --- .../models/account_move_line.py | 11 +++++ .../models/res_config_settings.py | 5 +++ .../tests/test_account_invoice_cutoff.py | 40 +++++++++++++++++++ .../test_account_supplier_refund_cutoff.py | 4 -- .../views/res_config_settings.xml | 14 +++++++ 5 files changed, 70 insertions(+), 4 deletions(-) diff --git a/account_move_cutoff/models/account_move_line.py b/account_move_cutoff/models/account_move_line.py index aedf1a3bc3e..cada98e95a1 100644 --- a/account_move_cutoff/models/account_move_line.py +++ b/account_move_cutoff/models/account_move_line.py @@ -7,6 +7,7 @@ from dateutil.relativedelta import relativedelta from odoo import _, api, fields, models +from odoo.tools.misc import str2bool logger = logging.getLogger(__name__) @@ -254,6 +255,14 @@ def _prepare_entry_lines(self, new_move, period, amount, is_cutoff=True): end_date = self.end_date else: start_date, end_date = self._get_period_start_end_dates(period) + + link_product = str2bool( + self.env["ir.config_parameter"] + .sudo() + .get_param("account_move_cutoff.link_product", "False"), + False, + ) + return self.env["account.move.line"].create( [ { @@ -270,6 +279,7 @@ def _prepare_entry_lines(self, new_move, period, amount, is_cutoff=True): "partner_id": self.partner_id.id, "analytic_account_id": self.analytic_account_id.id, "cutoff_source_id": self.id, + "product_id": self.product_id.id if link_product else False, }, { "move_id": new_move.id, @@ -287,6 +297,7 @@ def _prepare_entry_lines(self, new_move, period, amount, is_cutoff=True): "account_id": self.deferred_accrual_account_id.id, "partner_id": self.partner_id.id, "analytic_account_id": False, + "product_id": False, "cutoff_source_id": self.id, }, ] diff --git a/account_move_cutoff/models/res_config_settings.py b/account_move_cutoff/models/res_config_settings.py index 4f574473dc2..4f730e97660 100644 --- a/account_move_cutoff/models/res_config_settings.py +++ b/account_move_cutoff/models/res_config_settings.py @@ -17,3 +17,8 @@ class ResConfigSettings(models.TransientModel): readonly=False, string="Expense cut-off journal", ) + link_product = fields.Boolean( + "Link product", + config_parameter="account_move_cutoff.link_product", + help="Link product on deferred account.move.line.", + ) diff --git a/account_move_cutoff/tests/test_account_invoice_cutoff.py b/account_move_cutoff/tests/test_account_invoice_cutoff.py index 8440acfcbd8..cd86e7ffc66 100644 --- a/account_move_cutoff/tests/test_account_invoice_cutoff.py +++ b/account_move_cutoff/tests/test_account_invoice_cutoff.py @@ -53,13 +53,53 @@ def test_action_view_deferred_entries(self): action = self.invoice.action_view_deferred_entries() self.assertEqual(action["domain"][0][2], self.invoice.cutoff_entry_ids.ids) + def test_link_product(self): + self.addCleanup( + self.env["ir.config_parameter"].sudo().set_param, + "account_move_cutoff.link_product", + False, + ) + self.env["ir.config_parameter"].sudo().set_param( + "account_move_cutoff.link_product", "True" + ) + self.invoice.line_ids.cutoff_method = "monthly_prorata_temporis" + + with freeze_time("2023-01-15"): + self.invoice.action_post() + self.assertEqual(self.invoice.cutoff_move_count, 4) + + self.assertEqual( + len( + self.invoice.cutoff_entry_ids.line_ids.filtered( + lambda ml: ml.product_id + ) + ), + 18, + ) + def test_account_invoice_cutoff_monthly_factor_prorata(self): + self.addCleanup( + self.env["ir.config_parameter"].sudo().set_param, + "account_move_cutoff.link_product", + False, + ) + self.env["ir.config_parameter"].sudo().set_param( + "account_move_cutoff.link_product", "False" + ) self.invoice.line_ids.cutoff_method = "monthly_prorata_temporis" with freeze_time("2023-01-15"): self.invoice.action_post() self.assertEqual(self.invoice.cutoff_move_count, 4) + self.assertEqual( + len( + self.invoice.cutoff_entry_ids.line_ids.filtered( + lambda ml: ml.product_id + ) + ), + 0, + ) cutoff_move = self.invoice.cutoff_entry_ids.filtered( lambda move, move_date=date(2023, 1, 15): move.date == move_date ) diff --git a/account_move_cutoff/tests/test_account_supplier_refund_cutoff.py b/account_move_cutoff/tests/test_account_supplier_refund_cutoff.py index 1f48e3c81de..ccc97832602 100644 --- a/account_move_cutoff/tests/test_account_supplier_refund_cutoff.py +++ b/account_move_cutoff/tests/test_account_supplier_refund_cutoff.py @@ -43,10 +43,6 @@ def test_ensure_refund_without_start_end_date_are_postable(self): self.assertEqual(self.refund.state, "posted") def test_account_refund_cutoff_equals(self): - # self.env["ir.config_parameter"].set_param( - # "account_move_cutoff.default_cutoff_method", - # "equal" - # ) self.refund.line_ids.cutoff_method = "equal" with freeze_time("2023-01-15"): self.refund.action_post() diff --git a/account_move_cutoff/views/res_config_settings.xml b/account_move_cutoff/views/res_config_settings.xml index 4300eaf2986..dce6da96aaf 100644 --- a/account_move_cutoff/views/res_config_settings.xml +++ b/account_move_cutoff/views/res_config_settings.xml @@ -31,6 +31,20 @@ +
+
+ +
+
+
+
From 1a86d458c42fe3866c3890c09adac1fe7c255452 Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Mon, 23 Sep 2024 11:24:16 +0200 Subject: [PATCH 2/2] [IMP] account_move_cutoff: improve module settings * exposing default cutoff method using default odoo features * allow to forward product attribute on account move line to make better analysis --- .../models/account_move_line.py | 12 +--------- .../models/res_config_settings.py | 17 ++++++++++++++ account_move_cutoff/views/account_move.xml | 9 +++++++- .../views/account_move_line.xml | 3 +++ .../views/res_config_settings.xml | 22 +++++++++++++++++++ 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/account_move_cutoff/models/account_move_line.py b/account_move_cutoff/models/account_move_line.py index cada98e95a1..f30674733fc 100644 --- a/account_move_cutoff/models/account_move_line.py +++ b/account_move_cutoff/models/account_move_line.py @@ -19,16 +19,6 @@ class AccountMoveLine(models.Model): "cutoff.period.mixin", ] - @api.model - def _get_default_cutoff_method(self): - return ( - self.env["ir.config_parameter"] - .sudo() - .get_param( - "account_move_cutoff.default_cutoff_method", "monthly_prorata_temporis" - ) - ) - is_deferrable_line = fields.Boolean( string="Is deferrable line", compute="_compute_is_deferrable_line", @@ -41,7 +31,7 @@ def _get_default_cutoff_method(self): ], string="Cut-off method", required=True, - default=lambda self: self._get_default_cutoff_method(), + default="monthly_prorata_temporis", help=( "Determine how to split amounts over periods:\n" " * Equal: same amount is splitted over periods of the service" diff --git a/account_move_cutoff/models/res_config_settings.py b/account_move_cutoff/models/res_config_settings.py index 4f730e97660..2f59c9e6343 100644 --- a/account_move_cutoff/models/res_config_settings.py +++ b/account_move_cutoff/models/res_config_settings.py @@ -22,3 +22,20 @@ class ResConfigSettings(models.TransientModel): config_parameter="account_move_cutoff.link_product", help="Link product on deferred account.move.line.", ) + default_cutoff_method = fields.Selection( + [ + ("equal", "Equal"), + ("monthly_prorata_temporis", "Prorata temporis (by month %)"), + ], + string="Default Cutoff method", + default="monthly_prorata_temporis", + default_model="account.move.line", + required=True, + help=( + "Determine how to split amounts over periods:\n" + " * Equal: same amount is splitted over periods of the service" + " (using start and end date on the invoice line).\n" + " * Prorata temporis by month %: amount is splitted over" + " the rate of service days in the month.\n" + ), + ) diff --git a/account_move_cutoff/views/account_move.xml b/account_move_cutoff/views/account_move.xml index 1b402ca08cf..6022589388a 100644 --- a/account_move_cutoff/views/account_move.xml +++ b/account_move_cutoff/views/account_move.xml @@ -33,9 +33,16 @@ /> + + + + diff --git a/account_move_cutoff/views/account_move_line.xml b/account_move_cutoff/views/account_move_line.xml index 6cface65cbe..15b5ad2d168 100644 --- a/account_move_cutoff/views/account_move_line.xml +++ b/account_move_cutoff/views/account_move_line.xml @@ -14,6 +14,7 @@ string="Deferred Revenue/Expense" attrs="{'invisible': [('cutoff_source_move_id', '=', False)]}" > + @@ -28,6 +29,7 @@ + @@ -41,6 +43,7 @@ + diff --git a/account_move_cutoff/views/res_config_settings.xml b/account_move_cutoff/views/res_config_settings.xml index dce6da96aaf..3c26b2a2558 100644 --- a/account_move_cutoff/views/res_config_settings.xml +++ b/account_move_cutoff/views/res_config_settings.xml @@ -45,6 +45,28 @@ + +
+
+
+ +
+ Determine how to split amounts over periods: +
  • +
      Equal: same amount is splitted over periods of the service" + (using start and end date on the invoice line).
    +
      + Prorata temporis by month %: amount is splitted over" + the rate of service days in the month.\n" +
    +
  • +
    +
    +