diff --git a/l10n_es_aeat_sii_oca/models/account_move.py b/l10n_es_aeat_sii_oca/models/account_move.py index 2ad0ee73272..df980310e4b 100644 --- a/l10n_es_aeat_sii_oca/models/account_move.py +++ b/l10n_es_aeat_sii_oca/models/account_move.py @@ -17,6 +17,7 @@ from odoo import _, api, exceptions, fields, models from odoo.modules.registry import Registry +from odoo.osv.expression import AND, OR SII_VALID_INVOICE_STATES = ["posted"] _logger = logging.getLogger(__name__) @@ -810,6 +811,23 @@ def _compute_sii_enabled(self): else: invoice.sii_enabled = False + @api.model + def _search_sii_enabled(self, operator, value): + domain = super()._search_sii_enabled(operator, value) + invoice_types = self.get_sale_types() + self.get_purchase_types() + condition_1 = [("journal_id.sii_enabled", operator, value)] + condition_2 = [("fiscal_position_id.aeat_active", operator, value)] + search_ko = (operator == "=" and not value) or (operator == "!=" and value) + exp_condition = OR if search_ko else AND + if not search_ko: + condition_2 = OR([condition_2, [("fiscal_position_id", "=", False)]]) + return AND( + [ + [("move_type", "in", invoice_types)], + exp_condition([domain, exp_condition([condition_1, condition_2])]), + ] + ) + def _reverse_moves(self, default_values_list=None, cancel=False): # OVERRIDE if not default_values_list: diff --git a/l10n_es_aeat_sii_oca/models/sii_mixin.py b/l10n_es_aeat_sii_oca/models/sii_mixin.py index ed9dabbcd58..eeaa89831ea 100644 --- a/l10n_es_aeat_sii_oca/models/sii_mixin.py +++ b/l10n_es_aeat_sii_oca/models/sii_mixin.py @@ -88,6 +88,7 @@ class SiiMixin(models.AbstractModel): sii_enabled = fields.Boolean( string="Enable SII", compute="_compute_sii_enabled", + search="_search_sii_enabled", ) sii_macrodata = fields.Boolean( string="MacroData", @@ -151,6 +152,16 @@ def _compute_sii_registration_key_code(self): def _compute_sii_enabled(self): raise NotImplementedError + @api.model + def _is_unsupported_search_operator(self, operator): + return operator not in ("=", "!=") + + @api.model + def _search_sii_enabled(self, operator, value): + if self._is_unsupported_search_operator(operator): + raise ValueError(_("Unsupported search operator")) + return [("company_id.sii_enabled", operator, value)] + def _compute_macrodata(self): for document in self: document.sii_macrodata = ( diff --git a/l10n_es_aeat_sii_oca/tests/test_l10n_es_aeat_sii.py b/l10n_es_aeat_sii_oca/tests/test_l10n_es_aeat_sii.py index b982522c4d4..8503e89bd3a 100644 --- a/l10n_es_aeat_sii_oca/tests/test_l10n_es_aeat_sii.py +++ b/l10n_es_aeat_sii_oca/tests/test_l10n_es_aeat_sii.py @@ -186,6 +186,25 @@ def setUpClass(cls): [("sii_wsdl_out", "!=", False)] ) + def test_invoice_search_sii_enabled(self): + domain_base = [("id", "=", self.invoice.id)] + domain_ok = domain_base + [("sii_enabled", "=", True)] + items = self.env["account.move"].search(domain_ok) + self.assertIn(self.invoice, items) + domain_ko_1 = domain_base + [("sii_enabled", "=", False)] + items = self.env["account.move"].search(domain_ko_1) + self.assertNotIn(self.invoice, items) + domain_ko_2 = domain_base + [("sii_enabled", "!=", True)] + items = self.env["account.move"].search(domain_ko_2) + self.assertNotIn(self.invoice, items) + self.invoice.journal_id.sii_enabled = False + items = self.env["account.move"].search(domain_ok) + self.assertNotIn(self.invoice, items) + items = self.env["account.move"].search(domain_ko_1) + self.assertIn(self.invoice, items) + items = self.env["account.move"].search(domain_ko_2) + self.assertIn(self.invoice, items) + def test_intracomunitary_customer_extracomunitary_delivery(self): """Comprobar venta a un cliente intracomunitario enviada al extranjero. diff --git a/l10n_es_aeat_sii_oca/views/account_move_views.xml b/l10n_es_aeat_sii_oca/views/account_move_views.xml index c0f8819dda1..f3b4ca9fde5 100644 --- a/l10n_es_aeat_sii_oca/views/account_move_views.xml +++ b/l10n_es_aeat_sii_oca/views/account_move_views.xml @@ -202,7 +202,7 @@