1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
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
##############################################################################
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
27
class res_partner(Model):
28
_inherit = 'res.partner'
30
def _get_relation_ids(self, cr, uid, ids, field_name, arg,
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)))
37
result = dict([(i, []) for i in ids])
38
for row in cr.fetchall():
40
result[row[1]].append((1, row[0], {}))
42
result[row[2]].append((1, row[0], {}))
46
def _set_relation_ids(self, cr, uid, ids, field_name, field_value, arg,
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],
55
context2 = self._update_context(context, ids)
56
for value in field_value:
58
relation_obj.create(cr, uid, value[2], context=context2)
60
relation_obj.write(cr, uid, value[1], value[2],
63
relation_obj.unlink(cr, uid, value[1], context=context2)
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()])]
84
def _search_related_partner_id(self, cr, uid, obj, name, args,
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)
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()])]
107
'relation_ids': fields.function(
109
fnct_inv=_set_relation_ids,
110
type='one2many', obj='res.partner.relation',
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'),
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))
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))
142
def _update_context(self, context, ids):
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)