diff --git a/account_move_cutoff/models/account_move_line.py b/account_move_cutoff/models/account_move_line.py index aedf1a3bc3e..f30674733fc 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__) @@ -18,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", @@ -40,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" @@ -254,6 +245,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 +269,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 +287,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..2f59c9e6343 100644 --- a/account_move_cutoff/models/res_config_settings.py +++ b/account_move_cutoff/models/res_config_settings.py @@ -17,3 +17,25 @@ 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.", + ) + 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/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/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 4300eaf2986..3c26b2a2558 100644 --- a/account_move_cutoff/views/res_config_settings.xml +++ b/account_move_cutoff/views/res_config_settings.xml @@ -31,6 +31,42 @@ +
+
+ +
+
+
+
+ +
+
+
+ +
+ 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" +
    +
  • +
    +
    +