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 datetime import datetime
24 from dateutil.relativedelta import relativedelta
25 from operator import itemgetter
29 from osv import fields, osv
30 import decimal_precision as dp
31 from tools.translate import _
33 # ---------------------------------------------------------
34 # Account Financial Report
35 # ---------------------------------------------------------
37 class account_financial_report(osv.osv):
38 _name = "account.financial.report"
39 _description = "Account Report"
41 def _get_level(self, cr, uid, ids, field_name, arg, context=None):
43 for report in self.browse(cr, uid, ids, context=context):
46 level = report.parent_id.level + 1
47 res[report.id] = level
50 def _get_children_by_order(self, cr, uid, ids, context=None):
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)
58 def _get_balance(self, cr, uid, ids, name, args, context=None):
59 account_obj = self.pool.get('account.account')
62 for report in self.browse(cr, uid, ids, context=context):
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:
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):
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)
80 for key, value in res2.items():
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)
87 for key, value in res2.items():
89 res[report.id] = balance
90 res_all[report.id] = balance
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([
102 ('accounts','Accounts'),
103 ('account_type','Account Type'),
104 ('account_report','Report Value'),
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.'),
119 'display_detail': 'detail_flat',
123 account_financial_report()
125 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: