1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ##############################################################################
33 from osv import fields,osv
38 class res_partner_function(osv.osv):
39 _name = 'res.partner.function'
40 _description = 'Function of the contact'
42 'name': fields.char('Function name', size=64, required=True),
43 'code': fields.char('Code', size=8),
46 res_partner_function()
49 class res_payterm(osv.osv):
50 _description = 'Payment term'
53 'name': fields.char('Payment term (short name)', size=64),
57 class res_partner_category(osv.osv):
58 def name_get(self, cr, uid, ids, context={}):
61 reads = self.read(cr, uid, ids, ['name','parent_id'], context)
65 if record['parent_id']:
66 name = record['parent_id'][1]+' / '+name
67 res.append((record['id'], name))
70 def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, unknow_dict):
71 res = self.name_get(cr, uid, ids)
73 def _check_recursion(self, cr, uid, ids):
76 cr.execute('select distinct parent_id from res_partner_category where id in ('+','.join(map(str,ids))+')')
77 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
83 _description='Partner Categories'
84 _name = 'res.partner.category'
86 'name': fields.char('Category Name', required=True, size=64, translate=True),
87 'parent_id': fields.many2one('res.partner.category', 'Parent Category', select=True),
88 'complete_name': fields.function(_name_get_fnc, method=True, type="char", string='Name'),
89 'child_ids': fields.one2many('res.partner.category', 'parent_id', 'Childs Category'),
90 'active' : fields.boolean('Active', help="The active field allows you to hide the category, without removing it."),
93 (_check_recursion, 'Error ! You can not create recursive categories.', ['parent_id'])
96 'active' : lambda *a: 1,
98 _order = 'parent_id,name'
99 res_partner_category()
101 class res_partner_title(osv.osv):
102 _name = 'res.partner.title'
104 'name': fields.char('Title', required=True, size=46, translate=True),
105 'shortcut': fields.char('Shortcut', required=True, size=16),
106 'domain': fields.selection([('partner','Partner'),('contact','Contact')], 'Domain', required=True, size=24)
111 def _contact_title_get(self, cr, uid, context={}):
112 obj = self.pool.get('res.partner.title')
113 ids = obj.search(cr, uid, [('domain', '=', 'contact')])
114 res = obj.read(cr, uid, ids, ['shortcut','name'], context)
115 return [(r['shortcut'], r['name']) for r in res]
117 def _partner_title_get(self, cr, uid, context={}):
118 obj = self.pool.get('res.partner.title')
119 ids = obj.search(cr, uid, [('domain', '=', 'partner')])
120 res = obj.read(cr, uid, ids, ['shortcut','name'], context)
121 return [(r['shortcut'], r['name']) for r in res]
123 def _lang_get(self, cr, uid, context={}):
124 obj = self.pool.get('res.lang')
125 ids = obj.search(cr, uid, [], context=context)
126 res = obj.read(cr, uid, ids, ['code', 'name'], context)
127 return [(r['code'], r['name']) for r in res]
130 class res_partner(osv.osv):
131 _description='Partner'
132 _name = "res.partner"
135 'name': fields.char('Name', size=128, required=True, select=True),
136 'date': fields.date('Date', select=1),
137 'title': fields.selection(_partner_title_get, 'Title', size=32),
138 'parent_id': fields.many2one('res.partner','Main Company', select=2),
139 'child_ids': fields.one2many('res.partner', 'parent_id', 'Partner Ref.'),
140 'ref': fields.char('Code', size=64),
141 'lang': fields.selection(_lang_get, 'Language', size=5, help="If the selected language is loaded in the system, all documents related to this partner will be printed in this language. If not, it will be english."),
142 'user_id': fields.many2one('res.users', 'Dedicated Salesman', help='The internal user that is in charge of communicating with this partner if any.'),
143 'responsible': fields.many2one('res.users', 'Users'),
144 'vat': fields.char('VAT',size=32 ,help="Value Added Tax number"),
145 'bank_ids': fields.one2many('res.partner.bank', 'partner_id', 'Banks'),
146 'website': fields.char('Website',size=64),
147 'comment': fields.text('Notes'),
148 'address': fields.one2many('res.partner.address', 'partner_id', 'Contacts'),
149 'category_id': fields.many2many('res.partner.category', 'res_partner_category_rel', 'partner_id', 'category_id', 'Categories'),
150 'events': fields.one2many('res.partner.event', 'partner_id', 'Events'),
151 'credit_limit': fields.float(string='Credit Limit'),
152 'ean13': fields.char('EAN13', size=13),
153 'active': fields.boolean('Active'),
154 'customer': fields.boolean('Customer'),
155 'supplier': fields.boolean('Supplier'),
156 'city':fields.related('address','city',type='char', string='City'),
157 'country':fields.related('address','country_id','name',type='char', string='Country'),
160 'active': lambda *a: 1,
161 'customer': lambda *a: 1,
164 ('name_uniq', 'unique (name)', 'The name of the partner must be unique !')
167 def copy(self, cr, uid, id, default=None, context={}):
168 name = self.read(cr, uid, [id], ['name'])[0]['name']
169 default.update({'name': name+' (copy)'})
170 return super(res_partner, self).copy(cr, uid, id, default, context)
172 def _check_ean_key(self, cr, uid, ids):
173 for partner_o in pooler.get_pool(cr.dbname).get('res.partner').read(cr, uid, ids, ['ean13',]):
174 thisean=partner_o['ean13']
175 if thisean and thisean!='':
183 sum+=3*int(thisean[i])
184 if math.ceil(sum/10.0)*10-sum!=int(thisean[12]):
188 # _constraints = [(_check_ean_key, 'Error: Invalid ean code', ['ean13'])]
190 def name_get(self, cr, uid, ids, context={}):
193 if context.get('show_ref', False):
198 res = [(r['id'], r[rec_name]) for r in self.read(cr, uid, ids, [rec_name], context)]
201 def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=80):
207 ids = self.search(cr, uid, [('ref', '=', name)] + args, limit=limit, context=context)
209 ids = self.search(cr, uid, [('name', operator, name)] + args, limit=limit, context=context)
211 ids = self.search(cr, uid, args, limit=limit, context=context)
212 return self.name_get(cr, uid, ids, context)
214 def _email_send(self, cr, uid, ids, email_from, subject, body, on_error=None):
215 partners = self.browse(cr, uid, ids)
216 for partner in partners:
217 if len(partner.address):
218 if partner.address[0].email:
219 tools.email_send(email_from, [partner.address[0].email], subject, body, on_error)
222 def email_send(self, cr, uid, ids, email_from, subject, body, on_error=''):
224 self.pool.get('ir.cron').create(cr, uid, {
225 'name': 'Send Partner Emails',
228 'model': 'res.partner',
229 'function': '_email_send',
230 'args': repr([ids[:16], email_from, subject, body, on_error])
235 def address_get(self, cr, uid, ids, adr_pref=['default']):
236 cr.execute('select type,id from res_partner_address where partner_id in ('+','.join(map(str,ids))+')')
239 # get the id of the (first) default address if there is one,
240 # otherwise get the id of the first address in the list
242 default_address = adr.get('default', res[0][1])
244 default_address = False
247 result[a] = adr.get(a, default_address)
250 def gen_next_ref(self, cr, uid, ids):
254 # compute the next number ref
255 cr.execute("select ref from res_partner where ref is not null order by char_length(ref) desc, ref desc limit 1")
256 res = cr.dictfetchall()
257 ref = res and res[0]['ref'] or '0'
261 raise osv.except_osv(_('Warning'), _("Couldn't generate the next id because some partners have an alphabetic id !"))
263 # update the current partner
264 cr.execute("update res_partner set ref=%d where id=%d", (nextref, ids[0]))
267 def view_header_get(self, cr, uid, view_id, view_type, context):
268 res = super(res_partner, self).view_header_get(cr, uid, view_id, view_type, context)
270 if (not context.get('category_id', False)):
272 return _('Partners: ')+self.pool.get('res.partner.category').browse(cr, uid, context['category_id'], context).name
276 class res_partner_address(osv.osv):
277 _description ='Partner Addresses'
278 _name = 'res.partner.address'
281 'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade', select=True),
282 'type': fields.selection( [ ('default','Default'),('invoice','Invoice'), ('delivery','Delivery'), ('contact','Contact'), ('other','Other') ],'Address Type'),
283 'function': fields.many2one('res.partner.function', 'Function'),
284 'title': fields.selection(_contact_title_get, 'Title', size=32),
285 'name': fields.char('Contact Name', size=64),
286 'street': fields.char('Street', size=128),
287 'street2': fields.char('Street2', size=128),
288 'zip': fields.char('Zip', change_default=True, size=24),
289 'city': fields.char('City', size=128),
290 'state_id': fields.many2one("res.country.state", 'State', change_default=True, domain="[('country_id','=',country_id)]"),
291 'country_id': fields.many2one('res.country', 'Country', change_default=True),
292 'email': fields.char('E-Mail', size=240),
293 'phone': fields.char('Phone', size=64),
294 'fax': fields.char('Fax', size=64),
295 'mobile': fields.char('Mobile', size=64),
296 'birthdate': fields.char('Birthdate', size=64),
297 'active': fields.boolean('Active'),
300 'active': lambda *a: 1,
303 def name_get(self, cr, user, ids, context={}):
307 for r in self.read(cr, user, ids, ['name','zip','city','partner_id', 'street']):
308 if context.get('contact_display', 'contact')=='partner':
309 res.append((r['id'], r['partner_id'][1]))
311 addr = str(r['name'] or '')
312 if r['name'] and (r['zip'] or r['city']):
314 addr += str(r['street'] or '') + ' ' + str(r['zip'] or '') + ' ' + str(r['city'] or '')
315 res.append((r['id'], addr.strip() or '/'))
318 def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
323 if context.get('contact_display', 'contact')=='partner':
324 ids = self.search(cr, user, [('partner_id',operator,name)], limit=limit, context=context)
326 ids = self.search(cr, user, [('zip','=',name)] + args, limit=limit, context=context)
328 ids = self.search(cr, user, [('city',operator,name)] + args, limit=limit, context=context)
330 ids += self.search(cr, user, [('name',operator,name)] + args, limit=limit, context=context)
331 ids += self.search(cr, user, [('partner_id',operator,name)] + args, limit=limit, context=context)
332 return self.name_get(cr, user, ids, context=context)
333 res_partner_address()
335 class res_partner_bank_type(osv.osv):
336 _description='Bank Account Type'
337 _name = 'res.partner.bank.type'
339 'name': fields.char('Name', size=64, required=True),
340 'code': fields.char('Code', size=64, required=True),
341 'field_ids': fields.one2many('res.partner.bank.type.field', 'bank_type_id', 'Type fields'),
343 res_partner_bank_type()
345 class res_partner_bank_type_fields(osv.osv):
346 _description='Bank type fields'
347 _name = 'res.partner.bank.type.field'
349 'name': fields.char('Field name', size=64, required=True),
350 'bank_type_id': fields.many2one('res.partner.bank.type', 'Bank type', required=True, ondelete='cascade'),
351 'required': fields.boolean('Required'),
352 'readonly': fields.boolean('Readonly'),
353 'size': fields.integer('Max. Size'),
355 res_partner_bank_type_fields()
358 class res_partner_bank(osv.osv):
360 _name = "res.partner.bank"
362 _description = __doc__
365 def _bank_type_get(self, cr, uid, context=None):
366 bank_type_obj = self.pool.get('res.partner.bank.type')
369 type_ids = bank_type_obj.search(cr, uid, [])
370 bank_types = bank_type_obj.browse(cr, uid, type_ids)
371 for bank_type in bank_types:
372 result.append((bank_type.code, bank_type.name))
375 def _default_value(self, cursor, user, field, context=None):
376 if field in ('country_id', 'state_id'):
380 if not context.get('address', False):
382 for ham, spam, address in context['address']:
383 if address.get('type', False) == 'default':
384 return address.get(field, value)
385 elif not address.get('type', False):
386 value = address.get(field, value)
390 'name': fields.char('Description', size=128),
391 'acc_number': fields.char('Account number', size=64, required=False),
392 'bank': fields.many2one('res.bank', 'Bank'),
393 'owner_name': fields.char('Account owner', size=64),
394 'street': fields.char('Street', size=128),
395 'zip': fields.char('Zip', change_default=True, size=24),
396 'city': fields.char('City', size=128),
397 'country_id': fields.many2one('res.country', 'Country',
398 change_default=True),
399 'state_id': fields.many2one("res.country.state", 'State',
400 change_default=True, domain="[('country_id','=',country_id)]"),
401 'partner_id': fields.many2one('res.partner', 'Partner', required=True,
402 ondelete='cascade', select=True),
403 'state': fields.selection(_bank_type_get, 'Bank type', required=True,
404 change_default=True),
405 'sequence': fields.integer('Sequence'),
406 'state_id': fields.many2one('res.country.state', 'State',
407 domain="[('country_id', '=', country_id)]"),
410 'owner_name': lambda obj, cursor, user, context: obj._default_value(
411 cursor, user, 'name', context=context),
412 'street': lambda obj, cursor, user, context: obj._default_value(
413 cursor, user, 'street', context=context),
414 'city': lambda obj, cursor, user, context: obj._default_value(
415 cursor, user, 'city', context=context),
416 'zip': lambda obj, cursor, user, context: obj._default_value(
417 cursor, user, 'zip', context=context),
418 'country_id': lambda obj, cursor, user, context: obj._default_value(
419 cursor, user, 'country_id', context=context),
420 'state_id': lambda obj, cursor, user, context: obj._default_value(
421 cursor, user, 'state_id', context=context),
424 def fields_get(self, cr, uid, fields=None, context=None):
425 res = super(res_partner_bank, self).fields_get(cr, uid, fields, context)
426 bank_type_obj = self.pool.get('res.partner.bank.type')
427 type_ids = bank_type_obj.search(cr, uid, [])
428 types = bank_type_obj.browse(cr, uid, type_ids)
430 for field in type.field_ids:
431 if field.name in res:
432 res[field.name].setdefault('states', {})
433 res[field.name]['states'][type.code] = [
434 ('readonly', field.readonly),
435 ('required', field.required)]
438 def name_get(self, cr, uid, ids, context=None):
442 for id in self.browse(cr, uid, ids):
443 res.append((id.id,id.acc_number))
450 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: