1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Author: Nicolas Bessi. Copyright Camptocamp SA
5 # Donors: Hasa Sàrl, Open Net Sàrl and Prisme Solutions Informatique SA
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from tools.translate import _
23 from osv import fields, osv
27 """Inherit res.bank class in order to add swiss specific field"""
30 ### Internal reference
31 'code': fields.char('Code', size=64),
32 ###Swiss unik bank identifier also use in IBAN number
33 'clearing': fields.char('Clearing number', size=64),
35 'city': fields.char('City', size=128, select=1),
41 class ResPartnerBank(osv.osv):
42 _inherit = "res.partner.bank"
45 'name': fields.char('Description', size=128, required=True),
46 'post_number': fields.char('Post number', size=64, help="Postal number 0x-xxxxxx-x or xxxxx"),
47 'bvr_adherent_num': fields.char('Bank BVR adherent number', size=11, help="Your Bank adherent number to be printed in references of your BVR. This is not a postal account number."),
48 'dta_code': fields.char('DTA code', size=5),
49 'print_bank': fields.boolean('Print Bank on BVR'),
50 'print_account': fields.boolean('Print Account Number on BVR'),
51 'acc_number': fields.char('Account/IBAN Number', size=64),
52 'my_bank': fields.boolean('Use my account to print BVR ?', help="Check to print BVR invoices"),
55 def name_get(self, cursor, uid, ids, context=None):
58 bank_type_obj = self.pool.get('res.partner.bank.type')
60 type_ids = bank_type_obj.search(cursor, uid, [])
62 for bank_type in bank_type_obj.browse(cursor, uid, type_ids,
64 bank_type_names[bank_type.code] = bank_type.name
66 for r in self.read(cursor, uid, ids, ['name','state'], context):
67 res.append((r['id'], r['name']+' : '+bank_type_names.get(r['state'], '')))
70 def _prepare_name(self, bank):
71 "Hook to get bank number of bank account"
74 res = super(ResPartnerBank, self)._prepare_name(bank) or u''
77 res = u"%s - %s" % (res, bank.post_number)
79 res = bank.post_number
82 def _check_9_pos_postal_num(self, number):
84 check if a postal number in format xx-xxxxxx-x is correct,
85 return true if it matches the pattern
86 and if check sum mod10 is ok
88 from tools import mod10r
89 pattern = r'^[0-9]{2}-[0-9]{1,6}-[0-9]$'
90 if not re.search(pattern, number):
92 num, checksum = (number.replace('-','')[:-1], number[-1:])
93 return mod10r(num)[-1:] == checksum
96 def _check_5_pos_postal_num(self, number):
98 check if a postal number on 5 positions is correct
100 pattern = r'^[0-9]{1,5}$'
101 if not re.search(pattern, number):
105 def _check_postal_num(self, cursor, uid, ids):
106 banks = self.browse(cursor, uid, ids)
108 if not b.post_number:
110 return self._check_9_pos_postal_num(b.post_number) or \
111 self._check_5_pos_postal_num(b.post_number)
114 _constraints = [(_check_postal_num,
115 'Please enter a correct postal number. (01-23456-5 or 12345)',
118 _sql_constraints = [('bvr_adherent_uniq', 'unique (bvr_adherent_num)',
119 'The BVR adherent number must be unique !')]
123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: