[FIX] Corrected language code for Slovenian and Danish locales and renamed correspond...
[odoo/odoo.git] / addons / base_contact / base_contact.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import netsvc
24 from osv import fields, osv
25
26 class res_partner_contact(osv.osv):
27     _name = "res.partner.contact"
28     _description = "res.partner.contact"
29
30     def _title_get(self,cr, user, context={}):
31         obj = self.pool.get('res.partner.title')
32         ids = obj.search(cr, user, [])
33         res = obj.read(cr, user, ids, ['shortcut', 'name','domain'], context)
34         res = [(r['shortcut'], r['name']) for r in res if r['domain']=='contact']
35         return res
36
37     def _main_job(self, cr, uid, ids, fields, arg, context=None):
38         res = dict.fromkeys(ids, False)
39         for contact in self.browse(cr, uid, ids, context):
40             if contact.job_ids:
41                 res[contact.id] = contact.job_ids[0].name_get()[0]
42         return res
43
44     _columns = {
45         'name': fields.char('Last Name', size=30,required=True),
46         'first_name': fields.char('First Name', size=30),
47         'mobile':fields.char('Mobile',size=30),
48         'title': fields.selection(_title_get, 'Title'),
49         'website':fields.char('Website',size=120),
50         'lang_id':fields.many2one('res.lang','Language'),
51         'job_ids':fields.one2many('res.partner.job','contact_id','Functions and Addresses'),
52         'country_id':fields.many2one('res.country','Nationality'),
53         'birthdate':fields.date('Birth Date'),
54         'active' : fields.boolean('Active'),
55         'partner_id':fields.related('job_ids','address_id','partner_id',type='many2one', relation='res.partner', string='Main Employer'),
56         'function_id':fields.related('job_ids','function_id',type='many2one', relation='res.partner.function', string='Main Function'),
57         'job_id': fields.function(_main_job, method=True, type='many2one', relation='res.partner.job', string='Main Job'),
58         'email': fields.char('E-Mail', size=240),
59     }
60     _defaults = {
61         'active' : lambda *a: True,
62     }
63     def name_get(self, cr, user, ids, context={}):
64         #will return name and first_name.......
65         if not len(ids):
66             return []
67         res = []
68         for r in self.read(cr, user, ids, ['name','first_name','title']):
69             addr = r['title'] and str(r['title'])+" " or ''
70             addr += r.get('name', '')
71             if r['name'] and r['first_name']:
72                 addr += ' '
73             addr += (r.get('first_name', '') or '')
74             res.append((r['id'], addr))
75         return res
76 res_partner_contact()
77
78 class res_partner_address(osv.osv):
79
80     def search(self, cr, user, args, offset=0, limit=None, order=None,
81             context=None, count=False):
82         if context and context.has_key('address_partner_id' ) and context['address_partner_id']:
83             args.append(('partner_id', '=', context['address_partner_id']))
84         return super(res_partner_address, self).search(cr, user, args, offset, limit, order, context, count)
85
86     #overriding of the name_get defined in base in order to remove the old contact name
87     def name_get(self, cr, user, ids, context={}):
88         if not len(ids):
89             return []
90         res = []
91         for r in self.read(cr, user, ids, ['zip','city','partner_id', 'street']):
92             if context.get('contact_display', 'contact')=='partner' and r['partner_id']:
93                 res.append((r['id'], r['partner_id'][1]))
94             else:
95                 addr = str('')
96                 addr += "%s %s %s" % ( r.get('street', '') or '', r.get('zip', '') or '', r.get('city', '') or '' )
97                 res.append((r['id'], addr.strip() or '/'))
98         return res
99
100     _name = 'res.partner.address'
101     _inherit='res.partner.address'
102     _description ='Partner Address'
103     _columns = {
104         'job_id':fields.related('job_ids','contact_id','job_id',type='many2one', relation='res.partner.job', string='Main Job'),
105         'job_ids':fields.one2many('res.partner.job', 'address_id', 'Contacts'),
106     }
107 res_partner_address()
108
109 class res_partner_job(osv.osv):
110
111     def name_get(self, cr, uid, ids, context={}):
112         if not len(ids):
113             return []
114         res = []
115         for r in self.browse(cr, uid, ids):
116             funct = r.function_id and (", " + r.function_id.name) or ""
117             res.append((r.id, self.pool.get('res.partner.contact').name_get(cr, uid, [r.contact_id.id])[0][1] + funct))
118         return res
119
120     def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
121         job_ids = []
122         for arg in args:
123             if arg[0] == 'address_id':
124                 self._order = 'sequence_partner'
125             elif arg[0] == 'contact_id':
126                 self._order = 'sequence_contact'
127
128                 contact_obj = self.pool.get('res.partner.contact')
129                 search_arg = ['|', ('first_name', 'ilike', arg[2]), ('name', 'ilike', arg[2])]
130                 contact_ids = contact_obj.search(cr, user, search_arg, offset=offset, limit=limit, order=order, context=context, count=count)
131                 contacts = contact_obj.browse(cr, user, contact_ids, context=context)
132                 for contact in contacts:
133                     job_ids.extend([item.id for item in contact.job_ids])
134
135         res = super(res_partner_job,self).search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
136         if job_ids:
137             res = list(set(res + job_ids))
138
139         return res
140
141     _name = 'res.partner.job'
142     _description ='Contact Partner Function'
143     _order = 'sequence_contact'
144     _columns = {
145         'name': fields.related('address_id','partner_id', type='many2one', relation='res.partner', string='Partner'),
146         'address_id':fields.many2one('res.partner.address','Address'),
147         'contact_id':fields.many2one('res.partner.contact','Contact', required=True, ondelete='cascade'),
148         'function_id': fields.many2one('res.partner.function','Partner Function'),
149         'sequence_contact':fields.integer('Contact Seq.',help='Order of importance of this address in the list of addresses of the linked contact'),
150         'sequence_partner':fields.integer('Partner Seq.',help='Order of importance of this job title in the list of job title of the linked partner'),
151         'email': fields.char('E-Mail', size=240),
152         'phone': fields.char('Phone', size=64),
153         'fax': fields.char('Fax', size=64),
154         'extension': fields.char('Extension', size=64, help='Internal/External extension phone number'),
155         'other': fields.char('Other', size=64, help='Additional phone field'),
156         'date_start' : fields.date('Date Start'),
157         'date_stop' : fields.date('Date Stop'),
158         'state' : fields.selection([('past', 'Past'),('current', 'Current')], 'State', required=True),
159     }
160
161     _defaults = {
162         'sequence_contact' : lambda *a: 0,
163         'state' : lambda *a: 'current',
164     }
165 res_partner_job()
166
167
168 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
169