[FIX] correct various date issues in reporting
[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 from openerp import tools
23 from openerp.osv import fields,osv
24 import openerp.addons.decimal_precision as dp
25
26 class account_entries_report(osv.osv):
27     _name = "account.entries.report"
28     _description = "Journal Items Analysis"
29     _auto = False
30     _rec_name = 'date'
31     _columns = {
32         'date': fields.date('Effective Date', readonly=True),
33         'date_created': fields.date('Date Created', readonly=True),
34         'date_maturity': fields.date('Date Maturity', readonly=True),
35         'ref': fields.char('Reference', readonly=True),
36         'nbr': fields.integer('# of Items', readonly=True),
37         'debit': fields.float('Debit', readonly=True),
38         'credit': fields.float('Credit', readonly=True),
39         'balance': fields.float('Balance', readonly=True),
40         'currency_id': fields.many2one('res.currency', 'Currency', readonly=True),
41         'amount_currency': fields.float('Amount Currency', digits_compute=dp.get_precision('Account'), readonly=True),
42         'period_id': fields.many2one('account.period', 'Period', readonly=True),
43         'account_id': fields.many2one('account.account', 'Account', readonly=True),
44         'journal_id': fields.many2one('account.journal', 'Journal', readonly=True),
45         'fiscalyear_id': fields.many2one('account.fiscalyear', 'Fiscal Year', readonly=True),
46         'product_id': fields.many2one('product.product', 'Product', readonly=True),
47         'product_uom_id': fields.many2one('product.uom', 'Product Unit of Measure', readonly=True),
48         'move_state': fields.selection([('draft','Unposted'), ('posted','Posted')], 'Status', readonly=True),
49         'move_line_state': fields.selection([('draft','Unbalanced'), ('valid','Valid')], 'State of Move Line', readonly=True),
50         'reconcile_id': fields.many2one('account.move.reconcile', 'Reconciliation number', readonly=True),
51         'partner_id': fields.many2one('res.partner','Partner', readonly=True),
52         'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
53         'quantity': fields.float('Products Quantity', digits=(16,2), readonly=True),
54         'user_type': fields.many2one('account.account.type', 'Account Type', readonly=True),
55         'type': fields.selection([
56             ('receivable', 'Receivable'),
57             ('payable', 'Payable'),
58             ('cash', 'Cash'),
59             ('view', 'View'),
60             ('consolidation', 'Consolidation'),
61             ('other', 'Regular'),
62             ('closed', 'Closed'),
63         ], 'Internal Type', readonly=True, help="This type is used to differentiate types with "\
64             "special effects in Odoo: view can not have entries, consolidation are accounts that "\
65             "can have children accounts for multi-company consolidations, payable/receivable are for "\
66             "partners accounts (for debit/credit computations), closed for depreciated accounts."),
67         'company_id': fields.many2one('res.company', 'Company', readonly=True),
68     }
69
70     _order = 'date desc'
71
72     def search(self, cr, uid, args, offset=0, limit=None, order=None,
73             context=None, count=False):
74         fiscalyear_obj = self.pool.get('account.fiscalyear')
75         period_obj = self.pool.get('account.period')
76         for arg in args:
77             if arg[0] == 'period_id' and arg[2] == 'current_period':
78                 current_period = period_obj.find(cr, uid, context=context)[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 = fiscalyear_obj.find(cr, uid)
83                 ids = fiscalyear_obj.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, orderby=False,lazy=True):
92         if context is None:
93             context = {}
94         fiscalyear_obj = self.pool.get('account.fiscalyear')
95         period_obj = self.pool.get('account.period')
96         if context.get('period', False) == 'current_period':
97             current_period = period_obj.find(cr, uid, context=context)[0]
98             domain.append(['period_id','in',[current_period]])
99         elif context.get('year', False) == 'current_year':
100             current_year = fiscalyear_obj.find(cr, uid)
101             ids = fiscalyear_obj.read(cr, uid, [current_year], ['period_ids'])[0]['period_ids']
102             domain.append(['period_id','in',ids])
103         else:
104             domain = domain
105         return super(account_entries_report, self).read_group(cr, uid, domain, fields, groupby, offset, limit, context, orderby,lazy)
106
107     def init(self, cr):
108         tools.drop_view_if_exists(cr, 'account_entries_report')
109         cr.execute("""
110             create or replace view account_entries_report as (
111             select
112                 l.id as id,
113                 am.date as date,
114                 l.date_maturity as date_maturity,
115                 l.date_created as date_created,
116                 am.ref as ref,
117                 am.state as move_state,
118                 l.state as move_line_state,
119                 l.reconcile_id as reconcile_id,
120                 l.partner_id as partner_id,
121                 l.product_id as product_id,
122                 l.product_uom_id as product_uom_id,
123                 am.company_id as company_id,
124                 am.journal_id as journal_id,
125                 p.fiscalyear_id as fiscalyear_id,
126                 am.period_id as period_id,
127                 l.account_id as account_id,
128                 l.analytic_account_id as analytic_account_id,
129                 a.type as type,
130                 a.user_type as user_type,
131                 1 as nbr,
132                 l.quantity as quantity,
133                 l.currency_id as currency_id,
134                 l.amount_currency as amount_currency,
135                 l.debit as debit,
136                 l.credit as credit,
137                 l.debit-l.credit as balance
138             from
139                 account_move_line l
140                 left join account_account a on (l.account_id = a.id)
141                 left join account_move am on (am.id=l.move_id)
142                 left join account_period p on (am.period_id=p.id)
143                 where l.state != 'draft'
144             )
145         """)
146
147 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: