[MERGE] lp:~xrg/openobject-addons/trunk-patch18
[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 osv import osv, fields
26 from 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     }
39     _defaults = {
40         'period_length': 30,
41         'date_from': lambda *a: time.strftime('%Y-%m-%d'),
42         'direction_selection': 'past',
43     }
44
45     def _print_report(self, cr, uid, ids, data, context=None):
46         res = {}
47         if context is None:
48             context = {}
49
50         data = self.pre_print_report(cr, uid, ids, data, context=context)
51         data['form'].update(self.read(cr, uid, ids, ['period_length', 'direction_selection'])[0])
52
53         period_length = data['form']['period_length']
54         if period_length<=0:
55             raise osv.except_osv(_('UserError'), _('You must enter a period length that cannot be 0 or below !'))
56         if not data['form']['date_from']:
57             raise osv.except_osv(_('UserError'), _('Enter a Start date !'))
58
59         start = datetime.strptime(data['form']['date_from'], "%Y-%m-%d")
60
61         if data['form']['direction_selection'] == 'past':
62             for i in range(5)[::-1]:
63                 stop = start - relativedelta(days=period_length)
64                 res[str(i)] = {
65                     'name': (i!=0 and (str((5-(i+1)) * period_length) + '-' + str((5-i) * period_length)) or ('+'+str(4 * period_length))),
66                     'stop': start.strftime('%Y-%m-%d'),
67                     'start': (i!=0 and stop.strftime('%Y-%m-%d') or False),
68                 }
69                 start = stop - relativedelta(days=1)
70         else:
71             for i in range(5):
72                 stop = start + relativedelta(days=period_length)
73                 res[str(5-(i+1))] = {
74                     'name': (i!=4 and str((i) * period_length)+'-' + str((i+1) * period_length) or ('+'+str(4 * period_length))),
75                     'start': start.strftime('%Y-%m-%d'),
76                     'stop': (i!=4 and stop.strftime('%Y-%m-%d') or False),
77                 }
78                 start = stop + relativedelta(days=1)
79         data['form'].update(res)
80         if data.get('form',False):
81             data['ids']=[data['form'].get('chart_account_id',False)]
82         return {
83             'type': 'ir.actions.report.xml',
84             'report_name': 'account.aged_trial_balance',
85             'datas': data
86         }
87
88 account_aged_trial_balance()
89
90 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: