1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 from datetime import timedelta
25 from lxml import etree
27 from osv import fields, osv
28 from tools.translate import _
30 class account_common_report(osv.osv_memory):
31 _name = "account.common.report"
32 _description = "Account Common Report"
35 'chart_account_id': fields.many2one('account.account', 'Chart of account', help='Select Charts of Accounts', required=True, domain = [('parent_id','=',False)]),
36 'fiscalyear_id': fields.many2one('account.fiscalyear', 'Fiscal year', help='Keep empty for all open fiscal year'),
37 'filter': fields.selection([('filter_no', 'No Filters'), ('filter_date', 'Date'), ('filter_period', 'Periods')], "Filter by", required=True),
38 'period_from': fields.many2one('account.period', 'Start period'),
39 'period_to': fields.many2one('account.period', 'End period'),
40 'journal_ids': fields.many2many('account.journal', 'account_common_journal_rel', 'account_id', 'journal_id', 'Journals', required=True),
41 'date_from': fields.date("Start Date"),
42 'date_to': fields.date("End Date"),
45 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
46 mod_obj = self.pool.get('ir.model.data')
47 res = super(account_common_report, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False)
48 if context.get('active_model', False) == 'account.account' and view_id:
49 doc = etree.XML(res['arch'])
50 nodes = doc.xpath("//field[@name='chart_account_id']")
52 node.set('readonly', '1')
53 node.set('help', 'If you print the report from Account list/form view it will not consider Charts of account')
54 res['arch'] = etree.tostring(doc)
57 def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
59 if filter == 'filter_no':
60 res['value'] = {'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False}
61 if filter == 'filter_date':
62 res['value'] = {'period_from': False, 'period_to': False, 'date_from': time.strftime('%Y-01-01'), 'date_to': time.strftime('%Y-%m-%d')}
63 if filter == 'filter_period' and fiscalyear_id:
64 start_period = end_period = False
66 SELECT * FROM (SELECT p.id
68 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
70 ORDER BY p.date_start ASC
71 LIMIT 1) AS period_start
73 SELECT * FROM (SELECT p.id
75 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
77 AND p.date_start < NOW()
78 ORDER BY p.date_stop DESC
79 LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
80 periods = [i[0] for i in cr.fetchall()]
81 if periods and len(periods) > 1:
82 start_period = periods[0]
83 end_period = periods[1]
84 res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
87 def _get_account(self, cr, uid, context=None):
88 accounts = self.pool.get('account.account').search(cr, uid, [], limit=1)
89 return accounts and accounts[0] or False
91 def _get_fiscalyear(self, cr, uid, context=None):
92 now = time.strftime('%Y-%m-%d')
93 fiscalyears = self.pool.get('account.fiscalyear').search(cr, uid, [('date_start', '<', now), ('date_stop', '>', now)], limit=1 )
94 return fiscalyears and fiscalyears[0] or False
96 def _get_all_journal(self, cr, uid, context=None):
97 return self.pool.get('account.journal').search(cr, uid ,[])
100 'fiscalyear_id' : _get_fiscalyear,
101 'journal_ids': _get_all_journal,
102 'filter': 'filter_no',
103 'chart_account_id': _get_account,
106 def _build_contexts(self, cr, uid, ids, data, context=None):
110 period_obj = self.pool.get('account.period')
111 fiscal_obj = self.pool.get('account.fiscalyear')
112 result['fiscalyear'] = 'fiscalyear_id' in data['form'] and data['form']['fiscalyear_id'] or False
113 result['journal_ids'] = 'journal_ids' in data['form'] and data['form']['journal_ids'] or False
114 result['chart_account_id'] = 'chart_account_id' in data['form'] and data['form']['chart_account_id'] or False
115 result_initial_bal = result.copy()
116 if data['form']['filter'] == 'filter_date':
117 result['date_from'] = data['form']['date_from']
118 result['date_to'] = data['form']['date_to']
119 result_initial_bal['date_from'] = '0001-01-01'
120 result_initial_bal['date_to'] = (datetime.datetime.strptime(data['form']['date_from'], "%Y-%m-%d") + timedelta(days=-1)).strftime('%Y-%m-%d')
121 elif data['form']['filter'] == 'filter_period':
122 if not data['form']['period_from'] or not data['form']['period_to']:
123 raise osv.except_osv(_('Error'),_('Select a starting and an ending period'))
124 company_id = period_obj.browse(cr, uid, data['form']['period_from'], context=context).company_id
125 result['periods'] = period_obj.build_ctx_periods(cr, uid, data['form']['period_from'], data['form']['period_to'])
126 first_period = self.pool.get('account.period').search(cr, uid, [('company_id', '=', company_id)], order='date_start', limit=1)[0]
127 result_initial_bal['periods'] = period_obj.build_ctx_periods(cr, uid, first_period, data['form']['period_from'])
129 if data['form']['fiscalyear_id']:
130 fiscal_date_start = fiscal_obj.browse(cr, uid, [data['form']['fiscalyear_id']], context=context)[0].date_start
131 result_initial_bal['empty_fy_allow'] = True #Improve me => there should be something generic in account.move.line -> query get
132 result_initial_bal['fiscalyear'] = fiscal_obj.search(cr, uid, [('date_stop', '<', fiscal_date_start), ('state', '=', 'draft')], context=context)
133 result_initial_bal['date_from'] = '0001-01-01'
134 result_initial_bal['date_to'] = (datetime.datetime.strptime(fiscal_date_start, "%Y-%m-%d") + timedelta(days=-1)).strftime('%Y-%m-%d')
135 return result, result_initial_bal
137 def _print_report(self, cr, uid, ids, data, query_line, context=None):
138 raise (_('Error'), _('not implemented'))
140 def check_report(self, cr, uid, ids, context=None):
143 obj_move = self.pool.get('account.move.line')
145 data['ids'] = context.get('active_ids', [])
146 data['model'] = context.get('active_model', 'ir.ui.menu')
147 data['form'] = self.read(cr, uid, ids, ['date_from', 'date_to', 'fiscalyear_id', 'journal_ids', 'period_from', 'period_to', 'filter', 'chart_account_id'])[0]
148 used_context, used_context_initial_bal = self._build_contexts(cr, uid, ids, data, context=context)
149 query_line = obj_move._query_get(cr, uid, obj='l', context=used_context)
150 data['form']['periods'] = used_context.get('periods', False) and used_context['periods'] or []
151 data['form']['query_line'] = query_line
152 data['form']['initial_bal_query'] = obj_move._query_get(cr, uid, obj='l', context=used_context_initial_bal)
153 return self._print_report(cr, uid, ids, data, query_line, context=context)
155 account_common_report()
157 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: