- _description='Partner'
- _name = "res.partner"
- _order = "name"
- _columns = {
- 'name': fields.char('Name', size=128, required=True, select=True),
- 'date': fields.date('Date'),
- 'title': fields.selection(_partner_title_get, 'Title', size=32),
- 'parent_id': fields.many2one('res.partner','Main Company', select=True),
- 'child_ids': fields.one2many('res.partner', 'parent_id', 'Partner Ref.'),
- 'ref': fields.char('Code', size=64),
- 'lang': fields.selection(_lang_get, 'Language', size=5),
- 'user_id': fields.many2one('res.users', 'Salesman'),
- 'responsible': fields.many2one('res.users', 'Users'),
- 'vat': fields.char('VAT',size=32 ,help="Value Added Tax number"),
- 'bank_ids': fields.one2many('res.partner.bank', 'partner_id', 'Banks'),
- 'website': fields.char('Website',size=64),
- 'comment': fields.text('Notes'),
- 'address': fields.one2many('res.partner.address', 'partner_id', 'Contacts'),
- 'category_id': fields.many2many('res.partner.category', 'res_partner_category_rel', 'partner_id', 'category_id', 'Categories'),
- 'events': fields.one2many('res.partner.event', 'partner_id', 'events'),
- 'credit_limit': fields.float(string='Credit Limit'),
- 'ean13': fields.char('EAN13', size=13),
- 'active': fields.boolean('Active'),
- }
- _defaults = {
- 'active': lambda *a: 1,
- }
- _sql_constraints = [
- ('name_uniq', 'unique (name)', 'The name of the partner must be unique !')
- ]
-
- def copy(self, cr, uid, id, default=None, context={}):
- name = self.read(cr, uid, [id], ['name'])[0]['name']
- default.update({'name': name+' (copy)'})
- return super(res_partner, self).copy(cr, uid, id, default, context)
-
- def _check_ean_key(self, cr, uid, ids):
- for partner_o in pooler.get_pool(cr.dbname).get('res.partner').read(cr, uid, ids, ['ean13',]):
- thisean=partner_o['ean13']
- if thisean and thisean!='':
- if len(thisean)!=13:
- return False
- sum=0
- for i in range(12):
- if not (i % 2):
- sum+=int(thisean[i])
- else:
- sum+=3*int(thisean[i])
- if math.ceil(sum/10.0)*10-sum!=int(thisean[12]):
- return False
- return True
-
-# _constraints = [(_check_ean_key, 'Error: Invalid ean code', ['ean13'])]
-
- def name_get(self, cr, uid, ids, context={}):
- if not len(ids):
- return []
- if context.get('show_ref', False):
- rec_name = 'ref'
- else:
- rec_name = 'name'
-
- res = [(r['id'], r[rec_name]) for r in self.read(cr, uid, ids, [rec_name], context)]
- return res
-
- def name_search(self, cr, uid, name, args=[], operator='ilike', context={}, limit=80):
- if name:
- ids = self.search(cr, uid, [('ref', '=', name)] + args, limit=limit)
- if not ids:
- ids = self.search(cr, uid, [('name', operator, name)] + args, limit=limit)
- else:
- ids = self.search(cr, uid, args, limit=limit)
- return self.name_get(cr, uid, ids, context)
-
- def _email_send(self, cr, uid, ids, email_from, subject, body, on_error=None):
- partners = self.browse(cr, uid, ids)
- for partner in partners:
- if len(partner.address):
- if partner.address[0].email:
- tools.email_send(email_from, [partner.address[0].email], subject, body, on_error)
- return True
-
- def email_send(self, cr, uid, ids, email_from, subject, body, on_error=''):
- while len(ids):
- self.pool.get('ir.cron').create(cr, uid, {
- 'name': 'Send Partner Emails',
- 'user_id': uid,
-# 'nextcall': False,
- 'model': 'res.partner',
- 'function': '_email_send',
- 'args': repr([ids[:16], email_from, subject, body, on_error])
- })
- ids = ids[16:]
- return True
-
- def address_get(self, cr, uid, ids, adr_pref=['default']):
- cr.execute('select type,id from res_partner_address where partner_id in ('+','.join(map(str,ids))+')')
- res = cr.fetchall()
- adr = dict(res)
- # get the id of the (first) default address if there is one,
- # otherwise get the id of the first address in the list
- if res:
- default_address = adr.get('default', res[0][1])
- else:
- default_address = False
- result = {}
- for a in adr_pref:
- result[a] = adr.get(a, default_address)
- return result
-
- def gen_next_ref(self, cr, uid, ids):
- if len(ids) != 1:
- return True
-
- # compute the next number ref
- cr.execute("select ref from res_partner where ref is not null order by char_length(ref) desc, ref desc limit 1")
- res = cr.dictfetchall()
- ref = res and res[0]['ref'] or '0'
- try:
- nextref = int(ref)+1
- except e:
- raise osv.except_osv('Warning', "Couldn't generate the next id because some partners have an alphabetic id !")
-
- # update the current partner
- cr.execute("update res_partner set ref=%d where id=%d", (nextref, ids[0]))
- return True
+ _description='Partner'
+ _name = "res.partner"
+ _order = "name"
+ _columns = {
+ 'name': fields.char('Name', size=128, required=True, select=True),
+ 'date': fields.date('Date', select=1),
+ 'title': fields.selection(_partner_title_get, 'Title', size=32),
+ 'parent_id': fields.many2one('res.partner','Main Company', select=2),
+ 'child_ids': fields.one2many('res.partner', 'parent_id', 'Partner Ref.'),
+ 'ref': fields.char('Code', size=64),
+ '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."),
+ 'user_id': fields.many2one('res.users', 'Dedicated Salesman', help='The internal user that is in charge of communicating with this partner if any.'),
+ 'vat': fields.char('VAT',size=32 ,help="Value Added Tax number. Check the box if the partner is subjected to the VAT. Used by the VAT legal statement."),
+ 'bank_ids': fields.one2many('res.partner.bank', 'partner_id', 'Banks'),
+ 'website': fields.char('Website',size=64),
+ 'comment': fields.text('Notes'),
+ 'address': fields.one2many('res.partner.address', 'partner_id', 'Contacts'),
+ 'category_id': fields.many2many('res.partner.category', 'res_partner_category_rel', 'partner_id', 'category_id', 'Categories'),
+ 'events': fields.one2many('res.partner.event', 'partner_id', 'Events'),
+ 'credit_limit': fields.float(string='Credit Limit'),
+ 'ean13': fields.char('EAN13', size=13),
+ 'active': fields.boolean('Active'),
+ 'customer': fields.boolean('Customer', help="Check this box if the partner is a customer."),
+ 'supplier': fields.boolean('Supplier', help="Check this box if the partner is a supplier. If it's not checked, purchase people will not see it when encoding a purchase order."),
+ 'city':fields.related('address','city',type='char', string='City'),
+ 'country':fields.related('address','country_id',type='many2one', relation='res.country', string='Country'),
+ }
+
+ def _default_category(self, cr, uid, context={}):
+ if 'category_id' in context and context['category_id']:
+ return [context['category_id']]
+ return []
+
+ _defaults = {
+ 'active': lambda *a: 1,
+ 'customer': lambda *a: 1,
+ 'category_id': _default_category,
+ }
+ def copy(self, cr, uid, id, default=None, context={}):
+ name = self.read(cr, uid, [id], ['name'])[0]['name']
+ default.update({'name': name+' (copy)', 'events':[]})
+ return super(res_partner, self).copy(cr, uid, id, default, context)
+
+ def _check_ean_key(self, cr, uid, ids):
+ for partner_o in pooler.get_pool(cr.dbname).get('res.partner').read(cr, uid, ids, ['ean13',]):
+ thisean=partner_o['ean13']
+ if thisean and thisean!='':
+ if len(thisean)!=13:
+ return False
+ sum=0
+ for i in range(12):
+ if not (i % 2):
+ sum+=int(thisean[i])
+ else:
+ sum+=3*int(thisean[i])
+ if math.ceil(sum/10.0)*10-sum!=int(thisean[12]):
+ return False
+ return True
+
+# _constraints = [(_check_ean_key, 'Error: Invalid ean code', ['ean13'])]
+
+ def name_get(self, cr, uid, ids, context={}):
+ if not len(ids):
+ return []
+ if context.get('show_ref', False):
+ rec_name = 'ref'
+ else:
+ rec_name = 'name'
+
+ res = [(r['id'], r[rec_name]) for r in self.read(cr, uid, ids, [rec_name], context)]
+ return res
+
+ def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=80):
+ if not args:
+ args=[]
+ if not context:
+ context={}
+ if name:
+ ids = self.search(cr, uid, [('ref', '=', name)] + args, limit=limit, context=context)
+ if not ids:
+ ids = self.search(cr, uid, [('name', operator, name)] + args, limit=limit, context=context)
+ else:
+ ids = self.search(cr, uid, args, limit=limit, context=context)
+ return self.name_get(cr, uid, ids, context)
+
+ def _email_send(self, cr, uid, ids, email_from, subject, body, on_error=None):
+ partners = self.browse(cr, uid, ids)
+ for partner in partners:
+ if len(partner.address):
+ if partner.address[0].email:
+ tools.email_send(email_from, [partner.address[0].email], subject, body, on_error)
+ return True
+
+ def email_send(self, cr, uid, ids, email_from, subject, body, on_error=''):
+ while len(ids):
+ self.pool.get('ir.cron').create(cr, uid, {
+ 'name': 'Send Partner Emails',
+ 'user_id': uid,
+# 'nextcall': False,
+ 'model': 'res.partner',
+ 'function': '_email_send',
+ 'args': repr([ids[:16], email_from, subject, body, on_error])
+ })
+ ids = ids[16:]
+ return True
+
+ def address_get(self, cr, uid, ids, adr_pref=['default']):
+ cr.execute('select type,id from res_partner_address where partner_id in ('+','.join(map(str,map(int, ids)))+')')
+ res = cr.fetchall()
+ adr = dict(res)
+ # get the id of the (first) default address if there is one,
+ # otherwise get the id of the first address in the list
+ if res:
+ default_address = adr.get('default', res[0][1])
+ else:
+ default_address = False
+ result = {}
+ for a in adr_pref:
+ result[a] = adr.get(a, default_address)
+ return result
+
+ def gen_next_ref(self, cr, uid, ids):
+ if len(ids) != 1:
+ return True
+
+ # compute the next number ref
+ cr.execute("select ref from res_partner where ref is not null order by char_length(ref) desc, ref desc limit 1")
+ res = cr.dictfetchall()
+ ref = res and res[0]['ref'] or '0'
+ try:
+ nextref = int(ref)+1
+ except:
+ raise osv.except_osv(_('Warning'), _("Couldn't generate the next id because some partners have an alphabetic id !"))
+
+ # update the current partner
+ cr.execute("update res_partner set ref=%s where id=%s", (nextref, ids[0]))
+ return True
+
+ def view_header_get(self, cr, uid, view_id, view_type, context):
+ res = super(res_partner, self).view_header_get(cr, uid, view_id, view_type, context)
+ if res: return res
+ if (not context.get('category_id', False)):
+ return False
+ return _('Partners: ')+self.pool.get('res.partner.category').browse(cr, uid, context['category_id'], context).name
+