Launchpad automatic translations update.
[odoo/odoo.git] / addons / account / account_financial_report.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 operator import itemgetter
26
27 import netsvc
28 import pooler
29 from osv import fields, osv
30 import decimal_precision as dp
31 from tools.translate import _
32
33 # ---------------------------------------------------------
34 # Account Financial Report
35 # ---------------------------------------------------------
36
37 class account_financial_report(osv.osv):
38     _name = "account.financial.report"
39     _description = "Account Report"
40
41     def _get_level(self, cr, uid, ids, field_name, arg, context=None):
42         res = {}
43         for report in self.browse(cr, uid, ids, context=context):
44             level = 0
45             if report.parent_id:
46                 level = report.parent_id.level + 1
47             res[report.id] = level
48         return res
49
50     def _get_children_by_order(self, cr, uid, ids, context=None):
51         res = []
52         for id in ids:
53             res.append(id)
54             ids2 = self.search(cr, uid, [('parent_id', '=', id)], order='sequence ASC', context=context)
55             res += self._get_children_by_order(cr, uid, ids2, context=context)
56         return res
57
58     def _get_balance(self, cr, uid, ids, name, args, context=None):
59         account_obj = self.pool.get('account.account')
60         res = {}
61         res_all = {}
62         for report in self.browse(cr, uid, ids, context=context):
63             balance = 0.0
64             if report.id in res_all:
65                 balance = res_all[report.id]
66             elif report.type == 'accounts':
67                 # it's the sum of balance of the linked accounts
68                 for a in report.account_ids:
69                     balance += a.balance
70             elif report.type == 'account_type':
71                 # it's the sum of balance of the leaf accounts with such an account type
72                 report_types = [x.id for x in report.account_type_ids]
73                 account_ids = account_obj.search(cr, uid, [('user_type','in', report_types), ('type','!=','view')], context=context)
74                 for a in account_obj.browse(cr, uid, account_ids, context=context):
75                     balance += a.balance
76             elif report.type == 'account_report' and report.account_report_id:
77                 # it's the amount of the linked report
78                 res2 = self._get_balance(cr, uid, [report.account_report_id.id], 'balance', False, context=context)
79                 res_all.update(res2)
80                 for key, value in res2.items():
81                     balance += value
82             elif report.type == 'sum':
83                 # it's the sum of balance of the children of this account.report
84                 #for child in report.children_ids:
85                 res2 = self._get_balance(cr, uid, [rec.id for rec in report.children_ids], 'balance', False, context=context)
86                 res_all.update(res2)
87                 for key, value in res2.items():
88                     balance += value
89             res[report.id] = balance
90             res_all[report.id] = balance
91         return res
92
93     _columns = {
94         'name': fields.char('Report Name', size=128, required=True, translate=True),
95         'parent_id': fields.many2one('account.financial.report', 'Parent'),
96         'children_ids':  fields.one2many('account.financial.report', 'parent_id', 'Account Report'),
97         'sequence': fields.integer('Sequence'),
98         'balance': fields.function(_get_balance, 'Balance'),
99         'level': fields.function(_get_level, string='Level', store=True, type='integer'),
100         'type': fields.selection([
101             ('sum','View'),
102             ('accounts','Accounts'),
103             ('account_type','Account Type'),
104             ('account_report','Report Value'),
105             ],'Type'),
106         'account_ids': fields.many2many('account.account', 'account_account_financial_report', 'report_line_id', 'account_id', 'Accounts'),
107         'display_detail': fields.selection([
108             ('no_detail','No detail'),
109             ('detail_flat','Display children flat'),
110             ('detail_with_hierarchy','Display children with hierarchy')
111             ], 'Display details'),
112         'account_report_id':  fields.many2one('account.financial.report', 'Report Value'),
113         'account_type_ids': fields.many2many('account.account.type', 'account_account_financial_report_type', 'report_id', 'account_type_id', 'Account Types'),
114         'sign': fields.selection([(-1, 'Reverse balance sign'), (1, 'Preserve balance sign')], 'Sign on Reports', required=True, help='For accounts that are typically more debited than credited and that you would like to print as negative amounts in your reports, you should reverse the sign of the balance; e.g.: Expense account. The same applies for accounts that are typically more credited than debited and that you would like to print as positive amounts in your reports; e.g.: Income account.'),
115     }
116
117     _defaults = {
118         'type': 'sum',
119         'display_detail': 'detail_flat',
120         'sign': 1,
121     }
122
123 account_financial_report()
124
125 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: