2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-09-11 10:02:28 +00:00
##############################################################################
2010-09-01 07:01:00 +00:00
#
2009-10-14 11:15:34 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-09-11 10:02:28 +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.
2008-09-11 10:02:28 +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.
2008-09-11 10:02:28 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-09-01 07:01:00 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-09-11 10:02:28 +00:00
#
##############################################################################
import time
import datetime
2010-09-28 12:58:00 +00:00
from report import report_sxw
2008-09-11 10:02:28 +00:00
import operator
class budget_report ( report_sxw . rml_parse ) :
def __init__ ( self , cr , uid , name , context ) :
2009-10-09 11:49:00 +00:00
super ( budget_report , self ) . __init__ ( cr , uid , name , context = context )
2008-09-11 10:02:28 +00:00
self . localcontext . update ( {
' lines ' : self . lines ,
' budget_total ' : self . budget_total ,
' post_total ' : self . post_total ,
' time ' : time ,
} )
def post_total ( self , post_obj , date1 , date2 ) :
def str2date ( date_str ) :
return datetime . date . fromtimestamp ( time . mktime ( time . strptime ( date_str , ' % Y- % m- %d ' ) ) )
def interval ( d1str , d2str ) :
return ( str2date ( d2str ) - str2date ( d1str ) + datetime . timedelta ( days = 1 ) ) . days
prev = reduce ( lambda x , d : x + d . amount , post_obj . dotation_ids , 0.0 )
period_days = interval ( date1 , date2 )
for d in post_obj . dotation_ids :
i = interval ( d . period_id . date_start , d . period_id . date_stop )
total_days = reduce ( lambda x , d : x + interval ( d . period_id . date_start , d . period_id . date_stop ) , post_obj . dotation_ids , 0 )
achievements = reduce ( lambda x , l : x + l [ ' achievements ' ] , self . lines ( post_obj , date1 , date2 ) , 0.0 )
2008-09-27 08:38:27 +00:00
prev_1 = 1.00
if total_days < > 0.00 :
prev_1 = prev * period_days / total_days
return [ { ' prev ' : prev , ' prev_period ' : prev_1 , ' achievements ' : achievements } ]
2008-09-11 10:02:28 +00:00
2008-10-04 08:12:11 +00:00
def budget_total ( self , post_obj , date1 , date2 ) :
2008-09-11 10:02:28 +00:00
res = { ' prev ' : 0.0 , ' prev_period ' : 0.0 , ' achievements ' : 0.0 }
2008-10-04 08:12:11 +00:00
r = self . post_total ( post_obj , date1 , date2 ) [ 0 ]
for k in r :
res [ k ] + = r [ k ]
2008-09-11 10:02:28 +00:00
return [ res ]
2008-09-12 10:43:45 +00:00
2008-09-11 10:02:28 +00:00
def lines ( self , post_obj , date1 , date2 ) :
res = [ ]
for a in post_obj . account_ids :
2008-12-10 14:29:55 +00:00
self . cr . execute ( " SELECT COALESCE(SUM(debit-credit), 0) FROM account_move_line WHERE account_id= %s AND date>= %s AND date<= %s and state<> ' draft ' " , ( a . id , date1 , date2 ) )
2008-10-03 10:16:40 +00:00
achievements = float ( self . cr . fetchone ( ) [ 0 ] )
2008-09-11 10:02:28 +00:00
res . append ( { ' name ' : a . name , ' code ' : a . code , ' achievements ' : achievements } )
return res
2010-09-28 12:58:00 +00:00
report_sxw . report_sxw ( ' report.account.budget ' , ' account.budget.post ' , ' addons/account_budget/report/budget_report.rml ' , parser = budget_report , header = " internal " )
2008-09-11 10:02:28 +00:00
2010-09-28 12:58:00 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: