[IMP] improved description of consistency test, code help and removed unused function...
[odoo/odoo.git] / addons / account_test / report / account_test_report.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import datetime
24 import time
25 import re
26 from report import report_sxw
27 from itertools import groupby
28 from operator import itemgetter
29 from tools.translate import _
30 #
31 # Use period and Journal for selection or resources
32 #
33 class report_assert_account(report_sxw.rml_parse):
34     def __init__(self, cr, uid, name, context):
35         super(report_assert_account, self).__init__(cr, uid, name, context=context)
36         self.localcontext.update( {
37             'time': time,
38             'datetime': datetime,
39             'execute_code': self.execute_code,
40         })
41
42     def execute_code(self, code_exec):
43         def group(lst, col):
44             return dict((k, [v for v in itr]) for k, itr in groupby(sorted(lst, key=lambda x: x[col]), itemgetter(col)))
45
46         def reconciled_inv():
47             reconciled_inv_ids = self.pool.get('account.invoice').search(self.cr, self.uid, [('reconciled','=',True)])
48             return reconciled_inv_ids
49
50         def get_parent(acc_id):
51             acc_an_id = self.pool.get('account.analytic.account').browse(self.cr, self.uid, acc_id).parent_id
52             while acc_an_id.parent_id:
53                 acc_an_id = acc_an_id.parent_id
54             return acc_an_id.id
55
56         def order_columns(item, cols=None):
57             if cols is None:
58                 cols = item.keys()
59             return [(col, item.get(col)) for col in cols if col in item.keys()]
60
61         localdict = {
62             'cr': self.cr,
63             '_': _,
64             'reconciled_inv' : reconciled_inv,
65             'group' : group,
66             'get_parent' : get_parent,
67             'now': datetime.datetime.now(),
68             'result': None,
69             'column_order': None,
70         }
71
72         exec code_exec in localdict
73
74         result = localdict['result']
75         column_order = localdict.get('column_order', None)
76
77         if not isinstance(result, (tuple, list, set)):
78             result = [result]
79         if not result:
80             result = [_('The test was passed successfully')]
81         else:
82             def _format(a):
83                 if isinstance(a, dict):
84                     return ', '.join(["%s: %s" % (tup[0], tup[1]) for tup in order_columns(a, column_order)])
85                 else:
86                     return a
87             result = [_format(rec) for rec in result]
88
89         return result
90
91 report_sxw.report_sxw('report.account.test.assert.print', 'accounting.assert.test', 'addons/account_test/report/account_test.rml', parser=report_assert_account, header=False)
92
93 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: