~hbrunn/+junk/partner_relations

« back to all changes in this revision

Viewing changes to partner_relations/model/res_partner.py

  • Committer: Holger Brunn
  • Date: 2014-06-02 19:28:04 UTC
  • Revision ID: hbrunn@therp.nl-20140602192804-9p0msf4nzwkarg30
[ADD] partner_relations

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    This module copyright (C) 2013 Therp BV (<http://therp.nl>).
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
#
 
20
##############################################################################
 
21
from openerp.osv.orm import Model
 
22
from openerp.osv import fields
 
23
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DATE_FMT
 
24
from datetime import datetime
 
25
 
 
26
 
 
27
class res_partner(Model):
 
28
    _inherit = 'res.partner'
 
29
 
 
30
    def _get_relation_ids(self, cr, uid, ids, field_name, arg,
 
31
                            context=None):
 
32
        cr.execute('''select id, left_partner_id, right_partner_id
 
33
            from res_partner_relation
 
34
            where left_partner_id in %s or right_partner_id in %s''',
 
35
                    (tuple(ids), tuple(ids)))
 
36
 
 
37
        result = dict([(i, []) for i in ids])
 
38
        for row in cr.fetchall():
 
39
            if row[1] in result:
 
40
                result[row[1]].append((1, row[0], {}))
 
41
            if row[2] in result:
 
42
                result[row[2]].append((1, row[0], {}))
 
43
 
 
44
        return result
 
45
 
 
46
    def _set_relation_ids(self, cr, uid, ids, field_name, field_value, arg,
 
47
                            context=None):
 
48
        if context is None:
 
49
            context = {}
 
50
 
 
51
        relation_obj = self.pool.get('res.partner.relation')
 
52
        for this in self.browse(cr, uid,
 
53
                                ids if isinstance(ids, list) else [ids],
 
54
                                context=context):
 
55
            context2 = self._update_context(context, ids)
 
56
            for value in field_value:
 
57
                if value[0] == 0:
 
58
                    relation_obj.create(cr, uid, value[2], context=context2)
 
59
                if value[0] == 1:
 
60
                    relation_obj.write(cr, uid, value[1], value[2],
 
61
                                       context=context2)
 
62
                if value[0] == 2:
 
63
                    relation_obj.unlink(cr, uid, value[1], context=context2)
 
64
 
 
65
    def _search_relation_id(self, cr, uid, obj, name, args, context=None):
 
66
        relation_type_ids = []
 
67
        for condition in args:
 
68
            if isinstance(condition, tuple) and condition[0] == name:
 
69
                if condition[1] == '=':
 
70
                    relation_type_ids.append(condition[2])
 
71
                # TODO: care cor other operators
 
72
        date = datetime.strptime(fields.date.context_today(self, cr, uid,
 
73
                                    context=context), DATE_FMT)
 
74
        cr.execute('''select %s_partner_id from res_partner_relation
 
75
            where res_partner_relation.type_id in %%(ids)s and
 
76
            (res_partner_relation.date_start is null or
 
77
            res_partner_relation.date_start < %%(date)s)
 
78
            and (res_partner_relation.date_end is null or
 
79
            res_partner_relation.date_end > %%(date)s)''' % (
 
80
                'right' if name == 'search_relation_id_inverse' else 'left'),
 
81
            {'ids': (tuple(relation_type_ids),), 'date': date})
 
82
        return [('id', 'in', [r[0] for r in cr.fetchall()])]
 
83
 
 
84
    def _search_related_partner_id(self, cr, uid, obj, name, args,
 
85
                                   context=None):
 
86
        partner_ids = []
 
87
        for condition in args:
 
88
            if isinstance(condition, tuple) and condition[0] == name:
 
89
                if condition[1] == '=':
 
90
                    partner_ids.append(condition[2])
 
91
                # TODO: care cor other operators
 
92
        date = datetime.strptime(fields.date.context_today(self, cr, uid,
 
93
                                    context=context), DATE_FMT)
 
94
        cr.execute('''select left_partner_id from res_partner_relation
 
95
            where right_partner_id in %(ids)s
 
96
            and (date_start is null or date_start < %(date)s)
 
97
            and (date_end is null or date_end > %(date)s)
 
98
            union
 
99
            select right_partner_id from res_partner_relation
 
100
            where left_partner_id in %(ids)s
 
101
            and (date_start is null or date_start < %(date)s)
 
102
            and (date_end is null or date_end > %(date)s)''',
 
103
            {'ids': tuple(partner_ids), 'date': date})
 
104
        return [('id', 'in', [r[0] for r in cr.fetchall()])]
 
105
 
 
106
    _columns = {
 
107
            'relation_ids': fields.function(
 
108
                _get_relation_ids,
 
109
                fnct_inv=_set_relation_ids,
 
110
                type='one2many', obj='res.partner.relation',
 
111
                string='Relations'),
 
112
            'search_relation_id': fields.function(
 
113
                lambda self, cr, uid, ids, *args: dict([
 
114
                    (i, False) for i in ids]),
 
115
                fnct_search=_search_relation_id,
 
116
                string='Has an actual relation of type',
 
117
                type='many2one', obj='res.partner.relation.type'),
 
118
            'search_relation_id_inverse': fields.function(
 
119
                lambda self, cr, uid, ids, *args: dict([
 
120
                    (i, False) for i in ids]),
 
121
                fnct_search=_search_relation_id,
 
122
                string='Has an actual relation of type',
 
123
                type='many2one', obj='res.partner.relation.type.inverse'),
 
124
            'search_related_partner_id': fields.function(
 
125
                lambda self, cr, uid, ids, *args: dict([
 
126
                    (i, False) for i in ids]),
 
127
                fnct_search=_search_related_partner_id,
 
128
                string='Has an actual relation with',
 
129
                type='many2one', obj='res.partner'),
 
130
            }
 
131
 
 
132
    def read(self, cr, uid, ids, fields=None, context=None,
 
133
             load='_classic_read'):
 
134
        return super(res_partner, self).read(
 
135
                cr, uid, ids, fields=fields,
 
136
                context=self._update_context(context, ids))
 
137
 
 
138
    def write(self, cr, uid, ids, vals, context=None):
 
139
        return super(res_partner, self).write(
 
140
                cr, uid, ids, vals, context=self._update_context(context, ids))
 
141
 
 
142
    def _update_context(self, context, ids):
 
143
        if context is None:
 
144
            context = {}
 
145
        ids = ids if isinstance(ids, list) else [ids] if ids else []
 
146
        result = context.copy()
 
147
        result.setdefault('active_id', ids[0] if ids else None)
 
148
        result.setdefault('active_ids', ids)
 
149
        result.setdefault('active_model', self._name)
 
150
        return result