~l-turchetti/openobject-italia/add_account_central_journal

« back to all changes in this revision

Viewing changes to l10n_it_prima_nota_cassa/report/prima_nota_cassa.py

[ADD] l10n_it_prima_nota_cassa
merging lp:~openobject-italia-core-devs/openobject-italia/adding_l10n_it_prima_nota_cassa
moving not working revisions to lp:~openobject-italia-core-devs/openobject-italia/adding_l10n_it_prima_nota_cassa_trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#    
 
4
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
5
#    Copyright (C) 2011-2012 Associazione OpenERP Italia
 
6
#    (<http://www.openerp-italia.org>). 
 
7
#    All Rights Reserved
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU Affero General Public License as published by
 
11
#    the Free Software Foundation, either version 3 of the License, or
 
12
#    (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU Affero General Public License
 
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
import time
 
25
from report import report_sxw
 
26
from account.report.common_report_header import common_report_header
 
27
from tools.translate import _
 
28
 
 
29
class print_prima_nota_cassa(report_sxw.rml_parse, common_report_header):
 
30
    _name = 'report.account.prima_nota_cassa'
 
31
 
 
32
    def set_context(self, objects, data, ids, report_type=None):
 
33
        new_ids = ids
 
34
        obj_move = self.pool.get('account.move.line')
 
35
        self.sortby = data['form'].get('sortby', 'sort_date')
 
36
        self.query = obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context',{}))
 
37
        ctx2 = data['form'].get('used_context',{}).copy()
 
38
        print _('Debit')
 
39
        self.init_balance = data['form']['initial_balance']
 
40
        self.display_account = data['form']['display_account']
 
41
        self.target_move = data['form'].get('target_move', 'all')
 
42
        self.journal_ids = data['form'].get('journal_ids', [])
 
43
        ctx = self.context.copy()
 
44
        ctx['fiscalyear'] = data['form']['fiscalyear_id']
 
45
        if data['form']['filter'] == 'filter_period':
 
46
            ctx['periods'] = data['form']['periods']
 
47
        elif data['form']['filter'] == 'filter_date':
 
48
            ctx['date_from'] = data['form']['date_from']
 
49
            ctx['date_to'] =  data['form']['date_to']
 
50
        ctx['state'] = data['form']['target_move']
 
51
        self.context.update(ctx)
 
52
        if (data['model'] == 'ir.ui.menu'):
 
53
            new_ids = [data['form']['chart_account_id']]
 
54
            objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
 
55
        return super(print_prima_nota_cassa, self).set_context(objects, data, new_ids, report_type=report_type)
 
56
 
 
57
    def __init__(self, cr, uid, name, context=None):
 
58
        if context is None:
 
59
            context = {}
 
60
        super(print_prima_nota_cassa, self).__init__(cr, uid, name, context=context)
 
61
        self.query = ""
 
62
        self.tot_currency = 0.0
 
63
        self.period_sql = ""
 
64
        self.sold_accounts = {}
 
65
        self.sortby = 'sort_date'
 
66
        self.localcontext.update( {
 
67
            'time': time,
 
68
            'lines': self.lines,
 
69
            'sum_debit_account': self._sum_debit_account,
 
70
            'sum_credit_account': self._sum_credit_account,
 
71
            'sum_balance_account': self._sum_balance_account,
 
72
            'sum_currency_amount_account': self._sum_currency_amount_account,
 
73
#            'get_children_accounts': self.get_children_accounts,
 
74
            'get_fiscalyear': self._get_fiscalyear,
 
75
            'get_journal': self._get_journal,
 
76
            'get_account': self._get_account,
 
77
            'get_start_period': self.get_start_period,
 
78
            'get_end_period': self.get_end_period,
 
79
            'get_filter': self._get_filter,
 
80
            'get_sortby': self._get_sortby,
 
81
            'get_start_date':self._get_start_date,
 
82
            'get_end_date':self._get_end_date,
 
83
            'get_target_move': self._get_target_move,
 
84
            'ipdb': self.ipdb
 
85
        })
 
86
        self.context = context
 
87
 
 
88
    def ipdb(self, data):
 
89
        import ipdb;ipdb.set_trace()
 
90
        return 'LEP WAS HERE'
 
91
 
 
92
    def _sum_currency_amount_account(self, account):
 
93
        self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
 
94
                FROM account_move_line l \
 
95
                WHERE l.account_id = %s AND %s' %(account.id, self.query))
 
96
        sum_currency = self.cr.fetchone()[0] or 0.0
 
97
        if self.init_balance:
 
98
            self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
 
99
                            FROM account_move_line l \
 
100
                            WHERE l.account_id = %s AND %s '%(account.id, self.init_query))
 
101
            sum_currency += self.cr.fetchone()[0] or 0.0
 
102
        return sum_currency
 
103
 
 
104
    def get_children_accounts(self, account):
 
105
        """ Return all the accounts that are children of the chosen main one
 
106
        and are set as default for the selected cash and bank accounts"""
 
107
 
 
108
        currency_obj = self.pool.get('res.currency')
 
109
        journal_obj = self.pool.get('account.journal')
 
110
        
 
111
        cash_bank_journals = journal_obj.search(self.cr, self.uid, [ ('type','in',('bank','cash')) ] )
 
112
        
 
113
        cash_bank_accounts = [journal_obj.browse(self.cr, self.uid, j).default_credit_account_id.id for j in cash_bank_journals] + \
 
114
            [journal_obj.browse(self.cr, self.uid, j).default_debit_account_id.id for j in cash_bank_journals]
 
115
        
 
116
        ids_acc = [acc for acc in self.pool.get('account.account')._get_children_and_consol(self.cr, self.uid, account.id) \
 
117
            if acc in cash_bank_accounts]
 
118
        
 
119
        currency = account.currency_id and account.currency_id or account.company_id.currency_id
 
120
        
 
121
        return ids_acc
 
122
 
 
123
    def lines(self, main_account):
 
124
        """ Return all the account_move_line of account with their account code counterparts """
 
125
        #import ipdb;ipdb.set_trace()
 
126
        account_ids = self.get_children_accounts(main_account)
 
127
        
 
128
        move_state = ['draft','posted']
 
129
        if self.target_move == 'posted':
 
130
            move_state = ['posted', '']
 
131
 
 
132
        # Then select all account_move_line of this account
 
133
        if self.sortby == 'sort_journal_partner':
 
134
            sql_sort='j.code, p.name, l.move_id'
 
135
        else:
 
136
            sql_sort='l.date, l.move_id'
 
137
        sql = """
 
138
            SELECT
 
139
                l.id AS lid,
 
140
                l.date AS ldate,
 
141
                j.code AS lcode,
 
142
                j.name AS jname,
 
143
                l.currency_id,
 
144
                l.amount_currency,
 
145
                l.ref AS lref,
 
146
                l.name AS lname,
 
147
                COALESCE(l.debit,0) AS debit,
 
148
                COALESCE(l.credit,0) AS credit,
 
149
                l.period_id AS lperiod_id,
 
150
                l.partner_id AS lpartner_id,
 
151
                m.name AS move_name,
 
152
                m.id AS mmove_id,
 
153
                per.code as period_code,
 
154
                c.symbol AS currency_code,
 
155
                i.id AS invoice_id,
 
156
                i.type AS invoice_type,
 
157
                i.number AS invoice_number,
 
158
                p.name AS partner_name
 
159
            FROM account_move_line l
 
160
            JOIN account_move m on (l.move_id=m.id)
 
161
            LEFT JOIN res_currency c on (l.currency_id=c.id)
 
162
            LEFT JOIN res_partner p on (l.partner_id=p.id)
 
163
            LEFT JOIN account_invoice i on (m.id =i.move_id)
 
164
            LEFT JOIN account_period per on (per.id=l.period_id)
 
165
            JOIN account_journal j on (l.journal_id=j.id)
 
166
            WHERE %s
 
167
                AND m.state IN %s 
 
168
                AND l.account_id in %%s
 
169
            ORDER by %s
 
170
        """ %(self.query, tuple(move_state), sql_sort)
 
171
        self.cr.execute(sql, (tuple(account_ids),))
 
172
        res = self.cr.dictfetchall()
 
173
        for l in res:
 
174
            l['move'] = l['move_name'] != '/' and l['move_name'] or ('*'+str(l['mmove_id']))
 
175
            l['partner'] = l['partner_name'] or ''
 
176
            # Modification of amount Currency
 
177
            if l['credit'] > 0:
 
178
                if l['amount_currency'] != None:
 
179
                    l['amount_currency'] = abs(l['amount_currency']) * -1
 
180
            if l['amount_currency'] != None:
 
181
                self.tot_currency = self.tot_currency + l['amount_currency']
 
182
        return res
 
183
 
 
184
    def _sum_total_debit(self, account):
 
185
        move_state = ['draft','posted']
 
186
        
 
187
        account_ids = self.get_children_accounts(main_account)
 
188
        
 
189
        if self.target_move == 'posted':
 
190
            move_state = ['posted','']
 
191
        self.cr.execute('SELECT sum(debit) \
 
192
                FROM account_move_line l \
 
193
                JOIN account_move am ON (am.id = l.move_id) \
 
194
                WHERE (l.account_id = %s) \
 
195
                AND (am.state IN %s) \
 
196
                AND '+ self.query +' '
 
197
                ,(account.id, tuple(move_state)))
 
198
        sum_debit = self.cr.fetchone()[0] or 0.0
 
199
        if self.init_balance:
 
200
            self.cr.execute('SELECT sum(debit) \
 
201
                    FROM account_move_line l \
 
202
                    JOIN account_move am ON (am.id = l.move_id) \
 
203
                    WHERE (l.account_id = %s) \
 
204
                    AND (am.state IN %s) \
 
205
                    AND '+ self.init_query +' '
 
206
                    ,(account.id, tuple(move_state)))
 
207
            # Add initial balance to the result
 
208
            sum_debit += self.cr.fetchone()[0] or 0.0
 
209
        return sum_debit
 
210
 
 
211
    def _sum_debit_account(self, account):
 
212
        if account.type == 'view':
 
213
            return account.debit
 
214
        move_state = ['draft','posted']
 
215
        if self.target_move == 'posted':
 
216
            move_state = ['posted','']
 
217
        self.cr.execute('SELECT sum(debit) \
 
218
                FROM account_move_line l \
 
219
                JOIN account_move am ON (am.id = l.move_id) \
 
220
                WHERE (l.account_id = %s) \
 
221
                AND (am.state IN %s) \
 
222
                AND '+ self.query +' '
 
223
                ,(account.id, tuple(move_state)))
 
224
        sum_debit = self.cr.fetchone()[0] or 0.0
 
225
        if self.init_balance:
 
226
            self.cr.execute('SELECT sum(debit) \
 
227
                    FROM account_move_line l \
 
228
                    JOIN account_move am ON (am.id = l.move_id) \
 
229
                    WHERE (l.account_id = %s) \
 
230
                    AND (am.state IN %s) \
 
231
                    AND '+ self.init_query +' '
 
232
                    ,(account.id, tuple(move_state)))
 
233
            # Add initial balance to the result
 
234
            sum_debit += self.cr.fetchone()[0] or 0.0
 
235
        return sum_debit
 
236
 
 
237
    def _sum_credit_account(self, account):
 
238
        if account.type == 'view':
 
239
            return account.credit
 
240
        move_state = ['draft','posted']
 
241
        if self.target_move == 'posted':
 
242
            move_state = ['posted','']
 
243
        self.cr.execute('SELECT sum(credit) \
 
244
                FROM account_move_line l \
 
245
                JOIN account_move am ON (am.id = l.move_id) \
 
246
                WHERE (l.account_id = %s) \
 
247
                AND (am.state IN %s) \
 
248
                AND '+ self.query +' '
 
249
                ,(account.id, tuple(move_state)))
 
250
        sum_credit = self.cr.fetchone()[0] or 0.0
 
251
        if self.init_balance:
 
252
            self.cr.execute('SELECT sum(credit) \
 
253
                    FROM account_move_line l \
 
254
                    JOIN account_move am ON (am.id = l.move_id) \
 
255
                    WHERE (l.account_id = %s) \
 
256
                    AND (am.state IN %s) \
 
257
                    AND '+ self.init_query +' '
 
258
                    ,(account.id, tuple(move_state)))
 
259
            # Add initial balance to the result
 
260
            sum_credit += self.cr.fetchone()[0] or 0.0
 
261
        return sum_credit
 
262
 
 
263
    def _sum_balance_account(self, account):
 
264
        if account.type == 'view':
 
265
            return account.balance
 
266
        move_state = ['draft','posted']
 
267
        if self.target_move == 'posted':
 
268
            move_state = ['posted','']
 
269
        self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
 
270
                FROM account_move_line l \
 
271
                JOIN account_move am ON (am.id = l.move_id) \
 
272
                WHERE (l.account_id = %s) \
 
273
                AND (am.state IN %s) \
 
274
                AND '+ self.query +' '
 
275
                ,(account.id, tuple(move_state)))
 
276
        sum_balance = self.cr.fetchone()[0] or 0.0
 
277
        if self.init_balance:
 
278
            self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
 
279
                    FROM account_move_line l \
 
280
                    JOIN account_move am ON (am.id = l.move_id) \
 
281
                    WHERE (l.account_id = %s) \
 
282
                    AND (am.state IN %s) \
 
283
                    AND '+ self.init_query +' '
 
284
                    ,(account.id, tuple(move_state)))
 
285
            # Add initial balance to the result
 
286
            sum_balance += self.cr.fetchone()[0] or 0.0
 
287
        return sum_balance
 
288
 
 
289
    def _get_account(self, data):
 
290
        if data['model'] == 'account.account':
 
291
            return self.pool.get('account.account').browse(self.cr, self.uid, data['form']['id']).company_id.name
 
292
        return super(print_prima_nota_cassa ,self)._get_account(data)
 
293
 
 
294
    def _get_sortby(self, data):
 
295
        if self.sortby == 'sort_date':
 
296
            return 'Date'
 
297
        elif self.sortby == 'sort_journal_partner':
 
298
            return 'Journal & Partner'
 
299
        return 'Date'
 
300
        
 
301
report_sxw.report_sxw('report.account.print.prima_nota_cassa',
 
302
                      'account.account',
 
303
                      'addons/l10n_it_prima_nota_cassa/report/prima_nota_cassa.mako',
 
304
                      parser=print_prima_nota_cassa)
 
305
 
 
306
 
 
307
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: