c9010b64319906fef9de07af2d4c0fd396ddc134
[odoo/odoo.git] / openerp / addons / base / res / res_bank.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 osv import fields, osv
23 from tools.translate import _
24
25 class Bank(osv.osv):
26     _description='Bank'
27     _name = 'res.bank'
28     _order = 'name'
29     _columns = {
30         'name': fields.char('Name', size=128, required=True),
31         'street': fields.char('Street', size=128),
32         'street2': fields.char('Street2', size=128),
33         'zip': fields.char('Zip', change_default=True, size=24),
34         'city': fields.char('City', size=128),
35         'state': fields.many2one("res.country.state", 'Fed. State',
36             domain="[('country_id', '=', country)]"),
37         'country': fields.many2one('res.country', 'Country'),
38         'email': fields.char('Email', size=64),
39         'phone': fields.char('Phone', size=64),
40         'fax': fields.char('Fax', size=64),
41         'active': fields.boolean('Active'),
42         'bic': fields.char('Bank Identifier Code', size=64,
43             help="Sometimes called BIC or Swift."),
44     }
45     _defaults = {
46         'active': lambda *a: 1,
47     }
48     def name_get(self, cr, uid, ids, context=None):
49         result = []
50         for bank in self.browse(cr, uid, ids, context):
51             result.append((bank.id, (bank.bic and (bank.bic + ' - ') or '') + bank.name))
52         return result
53
54 Bank()
55
56
57 class res_partner_bank_type(osv.osv):
58     _description='Bank Account Type'
59     _name = 'res.partner.bank.type'
60     _order = 'name'
61     _columns = {
62         'name': fields.char('Name', size=64, required=True, translate=True),
63         'code': fields.char('Code', size=64, required=True),
64         'field_ids': fields.one2many('res.partner.bank.type.field', 'bank_type_id', 'Type Fields'),
65         'format_layout': fields.text('Format Layout', translate=True)
66     }
67     _defaults = {
68         'format_layout': lambda *args: "%(bank_name)s: %(acc_number)s"
69     }
70 res_partner_bank_type()
71
72 class res_partner_bank_type_fields(osv.osv):
73     _description='Bank type fields'
74     _name = 'res.partner.bank.type.field'
75     _order = 'name'
76     _columns = {
77         'name': fields.char('Field Name', size=64, required=True, translate=True),
78         'bank_type_id': fields.many2one('res.partner.bank.type', 'Bank Type', required=True, ondelete='cascade'),
79         'required': fields.boolean('Required'),
80         'readonly': fields.boolean('Readonly'),
81         'size': fields.integer('Max. Size'),
82     }
83 res_partner_bank_type_fields()
84
85
86 class res_partner_bank(osv.osv):
87     '''Bank Accounts'''
88     _name = "res.partner.bank"
89     _rec_name = "acc_number"
90     _description = __doc__
91     _order = 'sequence'
92
93     def _bank_type_get(self, cr, uid, context=None):
94         bank_type_obj = self.pool.get('res.partner.bank.type')
95
96         result = []
97         type_ids = bank_type_obj.search(cr, uid, [])
98         bank_types = bank_type_obj.browse(cr, uid, type_ids, context=context)
99         for bank_type in bank_types:
100             result.append((bank_type.code, bank_type.name))
101         return result
102
103     def _default_value(self, cursor, user, field, context=None):
104         if context is None: context = {}
105         if field in ('country_id', 'state_id'):
106             value = False
107         else:
108             value = ''
109         if not context.get('address'):
110             return value
111
112         for address in self.pool.get('res.partner').resolve_o2m_commands_to_record_dicts(
113             cursor, user, 'address', context['address'], ['type', field], context=context):
114
115             if address.get('type') == 'default':
116                 return address.get(field, value)
117             elif not address.get('type'):
118                 value = address.get(field, value)
119         return value
120
121     _columns = {
122         'name': fields.char('Bank Account', size=64), # to be removed in v6.2 ?
123         'acc_number': fields.char('Account Number', size=64, required=True),
124         'bank': fields.many2one('res.bank', 'Bank'),
125         'bank_bic': fields.char('Bank Identifier Code', size=16),
126         'bank_name': fields.char('Bank Name', size=32),
127         'owner_name': fields.char('Account Owner Name', size=128),
128         'street': fields.char('Street', size=128),
129         'zip': fields.char('Zip', change_default=True, size=24),
130         'city': fields.char('City', size=128),
131         'country_id': fields.many2one('res.country', 'Country',
132             change_default=True),
133         'state_id': fields.many2one("res.country.state", 'Fed. State',
134             change_default=True, domain="[('country_id','=',country_id)]"),
135         'company_id': fields.many2one('res.company', 'Company',
136             ondelete='cascade', help="Only if this bank account belong to your company"),
137         'partner_id': fields.many2one('res.partner', 'Account Owner', required=True,
138             ondelete='cascade', select=True),
139         'state': fields.selection(_bank_type_get, 'Bank Account Type', required=True,
140             change_default=True),
141         'sequence': fields.integer('Sequence'),
142         'footer': fields.boolean("Display on Reports", help="Display this bank account on the footer of printed documents like invoices and sales orders.")
143     }
144
145     _defaults = {
146         'owner_name': lambda obj, cursor, user, context: obj._default_value(
147             cursor, user, 'name', context=context),
148         'street': lambda obj, cursor, user, context: obj._default_value(
149             cursor, user, 'street', context=context),
150         'city': lambda obj, cursor, user, context: obj._default_value(
151             cursor, user, 'city', context=context),
152         'zip': lambda obj, cursor, user, context: obj._default_value(
153             cursor, user, 'zip', context=context),
154         'country_id': lambda obj, cursor, user, context: obj._default_value(
155             cursor, user, 'country_id', context=context),
156         'state_id': lambda obj, cursor, user, context: obj._default_value(
157             cursor, user, 'state_id', context=context),
158         'name': lambda *args: '/'
159     }
160
161     def fields_get(self, cr, uid, fields=None, context=None):
162         res = super(res_partner_bank, self).fields_get(cr, uid, fields, context)
163         bank_type_obj = self.pool.get('res.partner.bank.type')
164         type_ids = bank_type_obj.search(cr, uid, [])
165         types = bank_type_obj.browse(cr, uid, type_ids)
166         for type in types:
167             for field in type.field_ids:
168                 if field.name in res:
169                     res[field.name].setdefault('states', {})
170                     res[field.name]['states'][type.code] = [
171                             ('readonly', field.readonly),
172                             ('required', field.required)]
173         return res
174
175     def name_get(self, cr, uid, ids, context=None):
176         if not len(ids):
177             return []
178         bank_type_obj = self.pool.get('res.partner.bank.type')
179         res = []
180         for val in self.browse(cr, uid, ids, context=context):
181             result = val.acc_number
182             if val.state:
183                 type_ids = bank_type_obj.search(cr, uid, [('code','=',val.state)])
184                 if type_ids:
185                     t = bank_type_obj.browse(cr, uid, type_ids[0], context=context)
186                     try:
187                         # avoid the default format_layout to result in "False: ..."
188                         if not val._data[val.id]['bank_name']:
189                             val._data[val.id]['bank_name'] = _('BANK')
190                         result = t.format_layout % val._data[val.id]
191                     except:
192                         result += ' [Formatting Error]'
193                         raise
194             res.append((val.id, result))
195         return res
196
197     def onchange_company_id(self, cr, uid, ids, company_id, context=None):
198         result = {}
199         if company_id:
200             c = self.pool.get('res.company').browse(cr, uid, company_id, context=context)
201             if c.partner_id:
202                 r = self.onchange_partner_id(cr, uid, ids, c.partner_id.id, context=context)
203                 r['value']['partner_id'] = c.partner_id.id
204                 r['value']['footer'] = 1
205                 result = r
206         return result
207
208     def onchange_bank_id(self, cr, uid, ids, bank_id, context=None):
209         result = {}
210         if bank_id:
211             bank = self.pool.get('res.bank').browse(cr, uid, bank_id, context=context)
212             result['bank_name'] = bank.name
213             result['bank_bic'] = bank.bic
214         return {'value': result}
215
216
217     def onchange_partner_id(self, cr, uid, id, partner_id, context=None):
218         result = {}
219         if partner_id:
220             part = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
221             result['owner_name'] = part.name
222             result['street'] = part.street or False
223             result['city'] = part.city or False
224             result['zip'] =  part.zip or False
225             result['country_id'] =  part.country_id.id
226             result['state_id'] = part.state_id.id
227         return {'value': result}
228
229 res_partner_bank()
230
231 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: