~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to base_partner_relation/partner_relation.py

  • Committer: Fabien Pinckaers
  • Date: 2008-11-12 06:43:12 UTC
  • Revision ID: fp@tinyerp.com-20081112064312-fp85io97i1e95tuz
moved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
1
2
##############################################################################
2
3
#
3
 
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
4
 
#
5
 
# $Id: partner.py 1007 2005-07-25 13:18:09Z kayhman $
6
 
#
7
 
# WARNING: This program as such is intended to be used by professional
8
 
# programmers who take the whole responsability of assessing all potential
9
 
# consequences resulting from its eventual inadequacies and bugs
10
 
# End users who are looking for a ready-to-use solution with commercial
11
 
# garantees and support are strongly adviced to contract a Free Software
12
 
# Service Company
13
 
#
14
 
# This program is Free Software; you can redistribute it and/or
15
 
# modify it under the terms of the GNU General Public License
16
 
# as published by the Free Software Foundation; either version 2
17
 
# of the License, or (at your option) any later version.
18
 
#
19
 
# This program is distributed in the hope that it will be useful,
20
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 
# GNU General Public License for more details.
23
 
#
24
 
# You should have received a copy of the GNU General Public License
25
 
# along with this program; if not, write to the Free Software
26
 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
20
#
28
21
##############################################################################
29
22
 
31
24
 
32
25
 
33
26
class res_partner_relation(osv.osv):
34
 
        _description='The partner object'
35
 
        _name = "res.partner.relation"
36
 
        _columns = {
37
 
                'name': fields.selection( [ ('default','Default'),('invoice','Invoice'), ('delivery','Delivery'), ('contact','Contact'), ('other','Other') ],'Relation Type', required=True),
38
 
                'partner_id': fields.many2one('res.partner', 'Main Partner', required=True, ondelete='cascade'),
39
 
                'relation_id': fields.many2one('res.partner', 'Relation Partner', required=True, ondelete='cascade')
40
 
        }
41
 
        _defaults = {
42
 
                'name' : lambda *a: 'invoice',
43
 
        }
 
27
    _description='The partner object'
 
28
    _name = "res.partner.relation"
 
29
    _columns = {
 
30
        'name': fields.selection( [ ('default','Default'),('invoice','Invoice'), ('delivery','Delivery'), ('contact','Contact'), ('other','Other') ],'Relation Type', required=True),
 
31
        'partner_id': fields.many2one('res.partner', 'Main Partner', required=True, ondelete='cascade'),
 
32
        'relation_id': fields.many2one('res.partner', 'Relation Partner', required=True, ondelete='cascade')
 
33
    }
 
34
    _defaults = {
 
35
        'name' : lambda *a: 'invoice',
 
36
    }
44
37
 
45
38
res_partner_relation()
46
39
 
47
40
 
48
41
class res_partner(osv.osv):
49
 
        _inherit = "res.partner"
50
 
        _columns = {
51
 
                'relation_ids': fields.one2many('res.partner.relation', 'partner_id', 'Relations')
52
 
        }
53
 
 
54
 
        def _is_related_to(self, cr, uid, ids, toid):
55
 
                related=[]
56
 
                for id in ids:
57
 
                        cr.execute("select id from res_partner_relation where (partner_id=%s and relation_id=%s) or (partner_id=%s and relation_id=%s)" % (id,toid,toid,id))
58
 
                        res=cr.fetchone()
59
 
                        if res and len(res):
60
 
                                related.append(True)
61
 
                        else:
62
 
                                related.append(False)
63
 
                return related
64
 
 
65
 
        def address_get(self, cr, uid, ids, adr_pref=['default']):
66
 
                todo = []
67
 
                result = {}
68
 
                cr.execute('select name,relation_id from res_partner_relation where partner_id in ('+','.join(map(str,ids))+')')
69
 
                adrs = dict(cr.fetchall())
70
 
                for adr in adr_pref:
71
 
                        if adr in adrs:
72
 
                                adr_prov = super(res_partner, self).address_get(cr, uid, [adrs[adr]], [adr]).values()[0]
73
 
                                result[adr] = adr_prov
74
 
                        else:
75
 
                                todo.append(adr)
76
 
                if len(todo):
77
 
                        result.update(super(res_partner, self).address_get(cr, uid, ids, todo))
78
 
                return result
 
42
    _inherit = "res.partner"
 
43
    _columns = {
 
44
        'relation_ids': fields.one2many('res.partner.relation', 'partner_id', 'Relations')
 
45
    }
 
46
    def _is_related_to(self, cr, uid, ids, toid):
 
47
        related=[]
 
48
        for id in ids:
 
49
            cr.execute("select id from res_partner_relation where (partner_id=%s and relation_id=%s) or (partner_id=%s and relation_id=%s)" % (id,toid,toid,id))
 
50
            res=cr.fetchone()
 
51
            if res and len(res):
 
52
                related.append(True)
 
53
            else:
 
54
                related.append(False)
 
55
        return related
 
56
 
 
57
    def address_get(self, cr, uid, ids, adr_pref=['default']):
 
58
        todo = []
 
59
        result = {}
 
60
        cr.execute('select name,relation_id from res_partner_relation where partner_id in ('+','.join(map(str,ids))+')')
 
61
        adrs = dict(cr.fetchall())
 
62
        for adr in adr_pref:
 
63
            if adr in adrs:
 
64
                adr_prov = super(res_partner, self).address_get(cr, uid, [adrs[adr]], [adr]).values()[0]
 
65
                result[adr] = adr_prov
 
66
            else:
 
67
                todo.append(adr)
 
68
        if len(todo):
 
69
            result.update(super(res_partner, self).address_get(cr, uid, ids, todo))
 
70
        return result
79
71
 
80
72
res_partner()
81
73
 
82
74
 
83
75
class PartnerAddress(osv.osv):
84
 
        _inherit = 'res.partner.address'
85
 
 
86
 
        def _where_calc(self, cursor, user, args, active_test=True, context=None):
87
 
                partner_obj = self.pool.get('res.partner')
88
 
 
89
 
                args = args[:]
90
 
 
91
 
                i = 0
92
 
                while i < len(args):
93
 
                        if args[i][0] == 'partner_id' and args[i][1] == '=':
94
 
                                partner = partner_obj.browse(cursor, user, args[i][2],
95
 
                                                context=context)
96
 
                                args[i] = ('partner_id', 'in', [args[i][2]] + [x.relation_id.id \
97
 
                                                for x in partner.relation_ids])
98
 
                        i += 1
99
 
                return super(PartnerAddress, self)._where_calc(cursor, user, args,
100
 
                                active_test=active_test, context=context)
101
 
 
 
76
    _inherit = 'res.partner.address'
 
77
    def _where_calc(self, cursor, user, args, active_test=True, context=None):
 
78
        if not args:
 
79
            args=[]
 
80
        partner_obj = self.pool.get('res.partner')
 
81
        args = args[:]
 
82
        i = 0
 
83
        while i < len(args):
 
84
            if type(args[i])==tuple:
 
85
                continue
 
86
            if args[i][0] == 'partner_id' and args[i][1] == '=':
 
87
                partner = partner_obj.browse(cursor, user, args[i][2],
 
88
                        context=context)
 
89
                args[i] = ('partner_id', 'in', [args[i][2]] + [x.relation_id.id \
 
90
                        for x in partner.relation_ids])
 
91
            i += 1
 
92
        return super(PartnerAddress, self)._where_calc(cursor, user, args,
 
93
                active_test=active_test, context=context)
102
94
PartnerAddress()
 
95
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
96