~tempo-openerp/+junk/loewert-report-name

« back to all changes in this revision

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

  • Committer: jbe at tempo-consulting
  • Date: 2013-08-21 08:48:11 UTC
  • Revision ID: jbe@tempo-consulting.fr-20130821084811-913uo4l7b5ayxq8m
[NEW] Création de la branche trunk Loewert

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