1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 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 ##############################################################################
24 from osv import fields,osv
27 from tools.translate import _
29 class res_payterm(osv.osv):
30 _description = 'Payment term'
34 'name': fields.char('Payment Term (short name)', size=64),
38 class res_partner_category(osv.osv):
40 def name_get(self, cr, uid, ids, context=None):
41 """Return the categories' display name, including their direct
44 :param dict context: the ``partner_category_display`` key can be
45 used to select the short version of the
46 category name (without the direct parent),
47 when set to ``'short'``. The default is
51 if context.get('partner_category_display') == 'short':
52 return super(res_partner_category, self).name_get(cr, uid, ids, context=context)
53 reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
57 if record['parent_id']:
58 name = record['parent_id'][1]+' / '+name
59 res.append((record['id'], name))
62 def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
68 # Be sure name_search is symetric to name_get
69 name = name.split(' / ')[-1]
70 ids = self.search(cr, uid, [('name', operator, name)] + args, limit=limit, context=context)
72 ids = self.search(cr, uid, args, limit=limit, context=context)
73 return self.name_get(cr, uid, ids, context)
76 def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
77 res = self.name_get(cr, uid, ids, context=context)
80 _description='Partner Categories'
81 _name = 'res.partner.category'
83 'name': fields.char('Category Name', required=True, size=64, translate=True),
84 'parent_id': fields.many2one('res.partner.category', 'Parent Category', select=True, ondelete='cascade'),
85 'complete_name': fields.function(_name_get_fnc, type="char", string='Full Name'),
86 'child_ids': fields.one2many('res.partner.category', 'parent_id', 'Child Categories'),
87 'active' : fields.boolean('Active', help="The active field allows you to hide the category without removing it."),
88 'parent_left' : fields.integer('Left parent', select=True),
89 'parent_right' : fields.integer('Right parent', select=True),
90 'partner_ids': fields.many2many('res.partner', 'res_partner_category_rel', 'category_id', 'partner_id', 'Partners'),
93 (osv.osv._check_recursion, 'Error ! You can not create recursive categories.', ['parent_id'])
96 'active' : lambda *a: 1,
99 _parent_order = 'name'
100 _order = 'parent_left'
101 res_partner_category()
103 class res_partner_title(osv.osv):
104 _name = 'res.partner.title'
106 'name': fields.char('Title', required=True, size=46, translate=True),
107 'shortcut': fields.char('Shortcut', required=True, size=16, translate=True),
108 'domain': fields.selection([('partner','Partner'),('contact','Contact')], 'Domain', required=True, size=24)
113 def _lang_get(self, cr, uid, context=None):
114 obj = self.pool.get('res.lang')
115 ids = obj.search(cr, uid, [], context=context)
116 res = obj.read(cr, uid, ids, ['code', 'name'], context)
117 return [(r['code'], r['name']) for r in res] + [('','')]
120 class res_partner(osv.osv):
121 _description='Partner'
122 _name = "res.partner"
125 'name': fields.char('Name', size=128, required=True, select=True),
126 'date': fields.date('Date', select=1),
127 'title': fields.many2one('res.partner.title','Partner Firm'),
128 'parent_id': fields.many2one('res.partner','Parent Partner'),
129 'child_ids': fields.one2many('res.partner', 'parent_id', 'Partner Ref.'),
130 'ref': fields.char('Reference', size=64, select=1),
131 'lang': fields.selection(_lang_get, 'Language', 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."),
132 'user_id': fields.many2one('res.users', 'Salesman', help='The internal user that is in charge of communicating with this partner if any.'),
133 '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."),
134 'bank_ids': fields.one2many('res.partner.bank', 'partner_id', 'Banks'),
135 'website': fields.char('Website',size=64, help="Website of Partner."),
136 'comment': fields.text('Notes'),
137 'category_id': fields.many2many('res.partner.category', 'res_partner_category_rel', 'partner_id', 'category_id', 'Categories'),
138 'events': fields.one2many('res.partner.event', 'partner_id', 'Events'),
139 'credit_limit': fields.float(string='Credit Limit'),
140 'ean13': fields.char('EAN13', size=13),
141 'customer': fields.boolean('Customer', help="Check this box if the partner is a customer."),
142 '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."),
143 'employee': fields.boolean('Employee', help="Check this box if the partner is an Employee."),
144 'color': fields.integer('Color Index'),
145 'type': fields.selection( [ ('default','Default'),('invoice','Invoice'), ('delivery','Delivery'), ('contact','Contact'), ('other','Other') ],'Address Type', help="Used to select automatically the right address according to the context in sales and purchases documents."),
146 'function': fields.char('Function', size=128),
147 'street': fields.char('Street', size=128),
148 'street2': fields.char('Street2', size=128),
149 'zip': fields.char('Zip', change_default=True, size=24),
150 'city': fields.char('City', size=128),
151 'state_id': fields.many2one("res.country.state", 'Fed. State', domain="[('country_id','=',country_id)]"),
152 'country_id': fields.many2one('res.country', 'Country'),
153 'email': fields.char('E-Mail', size=240),
154 'phone': fields.char('Phone', size=64),
155 'fax': fields.char('Fax', size=64),
156 'mobile': fields.char('Mobile', size=64),
157 'birthdate': fields.char('Birthdate', size=64),
158 'active': fields.boolean('Active', help="Uncheck the active field to hide the contact."),
159 # 'company_id': fields.related('partner_id','company_id',type='many2one',relation='res.company',string='Company', store=True),
160 'company_id': fields.many2one('res.company', 'Company',select=1),
161 'is_company': fields.boolean('Company', help="Check the field to create company otherwise it is personal contacts"),
162 'color': fields.integer('Color Index'),
164 def _default_category(self, cr, uid, context=None):
167 if 'category_id' in context and context['category_id']:
168 return [context['category_id']]
172 'active': lambda *a: 1,
173 'customer': lambda *a: 1,
174 'category_id': _default_category,
175 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'res.partner', context=c),
177 'is_company': lambda *a: 0,
181 def copy(self, cr, uid, id, default=None, context=None):
184 name = self.read(cr, uid, [id], ['name'], context)[0]['name']
185 default.update({'name': name+ _(' (copy)'), 'events':[]})
186 return super(res_partner, self).copy(cr, uid, id, default, context)
188 def do_share(self, cr, uid, ids, *args):
191 def _check_ean_key(self, cr, uid, ids, context=None):
192 for partner_o in pooler.get_pool(cr.dbname).get('res.partner').read(cr, uid, ids, ['ean13',]):
193 thisean=partner_o['ean13']
194 if thisean and thisean!='':
202 sum+=3*int(thisean[i])
203 if math.ceil(sum/10.0)*10-sum!=int(thisean[12]):
207 # _constraints = [(_check_ean_key, 'Error: Invalid ean code', ['ean13'])]
209 def name_get(self, cr, uid, ids, context=None):
214 if context.get('show_ref'):
219 res = [(r['id'], r[rec_name]) for r in self.read(cr, uid, ids, [rec_name], context)]
222 def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
225 # short-circuit ref match when possible
226 if name and operator in ('=', 'ilike', '=ilike', 'like'):
227 ids = self.search(cr, uid, [('ref', '=', name)] + args, limit=limit, context=context)
229 return self.name_get(cr, uid, ids, context)
230 return super(res_partner,self).name_search(cr, uid, name, args, operator=operator, context=context, limit=limit)
232 def _email_send(self, cr, uid, ids, email_from, subject, body, on_error=None):
233 partners = self.browse(cr, uid, ids)
234 for partner in partners:
235 if len(partner.address):
236 if partner.address[0].email:
237 tools.email_send(email_from, [partner.address[0].email], subject, body, on_error)
240 def email_send(self, cr, uid, ids, email_from, subject, body, on_error=''):
242 self.pool.get('ir.cron').create(cr, uid, {
243 'name': 'Send Partner Emails',
246 'model': 'res.partner',
247 'function': '_email_send',
248 'args': repr([ids[:16], email_from, subject, body, on_error])
253 def address_get(self, cr, uid, ids, adr_pref=None):
255 adr_pref = ['default']
256 address_ids = self.search(cr, uid, [('partner_id', 'in', ids)])
257 address_rec = self.read(cr, uid, address_ids, ['type'])
258 res = list((addr['type'],addr['id']) for addr in address_rec)
260 # get the id of the (first) default address if there is one,
261 # otherwise get the id of the first address in the list
263 default_address = adr.get('default', res[0][1])
265 default_address = False
268 result[a] = adr.get(a, default_address)
271 def gen_next_ref(self, cr, uid, ids):
275 # compute the next number ref
276 cr.execute("select ref from res_partner where ref is not null order by char_length(ref) desc, ref desc limit 1")
277 res = cr.dictfetchall()
278 ref = res and res[0]['ref'] or '0'
282 raise osv.except_osv(_('Warning'), _("Couldn't generate the next id because some partners have an alphabetic id !"))
284 # update the current partner
285 cr.execute("update res_partner set ref=%s where id=%s", (nextref, ids[0]))
288 def view_header_get(self, cr, uid, view_id, view_type, context):
289 res = super(res_partner, self).view_header_get(cr, uid, view_id, view_type, context)
291 if (not context.get('category_id', False)):
293 return _('Partners: ')+self.pool.get('res.partner.category').browse(cr, uid, context['category_id'], context).name
294 def main_partner(self, cr, uid):
295 ''' Return the id of the main partner
297 model_data = self.pool.get('ir.model.data')
298 return model_data.browse(
300 model_data.search(cr, uid, [('module','=','base'),
301 ('name','=','main_partner')])[0],
306 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: