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 ##############################################################################
23 from lxml import etree
25 from osv import fields, osv
26 from tools.translate import _
27 from openerp.osv.orm import setup_modifiers
29 class account_common_report(osv.osv_memory):
30 _name = "account.common.report"
31 _description = "Account Common Report"
33 def onchange_chart_id(self, cr, uid, ids, chart_account_id=False, context=None):
36 company_id = self.pool.get('account.account').browse(cr, uid, chart_account_id, context=context).company_id.id
37 res['value'] = {'company_id': company_id}
41 'chart_account_id': fields.many2one('account.account', 'Chart of Account', help='Select Charts of Accounts', required=True, domain = [('parent_id','=',False)]),
42 'company_id': fields.related('chart_account_id', 'company_id', type='many2one', relation='res.company', string='Company', readonly=True),
43 'fiscalyear_id': fields.many2one('account.fiscalyear', 'Fiscal Year', help='Keep empty for all open fiscal year'),
44 'filter': fields.selection([('filter_no', 'No Filters'), ('filter_date', 'Date'), ('filter_period', 'Periods')], "Filter by", required=True),
45 'period_from': fields.many2one('account.period', 'Start Period'),
46 'period_to': fields.many2one('account.period', 'End Period'),
47 'journal_ids': fields.many2many('account.journal', string='Journals', required=True),
48 'date_from': fields.date("Start Date"),
49 'date_to': fields.date("End Date"),
50 'target_move': fields.selection([('posted', 'All Posted Entries'),
51 ('all', 'All Entries'),
52 ], 'Target Moves', required=True),
56 def _check_company_id(self, cr, uid, ids, context=None):
57 for wiz in self.browse(cr, uid, ids, context=context):
58 company_id = wiz.company_id.id
59 if wiz.fiscalyear_id and company_id != wiz.fiscalyear_id.company_id.id:
61 if wiz.period_from and company_id != wiz.period_from.company_id.id:
63 if wiz.period_to and company_id != wiz.period_to.company_id.id:
68 (_check_company_id, 'The fiscalyear, periods or chart of account chosen have to belong to the same company.', ['chart_account_id','fiscalyear_id','period_from','period_to']),
71 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
72 if context is None:context = {}
73 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)
74 if context.get('active_model', False) == 'account.account':
75 doc = etree.XML(res['arch'])
76 nodes = doc.xpath("//field[@name='chart_account_id']")
78 node.set('readonly', '1')
79 node.set('help', 'If you print the report from Account list/form view it will not consider Charts of account')
80 setup_modifiers(node, res['fields']['chart_account_id'])
81 res['arch'] = etree.tostring(doc)
84 def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
86 if filter == 'filter_no':
87 res['value'] = {'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False}
88 if filter == 'filter_date':
89 res['value'] = {'period_from': False, 'period_to': False, 'date_from': time.strftime('%Y-01-01'), 'date_to': time.strftime('%Y-%m-%d')}
90 if filter == 'filter_period' and fiscalyear_id:
91 start_period = end_period = False
93 SELECT * FROM (SELECT p.id
95 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
98 ORDER BY p.date_start ASC, p.special ASC
99 LIMIT 1) AS period_start
101 SELECT * FROM (SELECT p.id
102 FROM account_period p
103 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
105 AND p.date_start < NOW()
106 AND p.special = false
107 ORDER BY p.date_stop DESC
108 LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
109 periods = [i[0] for i in cr.fetchall()]
110 if periods and len(periods) > 1:
111 start_period = periods[0]
112 end_period = periods[1]
113 res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
116 def _get_account(self, cr, uid, context=None):
117 user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
118 accounts = self.pool.get('account.account').search(cr, uid, [('parent_id', '=', False), ('company_id', '=', user.company_id.id)], limit=1)
119 return accounts and accounts[0] or False
121 def _get_fiscalyear(self, cr, uid, context=None):
122 now = time.strftime('%Y-%m-%d')
124 ids = context.get('active_ids', [])
125 if ids and context.get('active_model') == 'account.account':
126 company_id = self.pool.get('account.account').browse(cr, uid, ids[0], context=context).company_id.id
127 fiscalyears = self.pool.get('account.fiscalyear').search(cr, uid, [('date_start', '<', now), ('date_stop', '>', now), ('company_id', '=', company_id)], limit=1)
128 return fiscalyears and fiscalyears[0] or False
130 def _get_all_journal(self, cr, uid, context=None):
131 return self.pool.get('account.journal').search(cr, uid ,[])
134 'fiscalyear_id': _get_fiscalyear,
135 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.common.report',context=c),
136 'journal_ids': _get_all_journal,
137 'filter': 'filter_no',
138 'chart_account_id': _get_account,
139 'target_move': 'posted',
142 def _build_contexts(self, cr, uid, ids, data, context=None):
146 result['fiscalyear'] = 'fiscalyear_id' in data['form'] and data['form']['fiscalyear_id'] or False
147 result['journal_ids'] = 'journal_ids' in data['form'] and data['form']['journal_ids'] or False
148 result['chart_account_id'] = 'chart_account_id' in data['form'] and data['form']['chart_account_id'] or False
149 if data['form']['filter'] == 'filter_date':
150 result['date_from'] = data['form']['date_from']
151 result['date_to'] = data['form']['date_to']
152 elif data['form']['filter'] == 'filter_period':
153 if not data['form']['period_from'] or not data['form']['period_to']:
154 raise osv.except_osv(_('Error!'),_('Select a starting and an ending period.'))
155 result['period_from'] = data['form']['period_from']
156 result['period_to'] = data['form']['period_to']
159 def _print_report(self, cr, uid, ids, data, context=None):
160 raise (_('Error!'), _('Not implemented.'))
162 def check_report(self, cr, uid, ids, context=None):
166 data['ids'] = context.get('active_ids', [])
167 data['model'] = context.get('active_model', 'ir.ui.menu')
168 data['form'] = self.read(cr, uid, ids, ['date_from', 'date_to', 'fiscalyear_id', 'journal_ids', 'period_from', 'period_to', 'filter', 'chart_account_id', 'target_move'], context=context)[0]
169 for field in ['fiscalyear_id', 'chart_account_id', 'period_from', 'period_to']:
170 if isinstance(data['form'][field], tuple):
171 data['form'][field] = data['form'][field][0]
172 used_context = self._build_contexts(cr, uid, ids, data, context=context)
173 data['form']['periods'] = used_context.get('periods', False) and used_context['periods'] or []
174 data['form']['used_context'] = used_context
175 return self._print_report(cr, uid, ids, data, context=context)
177 account_common_report()
179 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: