Merge remote-tracking branch 'odoo/7.0' into 7.0
[odoo/odoo.git] / addons / account / wizard / account_report_aged_partner_balance.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 time
23 from datetime import datetime
24 from dateutil.relativedelta import relativedelta
25 from openerp.osv import fields, osv
26 from openerp.tools.translate import _
27
28 class account_aged_trial_balance(osv.osv_memory):
29     _inherit = 'account.common.partner.report'
30     _name = 'account.aged.trial.balance'
31     _description = 'Account Aged Trial balance Report'
32
33     _columns = {
34         'period_length':fields.integer('Period Length (days)', required=True),
35         'direction_selection': fields.selection([('past','Past'),
36                                                  ('future','Future')],
37                                                  'Analysis Direction', required=True),
38         'journal_ids': fields.many2many('account.journal', 'account_aged_trial_balance_journal_rel', 'account_id', 'journal_id', 'Journals', required=True),
39     }
40     _defaults = {
41         'period_length': 30,
42         'date_from': lambda *a: time.strftime('%Y-%m-%d'),
43         'direction_selection': 'past',
44     }
45
46     def _print_report(self, cr, uid, ids, data, context=None):
47         res = {}
48         if context is None:
49             context = {}
50
51         data = self.pre_print_report(cr, uid, ids, data, context=context)
52         data['ids'] = context.get('active_ids', [])
53         data['model'] = context.get('active_model', 'ir.ui.menu')
54         data['form'].update(self.read(cr, uid, ids, ['period_length', 'direction_selection'])[0])
55
56         period_length = data['form']['period_length']
57         if period_length<=0:
58             raise osv.except_osv(_('User Error!'), _('You must set a period length greater than 0.'))
59         if not data['form']['date_from']:
60             raise osv.except_osv(_('User Error!'), _('You must set a start date.'))
61
62         start = datetime.strptime(data['form']['date_from'], "%Y-%m-%d")
63
64         if data['form']['direction_selection'] == 'past':
65             for i in range(5)[::-1]:
66                 stop = start - relativedelta(days=period_length)
67                 res[str(i)] = {
68                     'name': (i!=0 and (str((5-(i+1)) * period_length) + '-' + str((5-i) * period_length)) or ('+'+str(4 * period_length))),
69                     'stop': start.strftime('%Y-%m-%d'),
70                     'start': (i!=0 and stop.strftime('%Y-%m-%d') or False),
71                 }
72                 start = stop - relativedelta(days=1)
73         else:
74             for i in range(5):
75                 stop = start + relativedelta(days=period_length)
76                 res[str(5-(i+1))] = {
77                     'name': (i!=4 and str((i) * period_length)+'-' + str((i+1) * period_length) or ('+'+str(4 * period_length))),
78                     'start': start.strftime('%Y-%m-%d'),
79                     'stop': (i!=4 and stop.strftime('%Y-%m-%d') or False),
80                 }
81                 start = stop + relativedelta(days=1)
82         data['form'].update(res)
83         return {
84             'type': 'ir.actions.report.xml',
85             'report_name': 'account.aged_trial_balance',
86             'datas': data
87         }
88
89 account_aged_trial_balance()
90
91 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: