~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to account/project/report/cost_ledger.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#                    Fabien Pinckaers <fp@tiny.Be>
 
5
#
 
6
# WARNING: This program as such is intended to be used by professional
 
7
# programmers who take the whole responsability of assessing all potential
 
8
# consequences resulting FROM its eventual inadequacies AND bugs
 
9
# End users who are looking for a ready-to-use solution with commercial
 
10
# garantees AND support are strongly adviced to contract a Free Software
 
11
# Service Company
 
12
#
 
13
# This program is Free Software; you can redistribute it AND/or
 
14
# modify it under the terms of the GNU General Public License
 
15
# as published by the Free Software Foundation; either version 2
 
16
# of the License, or (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
26
#
 
27
##############################################################################
 
28
 
 
29
import pooler
 
30
import time
 
31
from report import report_sxw
 
32
 
 
33
class account_analytic_cost_ledger(report_sxw.rml_parse):
 
34
        def __init__(self, cr, uid, name, context):
 
35
                super(account_analytic_cost_ledger, self).__init__(cr, uid, name, context)
 
36
                self.localcontext.update( {
 
37
                        'time': time,
 
38
                        'lines_g': self._lines_g,
 
39
                        'lines_a': self._lines_a,
 
40
                        'account_sum_debit': self._account_sum_debit,
 
41
                        'account_sum_credit': self._account_sum_credit,
 
42
                        'sum_debit': self._sum_debit,
 
43
                        'sum_credit': self._sum_credit,
 
44
                        'sum_balance': self._sum_balance,
 
45
                        'account_sum_balance': self._account_sum_balance,
 
46
                })
 
47
                
 
48
        def _lines_g(self, account_id, date1, date2):
 
49
                self.cr.execute("       SELECT sum(aal.amount) AS balance, aa.code AS code, aa.name AS name, aa.id AS id \
 
50
                                                        FROM account_account AS aa, account_analytic_line AS aal \
 
51
                                                        WHERE (aal.account_id=%d) AND (aal.date>=%s) AND (aal.date<=%s) AND (aal.general_account_id=aa.id) \
 
52
                                                        GROUP BY aa.code, aa.name, aa.id ORDER BY aa.code", (account_id, date1, date2))
 
53
                res = self.cr.dictfetchall()
 
54
 
 
55
                for r in res:
 
56
                        if r['balance'] > 0:
 
57
                                r['debit'] = '%.2f' % r['balance']
 
58
                                r['credit'] = ''
 
59
                        elif r['balance'] < 0:
 
60
                                r['debit'] = ''
 
61
                                r['credit'] = '%.2f' % -r['balance']
 
62
                        else: # r['balance'] == 0
 
63
                                r['debit'] = ''
 
64
                                r['credit'] = ''
 
65
                return res
 
66
        
 
67
        def _lines_a(self, general_account_id, account_id, date1, date2):
 
68
                self.cr.execute("       SELECT aal.name AS name, aal.code AS code, aal.amount AS balance, aal.date AS date, aaj.code AS cj FROM account_analytic_line AS aal, account_analytic_journal AS aaj \
 
69
                                                        WHERE (aal.general_account_id=%d) AND (aal.account_id=%d) AND (aal.date>=%s) AND (aal.date<=%s) \
 
70
                                                        AND (aal.journal_id=aaj.id) \
 
71
                                                        ORDER BY aal.date, aaj.code, aal.code", (general_account_id, account_id, date1, date2))
 
72
                res = self.cr.dictfetchall()
 
73
 
 
74
                for r in res:
 
75
                        if r['balance'] > 0:
 
76
                                r['debit'] = '%.2f' % r['balance']
 
77
                                r['credit'] = ''
 
78
                        elif r['balance'] < 0:
 
79
                                r['debit'] = ''
 
80
                                r['credit'] = '%.2f' % -r['balance']
 
81
                        else: # r['balance'] == 0
 
82
                                r['debit'] = ''
 
83
                                r['credit'] = ''                                                                                                                                                                                
 
84
                return res
 
85
 
 
86
 
 
87
        def _account_sum_debit(self, account_id, date1, date2):
 
88
                self.cr.execute("SELECT sum(amount) FROM account_analytic_line WHERE account_id=%d AND date>=%s AND date<=%s AND amount>0", (account_id, date1, date2))
 
89
                return self.cr.fetchone()[0] or 0.0
 
90
 
 
91
        def _account_sum_credit(self, account_id, date1, date2):
 
92
                self.cr.execute("SELECT -sum(amount) FROM account_analytic_line WHERE account_id=%d AND date>=%s AND date<=%s AND amount<0", (account_id, date1, date2))
 
93
                return self.cr.fetchone()[0] or 0.0
 
94
        
 
95
        def _account_sum_balance(self, account_id, date1, date2):
 
96
                debit = self._account_sum_debit(account_id, date1, date2) 
 
97
                credit = self._account_sum_credit(account_id, date1, date2)
 
98
                return (debit-credit)
 
99
        
 
100
        
 
101
        def _sum_debit(self, accounts, date1, date2):
 
102
                ids = map(lambda x: x.id, accounts)
 
103
                if not len(ids):
 
104
                        return 0.0
 
105
                self.cr.execute("SELECT sum(amount) FROM account_analytic_line WHERE account_id IN ("+','.join(map(str, ids))+") AND date>=%s AND date<=%s AND amount>0", (date1, date2))
 
106
                return self.cr.fetchone()[0] or 0.0
 
107
                
 
108
        def _sum_credit(self, accounts, date1, date2):
 
109
                ids = map(lambda x: x.id, accounts)
 
110
                if not len(ids):
 
111
                        return 0.0
 
112
                ids = map(lambda x: x.id, accounts)
 
113
                self.cr.execute("SELECT -sum(amount) FROM account_analytic_line WHERE account_id IN ("+','.join(map(str, ids))+") AND date>=%s AND date<=%s AND amount<0", (date1, date2))
 
114
                return self.cr.fetchone()[0] or 0.0
 
115
 
 
116
        def _sum_balance(self, accounts, date1, date2):
 
117
                debit = self._sum_debit(accounts, date1, date2) or 0.0
 
118
                credit = self._sum_credit(accounts, date1, date2) or 0.0
 
119
                return (debit-credit)
 
120
 
 
121
report_sxw.report_sxw('report.account.analytic.account.cost_ledger', 'account.analytic.account', 'addons/account/project/report/cost_ledger.rml',parser=account_analytic_cost_ledger)
 
122