~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to account_financial_report/report/invoice_list.py

  • Committer: Borja L.S.
  • Date: 2010-04-07 17:17:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4498.
  • Revision ID: borjals@pexego.es-20100407171706-bdyhd0essc5237g2
[ADD] account_financial_report: Added the module from the 5.0 extra-addons.

  This module from the 5.0 extra-addons was missing on the 
  trunk extra-addons branch.
 
  We have just tested that it works with the 5.2 version.

  The module adds some extra financial/accounting reports:
  * Account chart list
  * Invoice list
  * Account move (journal ledger)
  * Account move line
  * Account balance compared period-fiscal year
  * Cumulative general ledger

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
#    Created by Nicolas Bessi on 13.10.08.
 
6
#    Copyright (c) 2008 CamptoCamp. All rights reserved.
 
7
#    Copyright (c) 2009 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
 
8
#                       Jordi Esteve <jesteve@zikzakmedia.com>
 
9
#
 
10
# WARNING: This program as such is intended to be used by professional
 
11
# programmers who take the whole responsability of assessing all potential
 
12
# consequences resulting from its eventual inadequacies and bugs
 
13
# End users who are looking for a ready-to-use solution with commercial
 
14
# garantees and support are strongly adviced to contract a Free Software
 
15
# Service Company
 
16
#
 
17
# This program is Free Software; you can redistribute it and/or
 
18
# modify it under the terms of the GNU General Public License
 
19
# as published by the Free Software Foundation; either version 2
 
20
# of the License, or (at your option) any later version.
 
21
#
 
22
# This program is distributed in the hope that it will be useful,
 
23
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
24
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
25
# GNU General Public License for more details.
 
26
#
 
27
# You should have received a copy of the GNU General Public License
 
28
# along with this program; if not, write to the Free Software
 
29
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
30
#
 
31
##############################################################################
 
32
 
 
33
import time
 
34
from mx.DateTime import *
 
35
from report import report_sxw
 
36
import xml
 
37
import rml_parse
 
38
import pooler
 
39
 
 
40
class print_invoice_list(rml_parse.rml_parse):
 
41
    """ Report that print invoices grouped by currency and type """
 
42
    _name = 'report.account.print_invoice_list'
 
43
 
 
44
 
 
45
    def __init__(self, cr, uid, name, context):
 
46
        super(print_invoice_list, self).__init__(cr, uid, name, context)
 
47
        #contain tuples of in invoices : (curreny, [browse records], currency_untaxed, currency_tax, currency_total)
 
48
        self.in_invoices = []
 
49
        #contain tuples of in refunds : (curreny, [browse records], currency_untaxed, currency_tax, currency_total)
 
50
        self.in_refunds = []
 
51
        #contain tuples of out invoices : (curreny, [browse records], currency_untaxed, currency_tax, currency_total)
 
52
        self.out_invoices = []
 
53
        #contain tuples of out refunds : (curreny, [browse records], currency_untaxed, currency_tax, currency_total)
 
54
        self.out_refunds = []
 
55
        self.localcontext.update({
 
56
            'time': time,
 
57
            'in_invoices': self.in_invoices,
 
58
            'in_refunds': self.in_refunds,
 
59
            'out_invoices': self.out_invoices,
 
60
            'out_refunds': self.out_refunds,
 
61
            'detailed_taxes': False,
 
62
        })
 
63
 
 
64
 
 
65
    def set_context(self, objects, data, ids, report_type = None):
 
66
        """We do the grouping and proccessing of invoices"""
 
67
        invoice_obj = self.pool.get('account.invoice')
 
68
 
 
69
        if data['model'] == 'ir.ui.menu':
 
70
            invoice_types = []
 
71
            if data['form']['out_invoice']:
 
72
                invoice_types.append('out_invoice')
 
73
            if data['form']['out_refund']:
 
74
                invoice_types.append('out_refund')
 
75
            if data['form']['in_invoice']:
 
76
                invoice_types.append('in_invoice')
 
77
            if data['form']['in_refund']:
 
78
                invoice_types.append('in_refund')
 
79
            invoice_states = []
 
80
            if data['form']['draft']:
 
81
                invoice_states.append('draft')
 
82
            if data['form']['proforma']:
 
83
                invoice_states.append('proforma')
 
84
                invoice_states.append('proforma2')
 
85
            if data['form']['open']:
 
86
                invoice_states.append('open')
 
87
            if data['form']['paid']:
 
88
                invoice_states.append('paid')
 
89
            if data['form']['cancel']:
 
90
                invoice_states.append('cancel')
 
91
            where = [('company_id','=',data['form']['company_id']),('type','in',invoice_types),('state','in',invoice_states)]
 
92
            if data['form']['state'] in ['byperiod','all']:
 
93
                period_ids = data['form']['periods'][0][2]
 
94
                periods = ','.join([str(id) for id in period_ids])
 
95
                where.append(('period_id','in',period_ids))
 
96
            if data['form']['state'] in ['bydate','all','none']:
 
97
                where.append(('date_invoice','>=',data['form']['date_from']))
 
98
                where.append(('date_invoice','<=',data['form']['date_to']))
 
99
            #print where
 
100
            ids = invoice_obj.search(self.cr, self.uid, where)
 
101
            objects = invoice_obj.browse(self.cr, self.uid, ids)
 
102
 
 
103
            self.localcontext.update({
 
104
                'detailed_taxes': data['form'].get('detailed_taxes'),
 
105
            })
 
106
 
 
107
        if not ids :
 
108
            return super(print_invoice_list, self).set_context(objects, data, ids, report_type)
 
109
        
 
110
        if not isinstance(ids, list) :
 
111
            ids = [ids]
 
112
        # we create temp list that will be used for store invoices by type
 
113
        ininv = []
 
114
        outinv = []
 
115
        inref = []
 
116
        outref = []
 
117
        # we get the invoices and sort them by types
 
118
        invoices = invoice_obj.browse(self.cr, self.uid, ids)
 
119
        for inv in invoices :
 
120
            if inv.type == ('in_invoice'):
 
121
                ininv.append(inv)
 
122
            if inv.type == ('in_refund'):
 
123
                inref.append(inv)
 
124
            if inv.type == ('out_invoice'):
 
125
                outinv.append(inv)
 
126
            if inv.type == ('out_refund'):
 
127
                outref.append(inv)
 
128
        # we process the invoice and attribute them to the property
 
129
        self.filter_invoices(ininv, self.in_invoices)
 
130
        self.filter_invoices(outinv, self.out_invoices)
 
131
        self.filter_invoices(inref, self.in_refunds)
 
132
        self.filter_invoices(outref, self.out_refunds)
 
133
        super(print_invoice_list, self).set_context(objects, data, ids, report_type)
 
134
 
 
135
 
 
136
    def filter_invoices(self, list, dest) :
 
137
        if not list :
 
138
            return 
 
139
        tmp = {}
 
140
        #We sort the invoice by currency in a tmp dict
 
141
        #{'currency':[browse, records]}
 
142
        for inv in list:
 
143
            currency = inv.currency_id.name
 
144
            if tmp.has_key(currency) :
 
145
                tmp[currency].append(inv)
 
146
            else :
 
147
                tmp[currency] = [inv]
 
148
        #We compute the total by currency 
 
149
        for curr in tmp:
 
150
            untaxed = tax = total = 0
 
151
            for tmpinv in tmp[curr]:
 
152
                untaxed += tmpinv.amount_untaxed
 
153
                tax += tmpinv.amount_tax
 
154
                total += tmpinv.amount_total
 
155
            #We append the tuple to the property
 
156
            dest.append((curr, tmp[curr], untaxed, tax, total))
 
157
        del tmp
 
158
 
 
159
 
 
160
report_sxw.report_sxw('report.account.invoice.list.report', 'account.invoice', 'addons/account_financial_report/report/invoice_list.rml', parser=print_invoice_list, header=False)