From 0e8fe08d2cd89472d77da47671dcc3fcae01c795 Mon Sep 17 00:00:00 2001 From: SimoRubi Date: Wed, 24 Jul 2019 12:32:13 +0200 Subject: [PATCH] [FIX] l10n_it_withholding_tax: Check overlapping taxes using ORM + test --- .../models/withholding_tax.py | 31 +++++++------ l10n_it_withholding_tax/tests/__init__.py | 5 +++ .../tests/test_withholding_tax.py | 43 +++++++++++++++++++ 3 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 l10n_it_withholding_tax/tests/__init__.py create mode 100644 l10n_it_withholding_tax/tests/test_withholding_tax.py diff --git a/l10n_it_withholding_tax/models/withholding_tax.py b/l10n_it_withholding_tax/models/withholding_tax.py index 749bf44281bb..d0042ef4de81 100644 --- a/l10n_it_withholding_tax/models/withholding_tax.py +++ b/l10n_it_withholding_tax/models/withholding_tax.py @@ -118,24 +118,23 @@ class WithholdingTaxRate(models.Model): @api.constrains('date_start', 'date_stop') def _check_date(self): if self.withholding_tax_id.active: - where = [] + domain = [ + ('withholding_tax_id', '=', self.withholding_tax_id.id), + ('id', '!=', self.id)] if self.date_start: - where.append("((date_stop>='%s') or (date_stop is null))" % - (self.date_start,)) + domain.extend([ + '|', + ('date_stop', '>=', self.date_start), + ('date_stop', '=', False)]) if self.date_stop: - where.append("((date_start<='%s') or (date_start is null))" % - (self.date_stop,)) - - # pylint: disable=sql-injection - self.env.cr.execute( - 'SELECT id ' - 'FROM withholding_tax_rate ' - 'WHERE ' + ' and '.join(where) + (where and ' and ' or '') + - 'withholding_tax_id = %s ' - 'AND id <> %s', ( - self.withholding_tax_id.id, - self.id)) - if self.env.cr.fetchall(): + domain.extend([ + '|', + ('date_start', '<=', self.date_stop), + ('date_start', '=', False)]) + + overlapping_rate = self.env['withholding.tax.rate'] \ + .search(domain, limit=1) + if overlapping_rate: raise ValidationError( _('Error! You cannot have 2 rates that overlap!')) diff --git a/l10n_it_withholding_tax/tests/__init__.py b/l10n_it_withholding_tax/tests/__init__.py new file mode 100644 index 000000000000..854f05080586 --- /dev/null +++ b/l10n_it_withholding_tax/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_withholding_tax diff --git a/l10n_it_withholding_tax/tests/test_withholding_tax.py b/l10n_it_withholding_tax/tests/test_withholding_tax.py new file mode 100644 index 000000000000..7413552e71f0 --- /dev/null +++ b/l10n_it_withholding_tax/tests/test_withholding_tax.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from datetime import date, timedelta +from odoo import fields +from odoo.exceptions import ValidationError +from odoo.tests import TransactionCase + + +class TestWithholdingTax(TransactionCase): + + def setUp(self): + super(TestWithholdingTax, self).setUp() + account_payable = self.env['account.account'].create({ + 'name': 'Test WH tax', + 'code': 'whtaxpay', + 'user_type_id': self.ref('account.data_account_type_payable'), + 'reconcile': True}) + account_receivable = self.env['account.account'].create({ + 'name': 'Test WH tax', + 'code': 'whtaxrec', + 'user_type_id': self.ref('account.data_account_type_receivable'), + 'reconcile': True}) + self.wh_tax = self.env['withholding.tax'].create({ + 'name': "Test WH tax", + 'code': "Test WH tax", + 'account_receivable_id': account_receivable.id, + 'account_payable_id': account_payable.id, + 'payment_term': self.ref('account.account_payment_term_advance') + }) + + def test_overlapping_rates(self): + """Check that overlapping rates cannot be created""" + self.wh_tax.rate_ids = [(0, 0, { + 'date_start': fields.Date.to_string( + date.today()) + })] + with self.assertRaises(ValidationError): + self.wh_tax.rate_ids = [(0, 0, { + 'date_start': fields.Date.to_string( + date.today() - timedelta(days=1)) + })]