[FIX] Complying with folder standard
[odoo/odoo.git] / addons / l10n_ch / bank.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    Author: Nicolas Bessi. Copyright Camptocamp SA
5 #    Donors: Hasa Sàrl, Open Net Sàrl and Prisme Solutions Informatique SA
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from tools.translate import _
23 from osv import fields, osv
24 import re
25
26 class Bank(osv.osv):
27     """Inherit res.bank class in order to add swiss specific field"""
28     _inherit = 'res.bank'
29     _columns = {
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),
34         ### city of the bank
35         'city': fields.char('City', size=128, select=1),
36     }
37
38 Bank()
39
40
41 class ResPartnerBank(osv.osv):
42     _inherit = "res.partner.bank"
43
44     _columns = {
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 Number', size=64),
52         'my_bank': fields.boolean('Use my account to print BVR ?', help="Check to print BVR invoices"),
53     }
54
55     def _prepare_name(self, bank):
56         "Hook to get bank number of bank account"
57         res = u''
58         if bank.acc_number:
59             res = super(ResPartnerBank, self)._prepare_name(bank) or u''
60         if bank.post_number:
61             if res:
62                 res =  u"%s - %s" % (res, bank.post_number)
63             else:
64                 res = bank.post_number
65         return res
66
67     def _check_9_pos_postal_num(self, number):
68         """
69         check if a postal number in format xx-xxxxxx-x is correct,
70         return true if it matches the pattern
71         and if check sum mod10 is ok
72         """
73         from tools import mod10r
74         pattern = r'^[0-9]{2}-[0-9]{1,6}-[0-9]$'
75         if not re.search(pattern, number):
76             return False
77         num, checksum = (number.replace('-','')[:-1], number[-1:])
78         return mod10r(num)[-1:] == checksum
79
80
81     def _check_5_pos_postal_num(self, number):
82         """
83         check if a postal number on 5 positions is correct
84         """
85         pattern = r'^[0-9]{1,5}$'
86         if not re.search(pattern, number):
87             return False
88         return True
89
90     def _check_postal_num(self, cursor, uid, ids):
91         banks = self.browse(cursor, uid, ids)
92         for b in banks:
93             if not b.post_number:
94                 return True
95             return self._check_9_pos_postal_num(b.post_number) or \
96                    self._check_5_pos_postal_num(b.post_number)
97
98  
99     _constraints = [(_check_postal_num,
100                     'Please enter a correct postal number. (01-23456-5 or 12345)',
101                     ['post_number'])]    
102
103     _sql_constraints = [('bvr_adherent_uniq', 'unique (bvr_adherent_num)',
104         'The BVR adherent number must be unique !')]
105
106 ResPartnerBank()
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: