2006-12-15 07:15:10 +00:00
##############################################################################
#
2008-06-16 11:00:21 +00:00
# Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
2006-12-15 07:15:10 +00:00
#
2008-06-16 06:44:24 +00:00
# $Id$
2006-12-15 07:15:10 +00:00
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from osv import fields , osv
2006-12-20 05:57:18 +00:00
import time
import mx . DateTime
2006-12-15 07:15:10 +00:00
class report_account_analytic_planning ( osv . osv ) :
_name = " report_account_analytic.planning "
_description = " Planning "
_columns = {
' name ' : fields . char ( ' Planning Name ' , size = 32 , required = True ) ,
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' , required = True ) ,
2007-12-27 09:20:01 +00:00
' date_from ' : fields . date ( ' Start Date ' , required = True ) ,
' date_to ' : fields . date ( ' End Date ' , required = True ) ,
2006-12-15 07:15:10 +00:00
' line_ids ' : fields . one2many ( ' report_account_analytic.planning.line ' , ' planning_id ' , ' Planning lines ' ) ,
' stat_ids ' : fields . one2many ( ' report_account_analytic.planning.stat ' , ' planning_id ' , ' Planning analysis ' , readonly = True ) ,
' stat_user_ids ' : fields . one2many ( ' report_account_analytic.planning.stat.user ' , ' planning_id ' , ' Planning by user ' , readonly = True ) ,
' stat_account_ids ' : fields . one2many ( ' report_account_analytic.planning.stat.account ' , ' planning_id ' , ' Planning by account ' , readonly = True ) ,
2008-02-05 20:27:36 +00:00
' state ' : fields . selection ( [ ( ' open ' , ' Open ' ) , ( ' done ' , ' Done ' ) ] , ' State ' , required = True )
2006-12-15 07:15:10 +00:00
}
2006-12-20 05:57:18 +00:00
_defaults = {
' name ' : lambda * a : time . strftime ( ' % Y- % m- %d ' ) ,
' date_from ' : lambda * a : time . strftime ( ' % Y- % m-01 ' ) ,
' date_to ' : lambda * a : ( mx . DateTime . now ( ) + mx . DateTime . RelativeDateTime ( months = 1 , day = 1 , days = - 1 ) ) . strftime ( ' % Y- % m- %d ' ) ,
2008-02-05 20:27:36 +00:00
' user_id ' : lambda self , cr , uid , c : uid ,
' state ' : lambda * args : ' open '
2006-12-20 05:57:18 +00:00
}
2006-12-15 07:15:10 +00:00
_order = ' date_from desc '
report_account_analytic_planning ( )
class report_account_analytic_planning_line ( osv . osv ) :
_name = " report_account_analytic.planning.line "
_description = " Planning Line "
_rec_name = ' user_id '
_columns = {
' account_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic account ' , required = True ) ,
' planning_id ' : fields . many2one ( ' report_account_analytic.planning ' , ' Planning ' , required = True , ondelete = ' cascade ' ) ,
2007-06-19 18:35:04 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User ' ) ,
2006-12-15 07:15:10 +00:00
' amount ' : fields . float ( ' Quantity ' , required = True ) ,
' amount_unit ' : fields . many2one ( ' product.uom ' , ' Qty UoM ' , required = True ) ,
2007-06-19 18:43:04 +00:00
' note ' : fields . text ( ' Note ' , size = 64 ) ,
2006-12-15 07:15:10 +00:00
}
_order = ' user_id,account_id '
report_account_analytic_planning_line ( )
class report_account_analytic_planning_stat_account ( osv . osv ) :
_name = " report_account_analytic.planning.stat.account "
_description = " Planning account stat "
_rec_name = ' account_id '
_auto = False
2007-06-15 06:01:18 +00:00
_log_access = False
2007-10-01 20:44:03 +00:00
def _sum_amount_real ( self , cr , uid , ids , name , args , context ) :
result = { }
for line in self . browse ( cr , uid , ids , context ) :
cr . execute ( ' select sum(unit_amount) from account_analytic_line where account_id= %d and date>= %s and date<= %s ' , ( line . account_id . id , line . planning_id . date_from , line . planning_id . date_to ) )
result [ line . id ] = cr . fetchone ( ) [ 0 ]
return result
2006-12-15 07:15:10 +00:00
_columns = {
' planning_id ' : fields . many2one ( ' report_account_analytic.planning ' , ' Planning ' ) ,
' account_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' , required = True ) ,
2007-10-01 20:44:03 +00:00
' quantity ' : fields . float ( ' Planned ' , required = True ) ,
' sum_amount_real ' : fields . function ( _sum_amount_real , method = True , string = ' Timesheet ' ) ,
2006-12-15 07:15:10 +00:00
}
def init ( self , cr ) :
cr . execute ( """
create or replace view report_account_analytic_planning_stat_account as (
select
min ( l . id ) as id ,
l . account_id as account_id ,
sum ( l . amount * u . factor ) as quantity ,
l . planning_id
from
report_account_analytic_planning_line l
left join
product_uom u on ( l . amount_unit = u . id )
group by
planning_id , account_id
)
""" )
report_account_analytic_planning_stat_account ( )
class report_account_analytic_planning_stat ( osv . osv ) :
_name = " report_account_analytic.planning.stat "
_description = " Planning stat "
_rec_name = ' user_id '
_auto = False
2007-05-21 14:00:24 +00:00
_log_access = False
2006-12-15 07:15:10 +00:00
def _sum_amount_real ( self , cr , uid , ids , name , args , context ) :
result = { }
for line in self . browse ( cr , uid , ids , context ) :
2007-06-19 18:43:04 +00:00
if line . user_id :
cr . execute ( ' select sum(unit_amount) from account_analytic_line where user_id= %d and account_id= %d and date>= %s and date<= %s ' , ( line . user_id . id , line . account_id . id , line . planning_id . date_from , line . planning_id . date_to ) )
else :
cr . execute ( ' select sum(unit_amount) from account_analytic_line where account_id= %d and date>= %s and date<= %s ' , ( line . account_id . id , line . planning_id . date_from , line . planning_id . date_to ) )
2006-12-15 07:15:10 +00:00
result [ line . id ] = cr . fetchone ( ) [ 0 ]
return result
def _sum_amount_tasks ( self , cr , uid , ids , name , args , context ) :
result = { }
for line in self . browse ( cr , uid , ids , context ) :
2007-06-19 18:43:04 +00:00
where = ' '
if line . user_id :
where = ' user_id= ' + str ( line . user_id . id ) + ' and '
2006-12-15 07:15:10 +00:00
cr . execute ( ''' select
sum ( planned_hours )
from
project_task
where
2007-06-19 18:43:04 +00:00
''' +where+ '''
2006-12-15 07:15:10 +00:00
project_id in ( select id from project_project where category_id = % d ) and
date_close > = % s and
date_close < = % s ''' , (
line . account_id . id ,
line . planning_id . date_from ,
line . planning_id . date_to )
)
result [ line . id ] = cr . fetchone ( ) [ 0 ]
return result
_columns = {
' planning_id ' : fields . many2one ( ' report_account_analytic.planning ' , ' Planning ' ) ,
2007-06-19 18:35:04 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User ' ) ,
2007-05-30 20:22:11 +00:00
' manager_id ' : fields . many2one ( ' res.users ' , ' Manager ' ) ,
2006-12-15 07:15:10 +00:00
' account_id ' : fields . many2one ( ' account.analytic.account ' , ' Account ' , required = True ) ,
2007-08-08 19:55:39 +00:00
' sum_amount ' : fields . float ( ' Planned hours ' , required = True ) ,
2007-06-10 10:44:02 +00:00
' sum_amount_real ' : fields . function ( _sum_amount_real , method = True , string = ' Timesheet ' ) ,
' sum_amount_tasks ' : fields . function ( _sum_amount_tasks , method = True , string = ' Tasks ' ) ,
2006-12-15 07:15:10 +00:00
}
2006-12-20 05:57:18 +00:00
_order = ' planning_id,user_id '
2006-12-15 07:15:10 +00:00
def init ( self , cr ) :
cr . execute ( """
create or replace view report_account_analytic_planning_stat as (
select
min ( l . id ) as id ,
l . user_id as user_id ,
2007-05-30 20:22:11 +00:00
a . user_id as manager_id ,
2006-12-15 07:15:10 +00:00
l . account_id as account_id ,
sum ( l . amount * u . factor ) as sum_amount ,
l . planning_id
from
report_account_analytic_planning_line l
2007-05-30 20:22:11 +00:00
left join
2008-02-05 20:27:36 +00:00
report_account_analytic_planning a on ( a . id = l . planning_id )
2006-12-15 07:15:10 +00:00
left join
product_uom u on ( l . amount_unit = u . id )
group by
2007-05-30 20:22:11 +00:00
l . planning_id , l . user_id , l . account_id , a . user_id
2006-12-15 07:15:10 +00:00
)
""" )
report_account_analytic_planning_stat ( )
class report_account_analytic_planning_stat_user ( osv . osv ) :
_name = " report_account_analytic.planning.stat.user "
_description = " Planning user stat "
_rec_name = ' user_id '
_auto = False
2007-06-15 06:01:18 +00:00
_log_access = False
2007-10-01 20:44:03 +00:00
def _sum_amount_real ( self , cr , uid , ids , name , args , context ) :
result = { }
for line in self . browse ( cr , uid , ids , context ) :
result [ line . id ] = 0.0
if line . user_id :
cr . execute ( ' select sum(unit_amount) from account_analytic_line where user_id= %d and date>= %s and date<= %s ' , ( line . user_id . id , line . planning_id . date_from , line . planning_id . date_to ) )
result [ line . id ] = cr . fetchone ( ) [ 0 ]
return result
2006-12-15 07:15:10 +00:00
_columns = {
2007-06-19 18:35:04 +00:00
' planning_id ' : fields . many2one ( ' report_account_analytic.planning ' , ' Planning ' , required = True ) ,
' user_id ' : fields . many2one ( ' res.users ' , ' User ' ) ,
2007-10-01 20:44:03 +00:00
' quantity ' : fields . float ( ' Planned ' , required = True ) ,
' sum_amount_real ' : fields . function ( _sum_amount_real , method = True , string = ' Timesheet ' ) ,
2006-12-15 07:15:10 +00:00
}
def init ( self , cr ) :
cr . execute ( """
create or replace view report_account_analytic_planning_stat_user as (
select
min ( l . id ) as id ,
l . user_id as user_id ,
sum ( l . amount * u . factor ) as quantity ,
l . planning_id
from
report_account_analytic_planning_line l
left join
product_uom u on ( l . amount_unit = u . id )
group by
planning_id , user_id
)
""" )
report_account_analytic_planning_stat_user ( )