~openerp/openobject-addons/trunk_analytic_analysis_rework_qdp

« back to all changes in this revision

Viewing changes to account_analytic_analysis/account_analytic_analysis.py

  • Committer: Quentin (OpenERP)
  • Date: 2012-01-06 15:19:07 UTC
  • Revision ID: qdp-launchpad@openerp.com-20120106151907-z0obfhzxojq894to
[IMP] account_analytic_analysis: removed consolidation on every fields

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    def _analysis_all(self, cr, uid, ids, fields, arg, context=None):
34
34
        dp = 2
35
35
        res = dict([(i, {}) for i in ids])
36
 
 
37
 
        parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)], context=context))
38
 
        res.update(dict([(i, {}) for i in parent_ids]))
 
36
        parent_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
 
37
        #parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)], context=context))
 
38
        #res.update(dict([(i, {}) for i in parent_ids]))
39
39
        accounts = self.browse(cr, uid, ids, context=context)
40
40
 
41
41
        for f in fields:
72
72
                        if account_id not in res:
73
73
                            res[account_id] = {}
74
74
                        res[account_id][f] = sum
75
 
                for account in accounts:
76
 
                    for child in account.child_ids:
77
 
                        if res[account.id][f] < res.get(child.id, {}).get(f):
78
 
                            res[account.id][f] = res.get(child.id, {}).get(f, False)
 
75
                #for account in accounts:
 
76
                #    for child in account.child_ids:
 
77
                #        if res[account.id][f] < res.get(child.id, {}).get(f):
 
78
                #            res[account.id][f] = res.get(child.id, {}).get(f, False)
79
79
            elif f == 'ca_to_invoice':
80
80
                for id in ids:
81
81
                    res[id][f] = 0.0
111
111
                            res[account_id] = {}
112
112
                        res[account_id][f] = round(sum, dp)
113
113
 
114
 
                for account in accounts:
115
 
                    #res.setdefault(account.id, 0.0)
116
 
                    res2.setdefault(account.id, 0.0)
117
 
                    for child in account.child_ids:
118
 
                        if child.id != account.id:
119
 
                            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
120
 
                            res2[account.id] += res2.get(child.id, 0.0)
 
114
                #for account in accounts:
 
115
                #    #res.setdefault(account.id, 0.0)
 
116
                #    res2.setdefault(account.id, 0.0)
 
117
                #    for child in account.child_ids:
 
118
                #        if child.id != account.id:
 
119
                #            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
 
120
                #            res2[account.id] += res2.get(child.id, 0.0)
121
121
                # sum both result on account_id
122
122
                for id in ids:
123
123
                    res[id][f] = round(res.get(id, {}).get(f, 0.0), dp) + round(res2.get(id, 0.0), 2)
135
135
                            GROUP BY account_analytic_line.account_id",(parent_ids,))
136
136
                    for account_id, lid in cr.fetchall():
137
137
                        res[account_id][f] = lid
138
 
                for account in accounts:
139
 
                    for child in account.child_ids:
140
 
                        if res[account.id][f] < res.get(child.id, {}).get(f):
141
 
                            res[account.id][f] = res.get(child.id, {}).get(f, False)
 
138
                #for account in accounts:
 
139
                #    for child in account.child_ids:
 
140
                #        if res[account.id][f] < res.get(child.id, {}).get(f):
 
141
                #            res[account.id][f] = res.get(child.id, {}).get(f, False)
142
142
            elif f == 'last_worked_date':
143
143
                for id in ids:
144
144
                    res[id][f] = False
152
152
                        if account_id not in res:
153
153
                            res[account_id] = {}
154
154
                        res[account_id][f] = lwd
155
 
                for account in accounts:
156
 
                    for child in account.child_ids:
157
 
                        if res[account.id][f] < res.get(child.id, {}).get(f):
158
 
                            res[account.id][f] = res.get(child.id, {}).get(f, False)
 
155
                #for account in accounts:
 
156
                #    for child in account.child_ids:
 
157
                #        if res[account.id][f] < res.get(child.id, {}).get(f):
 
158
                #            res[account.id][f] = res.get(child.id, {}).get(f, False)
159
159
            elif f == 'hours_qtt_non_invoiced':
160
160
                for id in ids:
161
161
                    res[id][f] = 0.0
173
173
                        if account_id not in res:
174
174
                            res[account_id] = {}
175
175
                        res[account_id][f] = round(sua, dp)
176
 
                for account in accounts:
177
 
                    for child in account.child_ids:
178
 
                        if account.id != child.id:
179
 
                            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
 
176
                #for account in accounts:
 
177
                #    for child in account.child_ids:
 
178
                #        if account.id != child.id:
 
179
                #            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
180
180
                for id in ids:
181
181
                    res[id][f] = round(res[id][f], dp)
182
182
            elif f == 'hours_quantity':
195
195
                        if account_id not in res:
196
196
                            res[account_id] = {}
197
197
                        res[account_id][f] = round(hq, dp)
198
 
                for account in accounts:
199
 
                    for child in account.child_ids:
200
 
                        if account.id != child.id:
201
 
                            if account.id not in res:
202
 
                                res[account.id] = {f: 0.0}
203
 
                            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
 
198
                #for account in accounts:
 
199
                #    for child in account.child_ids:
 
200
                #        if account.id != child.id:
 
201
                #            if account.id not in res:
 
202
                #                res[account.id] = {f: 0.0}
 
203
                #            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
204
204
                for id in ids:
205
205
                    res[id][f] = round(res[id][f], dp)
206
206
            elif f == 'ca_theorical':
207
207
                # TODO Take care of pricelist and purchase !
208
208
                for id in ids:
209
209
                    res[id][f] = 0.0
210
 
                res2 = {}
 
210
                #res2 = {}
211
211
                # Warning
212
212
                # This computation doesn't take care of pricelist !
213
213
                # Just consider list_price
232
232
                            AND account_analytic_journal.type IN ('purchase', 'general')
233
233
                        GROUP BY account_analytic_line.account_id""",(parent_ids,))
234
234
                    for account_id, sum in cr.fetchall():
235
 
                        res2[account_id] = round(sum, dp)
236
 
 
237
 
                for account in accounts:
238
 
                    res2.setdefault(account.id, 0.0)
239
 
                    for child in account.child_ids:
240
 
                        if account.id != child.id:
241
 
                            if account.id not in res:
242
 
                                res[account.id] = {f: 0.0}
243
 
                            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
244
 
                            res[account.id][f] += res2.get(child.id, 0.0)
 
235
                        res[account_id][f] = round(sum, dp)
 
236
                #for account in accounts:
 
237
                #    res2.setdefault(account.id, 0.0)
 
238
                #    for child in account.child_ids:
 
239
                #        if account.id != child.id:
 
240
                #            if account.id not in res:
 
241
                #                res[account.id] = {f: 0.0}
 
242
                #            res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
 
243
                #            res[account.id][f] += res2.get(child.id, 0.0)
245
244
 
246
245
                # sum both result on account_id
247
 
                for id in ids:
248
 
                    res[id][f] = round(res[id][f], dp) + round(res2.get(id, 0.0), dp)
 
246
                #for id in ids:
 
247
                #    res[id][f] = round(res[id][f], dp) + round(res2.get(id, 0.0), dp)
249
248
 
250
249
        return res
251
250
 
252
251
    def _ca_invoiced_calc(self, cr, uid, ids, name, arg, context=None):
253
252
        res = {}
254
253
        res_final = {}
255
 
        child_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)], context=context))
 
254
        child_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
 
255
        #child_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)], context=context))
256
256
        for i in child_ids:
257
 
            res[i] =  {}
258
 
            for n in [name]:
259
 
                res[i][n] = 0.0
 
257
            res[i] =  0.0
 
258
            #res[i] =  0.0
 
259
            #for n in [name]:
 
260
            #    res[i][n] = 0.0
260
261
        if not child_ids:
261
262
            return res
262
263
 
269
270
                        AND account_analytic_journal.type = 'sale' \
270
271
                    GROUP BY account_analytic_line.account_id", (child_ids,))
271
272
            for account_id, sum in cr.fetchall():
272
 
                res[account_id][name] = round(sum,2)
273
 
        data = self._compute_level_tree(cr, uid, ids, child_ids, res, [name], context=context)
274
 
        for i in data:
275
 
            res_final[i] = data[i][name]
 
273
                res[account_id] = round(sum,2)
 
274
        res_final = res
 
275
        #data = self._compute_level_tree(cr, uid, ids, child_ids, res, [name], context=context)
 
276
        #for i in data:
 
277
        #    res_final[i] = data[i][name]
276
278
        return res_final
277
279
 
278
280
    def _total_cost_calc(self, cr, uid, ids, name, arg, context=None):
279
281
        res = {}
280
282
        res_final = {}
281
 
        child_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)], context=context))
282
 
 
 
283
        child_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
 
284
        #child_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)], context=context))
283
285
        for i in child_ids:
284
 
            res[i] =  {}
285
 
            for n in [name]:
286
 
                res[i][n] = 0.0
 
286
            res[i] =  0.0
 
287
            #res[i] =  {}
 
288
            #for n in [name]:
 
289
            #    res[i][n] = 0.0
287
290
        if not child_ids:
288
291
            return res
289
 
 
290
292
        if child_ids:
291
293
            cr.execute("""SELECT account_analytic_line.account_id, COALESCE(SUM(amount), 0.0) \
292
294
                    FROM account_analytic_line \
296
298
                        AND amount<0 \
297
299
                    GROUP BY account_analytic_line.account_id""",(child_ids,))
298
300
            for account_id, sum in cr.fetchall():
299
 
                res[account_id][name] = round(sum,2)
300
 
        data = self._compute_level_tree(cr, uid, ids, child_ids, res, [name], context)
301
 
        for i in data:
302
 
            res_final[i] = data[i][name]
 
301
                res[account_id] = round(sum,2)
 
302
        res_final = res
 
303
        #data = self._compute_level_tree(cr, uid, ids, child_ids, res, [name], context)
 
304
        #for i in data:
 
305
        #    res_final[i] = data[i][name]
303
306
        return res_final
304
307
 
305
308
    def _remaining_hours_calc(self, cr, uid, ids, name, arg, context=None):
455
458
        max_user = cr.fetchone()[0]
456
459
        account_ids = [int(str(x/max_user - (x%max_user == 0 and 1 or 0))) for x in ids]
457
460
        user_ids = [int(str(x-((x/max_user - (x%max_user == 0 and 1 or 0)) *max_user))) for x in ids]
458
 
        parent_ids = tuple(account_obj.search(cr, uid, [('parent_id', 'child_of', account_ids)], context=context))
 
461
        parent_ids = tuple(account_ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
 
462
        #parent_ids = tuple(account_obj.search(cr, uid, [('parent_id', 'child_of', account_ids)], context=context))
 
463
        import pdb;pdb.set_trace()
459
464
        if parent_ids:
460
465
            cr.execute('SELECT id, unit_amount ' \
461
466
                    'FROM account_analytic_analysis_summary_user ' \
463
468
                        'AND "user" IN %s',(parent_ids, tuple(user_ids),))
464
469
            for sum_id, unit_amount in cr.fetchall():
465
470
                res[sum_id] = unit_amount
466
 
        for obj_id in ids:
467
 
            res.setdefault(obj_id, 0.0)
468
 
            for child_id in account_obj.search(cr, uid,
469
 
                    [('parent_id', 'child_of', [int(str(obj_id/max_user - (obj_id%max_user == 0 and 1 or 0)))])]):
470
 
                if child_id != int(str(obj_id/max_user - (obj_id%max_user == 0 and 1 or 0))):
471
 
                    res[obj_id] += res.get((child_id * max_user) + obj_id -((obj_id/max_user - (obj_id%max_user == 0 and 1 or 0)) * max_user), 0.0)
 
471
        #for obj_id in ids:
 
472
        #    res.setdefault(obj_id, 0.0)
 
473
        #    for child_id in account_obj.search(cr, uid,
 
474
        #            [('parent_id', 'child_of', [int(str(obj_id/max_user - (obj_id%max_user == 0 and 1 or 0)))])]):
 
475
        #        if child_id != int(str(obj_id/max_user - (obj_id%max_user == 0 and 1 or 0))):
 
476
        #            res[obj_id] += res.get((child_id * max_user) + obj_id -((obj_id/max_user - (obj_id%max_user == 0 and 1 or 0)) * max_user), 0.0)
472
477
        for id in ids:
473
478
            res[id] = round(res.get(id, 0.0), 2)
474
479
        return res
619
624
        account_obj = self.pool.get('account.analytic.account')
620
625
        account_ids = [int(str(int(x))[:-6]) for x in ids]
621
626
        month_ids = [int(str(int(x))[-6:]) for x in ids]
622
 
        parent_ids = tuple(account_obj.search(cr, uid, [('parent_id', 'child_of', account_ids)], context=context))
 
627
        parent_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
 
628
        #parent_ids = tuple(account_obj.search(cr, uid, [('parent_id', 'child_of', account_ids)], context=context))
623
629
        if parent_ids:
624
630
            cr.execute('SELECT id, unit_amount ' \
625
631
                    'FROM account_analytic_analysis_summary_month ' \
627
633
                        'AND month_id IN %s ',(parent_ids, tuple(month_ids),))
628
634
            for sum_id, unit_amount in cr.fetchall():
629
635
                res[sum_id] = unit_amount
630
 
        for obj_id in ids:
631
 
            res.setdefault(obj_id, 0.0)
632
 
            for child_id in account_obj.search(cr, uid,
633
 
                    [('parent_id', 'child_of', [int(str(int(obj_id))[:-6])])]):
634
 
                if child_id != int(str(int(obj_id))[:-6]):
635
 
                    res[obj_id] += res.get(int(child_id * 1000000 + int(str(int(obj_id))[-6:])), 0.0)
 
636
        #for obj_id in ids:
 
637
        #    res.setdefault(obj_id, 0.0)
 
638
        #    for child_id in account_obj.search(cr, uid,
 
639
        #            [('parent_id', 'child_of', [int(str(int(obj_id))[:-6])])]):
 
640
        #        if child_id != int(str(int(obj_id))[:-6]):
 
641
        #            res[obj_id] += res.get(int(child_id * 1000000 + int(str(int(obj_id))[-6:])), 0.0)
636
642
        for id in ids:
637
643
            res[id] = round(res.get(id, 0.0), 2)
638
644
        return res