1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
24 from osv import fields, osv
26 from tools.misc import currency
29 from mx.DateTime import RelativeDateTime, now, DateTime, localtime
32 class account_report(osv.osv):
33 _name = "account.report.report"
34 _description = "Account reporting"
41 # ('yellow','Yellow'),
43 # ('lightblue','Light Blue'),
44 # ('orange','Orange'),
55 def _amount_get(self, cr, uid, ids, field_name, arg, context={}):
56 def _calc_credit(*code):
57 acc = self.pool.get('account.account')
58 acc_id = acc.search(cr, uid, [('code','in',code)])
59 return reduce(lambda y,x=0: x.credit+y, acc.browse(cr, uid, acc_id, context),0)
60 def _calc_debit(*code):
61 acc = self.pool.get('account.account')
62 acc_id = acc.search(cr, uid, [('code','in',code)])
63 return reduce(lambda y,x=0: x.debit+y, acc.browse(cr, uid, acc_id, context),0)
64 def _calc_balance(*code):
65 acc = self.pool.get('account.account')
66 acc_id = acc.search(cr, uid, [('code','in',code)])
67 return reduce(lambda y,x=0: x.balance+y, acc.browse(cr, uid, acc_id, context),0)
68 def _calc_report(*code):
69 acc = self.pool.get('account.report.report')
70 acc_id = acc.search(cr, uid, [('code','in',code)])
71 return reduce(lambda y,x=0: x.amount+y, acc.browse(cr, uid, acc_id, context),0)
73 for rep in self.browse(cr, uid, ids, context):
76 'credit': _calc_credit,
77 'balance': _calc_balance,
78 'report': _calc_report,
80 # if field_name=='status':
81 # fld_name = 'expression_status'
83 # fld_name = 'expression'
85 val = eval(getattr(rep,'expression'), objdict)
89 if field_name=='status':
90 if val<rep.badness_limit:
91 result[rep.id] = 'very bad'
92 elif val==rep.badness_limit:
93 result[rep.id] = 'bad'
94 elif val<rep.goodness_limit:
95 result[rep.id] = 'normal'
96 elif val==rep.goodness_limit:
97 result[rep.id] = 'good'
99 result[rep.id] = 'very good'
104 def onchange_parent_id(self, cr, uid, ids, parent_id):
107 acc=self.pool.get('account.report.report').browse(cr,uid,parent_id)
109 # if int(acc.style) < 6:
110 # v['style'] = str(int(acc.style)+1)
114 'name': fields.char('Name', size=64, required=True),
115 'active': fields.boolean('Active'),
116 'sequence': fields.integer('Sequence'),
117 'code': fields.char('Code', size=64, required=True),
118 'type': fields.selection([
119 ('fiscal', 'Fiscal statement'),
120 ('indicator','Indicator'),
123 'Type', required=True),
124 'expression': fields.char('Expression', size=240, required=True),
125 # 'expression_status': fields.char('Status expression', size=240, required=True),
126 'badness_limit' :fields.float('Badness Indicator Limit', digits=(16,2),help='This Value depicts the limit of badness.'),
127 'goodness_limit' :fields.float('Goodness Indicator Limit', digits=(16,2),help='This Value depicts the limit of goodness.'),
128 'parent_id': fields.many2one('account.report.report', 'Parent'),
129 'child_ids': fields.one2many('account.report.report', 'parent_id', 'Childs'),
130 'note': fields.text('Note'),
131 'amount': fields.function(_amount_get, method=True, string='Value'),
132 'status': fields.function(_amount_get,
136 ('very bad', 'Very Bad'),
138 ('normal', 'Normal'),
140 ('very good', 'Very Good')
143 'disp_tree':fields.boolean('Display Tree',help='When the indicators will be printed, if one indicator is set with this field to True, then it will display one more graph with all its children in tree'),
144 'disp_graph':fields.boolean('Display as a Graph',help='If the field is set to True,information will be printed as a Graph; as an array otherwise.'),
145 # 'style': fields.selection(_style, 'Style', required=True),
146 # 'color_font' : fields.selection(_color, 'Font Color', help="Font Color for the report"),
147 # 'color_back' : fields.selection(_color, 'Back Color')
150 # 'style': lambda *args: '5',
151 'active': lambda *args: True,
152 'type': lambda *args: 'indicator',
155 def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
162 ids = self.search(cr, user, [('code','=',name)]+ args, limit=limit, context=context)
164 ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
166 ids = self.search(cr, user, args, limit=limit, context=context)
167 return self.name_get(cr, user, ids, context=context)
170 #TODO Put an expression to valid expression and expression_status
173 ('code_uniq', 'unique (code)', 'The code of the report entry must be unique !')
178 class account_report_history(osv.osv):
180 def _calc_value(self, cr, uid, ids, name, args, context):
181 acc_report_id=self.read(cr,uid,ids,['tmp','period_id'])
183 for a in acc_report_id:
184 period_val=pooler.get_pool(cr.dbname).get('account.period').read(cr,uid,[a['period_id'][0]])[0]
185 period_id=pooler.get_pool(cr.dbname).get('account.period').search(cr,uid,[('date_start','<=',period_val['date_start']),('fiscalyear_id','=',period_val['fiscalyear_id'][0])])
186 tmp_ids[a['id']] = pooler.get_pool(cr.dbname).get('account.report.report').read(cr,uid,[a['tmp']],context={'periods':period_id})[0]['amount']
189 _name = "account.report.history"
190 _description = "Indicator"
191 _table = "account_report"
195 'period_id': fields.many2one('account.period','Period', readonly=True, select=True),
196 'fiscalyear_id': fields.many2one('account.fiscalyear','Fiscal Year', readonly=True, select=True),
197 'name': fields.many2one('account.report.report','Indicator', readonly=True, select=True),
198 'val': fields.function(_calc_value, method=True, string='Value', readonly=True),
199 'tmp' : fields.integer(string='temp',readonly=True)
203 cr.execute('''create or replace view account_report as (select ar.id as tmp,((pr.id*100000)+ar.id) as id,ar.id as name,pr.id as period_id,pr.fiscalyear_id as fiscalyear_id from account_report_report as ar cross join account_period as pr group by ar.id,pr.id,pr.fiscalyear_id)''')
205 def unlink(self, cr, uid, ids, context={}):
206 raise osv.except_osv(_('Error !'), _('You can not delete an indicator history record. You may have to delete the concerned Indicator!'))
208 account_report_history()
210 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: