[FIX]: improvement and fix proposed by the pap@tinyerp.com
[odoo/odoo.git] / addons / account / report / account_entries_report.py
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 tools
23 from osv import fields,osv
24
25 class account_entries_report(osv.osv):
26     _name = "account.entries.report"
27     _description = "Journal Items Analysis"
28     _auto = False
29     _rec_name = 'date'
30     _columns = {
31         'date': fields.date('Effective Date', readonly=True),
32         'date_created': fields.date('Date Created', readonly=True),
33         'date_maturity': fields.date('Date Maturity', readonly=True),
34         'ref': fields.char('Reference', size=64, readonly=True),
35         'nbr':fields.integer('# of Items', readonly=True),
36         'debit':fields.float('Debit', readonly=True),
37         'credit':fields.float('Credit', readonly=True),
38         'balance': fields.float('Balance', readonly=True),
39         'day': fields.char('Day', size=128, readonly=True),
40         'year': fields.char('Year', size=4, readonly=True),
41         'date': fields.date('Date', size=128, readonly=True),
42         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'),
43             ('05','May'), ('06','June'), ('07','July'), ('08','August'), ('09','September'),
44             ('10','October'), ('11','November'), ('12','December')], 'Month', readonly=True),
45         'period_id': fields.many2one('account.period', 'Period', readonly=True),
46         'account_id': fields.many2one('account.account', 'Account', readonly=True),
47         'journal_id': fields.many2one('account.journal', 'Journal', readonly=True),
48         'fiscalyear_id': fields.many2one('account.fiscalyear', 'Fiscal Year', readonly=True),
49         'product_id': fields.many2one('product.product', 'Product', readonly=True),
50         'state': fields.selection([('draft','Draft'), ('posted','Posted')], 'State', readonly=True,
51                                   help='When new account move is created the state will be \'Draft\'. When all the payments are done it will be in \'Posted\' state.'),
52         'state_2': fields.selection([('draft','Draft'), ('valid','Valid')], 'State of Move Line', readonly=True,
53                                   help='When new move line is created the state will be \'Draft\'.\n* When all the payments are done it will be in \'Valid\' state.'),
54         'reconcile_id': fields.many2one('account.move.reconcile', readonly=True),
55         'partner_id': fields.many2one('res.partner','Partner', readonly=True),
56         'analytic_account_id' : fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
57         'quantity': fields.float('Products Quantity', digits=(16,2), readonly=True),
58         'user_type': fields.many2one('account.account.type', 'Account Type', readonly=True),
59         'type': fields.selection([
60             ('receivable', 'Receivable'),
61             ('payable', 'Payable'),
62             ('view', 'View'),
63             ('consolidation', 'Consolidation'),
64             ('other', 'Others'),
65             ('closed', 'Closed'),
66         ], 'Internal Type', readonly=True, help="This type is used to differentiate types with "\
67             "special effects in OpenERP: view can not have entries, consolidation are accounts that "\
68             "can have children accounts for multi-company consolidations, payable/receivable are for "\
69             "partners accounts (for debit/credit computations), closed for depreciated accounts."),
70         'company_id': fields.many2one('res.company', 'Company', readonly=True),
71     }
72     _order = 'date desc'
73     
74     def search(self, cr, uid, args, offset=0, limit=None, order=None,
75             context=None, count=False):
76         for arg in args:
77             if arg[0] == 'period_id' and arg[2] == 'current_period':
78                 current_period = self.pool.get('account.period').find(cr, uid)[0]
79                 args.append(['period_id','in',[current_period]])
80                 break
81             elif arg[0] == 'period_id' and arg[2] == 'current_year':
82                 current_year = self.pool.get('account.fiscalyear').find(cr, uid)
83                 ids = self.pool.get('account.fiscalyear').read(cr, uid, [current_year], ['period_ids'])[0]['period_ids']
84                 args.append(['period_id','in',ids])
85         for a in [['period_id','in','current_year'], ['period_id','in','current_period']]:
86             if a in args:
87                 args.remove(a)
88         return super(account_entries_report, self).search(cr, uid, args=args, offset=offset, limit=limit, order=order,
89             context=context, count=count)
90     
91     def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
92         todel=[]
93         for arg in domain:
94             if arg[0] == 'period_id' and arg[2] == 'current_period':
95                 current_period = self.pool.get('account.period').find(cr, uid)[0]
96                 domain.append(['period_id','in',[current_period]])
97                 todel.append(arg)
98                 break
99             elif arg[0] == 'period_id' and arg[2] == 'current_year':
100                 current_year = self.pool.get('account.fiscalyear').find(cr, uid)
101                 ids = self.pool.get('account.fiscalyear').read(cr, uid, [current_year], ['period_ids'])[0]['period_ids']
102                 domain.append(['period_id','in',ids])
103                 todel.append(arg)
104         for a in [['period_id','in','current_year'], ['period_id','in','current_period']]:
105             if a in domain:
106                 domain.remove(a)
107         return super(account_entries_report, self).read_group(cr, uid, domain, fields, groupby, offset, limit, context)
108     
109     def init(self, cr):
110         tools.drop_view_if_exists(cr, 'account_entries_report')
111         cr.execute("""
112             create or replace view account_entries_report as (
113             select
114                 l.id as id,
115                 am.date as date,
116                 l.date_maturity as date_maturity,
117                 l.date_created as date_created,
118                 am.ref as ref,
119                 am.state as state,
120                 l.state as state_2,
121                 l.reconcile_id as reconcile_id,
122                 to_char(am.date, 'YYYY') as year,
123                 to_char(am.date, 'MM') as month,
124                 to_char(am.date, 'YYYY-MM-DD') as day,
125                 l.partner_id as partner_id,
126                 l.product_id as product_id,
127                 am.company_id as company_id,
128                 am.journal_id as journal_id,
129                 p.fiscalyear_id as fiscalyear_id,
130                 am.period_id as period_id,
131                 l.account_id as account_id,
132                 l.analytic_account_id as analytic_account_id,
133                 a.type as type,
134                 a.user_type as user_type,
135                 1 as nbr,
136                 l.quantity as quantity,
137                 l.debit as debit,
138                 l.credit as credit,
139                 l.debit-l.credit as balance
140             from
141                 account_move_line l
142                 left join account_account a on (l.account_id = a.id)
143                 left join account_move am on (am.id=l.move_id)
144                 left join account_period p on (am.period_id=p.id)
145                 where l.state != 'draft'
146             )
147         """)
148 account_entries_report()