From ed62ead6b74e23dabf4e6460c51a5e44c0b1e7d6 Mon Sep 17 00:00:00 2001 From: Lindsay Date: Wed, 27 Nov 2024 12:23:42 +0100 Subject: [PATCH] [IMP] Move the constraint on uniqueness for cutoff date at line level + Add the possibility to create several cutoff with the same date Business case can be : - One user (A) creates a cutoff for some account moves. Another user (B) creates other moves, then wants to see those moves in a cutoff. However, B does not want to interfere with the cutoff A already made. B creates a new cutoff with the same date as the one of A, and when he generates lines, only retrieves the one that were not already in the cutoff of A. - One user wants to create several cutoff at the same date, but for different journals/accounts --- account_cutoff_base/__manifest__.py | 2 +- .../migrations/16.0.1.5.1/post-migrate.py | 19 +++++++++++++++++++ account_cutoff_base/models/account_cutoff.py | 8 -------- .../models/account_cutoff_line.py | 13 +++++++++++-- .../models/__init__.py | 1 + .../models/account_cutoff.py | 6 +++++- .../models/account_move_line.py | 15 +++++++++++++++ .../tests/test_account_cutoff_prepaid.py | 4 ++++ 8 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 account_cutoff_base/migrations/16.0.1.5.1/post-migrate.py create mode 100644 account_cutoff_start_end_dates/models/account_move_line.py diff --git a/account_cutoff_base/__manifest__.py b/account_cutoff_base/__manifest__.py index 637b6208966..d893ac04ec4 100644 --- a/account_cutoff_base/__manifest__.py +++ b/account_cutoff_base/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Account Cut-off Base", - "version": "16.0.1.5.0", + "version": "16.0.1.5.1", "category": "Accounting & Finance", "license": "AGPL-3", "summary": "Base module for Account Cut-offs", diff --git a/account_cutoff_base/migrations/16.0.1.5.1/post-migrate.py b/account_cutoff_base/migrations/16.0.1.5.1/post-migrate.py new file mode 100644 index 00000000000..8cdbf2136b9 --- /dev/null +++ b/account_cutoff_base/migrations/16.0.1.5.1/post-migrate.py @@ -0,0 +1,19 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging + +import odoo + +_logger = logging.getLogger(__name__) + + +def migrate(cr, version): + _logger.info("Remove constraint on account cutoff") + odoo.api.Environment(cr, odoo.SUPERUSER_ID, {}) + + cr.execute( + """ + ALTER TABLE account_cutoff + DROP CONSTRAINT IF EXISTS account_cutoff_date_type_company_uniq + """ + ) diff --git a/account_cutoff_base/models/account_cutoff.py b/account_cutoff_base/models/account_cutoff.py index ea600546baa..ad2d08c2350 100644 --- a/account_cutoff_base/models/account_cutoff.py +++ b/account_cutoff_base/models/account_cutoff.py @@ -203,14 +203,6 @@ def _compute_move_reversal_id(self): "the state is set to 'Done' and the fields become read-only.", ) - _sql_constraints = [ - ( - "date_type_company_uniq", - "unique(cutoff_date, company_id, cutoff_type)", - _("A cutoff of the same type already exists with this cut-off date !"), - ) - ] - def name_get(self): res = [] type2label = self.cutoff_type_label_map diff --git a/account_cutoff_base/models/account_cutoff_line.py b/account_cutoff_base/models/account_cutoff_line.py index 933df307640..78cd5a3fc94 100644 --- a/account_cutoff_base/models/account_cutoff_line.py +++ b/account_cutoff_base/models/account_cutoff_line.py @@ -2,7 +2,7 @@ # @author: Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import _, fields, models class AccountCutoffLine(models.Model): @@ -12,7 +12,8 @@ class AccountCutoffLine(models.Model): _description = "Account Cut-off Line" parent_id = fields.Many2one("account.cutoff", string="Cut-off", ondelete="cascade") - cutoff_type = fields.Selection(related="parent_id.cutoff_type") + cutoff_type = fields.Selection(related="parent_id.cutoff_type", store=True) + cutoff_date = fields.Date(related="parent_id.cutoff_date", store=True) company_id = fields.Many2one( "res.company", related="parent_id.company_id", store=True ) @@ -83,3 +84,11 @@ class AccountCutoffLine(models.Model): readonly=True, ) notes = fields.Text() + + _sql_constraints = [ + ( + "line_date_type_company_uniq", + "unique(cutoff_date, company_id, cutoff_type, origin_move_line_id)", + _("A cutoff line of the same type already exists with this cut-off date !"), + ) + ] diff --git a/account_cutoff_start_end_dates/models/__init__.py b/account_cutoff_start_end_dates/models/__init__.py index 2ee14ae58ef..a3c6ba1beaf 100644 --- a/account_cutoff_start_end_dates/models/__init__.py +++ b/account_cutoff_start_end_dates/models/__init__.py @@ -1,2 +1,3 @@ from . import account_cutoff from . import account_cutoff_line +from . import account_move_line diff --git a/account_cutoff_start_end_dates/models/account_cutoff.py b/account_cutoff_start_end_dates/models/account_cutoff.py index 7e706f56427..e6bf03c37e0 100644 --- a/account_cutoff_start_end_dates/models/account_cutoff.py +++ b/account_cutoff_start_end_dates/models/account_cutoff.py @@ -91,6 +91,7 @@ def _prepare_date_cutoff_line(self, aml, mapping): "name": aml.name, "start_date": aml.start_date, "end_date": aml.end_date, + "cutoff_date": self.cutoff_date, "account_id": aml.account_id.id, "cutoff_account_id": cutoff_account_id, "analytic_distribution": aml.analytic_distribution, @@ -209,8 +210,11 @@ def get_lines(self): ("start_date", "<=", self.cutoff_date), ("date", ">", self.cutoff_date), ] + amls = aml_obj.search(domain) for aml in amls: + if self.cutoff_date in aml.cutoff_line_ids.mapped("cutoff_date"): + # cutoff line already exists for this date + continue line_obj.create(self._prepare_date_cutoff_line(aml, mapping)) - return res diff --git a/account_cutoff_start_end_dates/models/account_move_line.py b/account_cutoff_start_end_dates/models/account_move_line.py new file mode 100644 index 00000000000..1348a49ae24 --- /dev/null +++ b/account_cutoff_start_end_dates/models/account_move_line.py @@ -0,0 +1,15 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class AccountMoveLine(models.Model): + + _inherit = "account.move.line" + cutoff_line_ids = fields.One2many( + "account.cutoff.line", + inverse_name="origin_move_line_id", + string="Related cutoff line", + readonly=True, + ) diff --git a/account_cutoff_start_end_dates/tests/test_account_cutoff_prepaid.py b/account_cutoff_start_end_dates/tests/test_account_cutoff_prepaid.py index a941d1a204f..560637ff679 100644 --- a/account_cutoff_start_end_dates/tests/test_account_cutoff_prepaid.py +++ b/account_cutoff_start_end_dates/tests/test_account_cutoff_prepaid.py @@ -129,6 +129,10 @@ def test_with_cutoff_before_after_and_in_the_middle(self): cutoff = self._create_cutoff("01-31") cutoff.get_lines() self.assertEqual(amount, cutoff.total_cutoff_amount) + # cutoff at the same date as the previous one : no new line created + cutoff = self._create_cutoff("01-31") + cutoff.get_lines() + self.assertEqual(0, cutoff.total_cutoff_amount) def tests_1(self): """generate move, and test move lines grouping"""