~eduardo-bayardo-bias/openobject-addons/addons_nixus

« back to all changes in this revision

Viewing changes to account/project/project.py

  • Committer: root
  • Date: 2011-09-16 15:29:00 UTC
  • Revision ID: root@openerp-20110916152900-3rzqdbv32mcenxit
cambio a version 5.0.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- encoding: utf-8 -*-
2
2
##############################################################################
3
3
#
4
 
#    OpenERP, Open Source Management Solution   
 
4
#    OpenERP, Open Source Management Solution
5
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6
6
#    $Id$
7
7
#
19
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
#
21
21
##############################################################################
22
 
 
23
22
import time
24
23
import operator
25
24
 
 
25
import netsvc
26
26
from osv import fields
27
27
from osv import osv
28
28
 
33
33
class account_analytic_account(osv.osv):
34
34
    _name = 'account.analytic.account'
35
35
    _description = 'Analytic Accounts'
 
36
    logger = netsvc.Logger()
36
37
 
37
38
    def _credit_calc(self, cr, uid, ids, name, arg, context={}):
38
 
        acc_set = ",".join(map(str, ids))
 
39
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
40
                                  'Entering _credit_calc; ids:%s'%ids)
 
41
        if not ids: return {}
39
42
 
40
43
        where_date = ''
41
 
        if context.get('from_date',False):
42
 
            where_date += " AND l.date >= '" + context['from_date'] + "'"
43
 
        if context.get('to_date',False):
44
 
            where_date += " AND l.date <= '" + context['to_date'] + "'"
45
 
            
46
 
        cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE l.amount<0 and a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
 
44
        if context.get('from_date'):
 
45
            where_date += " AND l.date >= %(from_date)s"
 
46
        if context.get('to_date'):
 
47
            where_date += " AND l.date <= %(to_date)s"
 
48
 
 
49
        cr.execute("SELECT a.id, COALESCE(SUM(l.amount), 0) "
 
50
                   "FROM account_analytic_account a "
 
51
                   "LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s)"
 
52
                   " WHERE l.amount < 0 AND a.id IN %%(ids)s "
 
53
                   "GROUP BY a.id" % (where_date),
 
54
                   dict(context, ids=tuple(ids)))
47
55
        r = dict(cr.fetchall())
 
56
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
57
                                  '_credit_calc results: %s'%r)
48
58
        for i in ids:
49
59
            r.setdefault(i,0.0)
50
60
        return r
51
61
 
52
62
    def _debit_calc(self, cr, uid, ids, name, arg, context={}):
 
63
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
64
                                  'Entering _debit_calc; ids:%s'%ids)
 
65
        if not ids: return {}
53
66
 
54
 
        acc_set = ",".join(map(str, ids))
55
 
        
56
67
        where_date = ''
57
 
        if context.get('from_date',False):
58
 
            where_date += " AND l.date >= '" + context['from_date'] + "'"
59
 
        if context.get('to_date',False):
60
 
            where_date += " AND l.date <= '" + context['to_date'] + "'"
61
 
            
62
 
        cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE l.amount>0 and a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
63
 
        r= dict(cr.fetchall())
 
68
        if context.get('from_date'):
 
69
            where_date += " AND l.date >= %(from_date)s"
 
70
        if context.get('to_date'):
 
71
            where_date += " AND l.date <= %(to_date)s"
 
72
 
 
73
        cr.execute("SELECT a.id, COALESCE(SUM(l.amount), 0) "
 
74
                   "FROM account_analytic_account a "
 
75
                   "LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s)"
 
76
                   " WHERE l.amount > 0 AND a.id IN %%(ids)s "
 
77
                   "GROUP BY a.id" % (where_date),
 
78
                   dict(context, ids=tuple(ids)))
 
79
        r = dict(cr.fetchall())
 
80
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
81
                                  '_debut_calc results: %s'%r)
64
82
        for i in ids:
65
83
            r.setdefault(i,0.0)
66
84
        return r
67
85
 
68
86
    def _balance_calc(self, cr, uid, ids, name, arg, context={}):
 
87
        res = {}
69
88
        ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
70
 
        acc_set = ",".join(map(str, ids2))
71
 
        
 
89
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
90
                                  'Entering _balance_calc; ids:%s; ids2:%s'%(
 
91
                            ids, ids2))
 
92
 
 
93
        for i in ids:
 
94
            res.setdefault(i,0.0)
 
95
 
 
96
        if not ids2:
 
97
            return res
 
98
 
72
99
        where_date = ''
73
 
        if context.get('from_date',False):
74
 
            where_date += " AND l.date >= '" + context['from_date'] + "'"
75
 
        if context.get('to_date',False):
76
 
            where_date += " AND l.date <= '" + context['to_date'] + "'"
77
 
            
78
 
        cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
79
 
        res = {}
 
100
        if context.get('from_date'):
 
101
            where_date += " AND l.date >= %(from_date)s"
 
102
        if context.get('to_date'):
 
103
            where_date += " AND l.date <= %(to_date)s"
 
104
 
 
105
        cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) "
 
106
                   "FROM account_analytic_account a "
 
107
                   "LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s)"
 
108
                   " WHERE a.id IN %%(ids)s "
 
109
                   "GROUP BY a.id" % (where_date),
 
110
                   dict(context, ids=tuple(ids2)))
 
111
 
80
112
        for account_id, sum in cr.fetchall():
81
113
            res[account_id] = sum
82
 
 
83
 
        cr.execute("SELECT a.id, r.currency_id FROM account_analytic_account a INNER JOIN res_company r ON (a.company_id = r.id) where a.id in (%s)" % acc_set)
84
 
 
85
 
        currency= dict(cr.fetchall())
 
114
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
115
                                  '_balance_calc, (id, sum): %s'%res)
 
116
 
 
117
        cr.execute("SELECT a.id, r.currency_id "
 
118
                   "FROM account_analytic_account a "
 
119
                   "INNER JOIN res_company r ON (a.company_id = r.id) "
 
120
                   "WHERE a.id in %s", (tuple(ids2),))
 
121
 
 
122
        currency = dict(cr.fetchall())
 
123
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
124
                                  '_balance_calc currency: %s'%currency)
86
125
 
87
126
        res_currency= self.pool.get('res.currency')
88
127
        for id in ids:
 
128
            if id not in ids2:
 
129
                continue
89
130
            for child in self.search(cr, uid, [('parent_id', 'child_of', [id])]):
90
 
                if child <> id:
 
131
                if child != id:
91
132
                    res.setdefault(id, 0.0)
92
 
                    if  currency[child]<>currency[id] :
 
133
                    if  currency[child]<>currency[id]:
93
134
                        res[id] += res_currency.compute(cr, uid, currency[child], currency[id], res.get(child, 0.0), context=context)
94
135
                    else:
95
136
                        res[id] += res.get(child, 0.0)
97
138
        cur_obj = res_currency.browse(cr,uid,currency.values(),context)
98
139
        cur_obj = dict([(o.id, o) for o in cur_obj])
99
140
        for id in ids:
100
 
            res[id] = res_currency.round(cr,uid,cur_obj[currency[id]],res.get(id,0.0))
 
141
            if id in ids2:
 
142
                res[id] = res_currency.round(cr,uid,cur_obj[currency[id]],res.get(id,0.0))
101
143
 
102
144
        return dict([(i, res[i]) for i in ids ])
103
145
 
104
146
    def _quantity_calc(self, cr, uid, ids, name, arg, context={}):
 
147
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
148
                                  '_quantity_calc ids:%s'%ids)
105
149
        #XXX must convert into one uom
 
150
        res = {}
106
151
        ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
107
 
        acc_set = ",".join(map(str, ids2))
108
 
        
 
152
 
 
153
        for i in ids:
 
154
            res.setdefault(i,0.0)
 
155
 
 
156
        if not ids2:
 
157
            return res
 
158
 
109
159
        where_date = ''
110
 
        if context.get('from_date',False):
111
 
            where_date += " AND l.date >= '" + context['from_date'] + "'"
112
 
        if context.get('to_date',False):
113
 
            where_date += " AND l.date <= '" + context['to_date'] + "'"
114
 
            
 
160
        if context.get('from_date'):
 
161
            where_date += " AND l.date >= %(from_date)s"
 
162
        if context.get('to_date'):
 
163
            where_date += " AND l.date <= %(to_date)s"
 
164
 
115
165
        cr.execute('SELECT a.id, COALESCE(SUM(l.unit_amount), 0) \
116
166
                FROM account_analytic_account a \
117
 
                    LEFT JOIN account_analytic_line l ON (a.id = l.account_id ' + where_date + ') \
118
 
                WHERE a.id IN ('+acc_set+') GROUP BY a.id')
119
 
        res = {}
 
167
                    LEFT JOIN account_analytic_line l ON (a.id = l.account_id %s) \
 
168
                WHERE a.id IN %%(ids)s GROUP BY a.id'%(where_date),
 
169
                   dict(context, ids=tuple(ids2)))
 
170
 
120
171
        for account_id, sum in cr.fetchall():
121
172
            res[account_id] = sum
 
173
        self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
 
174
                                  '_quantity_calc, (id, sum): %s'%res)
122
175
 
123
176
        for id in ids:
 
177
            if id not in ids2:
 
178
                continue
124
179
            for child in self.search(cr, uid, [('parent_id', 'child_of', [id])]):
125
 
                if child <> id:
 
180
                if child != id:
126
181
                    res.setdefault(id, 0.0)
127
182
                    res[id] += res.get(child, 0.0)
128
183
        return dict([(i, res[i]) for i in ids])
267
322
account_journal()
268
323
 
269
324
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
270