summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbfr-o <bfr@odoo.com>2020-06-30 10:52:30 +0000
committerBenjamin Frantzen (bfr) <bfr@odoo.com>2020-07-03 15:53:29 +0000
commit52949a1a839e90eae5daf6eac586b939d8e5819b (patch)
treed898672f77e750f4154e5cf70be2e6fa70576fa2
parentd935f9b77e4f07f6745e88bc043b106b855ef002 (diff)
[FIX] l10n_ch: Fix broken 'test_l10n_ch_isr
' X-original-commit: 10c14c63094870346143a42f5df39e1db3f0dc5c
-rw-r--r--addons/l10n_ch/models/res_bank.py35
-rw-r--r--addons/l10n_ch/tests/test_l10n_ch_isr.py122
2 files changed, 64 insertions, 93 deletions
diff --git a/addons/l10n_ch/models/res_bank.py b/addons/l10n_ch/models/res_bank.py
index 779acdc331a..5f6337541c0 100644
--- a/addons/l10n_ch/models/res_bank.py
+++ b/addons/l10n_ch/models/res_bank.py
@@ -27,10 +27,14 @@ def _is_l10n_ch_postal(account_ref):
class ResPartnerBank(models.Model):
_inherit = 'res.partner.bank'
- l10n_ch_postal = fields.Char(string='Swiss Postal Account', help='This field is used for the Swiss postal account number '
- 'on a vendor account and for the client number on your '
- 'own account. The client number is mostly 6 numbers without '
- '-, while the postal account number can be e.g. 01-162-8')
+ l10n_ch_postal = fields.Char(
+ string="Swiss Postal Account",
+ readonly=False, store=True,
+ compute='_compute_l10n_ch_postal',
+ help="This field is used for the Swiss postal account number on a vendor account and for the client number on "
+ "your own account. The client number is mostly 6 numbers without -, while the postal account number can "
+ "be e.g. 01-162-8")
+
# fields to configure ISR payment slip generation
l10n_ch_isr_subscription_chf = fields.Char(string='CHF ISR Subscription Number', help='The subscription number provided by the bank or Postfinance to identify the bank, used to generate ISR in CHF. eg. 01-162-8')
l10n_ch_isr_subscription_eur = fields.Char(string='EUR ISR Subscription Number', help='The subscription number provided by the bank or Postfinance to identify the bank, used to generate ISR in EUR. eg. 03-162-5')
@@ -75,17 +79,18 @@ class ResPartnerBank(models.Model):
else:
return super(ResPartnerBank, self).retrieve_acc_type(acc_number)
- @api.onchange('acc_number', 'partner_id', 'acc_type')
- def _onchange_set_l10n_ch_postal(self):
- if self.acc_type == 'iban':
- self.l10n_ch_postal = self._retrieve_l10n_ch_postal(self.sanitized_acc_number)
- elif self.acc_type == 'postal':
- if self.acc_number and " " in self.acc_number:
- self.l10n_ch_postal = self.acc_number.split(" ")[0]
- else:
- self.l10n_ch_postal = self.acc_number
- if self.partner_id:
- self.acc_number = self.acc_number + ' ' + self.partner_id.name
+ @api.depends('acc_number', 'partner_id', 'acc_type')
+ def _compute_l10n_ch_postal(self):
+ for record in self:
+ if record.acc_type == 'iban':
+ record.l10n_ch_postal = self._retrieve_l10n_ch_postal(record.sanitized_acc_number)
+ elif record.acc_type == 'postal':
+ if record.acc_number and " " in record.acc_number:
+ record.l10n_ch_postal = record.acc_number.split(" ")[0]
+ else:
+ record.l10n_ch_postal = record.acc_number
+ if record.partner_id:
+ record.acc_number = record.acc_number + ' ' + record.partner_id.name
@api.model
def _retrieve_l10n_ch_postal(self, iban):
diff --git a/addons/l10n_ch/tests/test_l10n_ch_isr.py b/addons/l10n_ch/tests/test_l10n_ch_isr.py
index 5879e92c3a8..196ad01346e 100644
--- a/addons/l10n_ch/tests/test_l10n_ch_isr.py
+++ b/addons/l10n_ch/tests/test_l10n_ch_isr.py
@@ -1,51 +1,16 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-
-import time
-
-from odoo.addons.account.tests.common import AccountTestCommon
-from odoo.exceptions import ValidationError
+from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged
+from odoo.exceptions import ValidationError
@tagged('post_install', '-at_install')
-class ISRTest(AccountTestCommon):
-
- def create_invoice(self, currency_to_use='base.CHF'):
- """ Generates a test invoice """
- invoice = self.env['account.move'].with_context(default_move_type='out_invoice').create({
- 'move_type': 'out_invoice',
- 'partner_id': self.env.ref("base.res_partner_2").id,
- 'currency_id': self.env.ref(currency_to_use).id,
- 'invoice_date': time.strftime('%Y') + '-12-22',
- 'invoice_line_ids': [
- (0, 0, {
- 'product_id': self.env.ref("product.product_product_4").id,
- 'quantity': 1,
- 'price_unit': 42,
- }),
- ],
- })
- invoice.post()
-
- return invoice
-
- def create_account(self, number):
- """ Generates a test res.partner.bank. """
- rslt = self.env['res.partner.bank'].create({
- 'acc_number': number,
- 'partner_id': self.env.ref("base.res_partner_2").id,
- })
- rslt._onchange_set_l10n_ch_postal()
- return rslt
+class ISRTest(AccountTestInvoicingCommon):
- def create_isr_issuer_account(self, number):
- """ Generates a test res.partner.bank on company """
- return self.env['res.partner.bank'].create({
- 'acc_number': "ISR {} number",
- 'partner_id': self.env.user.company_id.partner_id.id,
- 'l10n_ch_isr_subscription_chf': number,
- })
+ @classmethod
+ def setUpClass(cls, chart_template_ref='l10n_ch.l10nch_chart_template'):
+ super().setUpClass(chart_template_ref=chart_template_ref)
def print_isr(self, invoice):
try:
@@ -54,48 +19,49 @@ class ISRTest(AccountTestCommon):
except ValidationError:
return False
- def isr_not_generated(self, invoice):
- """ Prints the given invoice and tests that no ISR generation is triggered. """
- self.assertFalse(self.print_isr(invoice), 'No ISR should be generated for this invoice')
-
- def isr_generated(self, invoice):
- """ Prints the given invoice and tests that an ISR generation is triggered. """
- self.assertTrue(self.print_isr(invoice), 'An ISR should have been generated')
-
def test_l10n_ch_postals(self):
- #An account whose number is set to a valid postal number becomes a 'postal'
- #account and sets its postal reference field.
- account_test_postal_ok = self.create_account('010391391')
- self.assertEqual(account_test_postal_ok.acc_type, 'postal', "A valid postal number in acc_number should set its type to 'postal'")
- self.assertEqual(account_test_postal_ok.l10n_ch_postal, '010391391', "A postal account should have a postal reference identical to its account number")
- #An account whose number is set to a non-postal value should not get the
- #'postal' type
- account_test_postal_wrong = self.create_account('010391394')
- self.assertNotEqual(account_test_postal_wrong.acc_type, 'postal', "A non-postal account cannot be of type 'postal'")
+ def assertBankAccountValid(account_number, expected_account_type, expected_postal=None):
+ partner_bank = self.env['res.partner.bank'].create({
+ 'acc_number': account_number,
+ 'partner_id': self.partner_a.id,
+ })
+ expected_vals = {'acc_type': expected_account_type}
+ if expected_postal is not None:
+ expected_vals['l10n_ch_postal'] = expected_postal
- #A swiss IBAN account contains a postal reference
- account_test_iban_ok = self.create_account('CH6309000000250097798')
- self.assertEqual(account_test_iban_ok.acc_type, 'iban', "The IBAN must be valid")
- self.assertEqual(account_test_iban_ok.l10n_ch_postal, '000250097798', "A valid swiss IBAN should set the postal reference")
+ self.assertRecordValues(partner_bank, [expected_vals])
- #A non-swiss IBAN must not allow the computation of a postal reference
- account_test_iban_wrong = self.create_account('GR1601101250000000012300695')
- self.assertEqual(account_test_iban_wrong.acc_type, 'iban', "The IBAN must be valid")
- self.assertFalse(account_test_iban_wrong.l10n_ch_postal, "Only a valid swiss IBAN should set the postal reference")
+ assertBankAccountValid('010391391', 'postal', expected_postal='010391391')
+ assertBankAccountValid('010391394', 'bank')
+ assertBankAccountValid('CH6309000000250097798', 'iban', expected_postal='000250097798')
+ assertBankAccountValid('GR1601101250000000012300695', 'iban', expected_postal=False)
def test_isr(self):
- #Let us test the generation of an ISR for an invoice, first by showing an
- #ISR report is only generated when Odoo has all the data it needs.
- invoice_1 = self.create_invoice('base.CHF')
- self.isr_not_generated(invoice_1)
+ isr_bank_account = self.env['res.partner.bank'].create({
+ 'acc_number': "ISR {} number",
+ 'partner_id': self.env.company.partner_id.id,
+ 'l10n_ch_isr_subscription_chf': '01-39139-1',
+ })
- #Now we add an account for payment to our invoice, the ISR can be generated
- test_account = self.create_isr_issuer_account('01-39139-1')
- invoice_1.partner_bank_id = test_account
- self.isr_generated(invoice_1)
+ invoice_chf = self.env['account.move'].create({
+ 'move_type': 'out_invoice',
+ 'partner_id': self.partner_a.id,
+ 'partner_bank_id': isr_bank_account.id,
+ 'currency_id': self.env.ref('base.CHF').id,
+ 'invoice_date': '2019-01-01',
+ 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id})],
+ })
+ invoice_chf.post()
+ self.assertTrue(self.print_isr(invoice_chf))
- #Now, let us show that, with the same data, an invoice in euros does not generate any ISR (because the bank does not have any EUR postal reference)
- invoice_2 = self.create_invoice('base.EUR')
- invoice_2.partner_bank_id = test_account
- self.isr_not_generated(invoice_2)
+ invoice_eur = self.env['account.move'].create({
+ 'move_type': 'out_invoice',
+ 'partner_id': self.partner_a.id,
+ 'partner_bank_id': isr_bank_account.id,
+ 'currency_id': self.env.ref('base.EUR').id,
+ 'invoice_date': '2019-01-01',
+ 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id})],
+ })
+ invoice_eur.post()
+ self.assertFalse(self.print_isr(invoice_eur))