passing modules in GPL-3
[odoo/odoo.git] / addons / account_report / account.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 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 import time
23 import netsvc
24 from osv import fields, osv
25 import pooler
26 from tools.misc import currency
27
28 import mx.DateTime
29 from mx.DateTime import RelativeDateTime, now, DateTime, localtime
30
31
32 class account_report(osv.osv):
33     _name = "account.report.report"
34     _description = "Account reporting"
35 #    _color = [
36 #            ('', ''),
37 #            ('green','Green'),
38 #            ('red','Red'),
39 #            ('pink','Pink'),
40 #            ('blue','Blue'),
41 #            ('yellow','Yellow'),
42 #            ('cyan','Cyan'),
43 #            ('lightblue','Light Blue'),
44 #            ('orange','Orange'),
45 #            ]
46 #    _style = [
47 #            ('1','Header 1'),
48 #            ('2','Header 2'),
49 #            ('3','Header 3'),
50 #            ('4','Header 4'),
51 #            ('5','Normal'),
52 #            ('6', 'Small'),
53 #            ]
54
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)
72         result = {}
73         for rep in self.browse(cr, uid, ids, context):
74             objdict = {
75                 'debit': _calc_debit,
76                 'credit': _calc_credit,
77                 'balance': _calc_balance,
78                 'report': _calc_report,
79             }
80 #            if field_name=='status':
81 #                fld_name = 'expression_status'
82 #            else:
83 #                fld_name = 'expression'
84             try:
85                 val = eval(getattr(rep,'expression'), objdict)
86             except:
87                 val = 0.0
88
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'
98                 else:
99                     result[rep.id] = 'very good'
100             else:
101                 result[rep.id] =  val
102         return result
103
104     def onchange_parent_id(self, cr, uid, ids, parent_id):
105         v={}
106         if parent_id:
107             acc=self.pool.get('account.report.report').browse(cr,uid,parent_id)
108             v['type']=acc.type
109 #            if int(acc.style) < 6:
110 #                v['style'] = str(int(acc.style)+1)
111         return {'value': v}
112
113     _columns = {
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'),
121             ('view','View'),
122             ('other','Others')],
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,
133             method=True,
134             type="selection",
135             selection=[
136                 ('very bad', 'Very Bad'),
137                 ('bad', 'Bad'),
138                 ('normal', 'Normal'),
139                 ('good', 'Good'),
140                 ('very good', 'Very Good')
141             ],
142             string='Status'),
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')
148     }
149     _defaults = {
150 #        'style': lambda *args: '5',
151         'active': lambda *args: True,
152         'type': lambda *args: 'indicator',
153     }
154
155     def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
156         if not args:
157             args=[]
158         if not context:
159             context={}
160         ids = []
161         if name:
162             ids = self.search(cr, user, [('code','=',name)]+ args, limit=limit, context=context)
163             if not ids:
164                 ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
165         else:
166             ids = self.search(cr, user, args, limit=limit, context=context)
167         return self.name_get(cr, user, ids, context=context)
168
169     _constraints = [
170     #TODO Put an expression to valid expression and expression_status
171     ]
172     _sql_constraints = [
173         ('code_uniq', 'unique (code)', 'The code of the report entry must be unique !')
174     ]
175
176 account_report()
177
178 class account_report_history(osv.osv):
179
180     def _calc_value(self, cr, uid, ids, name, args, context):
181         acc_report_id=self.read(cr,uid,ids,['tmp','period_id'])
182         tmp_ids={}
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']
187         return tmp_ids
188
189     _name = "account.report.history"
190     _description = "Indicator"
191     _table = "account_report"
192     _auto = False
193     _order='name'
194     _columns = {
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)
200     }
201
202     def init(self, cr):
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)''')
204
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!'))
207
208 account_report_history()
209
210 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
211