[IMP] Update french translations
[odoo/odoo.git] / addons / account_report / account.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 import time
23 import netsvc
24 from osv import fields, osv
25 import pooler
26 from tools.misc import currency
27 from tools.translate import _
28
29 import mx.DateTime
30 from mx.DateTime import RelativeDateTime, now, DateTime, localtime
31
32
33 class account_report(osv.osv):
34     _name = "account.report.report"
35     _description = "Account reporting"
36 #    _color = [
37 #            ('', ''),
38 #            ('green','Green'),
39 #            ('red','Red'),
40 #            ('pink','Pink'),
41 #            ('blue','Blue'),
42 #            ('yellow','Yellow'),
43 #            ('cyan','Cyan'),
44 #            ('lightblue','Light Blue'),
45 #            ('orange','Orange'),
46 #            ]
47 #    _style = [
48 #            ('1','Header 1'),
49 #            ('2','Header 2'),
50 #            ('3','Header 3'),
51 #            ('4','Header 4'),
52 #            ('5','Normal'),
53 #            ('6', 'Small'),
54 #            ]
55
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')
59
60         def _calc_context(key,obj):
61             if key==0:
62                 return obj.find(cr,uid,exception=False)
63             else:
64                 obj_key=obj.browse(cr,uid,obj.find(cr,uid,exception=False))
65                 if isinstance(obj_key,list):
66                     obj_key=obj_key[0]
67                 key_ids=obj.search(cr,uid,[('date_stop','<',obj_key.date_start)])
68                 if len(key_ids)<abs(key):
69                     return False
70                 return key_ids[key]
71
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)
79
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)
87
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')
93             account_ids = []
94             for c in code:
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)
98
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)
103
104         def _calc_tax_code(code,period=0):
105             context['period_id']=_calc_context(period,obj_period)
106             if not context['period_id']:
107                 return 0.00
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)
112         result = {}
113         for rep in self.browse(cr, uid, ids, context):
114             objdict = {
115                 'debit': _calc_debit,
116                 'credit': _calc_credit,
117                 'balance': _calc_balance,
118                 'report': _calc_report,
119                 'tax_code': _calc_tax_code,
120             }
121 #            if field_name=='status':
122 #                fld_name = 'expression_status'
123 #            else:
124 #                fld_name = 'expression'
125             try:
126                 val = eval(getattr(rep,'expression'), objdict)
127             except:
128                 val = 0.0
129
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'
139                 else:
140                     result[rep.id] = 'very good'
141             else:
142                 result[rep.id] =  val
143         return result
144
145     def onchange_parent_id(self, cr, uid, ids, parent_id):
146         v={}
147         if parent_id:
148             acc=self.pool.get('account.report.report').browse(cr,uid,parent_id)
149             v['type']=acc.type
150 #            if int(acc.style) < 6:
151 #                v['style'] = str(int(acc.style)+1)
152         return {'value': v}
153
154     _columns = {
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'),
162             ('view','View'),
163             ('other','Others')],
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,
173             method=True,
174             type="selection",
175             selection=[
176                 ('very bad', 'Very Bad'),
177                 ('bad', 'Bad'),
178                 ('normal', 'Normal'),
179                 ('good', 'Good'),
180                 ('very good', 'Very Good')
181             ],
182             string='Status'),
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')
188     }
189     _defaults = {
190 #        'style': lambda *args: '5',
191         'active': lambda *args: True,
192         'type': lambda *args: 'indicator',
193     }
194
195     def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
196         if not args:
197             args=[]
198         if not context:
199             context={}
200         ids = []
201         if name:
202             ids = self.search(cr, user, [('code','=',name)]+ args, limit=limit, context=context)
203             if not ids:
204                 ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
205         else:
206             ids = self.search(cr, user, args, limit=limit, context=context)
207         return self.name_get(cr, user, ids, context=context)
208
209     _constraints = [
210     #TODO Put an expression to valid expression 
211     ]
212
213 account_report()
214
215 class account_report_history(osv.osv):
216
217     def _calc_value(self, cr, uid, ids, name, args, context):
218         acc_report_id=self.read(cr,uid,ids,['tmp','period_id'])
219         tmp_ids={}
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']
224         return tmp_ids
225
226     _name = "account.report.history"
227     _description = "Indicator"
228     _table = "account_report"
229     _auto = False
230     _order='name'
231     _columns = {
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)
237     }
238
239     def init(self, cr):
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)''')
241
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!'))
244
245 account_report_history()
246
247 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
248