Skip to content

Commit

Permalink
[FIX] l10n_it_reverse_charge: Validate new and old configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SirTakobi committed Jul 8, 2022
1 parent 29594dd commit 4e4920a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
40 changes: 38 additions & 2 deletions l10n_it_reverse_charge/models/account_rc_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# Copyright 2017 Alex Comba - Agile Business Group
# Copyright 2017 Lorenzo Battistini - Agile Business Group
# Copyright 2017 Marco Calcagni - Dinamiche Aziendali srl
# Copyright 2022 Simone Rubino - TAKOBI

from odoo import fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class AccountRCTypeTax(models.Model):
Expand Down Expand Up @@ -33,9 +35,33 @@ class AccountRCTypeTax(models.Model):
_sql_constraints = [
('purchase_sale_tax_uniq',
'unique (rc_type_id,purchase_tax_id,sale_tax_id)',
'Tax mappings can be defined only once per rc type.')
'Tax mappings from Purchase Tax to Sale Tax '
'can be defined only once per Reverse Charge Type.'),
('original_purchase_sale_tax_uniq',
'unique (rc_type_id,'
'original_purchase_tax_id,purchase_tax_id,sale_tax_id)',
'Tax mappings from Original Purchase Tax to Purchase Tax to Sale Tax '
'can be defined only once per Reverse Charge Type.'),
]

@api.constrains(
'original_purchase_tax_id',
'rc_type_id',
)
def _constrain_supplier_self_invoice_mapping(self):
for mapping in self:
rc_type = mapping.rc_type_id
if rc_type.with_supplier_self_invoice:
if not mapping.original_purchase_tax_id:
raise ValidationError(
_("Original Purchase Tax is required "
"for Reverse Charge Type {rc_type_name} having "
"With additional supplier self invoice enabled")
.format(
rc_type_name=rc_type.display_name,
)
)


class AccountRCType(models.Model):
_name = 'account.rc.type'
Expand Down Expand Up @@ -88,3 +114,13 @@ class AccountRCType(models.Model):
company_id = fields.Many2one(
'res.company', string='Company', required=True,
default=lambda self: self.env.user.company_id)

@api.constrains(
'with_supplier_self_invoice',
'tax_ids',
)
def _constrain_with_supplier_self_invoice(self):
with_supplier_types = self.filtered('with_supplier_self_invoice')
if with_supplier_types:
with_supplier_types.mapped('tax_ids') \
._constrain_supplier_self_invoice_mapping()
2 changes: 2 additions & 0 deletions l10n_it_reverse_charge/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import test_rc
from . import test_account_rc_type
28 changes: 28 additions & 0 deletions l10n_it_reverse_charge/tests/test_account_rc_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2022 Simone Rubino - TAKOBI
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.exceptions import ValidationError
from odoo.fields import first

from .rc_common import ReverseChargeCommon


class TestAccountRCType(ReverseChargeCommon):

def test_with_supplier_self_invoice(self):
"""
Check that Reverse Charge Types
that require a supplier self invoice,
must have an Original Purchase Tax in the mapping.
"""
rc_type = self.rc_type_eeu
rc_type_mapping = first(rc_type.tax_ids)
self.assertTrue(rc_type.with_supplier_self_invoice)
self.assertTrue(rc_type_mapping.original_purchase_tax_id)

with self.assertRaises(ValidationError) as ve:
rc_type_mapping.original_purchase_tax_id = False
exc_message = ve.exception.args[0]

self.assertIn("Original Purchase Tax is required", exc_message)
self.assertIn(rc_type.display_name, exc_message)

0 comments on commit 4e4920a

Please sign in to comment.