~ubuntu-branches/ubuntu/quantal/openerp6.1/quantal-proposed

« back to all changes in this revision

Viewing changes to openerp/addons/account/project/report/inverted_analytic_balance.py

  • Committer: Package Import Robot
  • Author(s): Yolanda Robla
  • Date: 2012-09-20 15:29:00 UTC
  • Revision ID: package-import@ubuntu.com-20120920152900-woyy3yww8z6acmsk
Tags: upstream-6.1-1+dfsg
ImportĀ upstreamĀ versionĀ 6.1-1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
import pooler
 
23
import time
 
24
from report import report_sxw
 
25
 
 
26
class account_inverted_analytic_balance(report_sxw.rml_parse):
 
27
    def __init__(self, cr, uid, name, context):
 
28
        super(account_inverted_analytic_balance, self).__init__(cr, uid, name, context=context)
 
29
        self.localcontext.update( {
 
30
            'time': time,
 
31
            'lines_g': self._lines_g,
 
32
            'lines_a': self._lines_a,
 
33
            'sum_debit': self._sum_debit,
 
34
            'sum_credit': self._sum_credit,
 
35
            'sum_balance': self._sum_balance,
 
36
            'sum_quantity': self._sum_quantity,
 
37
        })
 
38
 
 
39
    def _lines_g(self, accounts, date1, date2):
 
40
        ids = map(lambda x: x.id, accounts)
 
41
        self.cr.execute("SELECT aa.name AS name, aa.code AS code, "
 
42
                        "sum(aal.amount) AS balance, "
 
43
                        "sum(aal.unit_amount) AS quantity, aa.id AS id \
 
44
                FROM account_analytic_line AS aal, account_account AS aa \
 
45
                WHERE (aal.general_account_id=aa.id) "
 
46
                        "AND (aal.account_id IN %s) "
 
47
                        "AND (date>=%s) AND (date<=%s) AND aa.active \
 
48
                GROUP BY aal.general_account_id, aa.name, aa.code, aal.code, aa.id "
 
49
                        "ORDER BY aal.code",
 
50
                        (tuple(ids), date1, date2))
 
51
        res = self.cr.dictfetchall()
 
52
        for r in res:
 
53
            if r['balance'] > 0:
 
54
                r['debit'] = r['balance']
 
55
                r['credit'] = 0.0
 
56
            elif r['balance'] < 0:
 
57
                r['debit'] =  0.0
 
58
                r['credit'] = -r['balance']
 
59
            else:
 
60
                r['debit'] = 0.0
 
61
                r['credit'] = 0.0
 
62
        return res
 
63
 
 
64
    def _lines_a(self, accounts, general_account_id, date1, date2):
 
65
        ids = map(lambda x: x.id, accounts)
 
66
        self.cr.execute("SELECT sum(aal.amount) AS balance, "
 
67
                        "sum(aal.unit_amount) AS quantity, "
 
68
                        "aaa.code AS code, aaa.name AS name, account_id \
 
69
                FROM account_analytic_line AS aal, "
 
70
                        "account_analytic_account AS aaa \
 
71
                WHERE aal.account_id=aaa.id AND aal.account_id IN %s "
 
72
                        "AND aal.general_account_id=%s AND aal.date>=%s "
 
73
                        "AND aal.date<=%s \
 
74
                GROUP BY aal.account_id, general_account_id, aaa.code, aaa.name "
 
75
                        "ORDER BY aal.account_id",
 
76
                        (tuple(ids), general_account_id, date1, date2))
 
77
        res = self.cr.dictfetchall()
 
78
 
 
79
        aaa_obj = self.pool.get('account.analytic.account')
 
80
        res2 = aaa_obj.read(self.cr, self.uid, ids, ['complete_name'])
 
81
        complete_name = {}
 
82
        for r in res2:
 
83
            complete_name[r['id']] = r['complete_name']
 
84
        for r in res:
 
85
            r['complete_name'] = complete_name[r['account_id']]
 
86
            if r['balance'] > 0:
 
87
                r['debit'] = r['balance']
 
88
                r['credit'] = 0.0
 
89
            elif r['balance'] < 0:
 
90
                r['debit'] = 0.0
 
91
                r['credit'] = -r['balance']
 
92
            else:
 
93
                r['debit'] = 0.0
 
94
                r['credit'] = 0.0
 
95
        return res
 
96
 
 
97
    def _sum_debit(self, accounts, date1, date2):
 
98
        ids = map(lambda x: x.id, accounts)
 
99
        self.cr.execute("SELECT sum(amount) \
 
100
                FROM account_analytic_line \
 
101
                WHERE account_id IN %s AND date>=%s AND date<=%s AND amount>0", (tuple(ids),date1, date2,))
 
102
        return self.cr.fetchone()[0] or 0.0
 
103
 
 
104
    def _sum_credit(self, accounts, date1, date2):
 
105
        ids = map(lambda x: x.id, accounts)
 
106
        self.cr.execute("SELECT -sum(amount) \
 
107
                FROM account_analytic_line \
 
108
                WHERE account_id IN %s AND date>=%s AND date<=%s AND amount<0", (tuple(ids),date1, date2,))
 
109
        return self.cr.fetchone()[0] or 0.0
 
110
 
 
111
    def _sum_balance(self, accounts, date1, date2):
 
112
        debit = self._sum_debit(accounts, date1, date2)
 
113
        credit = self._sum_credit(accounts, date1, date2)
 
114
        return (debit-credit)
 
115
 
 
116
    def _sum_quantity(self, accounts, date1, date2):
 
117
        ids = map(lambda x: x.id, accounts)
 
118
        self.cr.execute("SELECT sum(unit_amount) \
 
119
                FROM account_analytic_line \
 
120
                WHERE account_id IN %s AND date>=%s AND date<=%s", (tuple(ids),date1, date2,))
 
121
        return self.cr.fetchone()[0] or 0.0
 
122
 
 
123
report_sxw.report_sxw('report.account.analytic.account.inverted.balance', 'account.analytic.account', 'addons/account/project/report/inverted_analytic_balance.rml',parser=account_inverted_analytic_balance, header="internal")
 
124
 
 
125
 
 
126
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
127