[imp] improvement of label handling
[odoo/odoo.git] / addons / base_contact / base_contact.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 import addons
24
25 class res_partner_contact(osv.osv):
26     """ Partner Contact """
27
28     _name = "res.partner.contact"
29     _description = "Contact"
30
31     def _name_get_full(self, cr, uid, ids, prop, unknow_none, context=None):
32         result = {}
33         for rec in self.browse(cr, uid, ids, context=context):
34             result[rec.id] = rec.last_name+' '+(rec.first_name or '')
35         return result
36
37     _columns = {
38         'name': fields.function(_name_get_full, string='Name', size=64, type="char", store=True, select=True),
39         'last_name': fields.char('Last Name', size=64, required=True),
40         'first_name': fields.char('First Name', size=64),
41         'mobile': fields.char('Mobile', size=64),
42         'title': fields.many2one('res.partner.title','Title', domain=[('domain','=','contact')]),
43         'website': fields.char('Website', size=120),
44         'lang_id': fields.many2one('res.lang', 'Language'),
45         'job_ids': fields.one2many('res.partner.address', 'contact_id', 'Functions and Addresses'),
46         'country_id': fields.many2one('res.country','Nationality'),
47         'birthdate': fields.char('Birthdate', size=64),
48         'active': fields.boolean('Active', help="If the active field is set to False,\
49                  it will allow you to hide the partner contact without removing it."),
50         'partner_id': fields.related('job_ids', 'partner_id', type='many2one',\
51                          relation='res.partner', string='Main Employer'),
52         'function': fields.related('job_ids', 'function', type='char', \
53                                  string='Main Function'),
54         'email': fields.char('E-Mail', size=240),
55         'comment': fields.text('Notes', translate=True),
56         'photo': fields.binary('Photo'),
57     }
58
59     def _get_photo(self, cr, uid, context=None):
60         photo_path = addons.get_module_resource('base_contact', 'images', 'photo.png')
61         return open(photo_path, 'rb').read().encode('base64')
62
63     _defaults = {
64         'photo' : _get_photo,
65         'active' : lambda *a: True,
66     }
67
68     _order = "name"
69
70     def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=None):
71         if not args:
72             args = []
73         if context is None:
74             context = {}
75         if name:
76             ids = self.search(cr, uid, ['|',('name', operator, name),('first_name', operator, name)] + args, limit=limit, context=context)
77         else:
78             ids = self.search(cr, uid, args, limit=limit, context=context)
79         return self.name_get(cr, uid, ids, context=context)
80
81     def name_get(self, cr, uid, ids, context=None):
82         result = {}
83         for obj in self.browse(cr, uid, ids, context=context):
84             result[obj.id] = obj.name or '/'
85             if obj.partner_id:
86                 result[obj.id] = result[obj.id] + ', ' + obj.partner_id.name
87         return result.items()
88
89     def _auto_init(self, cr, context=None):
90         def table_exists(view_name):
91             cr.execute('SELECT count(relname) FROM pg_class WHERE relname = %s', (view_name,))
92             value = cr.fetchone()[0]
93             return bool(value == 1)
94
95         exists = table_exists(self._table)
96         super(res_partner_contact, self)._auto_init(cr, context)
97
98         if not exists:
99             cr.execute("""
100                 INSERT INTO
101                     res_partner_contact
102                     (id,name,last_name,title,active,email,mobile,birthdate)
103                 SELECT
104                     id,COALESCE(name, '/'),COALESCE(name, '/'),title,true,email,mobile,birthdate
105                 FROM
106                     res_partner_address""")
107             cr.execute("alter table res_partner_address add contact_id int references res_partner_contact")
108             cr.execute("update res_partner_address set contact_id=id")
109             cr.execute("select setval('res_partner_contact_id_seq', (select max(id)+1 from res_partner_contact))")
110
111 res_partner_contact()
112
113 class res_partner_location(osv.osv):
114     _name = 'res.partner.location'
115     _rec_name = 'street'
116     _columns = {
117         'street': fields.char('Street', size=128),
118         'street2': fields.char('Street2', size=128),
119         'zip': fields.char('Zip', change_default=True, size=24),
120         'city': fields.char('City', size=128),
121         'state_id': fields.many2one("res.country.state", 'Fed. State', domain="[('country_id','=',country_id)]"),
122         'country_id': fields.many2one('res.country', 'Country'),
123         'company_id': fields.many2one('res.company', 'Company',select=1),
124         'job_ids': fields.one2many('res.partner.address', 'location_id', 'Contacts'),
125         'partner_id': fields.related('job_ids', 'partner_id', type='many2one',\
126                          relation='res.partner', string='Main Partner'),
127     }
128     _defaults = {
129         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'res.partner.address', context=c),
130     }
131     def _auto_init(self, cr, context=None):
132         def table_exists(view_name):
133             cr.execute('SELECT count(relname) FROM pg_class WHERE relname = %s', (view_name,))
134             value = cr.fetchone()[0]
135             return bool(value == 1)
136
137         exists = table_exists(self._table)
138         super(res_partner_location, self)._auto_init(cr, context)
139
140         if not exists:
141             cr.execute("""
142                 INSERT INTO
143                     res_partner_location
144                     (id,street,street2,zip,city,
145                      state_id,country_id,company_id)
146                 SELECT
147                     id,street,street2,zip,city,
148                     state_id,country_id,company_id
149                 FROM
150                     res_partner_address""")
151             cr.execute("alter table res_partner_address add location_id int references res_partner_location")
152             cr.execute("update res_partner_address set location_id=id")
153             cr.execute("select setval('res_partner_location_id_seq', (select max(id)+1 from res_partner_address))")
154
155     def name_get(self, cr, uid, ids, context=None):
156         result = {}
157         for obj in self.browse(cr, uid, ids, context=context):
158             res = []
159             if obj.partner_id: res.append(obj.partner_id.name_get()[0][1])
160             if obj.city: res.append(obj.city)
161             if obj.country_id: res.append(obj.country_id.name_get()[0][1])
162             result[obj.id] = ', '.join(res)
163         return result.items()
164
165 res_partner_location()
166
167 class res_partner_address(osv.osv):
168     _inherit = 'res.partner.address'
169
170     def _default_location_id(self, cr, uid, context=None):
171         if context is None:
172             context = {}
173         if not context.get('default_partner_id',False):
174             return False
175         ids = self.pool.get('res.partner.location').search(cr, uid, [('partner_id','=',context['default_partner_id'])], context=context)
176         return ids and ids[0] or False
177
178     def onchange_location_id(self,cr, uid, ids, location_id=False, context={}):
179         if not location_id:
180             return {}
181         location = self.pool.get('res.partner.location').browse(cr, uid, location_id, context=context)
182         return {'value':{
183             'street': location.street,
184             'street2': location.street2,
185             'zip': location.zip,
186             'city': location.city,
187             'country_id': location.country_id and location.country_id.id or False,
188             'state_id': location.state_id and location.state_id.id or False,
189         }}
190
191     _columns = {
192         'location_id' : fields.many2one('res.partner.location', 'Location'),
193         'contact_id' : fields.many2one('res.partner.contact', 'Contact'),
194
195         # fields from location
196         'street': fields.related('location_id', 'street', string='Street', type="char", store=True, size=128),
197         'street2': fields.related('location_id', 'street2', string='Street2', type="char", store=True, size=128),
198         'zip': fields.related('location_id', 'zip', string='Zip', type="char", store=True, change_default=True, size=24),
199         'city': fields.related('location_id', 'city', string='City', type="char", store=True, size=128),
200         'state_id': fields.related('location_id', 'state_id', relation="res.country.state", string='Fed. State', type="many2one", store=True, domain="[('country_id','=',country_id)]"),
201         'country_id': fields.related('location_id', 'country_id', type='many2one', string='Country', store=True, relation='res.country'),
202
203         'phone': fields.char('Phone', size=64),
204         'fax': fields.char('Fax', size=64),
205         'email': fields.char('E-Mail', size=240),
206
207         # fields from contact
208         'mobile' : fields.related('contact_id', 'mobile', type='char', size=64, string='Mobile'),
209         'name' : fields.related('contact_id', 'name', type='char', size=64, string="Contact Name", store=True),
210         'title' : fields.related('contact_id', 'title', type='many2one', relation='res.partner.title', string="Title", store=True),
211     }
212     def create(self, cr, uid, data, context={}):
213         if not data.get('location_id', False):
214             loc_id = self.pool.get('res.partner.location').create(cr, uid, {
215                 'street': data.get('street',''),
216                 'street2': data.get('street2',''),
217                 'zip': data.get('zip',''),
218                 'city': data.get('city',''),
219                 'country_id': data.get('country_id',False),
220                 'state_id': data.get('state_id',False)
221             }, context=context)
222             data['location_id'] = loc_id
223         result = super(res_partner_address, self).create(cr, uid, data, context=context)
224         return result
225
226     def name_get(self, cr, uid, ids, context=None):
227         result = {}
228         for rec in self.browse(cr,uid, ids, context=context):
229             res = []
230             if rec.partner_id:
231                 res.append(rec.partner_id.name_get()[0][1])
232             if rec.contact_id and rec.contact_id.name:
233                 res.append(rec.contact_id.name)
234             if rec.location_id:
235                 if rec.location_id.city: res.append(rec.location_id.city)
236                 if rec.location_id.country_id: res.append(rec.location_id.country_id.name_get()[0][1])
237             result[rec.id] = ', '.join(res)
238         return result.items()
239
240     _defaults = {
241         'location_id': _default_location_id
242     }
243
244     def default_get(self, cr, uid, fields=[], context=None):
245         if context is None:
246             context = {}
247         if 'default_type' in context:
248             del context['default_type']
249         return super(res_partner_address, self).default_get(cr, uid, fields, context)
250
251 res_partner_address()
252