1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 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
27 from tools.translate import _
30 from mx.DateTime import RelativeDateTime, now, DateTime, localtime
33 class account_report(osv.osv):
34 _name = "account.report.report"
35 _description = "Account reporting"
42 # ('yellow','Yellow'),
44 # ('lightblue','Light Blue'),
45 # ('orange','Orange'),
56 def _amount_get(self, cr, uid, ids, field_name, arg, context={}):
57 obj_fy=self.pool.get('account.fiscalyear')
58 obj_period=self.pool.get('account.period')
60 def _calc_context(key,obj):
62 return obj.find(cr,uid,exception=False)
64 obj_key=obj.browse(cr,uid,obj.find(cr,uid,exception=False))
65 if isinstance(obj_key,list):
67 key_ids=obj.search(cr,uid,[('date_stop','<',obj_key.date_start)])
68 if len(key_ids)<abs(key):
72 def _calc_credit(code,year=0):
73 context['fiscalyear']=_calc_context(year,obj_fy)
74 if not context['fiscalyear']:
75 del context['fiscalyear']
76 acc = self.pool.get('account.account')
77 acc_id = acc.search(cr, uid, [('code','in',code)])
78 return reduce(lambda y,x=0: x.credit+y, acc.browse(cr, uid, acc_id, context),0.0)
80 def _calc_debit(code,year=0):
81 context['fiscalyear']=_calc_context(year,obj_fy)
82 if not context['fiscalyear']:
83 del context['fiscalyear']
84 acc = self.pool.get('account.account')
85 acc_id = acc.search(cr, uid, [('code','in',code)])
86 return reduce(lambda y,x=0: x.debit+y, acc.browse(cr, uid, acc_id, context),0.0)
88 def _calc_balance(code,year=0):
89 context['fiscalyear']=_calc_context(year,obj_fy)
90 if not context['fiscalyear']:
91 del context['fiscalyear']
92 acc = self.pool.get('account.account')
95 for i in c.split('-'):
96 account_ids += acc.search(cr, uid, [('code', 'ilike', i)])
97 return reduce(lambda y,x=0: x.balance+y, acc.browse(cr, uid, account_ids, context),0.0)
99 def _calc_report(*code):
100 acc = self.pool.get('account.report.report')
101 acc_id = acc.search(cr, uid, [('code','in',code)])
102 return reduce(lambda y,x=0: x.amount+y, acc.browse(cr, uid, acc_id, context),0.0)
104 def _calc_tax_code(code,period=0):
105 context['period_id']=_calc_context(period,obj_period)
106 if not context['period_id']:
108 context['period_id']=context['period_id'][0]
109 acc = self.pool.get('account.tax.code')
110 acc_id = acc.search(cr, uid, [('code','in',code)])
111 return reduce(lambda y,x=0: x.sum_period+y, acc.browse(cr, uid, acc_id, context),0.0)
113 for rep in self.browse(cr, uid, ids, context):
115 'debit': _calc_debit,
116 'credit': _calc_credit,
117 'balance': _calc_balance,
118 'report': _calc_report,
119 'tax_code': _calc_tax_code,
121 # if field_name=='status':
122 # fld_name = 'expression_status'
124 # fld_name = 'expression'
126 val = eval(getattr(rep,'expression'), objdict)
130 if field_name=='status':
131 if val<rep.badness_limit:
132 result[rep.id] = 'very bad'
133 elif val==rep.badness_limit:
134 result[rep.id] = 'bad'
135 elif val<rep.goodness_limit:
136 result[rep.id] = 'normal'
137 elif val==rep.goodness_limit:
138 result[rep.id] = 'good'
140 result[rep.id] = 'very good'
145 def onchange_parent_id(self, cr, uid, ids, parent_id):
148 acc=self.pool.get('account.report.report').browse(cr,uid,parent_id)
150 # if int(acc.style) < 6:
151 # v['style'] = str(int(acc.style)+1)
155 'name': fields.char('Name', size=64, required=True),
156 'active': fields.boolean('Active'),
157 'sequence': fields.integer('Sequence'),
158 'code': fields.char('Code', size=64, required=True),
159 'type': fields.selection([
160 ('fiscal', 'Fiscal Statement'),
161 ('indicator','Indicator'),
164 'Type', required=True),
165 'expression': fields.char('Expression', size=240, required=True),
166 'badness_limit' :fields.float('Badness Indicator Limit', digits=(16,2),help='This Value sets the limit of badness.'),
167 'goodness_limit' :fields.float('Goodness Indicator Limit', digits=(16,2),help='This Value sets the limit of goodness.'),
168 'parent_id': fields.many2one('account.report.report', 'Parent'),
169 'child_ids': fields.one2many('account.report.report', 'parent_id', 'Children'),
170 'note': fields.text('Note'),
171 'amount': fields.function(_amount_get, method=True, string='Value'),
172 'status': fields.function(_amount_get,
176 ('very bad', 'Very Bad'),
178 ('normal', 'Normal'),
180 ('very good', 'Very Good')
183 'disp_tree':fields.boolean('Display Tree',help='When the indicators are printed, if one indicator is set with this field to True, then it will display one more graphs with all its children in tree'),
184 'disp_graph':fields.boolean('Display As Graph',help='If the field is set to True, information will be printed as a Graph, otherwise as an array.'),
185 # 'style': fields.selection(_style, 'Style', required=True),
186 # 'color_font' : fields.selection(_color, 'Font Color', help="Font Color for the report"),
187 # 'color_back' : fields.selection(_color, 'Back Color')
190 # 'style': lambda *args: '5',
191 'active': lambda *args: True,
192 'type': lambda *args: 'indicator',
195 def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
202 ids = self.search(cr, user, [('code','=',name)]+ args, limit=limit, context=context)
204 ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
206 ids = self.search(cr, user, args, limit=limit, context=context)
207 return self.name_get(cr, user, ids, context=context)
210 #TODO Put an expression to valid expression
215 class account_report_history(osv.osv):
217 def _calc_value(self, cr, uid, ids, name, args, context):
218 acc_report_id=self.read(cr,uid,ids,['tmp','period_id'])
220 for a in acc_report_id:
221 period_val=pooler.get_pool(cr.dbname).get('account.period').read(cr,uid,[a['period_id'][0]])[0]
222 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])])
223 tmp_ids[a['id']] = pooler.get_pool(cr.dbname).get('account.report.report').read(cr,uid,[a['tmp']],context={'periods':period_id})[0]['amount']
226 _name = "account.report.history"
227 _description = "Indicator"
228 _table = "account_report"
232 'period_id': fields.many2one('account.period','Period', readonly=True, select=True),
233 'fiscalyear_id': fields.many2one('account.fiscalyear','Fiscal Year', readonly=True, select=True),
234 'name': fields.many2one('account.report.report','Indicator', readonly=True, select=True),
235 'val': fields.function(_calc_value, method=True, string='Value', readonly=True),
236 'tmp' : fields.integer(string='temp',readonly=True)
240 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)''')
242 def unlink(self, cr, uid, ids, context={}):
243 raise osv.except_osv(_('Error !'), _('You cannot delete an indicator history record. You may have to delete the concerned Indicator!'))
245 account_report_history()
247 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: