[MERGE]: Merged with lp:openobject-addons
[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, field_names, args, context=None):
59         account_obj = self.pool.get('account.account')
60         res = {}
61         for report in self.browse(cr, uid, ids, context=context):
62             if report.id in res:
63                 continue
64             res[report.id] = dict((fn, 0.0) for fn in field_names)
65             if report.type == 'accounts':
66                 # it's the sum of the linked accounts
67                 for a in report.account_ids:
68                     for field in field_names:
69                         res[report.id][field] += getattr(a, field)
70             elif report.type == 'account_type':
71                 # it's the sum 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                     for field in field_names:
76                         res[report.id][field] += getattr(a, field)
77             elif report.type == 'account_report' and report.account_report_id:
78                 # it's the amount of the linked report
79                 res2 = self._get_balance(cr, uid, [report.account_report_id.id], field_names, False, context=context)
80                 for key, value in res2.items():
81                     for field in field_names:
82                         res[report.id][field] += value[field]
83             elif report.type == 'sum':
84                 # it's the sum of the children of this account.report
85                 res2 = self._get_balance(cr, uid, [rec.id for rec in report.children_ids], field_names, False, context=context)
86                 for key, value in res2.items():
87                     for field in field_names:
88                         res[report.id][field] += value[field]
89         return res
90
91     _columns = {
92         'name': fields.char('Report Name', size=128, required=True, translate=True),
93         'parent_id': fields.many2one('account.financial.report', 'Parent'),
94         'children_ids':  fields.one2many('account.financial.report', 'parent_id', 'Account Report'),
95         'sequence': fields.integer('Sequence'),
96         'balance': fields.function(_get_balance, 'Balance', multi='balance'),
97         'debit': fields.function(_get_balance, 'Debit', multi='balance'),
98         'credit': fields.function(_get_balance, 'Credit', multi="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         'account_report_id':  fields.many2one('account.financial.report', 'Report Value'),
108         'account_type_ids': fields.many2many('account.account.type', 'account_account_financial_report_type', 'report_id', 'account_type_id', 'Account Types'),
109         '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.'),
110         'display_detail': fields.selection([
111             ('no_detail','No detail'),
112             ('detail_flat','Display children flat'),
113             ('detail_with_hierarchy','Display children with hierarchy')
114             ], 'Display details'),
115         'style_overwrite': fields.selection([
116             (0, 'Automatic formatting'),
117             (1,'Main Title 1 (bold, underlined)'),
118             (2,'Title 2 (bold)'),
119             (3,'Title 3 (bold, smaller)'),
120             (4,'Normal Text'),
121             (5,'Italic Text (smaller)'),
122             (6,'Smallest Text'),
123             ],'Financial Report Style', help="You can set up here the format you want this record to be displayed. If you leave the automatic formatting, it will be computed based on the financial reports hierarchy (auto-computed field 'level')."),
124     }
125
126     _defaults = {
127         'type': 'sum',
128         'display_detail': 'detail_flat',
129         'sign': 1,
130         'style_overwrite': 0,
131     }
132
133 account_financial_report()
134
135 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: