~eduardo-bayardo-bias/bias-trunk/bias_trunk

« back to all changes in this revision

Viewing changes to bias_electronic_invoice_bf/report/consumo_suc.py

  • Committer: Jose Patricio
  • Date: 2010-09-10 00:15:14 UTC
  • Revision ID: josepato@ramona-20100910001514-pyvq1qhrwdui8h22
bias electronic invoice

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# WARNING: This program as such is intended to be used by professional
 
6
# programmers who take the whole responsability of assessing all potential
 
7
# consequences resulting from its eventual inadequacies and bugs
 
8
# End users who are looking for a ready-to-use solution with commercial
 
9
# garantees and support are strongly adviced to contract a Free Software
 
10
# Service Company
 
11
#
 
12
# This program is Free Software; you can redistribute it and/or
 
13
# modify it under the terms of the GNU General Public License
 
14
# as published by the Free Software Foundation; either version 2
 
15
# of the License, or (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
#
 
26
##############################################################################
 
27
 
 
28
import time
 
29
from report import report_sxw
 
30
 
 
31
class consumo_suc(report_sxw.rml_parse):
 
32
        def __init__(self, cr, uid, name, context):
 
33
                super(consumo_suc, self).__init__(cr, uid, name, context)
 
34
                self.localcontext.update( {
 
35
                        'time': time,
 
36
                        'lines': self.lines,
 
37
                        'sum_debit_account': self._sum_debit_account,
 
38
                        'sum_credit_account': self._sum_credit_account,
 
39
                        'sum_debit': self._sum_debit,
 
40
                        'sum_credit': self._sum_credit,
 
41
                        'category': self._category,
 
42
                        'periods': self._periods,
 
43
                })
 
44
                self.context = context
 
45
 
 
46
        def _get_child(self, parent):
 
47
                obj = self.pool.get('product.category')
 
48
                childs = []
 
49
                categ_id = obj.search(self.cr, self.uid, [])
 
50
                for c in categ_id:
 
51
                        if parent == obj.browse(self.cr, self.uid, c).parent_id.id:
 
52
                                childs.append(c)
 
53
                return childs
 
54
 
 
55
        def _category(self,form):
 
56
                print "category form = ",form
 
57
                if not form['category']:
 
58
                        categ_name = "Todas"
 
59
                        print "raul categ name = ",categ_name                        
 
60
                else:                
 
61
                        obj = self.pool.get('product.category')
 
62
                        categ_name = obj.browse(self.cr, self.uid, form['category']).name                
 
63
                        print "raul categ_name ",categ_name
 
64
                        categ_in = form['category']
 
65
                        print "raul categ name = ",categ_name
 
66
                return categ_name
 
67
 
 
68
        def _periods(self,form):
 
69
                if not form['periods'][0][2]:
 
70
                        period_name = "Todos"
 
71
                else:                
 
72
                        obj = self.pool.get('account.period')
 
73
                        periods = form['periods'][0][2]
 
74
                        todo = " "
 
75
                        for period in periods:
 
76
                                print "period = ",obj.browse(self.cr, self.uid, period).code
 
77
                                todo += obj.browse(self.cr, self.uid, period).code + ", "
 
78
                        period_name = todo
 
79
                        
 
80
                return period_name
 
81
 
 
82
        def raul_category(self,form):
 
83
                print "category form = ",form
 
84
#               if not form['category']:
 
85
#                        categ_name = "Todas"
 
86
#                        return categ_name
 
87
                obj = self.pool.get('product.category')
 
88
                categ_name = obj.browse(self.cr, self.uid, form['category']).name                
 
89
                print "raul categ_name ",categ_name
 
90
                categ_in = form['category']
 
91
                if categ_in == 0:
 
92
                        categ_name = "Todas"
 
93
                print "raul categ name = ",categ_name
 
94
                return categ_name
 
95
 
 
96
        def lines(self, account, form):
 
97
                ctx = self.context.copy()
 
98
                ctx['fiscalyear'] = form['fiscalyear']
 
99
                ctx['periods'] = form['periods'][0][2]
 
100
                query = self.pool.get('account.move.line')._query_get(self.cr, self.uid, context=ctx)
 
101
 
 
102
                categ_in = form['category']             
 
103
                childs = self._get_child(categ_in)
 
104
                for i in childs:
 
105
                        parent = self._get_child(i)
 
106
                        if parent:
 
107
                                childs += parent
 
108
                childs += [categ_in]
 
109
                print 'childs=',childs   
 
110
                categ_in = form['category']
 
111
                self.cr.execute("SELECT l.date, j.code, l.ref, l.name, l.debit, l.credit, l.quantity "\
 
112
                        "FROM account_move_line l, account_journal j "\
 
113
                        "WHERE l.name in (SELECT name from product_template q WHERE q.categ_id in (SELECT a.id from product_category a where a.parent_id = %d) ) AND l.journal_id = j.id "\
 
114
                                "AND account_id = %d AND "+query+" "\
 
115
                        "ORDER by l.id", (categ_in,account.id,))
 
116
                if categ_in == 0:
 
117
                        self.cr.execute("SELECT l.date, j.code, l.ref, l.name, l.debit, l.credit, l.quantity "\
 
118
                                "FROM account_move_line l, account_journal j "\
 
119
                                "WHERE l.journal_id = j.id "\
 
120
                                "AND account_id = %d AND "+query+" "\
 
121
                                "ORDER by l.id", (account.id,))
 
122
                
 
123
                res = self.cr.dictfetchall()
 
124
                sum = 0.0
 
125
                for l in res:
 
126
                        sum += (l['debit'] or 0.0) - (l['credit'] or 0.0)
 
127
                        l['progress'] = sum
 
128
                return res
 
129
 
 
130
        def _sum_debit_account(self, account, form):
 
131
                ctx = self.context.copy()
 
132
                ctx['fiscalyear'] = form['fiscalyear']
 
133
                ctx['periods'] = form['periods'][0][2]
 
134
                query = self.pool.get('account.move.line')._query_get(self.cr, self.uid, context=ctx)
 
135
                categ_in = form['category']     
 
136
#               self.cr.execute("SELECT sum(debit) "\
 
137
#                               "FROM account_move_line l "\
 
138
#                               "WHERE l.account_id = %d AND "+query, (account.id,))
 
139
                self.cr.execute("SELECT sum(l.debit) "\
 
140
                                "FROM account_move_line l, account_journal j "\
 
141
                                "WHERE l.name in (SELECT name from product_template q WHERE q.categ_id in (SELECT a.id from product_category a where a.parent_id = %d) ) AND l.journal_id = j.id "\
 
142
                                "AND account_id = %d AND "+query, (categ_in,account.id,))
 
143
                if categ_in == 0:
 
144
                        self.cr.execute("SELECT sum(debit) "\
 
145
                                "FROM account_move_line l "\
 
146
                                "WHERE l.account_id = %d AND "+query, (account.id,))                        
 
147
                return self.cr.fetchone()[0] or 0.0
 
148
 
 
149
        def _sum_credit_account(self, account, form):
 
150
                ctx = self.context.copy()
 
151
                ctx['fiscalyear'] = form['fiscalyear']
 
152
                ctx['periods'] = form['periods'][0][2]
 
153
                query = self.pool.get('account.move.line')._query_get(self.cr, self.uid, context=ctx)
 
154
                categ_in = form['category']     
 
155
                self.cr.execute("SELECT sum(l.credit) "\
 
156
                                "FROM account_move_line l, account_journal j "\
 
157
                                "WHERE l.name in (SELECT name from product_template q WHERE q.categ_id in (SELECT a.id from product_category a where a.parent_id = %d) ) AND l.journal_id = j.id "\
 
158
                                "AND account_id = %d AND "+query, (categ_in,account.id,))
 
159
                if categ_in == 0:
 
160
                        self.cr.execute("SELECT sum(credit) "\
 
161
                                "FROM account_move_line l "\
 
162
                                "WHERE l.account_id = %d AND "+query, (account.id,))
 
163
                return self.cr.fetchone()[0] or 0.0
 
164
 
 
165
        def _sum_debit(self, form):
 
166
                if not self.ids:
 
167
                        return 0.0
 
168
                ctx = self.context.copy()
 
169
                ctx['fiscalyear'] = form['fiscalyear']
 
170
                ctx['periods'] = form['periods'][0][2]
 
171
                query = self.pool.get('account.move.line')._query_get(self.cr, self.uid, context=ctx)
 
172
                self.cr.execute("SELECT sum(debit) "\
 
173
                                "FROM account_move_line l "\
 
174
                                "WHERE l.account_id in ("+','.join(map(str, self.ids))+") AND "+query)
 
175
                return self.cr.fetchone()[0] or 0.0
 
176
 
 
177
        def _sum_credit(self, form):
 
178
                if not self.ids:
 
179
                        return 0.0
 
180
                ctx = self.context.copy()
 
181
                ctx['fiscalyear'] = form['fiscalyear']
 
182
                ctx['periods'] = form['periods'][0][2]
 
183
                query = self.pool.get('account.move.line')._query_get(self.cr, self.uid, context=ctx)
 
184
                self.cr.execute("SELECT sum(credit) "\
 
185
                                "FROM account_move_line l "\
 
186
                                "WHERE l.account_id in ("+','.join(map(str, self.ids))+") AND "+query)
 
187
                return self.cr.fetchone()[0] or 0.0
 
188
 
 
189
report_sxw.report_sxw('report.account.consumo.suc', 'account.account', 'addons/bias_account/report/consumo_suc.rml', parser=consumo_suc, header=False)
 
190