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

951 by jf
Financing contrat: Accounts/Destinations order
1
# -*- coding: utf-8 -*-
2
1994.2.100 by Olivier DOSSMANN
REF-38 [IMP] Clean code for analytic_distribution module in order to do some move of account.invoice object in next commits
3
from osv import fields
961 by jf
[FIX] Missing import
4
import warnings
1241 by jf
many2many: direct insert prob with xml_id
5
import pooler
951 by jf
Financing contrat: Accounts/Destinations order
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
1994.2.100 by Olivier DOSSMANN
REF-38 [IMP] Clean code for analytic_distribution module in order to do some move of account.invoice object in next commits
19
        for i in ids:
20
            res[i] = []
951 by jf
Financing contrat: Accounts/Destinations order
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 = ''
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
38
        if obj._order:
951 by jf
Financing contrat: Accounts/Destinations order
39
            order_by = ' ORDER BY '
40
            order_tab = []
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
41
            for order in obj._order.split(','):
951 by jf
Financing contrat: Accounts/Destinations order
42
                order_tab.append('%s.%s' %(from_c, order.strip()))
43
            order_by += ','.join(order_tab)
44
45
        limit_str = ''
46
        if self._limit is not None:
47
            limit_str = ' LIMIT %d' % self._limit
48
49
        query = 'SELECT %(rel)s.%(id2)s, %(rel)s.%(id1)s \
50
                   FROM %(rel)s, %(from_c)s \
51
                  WHERE %(rel)s.%(id1)s IN %%s \
52
                    AND %(rel)s.%(id2)s = %(tbl)s.id \
53
                 %(where_c)s  \
54
                 %(order_by)s \
55
                 %(limit)s \
56
                 OFFSET %(offset)d' \
57
            % {'rel': self._rel,
58
               'from_c': from_c,
59
               'tbl': obj._table,
60
               'id1': self._id1,
61
               'id2': self._id2,
62
               'where_c': where_c,
63
               'limit': limit_str,
64
               'order_by': order_by,
65
               'offset': offset,
66
              }
67
        cr.execute(query, [tuple(ids),] + where_params)
68
        for r in cr.fetchall():
69
            res[r[1]].append(r[0])
70
        return res
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
71
72
73
class many2many_notlazy(many2many_sorted):
74
    def __init__(self, obj, rel, id1, id2, string='unknown', limit=None, **args):
75
        super(many2many_notlazy, self).__init__(obj, rel, id1, id2, string, limit, **args)
76
1994.2.104 by Olivier DOSSMANN
REF-38 [DEL] analytic_distribution_invoice module
77
    def set(self, cr, obj, m_id, name, values, user=None, context=None):
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
78
        if context is None:
79
            context = {}
80
        if not values:
81
            return
82
        obj = obj.pool.get(self._obj)
83
        newargs = []
84
        for act in values:
85
            if not (isinstance(act, list) or isinstance(act, tuple)) or not act:
86
                continue
87
            if act[0] == 6:
88
                d1, d2,tables = obj.pool.get('ir.rule').domain_get(cr, user, obj._name, context=context)
89
                if d1:
90
                    d1 = ' and ' + ' and '.join(d1)
91
                else:
92
                    d1 = ''
1994.2.104 by Olivier DOSSMANN
REF-38 [DEL] analytic_distribution_invoice module
93
                args = [m_id, m_id]+d2
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
94
                if not act[2]:
95
                    args.append((0,))
96
                else:
97
                    args.append(tuple(act[2]))
1994.2.100 by Olivier DOSSMANN
REF-38 [IMP] Clean code for analytic_distribution module in order to do some move of account.invoice object in next commits
98
1814.3.1 by Sean Carroll
UTP-334: [FIX] converted SQL delete to unlink
99
                # JIRA UTP-334
100
                if self._rel == 'account_destination_link':
101
                    cr.execute('select id from '+self._rel+' where '+self._id1+'=%s AND '+self._id2+' IN (SELECT '+self._rel+'.'+self._id2+' FROM '+self._rel+', '+','.join(tables)+' WHERE '+self._rel+'.'+self._id1+'=%s AND '+self._rel+'.'+self._id2+' = '+obj._table+'.id '+ d1 +' and '+self._rel+'.'+self._id2+' not in %s)', args)
102
                    unlink_obj = pooler.get_pool(cr.dbname).get('account.destination.link')
103
                    for unlinked_id in cr.fetchall():
104
                        unlink_obj.unlink(cr, user, unlinked_id[0])
105
                else:
106
                    cr.execute('delete from '+self._rel+' where '+self._id1+'=%s AND '+self._id2+' IN (SELECT '+self._rel+'.'+self._id2+' FROM '+self._rel+', '+','.join(tables)+' WHERE '+self._rel+'.'+self._id1+'=%s AND '+self._rel+'.'+self._id2+' = '+obj._table+'.id '+ d1 +' and '+self._rel+'.'+self._id2+' not in %s)', args)
1994.2.100 by Olivier DOSSMANN
REF-38 [IMP] Clean code for analytic_distribution module in order to do some move of account.invoice object in next commits
107
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
108
1994.2.104 by Olivier DOSSMANN
REF-38 [DEL] analytic_distribution_invoice module
109
                cr.execute('select '+self._id2+' from '+self._rel+' where '+self._id1+'=%s', [m_id, ])
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
110
                existing = [x[0] for x in cr.fetchall()]
111
112
                for act_nbr in act[2]:
113
                    if act_nbr not in existing:
1241 by jf
many2many: direct insert prob with xml_id
114
                        if self._rel == 'account_destination_link':
115
                            link_obj = pooler.get_pool(cr.dbname).get('account.destination.link')
1994.2.104 by Olivier DOSSMANN
REF-38 [DEL] analytic_distribution_invoice module
116
                            link_obj.create(cr, user, {self._id1: m_id, self._id2: act_nbr})
1241 by jf
many2many: direct insert prob with xml_id
117
                        else:
1994.2.104 by Olivier DOSSMANN
REF-38 [DEL] analytic_distribution_invoice module
118
                            cr.execute('insert into '+self._rel+' ('+self._id1+','+self._id2+') values (%s, %s)', (m_id, act_nbr))
1994.2.100 by Olivier DOSSMANN
REF-38 [IMP] Clean code for analytic_distribution module in order to do some move of account.invoice object in next commits
119
1116.1.1 by jf
UF-1291: on destination (analytic.account), un-lazy m2m destination_ids + check to not delete a default destination
120
            else:
121
                newargs.append(act)
122
        if newargs:
1994.2.104 by Olivier DOSSMANN
REF-38 [DEL] analytic_distribution_invoice module
123
            return super(many2many_notlazy, self).set(cr, obj, m_id, name, newargs, user=user, context=context)
124
125
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: