1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 osv import fields, osv
25 class res_partner_contact(osv.osv):
26 """ Partner Contact """
28 _name = "res.partner.contact"
29 _description = "Contact"
31 def _name_get_full(self, cr, uid, ids, prop, unknow_none, context=None):
33 for rec in self.browse(cr, uid, ids, context=context):
34 result[rec.id] = rec.last_name+' '+(rec.first_name or '')
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'),
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')
65 'active' : lambda *a: True,
70 def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=None):
76 ids = self.search(cr, uid, ['|',('name', operator, name),('first_name', operator, name)] + args, limit=limit, context=context)
78 ids = self.search(cr, uid, args, limit=limit, context=context)
79 return self.name_get(cr, uid, ids, context=context)
81 def name_get(self, cr, uid, ids, context=None):
83 for obj in self.browse(cr, uid, ids, context=context):
84 result[obj.id] = obj.name or '/'
86 result[obj.id] = result[obj.id] + ', ' + obj.partner_id.name
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)
95 exists = table_exists(self._table)
96 super(res_partner_contact, self)._auto_init(cr, context)
102 (id,name,last_name,title,active,email,mobile,birthdate)
104 id,COALESCE(name, '/'),COALESCE(name, '/'),title,true,email,mobile,birthdate
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))")
111 res_partner_contact()
113 class res_partner_location(osv.osv):
114 _name = 'res.partner.location'
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'),
129 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'res.partner.address', context=c),
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)
137 exists = table_exists(self._table)
138 super(res_partner_location, self)._auto_init(cr, context)
144 (id,street,street2,zip,city,
145 state_id,country_id,company_id)
147 id,street,street2,zip,city,
148 state_id,country_id,company_id
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))")
155 def name_get(self, cr, uid, ids, context=None):
157 for obj in self.browse(cr, uid, ids, context=context):
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()
165 res_partner_location()
167 class res_partner_address(osv.osv):
168 _inherit = 'res.partner.address'
170 def _default_location_id(self, cr, uid, context=None):
173 if not context.get('default_partner_id',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
178 def onchange_location_id(self,cr, uid, ids, location_id=False, context={}):
181 location = self.pool.get('res.partner.location').browse(cr, uid, location_id, context=context)
183 'street': location.street,
184 'street2': location.street2,
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,
192 'location_id' : fields.many2one('res.partner.location', 'Location'),
193 'contact_id' : fields.many2one('res.partner.contact', 'Contact'),
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'),
203 'phone': fields.char('Phone', size=64),
204 'fax': fields.char('Fax', size=64),
205 'email': fields.char('E-Mail', size=240),
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),
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)
222 data['location_id'] = loc_id
223 result = super(res_partner_address, self).create(cr, uid, data, context=context)
226 def name_get(self, cr, uid, ids, context=None):
228 for rec in self.browse(cr,uid, ids, context=context):
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)
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()
241 'location_id': _default_location_id
244 def default_get(self, cr, uid, fields=[], context=None):
247 if 'default_type' in context:
248 del context['default_type']
249 return super(res_partner_address, self).default_get(cr, uid, fields, context)
251 res_partner_address()