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