Skip to content

Commit

Permalink
[FIX] l10n_it_withholding_tax: Check overlapping taxes using ORM + test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoRubi authored and eLBati committed Jul 26, 2019
1 parent 15dbd46 commit 0e8fe08
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
31 changes: 15 additions & 16 deletions l10n_it_withholding_tax/models/withholding_tax.py
Expand Up @@ -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!'))

Expand Down
5 changes: 5 additions & 0 deletions 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
43 changes: 43 additions & 0 deletions 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))
})]

0 comments on commit 0e8fe08

Please sign in to comment.