1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6
# Copyright (C) 2013 initOS GmbH & Co. KG (<http://www.initos.com>).
7
# Author Thomas Rehn <thomas.rehn at initos.com>
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU Affero General Public License as
11
# published by the Free Software Foundation, either version 3 of the
12
# License, or (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU Affero General Public License for more details.
19
# You should have received a copy of the GNU Affero General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
##############################################################################
24
from openerp.osv import orm, fields
27
class ResPartner(orm.Model):
28
"""Assigns 'ref' from a sequence on creation and copying"""
30
_inherit = 'res.partner'
32
def create(self, cr, uid, vals, context=None):
33
context = context or {}
34
if not vals.get('ref') and self._needsRef(cr, uid, vals=vals,
36
vals['ref'] = self.pool.get('ir.sequence')\
37
.next_by_code(cr, uid, 'res.partner')
38
return super(ResPartner, self).create(cr, uid, vals, context)
40
def copy(self, cr, uid, id, default=None, context=None):
41
default = default or {}
42
if self._needsRef(cr, uid, id=id, context=context):
43
default['ref'] = self.pool.get('ir.sequence')\
44
.next_by_code(cr, uid, 'res.partner',
46
return super(ResPartner, self).copy(cr, uid, id, default,
49
def _needsRef(self, cr, uid, id=None, vals=None, context=None):
51
Checks whether a sequence value should be assigned to a partner's 'ref'
53
:param cr: database cursor
54
:param uid: current user id
55
:param id: id of the partner object
56
:param vals: known field values of the partner object
57
:return: true iff a sequence value should be assigned to the\
60
if not vals and not id:
61
raise Exception('Either field values or an id must be provided.')
62
# only assign a 'ref' to commercial partners
64
vals = self.read(cr, uid, id, ['parent_id', 'is_company'],
66
return vals.get('is_company') or not vals.get('parent_id')
69
'ref': fields.char('Reference', size=64, readonly=True),
72
def _commercial_fields(self, cr, uid, context=None):
74
Make the partner reference a field that is propagated
75
to the partner's contacts
77
return super(ResPartner, self)._commercial_fields(
78
cr, uid, context=context) + ['ref']
80
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: