2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2007-04-24 17:22:54 +00:00
##############################################################################
2009-10-14 11:15:34 +00:00
#
2008-11-07 13:22:04 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2007-04-24 17:22:54 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2007-04-24 17:22:54 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2007-04-24 17:22:54 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2007-04-24 17:22:54 +00:00
#
##############################################################################
2009-10-14 11:15:34 +00:00
2007-04-24 17:22:54 +00:00
import time
import netsvc
from osv import fields , osv
2008-10-08 11:22:31 +00:00
import pooler
2007-04-24 17:22:54 +00:00
from tools . misc import currency
2009-03-06 22:18:24 +00:00
from tools . translate import _
2007-04-24 17:22:54 +00:00
import mx . DateTime
from mx . DateTime import RelativeDateTime , now , DateTime , localtime
class account_report ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " account.report.report "
2010-05-19 18:32:32 +00:00
_description = " Account Reporting "
2008-10-08 12:19:24 +00:00
# _color = [
# ('', ''),
# ('green','Green'),
# ('red','Red'),
# ('pink','Pink'),
# ('blue','Blue'),
# ('yellow','Yellow'),
# ('cyan','Cyan'),
# ('lightblue','Light Blue'),
# ('orange','Orange'),
# ]
# _style = [
# ('1','Header 1'),
# ('2','Header 2'),
# ('3','Header 3'),
# ('4','Header 4'),
# ('5','Normal'),
# ('6', 'Small'),
# ]
2007-08-09 06:05:59 +00:00
2008-07-22 15:11:28 +00:00
def _amount_get ( self , cr , uid , ids , field_name , arg , context = { } ) :
2008-11-07 13:22:04 +00:00
obj_fy = self . pool . get ( ' account.fiscalyear ' )
obj_period = self . pool . get ( ' account.period ' )
def _calc_context ( key , obj ) :
if key == 0 :
2009-09-24 10:46:21 +00:00
return obj . find ( cr , uid , exception = False )
2008-11-07 13:22:04 +00:00
else :
2009-09-24 10:46:21 +00:00
obj_key = obj . browse ( cr , uid , obj . find ( cr , uid , exception = False ) )
2007-11-07 14:33:26 +00:00
if isinstance ( obj_key , list ) :
obj_key = obj_key [ 0 ]
2008-11-07 13:22:04 +00:00
key_ids = obj . search ( cr , uid , [ ( ' date_stop ' , ' < ' , obj_key . date_start ) ] )
if len ( key_ids ) < abs ( key ) :
return False
return key_ids [ key ]
def _calc_credit ( code , year = 0 ) :
context [ ' fiscalyear ' ] = _calc_context ( year , obj_fy )
if not context [ ' fiscalyear ' ] :
del context [ ' fiscalyear ' ]
2008-07-22 15:11:28 +00:00
acc = self . pool . get ( ' account.account ' )
acc_id = acc . search ( cr , uid , [ ( ' code ' , ' in ' , code ) ] )
2008-11-19 10:54:47 +00:00
return reduce ( lambda y , x = 0 : x . credit + y , acc . browse ( cr , uid , acc_id , context ) , 0.0 )
2007-11-07 14:33:26 +00:00
2008-11-07 13:22:04 +00:00
def _calc_debit ( code , year = 0 ) :
context [ ' fiscalyear ' ] = _calc_context ( year , obj_fy )
if not context [ ' fiscalyear ' ] :
del context [ ' fiscalyear ' ]
2008-07-22 15:11:28 +00:00
acc = self . pool . get ( ' account.account ' )
acc_id = acc . search ( cr , uid , [ ( ' code ' , ' in ' , code ) ] )
2008-11-19 10:54:47 +00:00
return reduce ( lambda y , x = 0 : x . debit + y , acc . browse ( cr , uid , acc_id , context ) , 0.0 )
2007-11-07 14:33:26 +00:00
2008-11-07 13:22:04 +00:00
def _calc_balance ( code , year = 0 ) :
context [ ' fiscalyear ' ] = _calc_context ( year , obj_fy )
if not context [ ' fiscalyear ' ] :
del context [ ' fiscalyear ' ]
2008-07-22 15:11:28 +00:00
acc = self . pool . get ( ' account.account ' )
2009-01-22 12:21:02 +00:00
account_ids = [ ]
for c in code :
for i in c . split ( ' - ' ) :
account_ids + = acc . search ( cr , uid , [ ( ' code ' , ' ilike ' , i ) ] )
return reduce ( lambda y , x = 0 : x . balance + y , acc . browse ( cr , uid , account_ids , context ) , 0.0 )
2007-11-07 14:33:26 +00:00
2008-07-22 15:11:28 +00:00
def _calc_report ( * code ) :
acc = self . pool . get ( ' account.report.report ' )
acc_id = acc . search ( cr , uid , [ ( ' code ' , ' in ' , code ) ] )
2008-11-19 10:54:47 +00:00
return reduce ( lambda y , x = 0 : x . amount + y , acc . browse ( cr , uid , acc_id , context ) , 0.0 )
2007-11-07 14:33:26 +00:00
2008-11-07 13:22:04 +00:00
def _calc_tax_code ( code , period = 0 ) :
context [ ' period_id ' ] = _calc_context ( period , obj_period )
if not context [ ' period_id ' ] :
return 0.00
2007-11-07 14:33:26 +00:00
context [ ' period_id ' ] = context [ ' period_id ' ] [ 0 ]
2008-11-07 13:22:04 +00:00
acc = self . pool . get ( ' account.tax.code ' )
acc_id = acc . search ( cr , uid , [ ( ' code ' , ' in ' , code ) ] )
2008-11-19 10:54:47 +00:00
return reduce ( lambda y , x = 0 : x . sum_period + y , acc . browse ( cr , uid , acc_id , context ) , 0.0 )
2008-07-22 15:11:28 +00:00
result = { }
for rep in self . browse ( cr , uid , ids , context ) :
objdict = {
' debit ' : _calc_debit ,
' credit ' : _calc_credit ,
' balance ' : _calc_balance ,
' report ' : _calc_report ,
2008-11-07 13:22:04 +00:00
' tax_code ' : _calc_tax_code ,
2008-07-22 15:11:28 +00:00
}
2008-10-10 06:56:59 +00:00
# if field_name=='status':
# fld_name = 'expression_status'
# else:
# fld_name = 'expression'
2008-07-22 15:11:28 +00:00
try :
2008-10-10 06:56:59 +00:00
val = eval ( getattr ( rep , ' expression ' ) , objdict )
2008-07-22 15:11:28 +00:00
except :
val = 0.0
2008-10-10 06:56:59 +00:00
2008-07-22 15:11:28 +00:00
if field_name == ' status ' :
2008-10-10 06:56:59 +00:00
if val < rep . badness_limit :
2008-07-22 15:11:28 +00:00
result [ rep . id ] = ' very bad '
2008-10-10 06:56:59 +00:00
elif val == rep . badness_limit :
2008-07-22 15:11:28 +00:00
result [ rep . id ] = ' bad '
2008-10-10 06:56:59 +00:00
elif val < rep . goodness_limit :
2008-07-22 15:11:28 +00:00
result [ rep . id ] = ' normal '
2008-10-10 06:56:59 +00:00
elif val == rep . goodness_limit :
2008-07-22 15:11:28 +00:00
result [ rep . id ] = ' good '
else :
2008-10-10 06:56:59 +00:00
result [ rep . id ] = ' very good '
2008-07-22 15:11:28 +00:00
else :
result [ rep . id ] = val
return result
2007-08-09 06:05:59 +00:00
2008-07-22 15:11:28 +00:00
def onchange_parent_id ( self , cr , uid , ids , parent_id ) :
v = { }
if parent_id :
acc = self . pool . get ( ' account.report.report ' ) . browse ( cr , uid , parent_id )
v [ ' type ' ] = acc . type
2008-10-08 12:19:24 +00:00
# if int(acc.style) < 6:
# v['style'] = str(int(acc.style)+1)
2008-07-22 15:11:28 +00:00
return { ' value ' : v }
2007-08-09 06:05:59 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , required = True ) ,
2009-12-21 12:21:32 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the account report without removing it. " ) ,
2009-12-21 13:14:12 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of account reports. " ) ,
2008-07-22 15:11:28 +00:00
' code ' : fields . char ( ' Code ' , size = 64 , required = True ) ,
' type ' : fields . selection ( [
2009-01-27 11:15:46 +00:00
( ' fiscal ' , ' Fiscal Statement ' ) ,
2008-07-22 15:11:28 +00:00
( ' indicator ' , ' Indicator ' ) ,
2008-10-08 13:42:40 +00:00
( ' view ' , ' View ' ) ,
2008-07-22 15:11:28 +00:00
( ' other ' , ' Others ' ) ] ,
' Type ' , required = True ) ,
' expression ' : fields . char ( ' Expression ' , size = 240 , required = True ) ,
2009-01-27 11:15:46 +00:00
' badness_limit ' : fields . float ( ' Badness Indicator Limit ' , digits = ( 16 , 2 ) , help = ' This Value sets the limit of badness. ' ) ,
' goodness_limit ' : fields . float ( ' Goodness Indicator Limit ' , digits = ( 16 , 2 ) , help = ' This Value sets the limit of goodness. ' ) ,
2008-07-22 15:11:28 +00:00
' parent_id ' : fields . many2one ( ' account.report.report ' , ' Parent ' ) ,
2009-01-27 11:15:46 +00:00
' child_ids ' : fields . one2many ( ' account.report.report ' , ' parent_id ' , ' Children ' ) ,
2008-07-22 15:11:28 +00:00
' note ' : fields . text ( ' Note ' ) ,
' amount ' : fields . function ( _amount_get , method = True , string = ' Value ' ) ,
' status ' : fields . function ( _amount_get ,
method = True ,
type = " selection " ,
selection = [
( ' very bad ' , ' Very Bad ' ) ,
( ' bad ' , ' Bad ' ) ,
2008-10-10 06:56:59 +00:00
( ' normal ' , ' Normal ' ) ,
2008-07-22 15:11:28 +00:00
( ' good ' , ' Good ' ) ,
2008-10-10 06:56:59 +00:00
( ' very good ' , ' Very Good ' )
2008-07-22 15:11:28 +00:00
] ,
string = ' Status ' ) ,
2009-01-27 11:15:46 +00:00
' 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 ' ) ,
' 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. ' ) ,
2008-10-08 12:19:24 +00:00
# 'style': fields.selection(_style, 'Style', required=True),
# 'color_font' : fields.selection(_color, 'Font Color', help="Font Color for the report"),
# 'color_back' : fields.selection(_color, 'Back Color')
2008-07-22 15:11:28 +00:00
}
_defaults = {
2008-10-08 12:19:24 +00:00
# 'style': lambda *args: '5',
2008-07-22 15:11:28 +00:00
' active ' : lambda * args : True ,
' type ' : lambda * args : ' indicator ' ,
}
2007-08-09 06:05:59 +00:00
2009-12-09 11:43:34 +00:00
def name_search ( self , cr , user , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2008-07-22 15:11:28 +00:00
if not args :
args = [ ]
if not context :
context = { }
ids = [ ]
if name :
ids = self . search ( cr , user , [ ( ' code ' , ' = ' , name ) ] + args , limit = limit , context = context )
if not ids :
ids = self . search ( cr , user , [ ( ' name ' , operator , name ) ] + args , limit = limit , context = context )
else :
ids = self . search ( cr , user , args , limit = limit , context = context )
return self . name_get ( cr , user , ids , context = context )
2007-04-24 17:22:54 +00:00
2008-07-22 15:11:28 +00:00
_constraints = [
2009-01-15 12:55:06 +00:00
#TODO Put an expression to valid expression
2008-07-22 15:11:28 +00:00
]
2007-08-09 06:05:59 +00:00
2007-04-24 17:22:54 +00:00
account_report ( )
2008-07-23 14:41:47 +00:00
2008-10-08 11:22:31 +00:00
class account_report_history ( osv . osv ) :
def _calc_value ( self , cr , uid , ids , name , args , context ) :
acc_report_id = self . read ( cr , uid , ids , [ ' tmp ' , ' period_id ' ] )
tmp_ids = { }
for a in acc_report_id :
period_val = pooler . get_pool ( cr . dbname ) . get ( ' account.period ' ) . read ( cr , uid , [ a [ ' period_id ' ] [ 0 ] ] ) [ 0 ]
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 ] ) ] )
tmp_ids [ a [ ' id ' ] ] = pooler . get_pool ( cr . dbname ) . get ( ' account.report.report ' ) . read ( cr , uid , [ a [ ' tmp ' ] ] , context = { ' periods ' : period_id } ) [ 0 ] [ ' amount ' ]
return tmp_ids
_name = " account.report.history "
_description = " Indicator "
_table = " account_report "
_auto = False
_order = ' name '
_columns = {
' period_id ' : fields . many2one ( ' account.period ' , ' Period ' , readonly = True , select = True ) ,
' fiscalyear_id ' : fields . many2one ( ' account.fiscalyear ' , ' Fiscal Year ' , readonly = True , select = True ) ,
' name ' : fields . many2one ( ' account.report.report ' , ' Indicator ' , readonly = True , select = True ) ,
' val ' : fields . function ( _calc_value , method = True , string = ' Value ' , readonly = True ) ,
' tmp ' : fields . integer ( string = ' temp ' , readonly = True )
}
def init ( self , cr ) :
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) ''' )
2008-10-10 11:58:17 +00:00
def unlink ( self , cr , uid , ids , context = { } ) :
2009-01-21 12:38:28 +00:00
raise osv . except_osv ( _ ( ' Error ! ' ) , _ ( ' You cannot delete an indicator history record. You may have to delete the concerned Indicator! ' ) )
2008-10-10 11:58:17 +00:00
2008-10-08 11:22:31 +00:00
account_report_history ( )
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: