From d7c2bf3d29c717f1b146a9684cdd63b3b1e6719c Mon Sep 17 00:00:00 2001 From: Saran440 Date: Fri, 12 May 2023 15:29:43 +0700 Subject: [PATCH] [ENH] analytic_tag_dimension_enhanced: add exclude required module --- analytic_tag_dimension_enhanced/__manifest__.py | 6 +++++- .../models/account_analytic_dimension.py | 4 ++++ .../models/account_analytic_tag.py | 10 +++++++++- .../security/ir.model.access.csv | 2 ++ .../tests/test_analytic_dimension.py | 7 +++++++ .../views/analytic_view.xml | 6 ++++++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 analytic_tag_dimension_enhanced/security/ir.model.access.csv diff --git a/analytic_tag_dimension_enhanced/__manifest__.py b/analytic_tag_dimension_enhanced/__manifest__.py index 8287aa7559..003105aa63 100644 --- a/analytic_tag_dimension_enhanced/__manifest__.py +++ b/analytic_tag_dimension_enhanced/__manifest__.py @@ -8,6 +8,10 @@ "author": "Ecosoft, Odoo Community Association (OCA)", "website": "https://github.com/OCA/account-analytic", "depends": ["analytic_tag_dimension"], - "data": ["views/analytic_view.xml", "views/account_move_view.xml"], + "data": [ + "security/ir.model.access.csv", + "views/analytic_view.xml", + "views/account_move_view.xml", + ], "installable": True, } diff --git a/analytic_tag_dimension_enhanced/models/account_analytic_dimension.py b/analytic_tag_dimension_enhanced/models/account_analytic_dimension.py index 68a677eda7..50d16dbc9b 100644 --- a/analytic_tag_dimension_enhanced/models/account_analytic_dimension.py +++ b/analytic_tag_dimension_enhanced/models/account_analytic_dimension.py @@ -28,6 +28,10 @@ class AccountAnalyticDimension(models.Model): default=False, help="If required, this dimension needed to be selected in working document", ) + exclude_required = fields.Many2many( + comodel_name="ir.model", + string="Exclude Module Required", + ) by_sequence = fields.Boolean( default=False, help="If checked, this dimemsion's tags will be available " diff --git a/analytic_tag_dimension_enhanced/models/account_analytic_tag.py b/analytic_tag_dimension_enhanced/models/account_analytic_tag.py index dad3ee230e..c01a881837 100644 --- a/analytic_tag_dimension_enhanced/models/account_analytic_tag.py +++ b/analytic_tag_dimension_enhanced/models/account_analytic_tag.py @@ -29,6 +29,7 @@ def _check_required_dimension(self, record): and record.move_id.move_type == "entry" ) or ("display_type" in record and record.display_type) + or self._context.get("bypass_required_dimension") ): return Dimension = self.env["account.analytic.dimension"] @@ -36,8 +37,15 @@ def _check_required_dimension(self, record): tags_dimension = self.filtered("analytic_dimension_id.required") dimensions = tags_dimension.mapped("analytic_dimension_id") missing = req_dimensions - dimensions + # Check record in exclude required, it will skip it + if ( + req_dimensions + and req_dimensions.exclude_required + and record._name in req_dimensions.exclude_required.mapped("model") + ): + missing = False if missing: raise ValidationError( - _("Following dimension(s) not selected: %s") + _("Following analytic tag dimension(s) not selected: %s") % ", ".join(missing.mapped("name")) ) diff --git a/analytic_tag_dimension_enhanced/security/ir.model.access.csv b/analytic_tag_dimension_enhanced/security/ir.model.access.csv new file mode 100644 index 0000000000..1b0db0ea1e --- /dev/null +++ b/analytic_tag_dimension_enhanced/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_ir_model_analytic_tags,access_ir_model_analytic_tags,base.model_ir_model,analytic.group_analytic_tags,1,0,0,0 diff --git a/analytic_tag_dimension_enhanced/tests/test_analytic_dimension.py b/analytic_tag_dimension_enhanced/tests/test_analytic_dimension.py index 6453842171..9e2f6c3d74 100644 --- a/analytic_tag_dimension_enhanced/tests/test_analytic_dimension.py +++ b/analytic_tag_dimension_enhanced/tests/test_analytic_dimension.py @@ -41,6 +41,7 @@ def test_invoice_line_dimension_required(self): """ self.dimension_1.required = True self.dimension_2.required = True + model = self.env["ir.model"].search([("model", "=", "account.move.line")]) values = { "name": "test", @@ -54,6 +55,12 @@ def test_invoice_line_dimension_required(self): # Error if missing required dimension with self.assertRaises(ValidationError): invoice_line_obj.create(values) + + # Exclude required dimension (for some model) + self.dimension_1.exclude_required = model + line = invoice_line_obj.create(values) + self.assertTrue(line.x_dimension_test_dim_1.id == self.analytic_tag_1a.id) + self.invoice.invoice_line_ids.unlink() values["analytic_tag_ids"] = [ (6, 0, [self.analytic_tag_1a.id, self.analytic_tag_2a.id]) diff --git a/analytic_tag_dimension_enhanced/views/analytic_view.xml b/analytic_tag_dimension_enhanced/views/analytic_view.xml index deb80cc233..d440e388b7 100644 --- a/analytic_tag_dimension_enhanced/views/analytic_view.xml +++ b/analytic_tag_dimension_enhanced/views/analytic_view.xml @@ -11,6 +11,12 @@ +