~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to analytic_distribution/destination_tools.py

  • Committer: jf
  • Date: 2011-05-16 09:55:17 UTC
  • mfrom: (129.1.1 unifield-wm)
  • Revision ID: jf@tempo4-20110516095517-giuzv2mouka39jb8
UF-270 Advance return in a currency that is not the functional currency

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
 
3
 
from osv import fields, osv
4
 
import warnings
5
 
 
6
 
 
7
 
class many2many_sorted(fields.many2many):
8
 
    def __init__(self, obj, rel, id1, id2, string='unknown', limit=None, **args):
9
 
        super(many2many_sorted, self).__init__(obj, rel, id1, id2, string, limit, **args)
10
 
 
11
 
    def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
12
 
        if not context:
13
 
            context = {}
14
 
        if not values:
15
 
            values = {}
16
 
        res = {}
17
 
        if not ids:
18
 
            return res
19
 
        for id in ids:
20
 
            res[id] = []
21
 
        if offset:
22
 
            warnings.warn("Specifying offset at a many2many.get() may produce unpredictable results.",
23
 
                      DeprecationWarning, stacklevel=2)
24
 
        obj = obj.pool.get(self._obj)
25
 
 
26
 
        # static domains are lists, and are evaluated both here and on client-side, while string
27
 
        # domains supposed by dynamic and evaluated on client-side only (thus ignored here)
28
 
        # FIXME: make this distinction explicit in API!
29
 
        domain = isinstance(self._domain, list) and self._domain or []
30
 
 
31
 
        wquery = obj._where_calc(cr, user, domain, context=context)
32
 
        obj._apply_ir_rules(cr, user, wquery, 'read', context=context)
33
 
        from_c, where_c, where_params = wquery.get_sql()
34
 
        if where_c:
35
 
            where_c = ' AND ' + where_c
36
 
 
37
 
        order_by = ''
38
 
        rel_obj = obj.pool.get('account.destination.link')
39
 
        if rel_obj._order:
40
 
            order_by = ' ORDER BY '
41
 
            order_tab = []
42
 
            for order in rel_obj._order.split(','):
43
 
                order_tab.append('%s.%s' %(from_c, order.strip()))
44
 
            order_by += ','.join(order_tab)
45
 
 
46
 
        limit_str = ''
47
 
        if self._limit is not None:
48
 
            limit_str = ' LIMIT %d' % self._limit
49
 
 
50
 
        query = 'SELECT %(rel)s.%(id2)s, %(rel)s.%(id1)s \
51
 
                   FROM %(rel)s, %(from_c)s \
52
 
                  WHERE %(rel)s.%(id1)s IN %%s \
53
 
                    AND %(rel)s.%(id2)s = %(tbl)s.id \
54
 
                 %(where_c)s  \
55
 
                 %(order_by)s \
56
 
                 %(limit)s \
57
 
                 OFFSET %(offset)d' \
58
 
            % {'rel': self._rel,
59
 
               'from_c': from_c,
60
 
               'tbl': obj._table,
61
 
               'id1': self._id1,
62
 
               'id2': self._id2,
63
 
               'where_c': where_c,
64
 
               'limit': limit_str,
65
 
               'order_by': order_by,
66
 
               'offset': offset,
67
 
              }
68
 
        cr.execute(query, [tuple(ids),] + where_params)
69
 
        for r in cr.fetchall():
70
 
            res[r[1]].append(r[0])
71
 
        return res