~camptocamp/openerp-swiss-localization/better-description-jge

« back to all changes in this revision

Viewing changes to l10n_ch_base_bank/bank.py

  • Committer: Vincent Renaville
  • Author(s): nicolas.bessi at camptocamp
  • Date: 2013-07-08 14:51:57 UTC
  • mfrom: (175.3.4 add_related_ccp_account)
  • Revision ID: vincent.renaville@camptocamp.com-20130708145157-9j2rx1auck47nxzu
Tags: V7.1
[FIX] Change addons with new release 7.1
  *) Add ccp account on res_bank but keep retro compatibility with old model.
     If no ccp is set on bank old behavior is kept
     Be aware if you have customize bvr.mako you have to replace acc_number by get_account_number()
  *) Add constraint to avoid wrong entries
  *) Disable invoice reference overwrite
  *) Better bank views
  *) Validating an account invoice does not overwrite invoice reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#
21
21
##############################################################################
22
22
import re
23
 
from openerp.osv.orm import Model, fields
 
23
from openerp.osv import orm, fields
24
24
from tools import mod10r
25
25
 
26
26
 
27
 
class Bank(Model):
28
 
    """Inherit res.bank class in order to add swiss specific field"""
29
 
    _inherit = 'res.bank'
30
 
    _columns = {
31
 
        ### Internal reference
32
 
        'code': fields.char('Code', size=64),
33
 
        ###Swiss unik bank identifier also use in IBAN number
34
 
        'clearing': fields.char('Clearing number', size=64),
35
 
        ### city of the bank
36
 
        'city': fields.char('City', size=128, select=1),
37
 
    }
38
 
 
39
 
 
40
 
class ResPartnerBank(Model):
41
 
    """
42
 
    Inherit res.partner.bank class in order to add swiss specific fields and state controls
43
 
    """
44
 
    _inherit = "res.partner.bank"
45
 
 
46
 
    _columns = {
47
 
        'name': fields.char('Description', size=128, required=True),
48
 
        'bvr_adherent_num': fields.char('Bank BVR adherent number', size=11, 
49
 
                                        help=("Your Bank adherent number to be printed in references of your BVR."
50
 
                                              "This is not a postal account number.")),
51
 
        'acc_number': fields.char('Account/IBAN Number', size=64, required=True),
52
 
    }
53
 
 
 
27
class BankCommon(object):
54
28
 
55
29
    def _check_9_pos_postal_num(self, number):
56
30
        """
63
37
            return False
64
38
        nums = number.split('-')
65
39
        prefix = nums[0]
66
 
        num = nums[1].rjust(6,'0')
 
40
        num = nums[1].rjust(6, '0')
67
41
        checksum = nums[2]
68
42
        expected_checksum = mod10r(prefix + num)[-1]
69
43
        return expected_checksum == checksum
70
44
 
71
 
 
72
45
    def _check_5_pos_postal_num(self, number):
73
46
        """
74
47
        check if a postal number on 5 positions is correct
78
51
            return False
79
52
        return True
80
53
 
 
54
 
 
55
class Bank(orm.Model, BankCommon):
 
56
    """Inherit res.bank class in order to add swiss specific field"""
 
57
    _inherit = 'res.bank'
 
58
    _columns = {
 
59
        ### Internal reference
 
60
        'code': fields.char('Code', size=64, select=True),
 
61
        ###Swiss unik bank identifier also use in IBAN number
 
62
        'clearing': fields.char('Clearing number', size=64),
 
63
        ### city of the bank
 
64
        'city': fields.char('City', size=128, select=1),
 
65
        ### ccp of the bank
 
66
        'ccp': fields.char('CCP', size=64, select=1)
 
67
    }
 
68
 
 
69
    def _check_ccp_duplication(self, cursor, uid, ids):
 
70
        p_acc_obj = self.pool['res.partner.bank']
 
71
        for bank in self.browse(cursor, uid, ids):
 
72
            p_acc_ids = p_acc_obj.search(cursor, uid, [('bank', '=', bank.id)])
 
73
            if p_acc_ids:
 
74
                check = p_acc_obj._check_ccp_duplication(cursor, uid, p_acc_ids)
 
75
                if not check:
 
76
                    return False
 
77
        return True
 
78
 
81
79
    def _check_postal_num(self, cursor, uid, ids):
82
80
        """
83
81
        validate postal number format
84
82
        """
85
83
        banks = self.browse(cursor, uid, ids)
86
 
        for b in banks:
87
 
            if not b.state in ('bv', 'bvr'):
88
 
                return True
89
 
            return self._check_9_pos_postal_num(b.acc_number) or \
90
 
                   self._check_5_pos_postal_num(b.acc_number)
91
 
 
92
 
 
93
 
    _constraints = [(_check_postal_num,
94
 
                    'Please enter a correct postal number. (01-23456-1 or 12345)',
95
 
                    ['acc_number'])]
 
84
        for bank in banks:
 
85
            if not bank.ccp:
 
86
                continue
 
87
            if not (self._check_9_pos_postal_num(bank.ccp) or
 
88
                    self._check_5_pos_postal_num(bank.ccp)):
 
89
                return False
 
90
        return True
 
91
 
 
92
    def name_get(self, cursor, uid, ids, context=None):
 
93
        res = []
 
94
        cols = ('bic', 'name', 'street', 'city')
 
95
        for bank in self.browse(cursor, uid, ids, context):
 
96
            vals = (bank[x] for x in cols if bank[x])
 
97
            res.append((bank.id, ' - '.join(vals)))
 
98
        return res
 
99
 
 
100
    def name_search(self, cursor, uid, name, args=None, operator='ilike', context=None, limit=80):
 
101
        if args is None:
 
102
            args = []
 
103
        if context is None:
 
104
            context = {}
 
105
        ids = []
 
106
        cols = ('code', 'bic', 'name', 'street', 'city')
 
107
        if name:
 
108
            for val in name.split(' '):
 
109
                for col in cols:
 
110
                    tmp_ids = self.search(cursor, uid, [(col, 'ilike', val)] + args, limit=limit)
 
111
                    if tmp_ids:
 
112
                        ids += tmp_ids
 
113
                        break
 
114
        # we sort by occurence
 
115
        to_ret_ids = list(set(ids))
 
116
        to_ret_ids = sorted(to_ret_ids, key=lambda x: ids.count(x), reverse=True)
 
117
 
 
118
        return self.name_get(cursor, uid, to_ret_ids, context=context)
 
119
 
 
120
    _constraints = [(_check_postal_num,
 
121
                    'Please enter a correct postal number. (01-23456-1 or 12345)',
 
122
                    ['ccp']),
 
123
 
 
124
                    (_check_ccp_duplication,
 
125
                    'You can not enter a ccp both on the bank and on an account'
 
126
                    ' of type BV, BVR',
 
127
                    ['acc_number', 'bank'])]
 
128
 
 
129
 
 
130
class ResPartnerBank(orm.Model, BankCommon):
 
131
    """
 
132
    Inherit res.partner.bank class in order to add swiss specific fields and state controls
 
133
    """
 
134
    _inherit = 'res.partner.bank'
 
135
 
 
136
    _columns = {
 
137
        'name': fields.char('Description', size=128, required=True),
 
138
        'bvr_adherent_num': fields.char('Bank BVR adherent number', size=11,
 
139
                                        help=("Your Bank adherent number to be printed in references of your BVR."
 
140
                                              "This is not a postal account number.")),
 
141
        'acc_number': fields.char('Account/IBAN Number', size=64, required=True),
 
142
        'ccp': fields.related('bank', 'ccp', type='char', string='CCP',
 
143
                              readonly=True),
 
144
    }
 
145
 
 
146
    def get_account_number(self, cursor, uid, bid, context=None):
 
147
        if isinstance(bid, list):
 
148
            bid = bid[0]
 
149
        current = self.browse(cursor, uid, bid, context=context)
 
150
        if current.state not in ('bv', 'bvr'):
 
151
            return current.acc_number
 
152
        if current.bank and current.bank.ccp:
 
153
            return current.bank.ccp
 
154
        else:
 
155
            return current.acc_number
 
156
 
 
157
    def _check_postal_num(self, cursor, uid, ids):
 
158
        """
 
159
        validate postal number format
 
160
        """
 
161
        p_banks = self.browse(cursor, uid, ids)
 
162
        for p_bank in p_banks:
 
163
            if not p_bank.state in ('bv', 'bvr'):
 
164
                continue
 
165
            if not (self._check_9_pos_postal_num(p_bank.get_account_number()) or
 
166
                    self._check_5_pos_postal_num(p_bank.get_account_number())):
 
167
                return False
 
168
        return True
 
169
 
 
170
    def _check_ccp_duplication(self, cursor, uid, ids):
 
171
        """
 
172
          Ensure that there is not a ccp in bank and res partner bank
 
173
          at same time
 
174
        """
 
175
        p_banks = self.browse(cursor, uid, ids)
 
176
        for p_bank in p_banks:
 
177
            if not p_bank.state in ('bv', 'bvr'):
 
178
                continue
 
179
            bank_ccp = p_bank.bank.ccp if p_bank.bank else False
 
180
            if not bank_ccp:
 
181
                continue
 
182
            part_bank_check = (self._check_5_pos_postal_num(p_bank.acc_number) or
 
183
                               self._check_9_pos_postal_num(p_bank.acc_number))
 
184
            bank_check = (self._check_5_pos_postal_num(p_bank.bank.ccp) or
 
185
                          self._check_9_pos_postal_num(p_bank.bank.ccp))
 
186
            if part_bank_check and bank_check:
 
187
                return False
 
188
        return True
 
189
 
 
190
    _constraints = [(_check_postal_num,
 
191
                    'Please enter a correct postal number. (01-23456-1 or 12345)',
 
192
                    ['acc_number']),
 
193
 
 
194
                    (_check_ccp_duplication,
 
195
                    'You can not enter a ccp both on the bank and on an account'
 
196
                    ' of type BV, BVR',
 
197
                    ['acc_number', 'bank'])]
96
198
 
97
199
    _sql_constraints = [('bvr_adherent_uniq', 'unique (bvr_adherent_num)',
98
 
        'The BVR adherent number must be unique !')]
 
200
                         'The BVR adherent number must be unique !')]
99
201
 
100
202
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: