~savoirfairelinux-openerp/partner-contact-management/base_contact_by_functions

« back to all changes in this revision

Viewing changes to base_partner_sequence/partner.py

[MRG] Merge with upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
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>
 
8
#
 
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.
 
13
#
 
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.
 
18
#
 
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/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from openerp.osv import orm, fields
 
25
 
 
26
 
 
27
class ResPartner(orm.Model):
 
28
    """Assigns 'ref' from a sequence on creation and copying"""
 
29
 
 
30
    _inherit = 'res.partner'
 
31
 
 
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,
 
35
                                                  context=context):
 
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)
 
39
 
 
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',
 
45
                                                    context=context)
 
46
        return super(ResPartner, self).copy(cr, uid, id, default,
 
47
                                            context=context)
 
48
 
 
49
    def _needsRef(self, cr, uid, id=None, vals=None, context=None):
 
50
        """
 
51
        Checks whether a sequence value should be assigned to a partner's 'ref'
 
52
 
 
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\
 
58
                      partner's 'ref'
 
59
        """
 
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
 
63
        if id:
 
64
            vals = self.read(cr, uid, id, ['parent_id', 'is_company'],
 
65
                             context=context)
 
66
        return vals.get('is_company') or not vals.get('parent_id')
 
67
 
 
68
    _columns = {
 
69
        'ref': fields.char('Reference', size=64, readonly=True),
 
70
    }
 
71
 
 
72
    def _commercial_fields(self, cr, uid, context=None):
 
73
        """
 
74
        Make the partner reference a field that is propagated
 
75
        to the partner's contacts
 
76
        """
 
77
        return super(ResPartner, self)._commercial_fields(
 
78
            cr, uid, context=context) + ['ref']
 
79
 
 
80
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: