2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-01-08 11:05:05 +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>).
2006-12-07 13:41:40 +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.
2006-12-07 13:41:40 +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.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-01-08 11:05:05 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
2010-07-09 07:27:50 +00:00
2008-09-07 23:24:39 +00:00
from lxml import etree
2006-12-07 13:41:40 +00:00
import time
2010-08-12 07:31:45 +00:00
from datetime import datetime , date
2010-07-19 12:17:16 +00:00
2009-03-16 14:07:32 +00:00
from tools . translate import _
2006-12-07 13:41:40 +00:00
from osv import fields , osv
2010-09-14 11:02:02 +00:00
2010-01-18 12:54:12 +00:00
class project_task_type ( osv . osv ) :
_name = ' project.task.type '
2010-01-31 21:29:06 +00:00
_description = ' Task Stage '
2010-08-05 07:20:23 +00:00
_order = ' sequence '
2010-01-18 12:54:12 +00:00
_columns = {
2010-01-31 21:29:06 +00:00
' name ' : fields . char ( ' Stage Name ' , required = True , size = 64 , translate = True ) ,
2010-01-18 12:54:12 +00:00
' description ' : fields . text ( ' Description ' ) ,
' sequence ' : fields . integer ( ' Sequence ' ) ,
}
2010-02-09 10:20:13 +00:00
2010-01-18 12:54:12 +00:00
_defaults = {
2010-07-02 14:37:06 +00:00
' sequence ' : 1
2010-01-18 12:54:12 +00:00
}
2010-07-27 12:43:02 +00:00
2010-01-18 12:54:12 +00:00
project_task_type ( )
2010-01-18 13:06:09 +00:00
2006-12-07 13:41:40 +00:00
class project ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " project.project "
_description = " Project "
2010-07-27 12:43:02 +00:00
_inherits = { ' account.analytic.account ' : " analytic_account_id " }
2010-02-09 14:06:16 +00:00
2010-03-16 12:51:46 +00:00
def search ( self , cr , user , args , offset = 0 , limit = None , order = None , context = None , count = False ) :
2010-02-11 06:35:49 +00:00
if user == 1 :
2010-07-09 07:27:50 +00:00
return super ( project , self ) . search ( cr , user , args , offset = offset , limit = limit , order = order , context = context , count = count )
2010-02-09 14:06:16 +00:00
if context and context . has_key ( ' user_prefence ' ) and context [ ' user_prefence ' ] :
cr . execute ( """ SELECT project.id FROM project_project project
2010-07-15 07:35:17 +00:00
LEFT JOIN account_analytic_account account ON account . id = project . analytic_account_id
LEFT JOIN project_user_rel rel ON rel . project_id = project . analytic_account_id
2010-02-11 06:35:49 +00:00
WHERE ( account . user_id = % s or rel . uid = % s ) """ % (user, user))
2010-08-05 07:20:23 +00:00
return [ ( r [ 0 ] ) for r in cr . fetchall ( ) ]
2010-02-09 14:06:16 +00:00
return super ( project , self ) . search ( cr , user , args , offset = offset , limit = limit , order = order ,
context = context , count = count )
2010-07-02 14:37:06 +00:00
def _complete_name ( self , cr , uid , ids , name , args , context = None ) :
2009-03-09 08:56:56 +00:00
res = { }
for m in self . browse ( cr , uid , ids , context = context ) :
2009-03-09 11:30:13 +00:00
res [ m . id ] = ( m . parent_id and ( m . parent_id . name + ' / ' ) or ' ' ) + m . name
2009-03-09 08:56:56 +00:00
return res
2010-07-02 14:37:06 +00:00
def onchange_partner_id ( self , cr , uid , ids , part = False , context = None ) :
2010-03-16 12:51:46 +00:00
partner_obj = self . pool . get ( ' res.partner ' )
2008-07-22 15:11:28 +00:00
if not part :
return { ' value ' : { ' contact_id ' : False , ' pricelist_id ' : False } }
2010-03-16 12:51:46 +00:00
addr = partner_obj . address_get ( cr , uid , [ part ] , [ ' contact ' ] )
pricelist = partner_obj . read ( cr , uid , part , [ ' property_product_pricelist ' ] , context = context )
pricelist_id = pricelist . get ( ' property_product_pricelist ' , False ) and pricelist . get ( ' property_product_pricelist ' ) [ 0 ] or False
return { ' value ' : { ' contact_id ' : addr [ ' contact ' ] , ' pricelist_id ' : pricelist_id } }
2006-12-07 13:41:40 +00:00
2010-07-02 14:37:06 +00:00
def _progress_rate ( self , cr , uid , ids , names , arg , context = None ) :
2008-09-04 17:16:05 +00:00
res = { } . fromkeys ( ids , 0.0 )
2009-08-03 14:17:02 +00:00
progress = { }
2008-09-04 17:16:05 +00:00
if not ids :
return res
2010-07-19 12:17:16 +00:00
cr . execute ( ''' SELECT
2010-10-18 19:59:42 +00:00
project_id , sum ( planned_hours ) , sum ( total_hours ) , sum ( effective_hours ) , SUM ( remaining_hours )
2010-07-19 12:17:16 +00:00
FROM
2010-10-28 11:36:41 +00:00
project_task
2010-07-19 12:17:16 +00:00
WHERE
2010-10-17 20:31:19 +00:00
project_id in % s AND
2010-07-19 12:17:16 +00:00
state < > ' cancelled '
GROUP BY
2010-10-18 19:59:42 +00:00
project_id ''' , (tuple(ids),))
progress = dict ( map ( lambda x : ( x [ 0 ] , ( x [ 1 ] , x [ 2 ] , x [ 3 ] , x [ 4 ] ) ) , cr . fetchall ( ) ) )
2010-10-17 20:31:19 +00:00
for project in self . browse ( cr , uid , ids , context = context ) :
2010-10-18 19:59:42 +00:00
s = progress . get ( project . id , ( 0.0 , 0.0 , 0.0 , 0.0 ) )
2008-11-23 10:18:15 +00:00
res [ project . id ] = {
' planned_hours ' : s [ 0 ] ,
' effective_hours ' : s [ 2 ] ,
' total_hours ' : s [ 1 ] ,
2010-10-18 19:59:42 +00:00
' progress_rate ' : s [ 1 ] and round ( 100.0 * s [ 2 ] / s [ 1 ] , 2 ) or 0.0
2008-11-23 10:18:15 +00:00
}
2008-08-27 13:48:40 +00:00
return res
2010-10-12 11:43:30 +00:00
def _get_project_task ( self , cr , uid , ids , context = None ) :
result = { }
for task in self . pool . get ( ' project.task ' ) . browse ( cr , uid , ids , context = context ) :
if task . project_id : result [ task . project_id . id ] = True
return result . keys ( )
def _get_project_work ( self , cr , uid , ids , context = None ) :
result = { }
for work in self . pool . get ( ' project.task.work ' ) . browse ( cr , uid , ids , context = context ) :
if work . task_id and work . task_id . project_id : result [ work . task_id . project_id . id ] = True
return result . keys ( )
2008-10-27 23:58:25 +00:00
def unlink ( self , cr , uid , ids , * args , * * kwargs ) :
for proj in self . browse ( cr , uid , ids ) :
if proj . tasks :
2009-01-03 09:17:06 +00:00
raise osv . except_osv ( _ ( ' Operation Not Permitted ! ' ) , _ ( ' You can not delete a project with tasks. I suggest you to deactivate it. ' ) )
2008-10-27 23:58:25 +00:00
return super ( project , self ) . unlink ( cr , uid , ids , * args , * * kwargs )
2010-08-05 07:20:23 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
2010-01-31 21:29:06 +00:00
' complete_name ' : fields . function ( _complete_name , method = True , string = " Project Name " , type = ' char ' , size = 250 ) ,
2010-11-15 11:12:22 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to False, it will allow you to hide the project without removing it. " ) ,
2010-02-11 05:34:49 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of Projects. " ) ,
2010-08-12 19:29:33 +00:00
' analytic_account_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' , help = " Link this project to an analytic account if you need financial management on projects. It enables you to connect projects with budgets, planning, cost and revenue analysis, timesheets on projects, etc. " , ondelete = " cascade " , required = True ) ,
2011-01-05 06:55:19 +00:00
' priority ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying the list of projects " ) ,
2010-07-29 05:31:04 +00:00
' warn_manager ' : fields . boolean ( ' Warn Manager ' , help = " If you check this field, the project manager will receive a request each time a task is completed by his team. " , states = { ' close ' : [ ( ' readonly ' , True ) ] , ' cancelled ' : [ ( ' readonly ' , True ) ] } ) ,
2011-01-10 08:53:57 +00:00
2011-01-17 07:41:14 +00:00
' members ' : fields . many2many ( ' res.users ' , ' project_user_rel ' , ' project_id ' , ' uid ' , ' Project Members ' ,
help = " Project ' s members are users who can have an access to the tasks related to this project. " , states = { ' close ' : [ ( ' readonly ' , True ) ] , ' cancelled ' : [ ( ' readonly ' , True ) ] } ) ,
2008-07-22 15:11:28 +00:00
' tasks ' : fields . one2many ( ' project.task ' , ' project_id ' , " Project tasks " ) ,
2010-10-15 11:52:46 +00:00
' planned_hours ' : fields . function ( _progress_rate , multi = " progress " , method = True , string = ' Planned Time ' , help = " Sum of planned hours of all tasks related to this project and its child projects. " ,
2010-10-12 11:43:30 +00:00
store = {
' project.project ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' tasks ' ] , 10 ) ,
2010-10-18 19:59:42 +00:00
' project.task ' : ( _get_project_task , [ ' planned_hours ' , ' effective_hours ' , ' remaining_hours ' , ' total_hours ' , ' progress ' , ' delay_hours ' , ' state ' ] , 10 ) ,
2010-10-12 11:43:30 +00:00
} ) ,
2011-02-18 09:13:13 +00:00
' effective_hours ' : fields . function ( _progress_rate , multi = " progress " , method = True , string = ' Time Spent ' , help = " Sum of spent hours of all tasks related to this project and its child projects. " ) ,
2010-10-12 11:43:30 +00:00
' total_hours ' : fields . function ( _progress_rate , multi = " progress " , method = True , string = ' Total Time ' , help = " Sum of total hours of all tasks related to this project and its child projects. " ,
store = {
' project.project ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' tasks ' ] , 10 ) ,
2010-10-18 19:59:42 +00:00
' project.task ' : ( _get_project_task , [ ' planned_hours ' , ' effective_hours ' , ' remaining_hours ' , ' total_hours ' , ' progress ' , ' delay_hours ' , ' state ' ] , 10 ) ,
2010-10-12 11:43:30 +00:00
} ) ,
2011-02-18 09:13:13 +00:00
' progress_rate ' : fields . function ( _progress_rate , multi = " progress " , method = True , string = ' Progress ' , type = ' float ' , group_operator = " avg " , help = " Percent of tasks closed according to the total of tasks todo. " ) ,
2010-07-29 05:31:04 +00:00
' warn_customer ' : fields . boolean ( ' Warn Partner ' , help = " If you check this, the user will have a popup when closing a task that propose a message to send by email to the customer. " , states = { ' close ' : [ ( ' readonly ' , True ) ] , ' cancelled ' : [ ( ' readonly ' , True ) ] } ) ,
' warn_header ' : fields . text ( ' Mail Header ' , help = " Header added at the beginning of the email for the warning message sent to the customer when a task is closed. " , states = { ' close ' : [ ( ' readonly ' , True ) ] , ' cancelled ' : [ ( ' readonly ' , True ) ] } ) ,
' warn_footer ' : fields . text ( ' Mail Footer ' , help = " Footer added at the beginning of the email for the warning message sent to the customer when a task is closed. " , states = { ' close ' : [ ( ' readonly ' , True ) ] , ' cancelled ' : [ ( ' readonly ' , True ) ] } ) ,
' type_ids ' : fields . many2many ( ' project.task.type ' , ' project_task_type_rel ' , ' project_id ' , ' type_id ' , ' Tasks Stages ' , states = { ' close ' : [ ( ' readonly ' , True ) ] , ' cancelled ' : [ ( ' readonly ' , True ) ] } ) ,
2008-07-22 15:11:28 +00:00
}
2010-02-11 05:34:49 +00:00
_order = " sequence "
2008-07-22 15:11:28 +00:00
_defaults = {
2010-07-02 14:37:06 +00:00
' active ' : True ,
' priority ' : 1 ,
' sequence ' : 10 ,
2008-07-22 15:11:28 +00:00
}
2010-07-27 12:43:02 +00:00
2010-10-17 20:31:19 +00:00
# TODO: Why not using a SQL contraints ?
2010-11-22 10:37:53 +00:00
def _check_dates ( self , cr , uid , ids , context = None ) :
for leave in self . read ( cr , uid , ids , [ ' date_start ' , ' date ' ] , context = context ) :
2010-10-17 20:31:19 +00:00
if leave [ ' date_start ' ] and leave [ ' date ' ] :
if leave [ ' date_start ' ] > leave [ ' date ' ] :
return False
2010-11-22 10:37:53 +00:00
return True
2010-06-08 12:34:12 +00:00
2010-01-18 13:44:53 +00:00
_constraints = [
2010-07-26 09:41:14 +00:00
( _check_dates , ' Error! project start-date must be lower then project end-date. ' , [ ' date_start ' , ' date ' ] )
2010-01-18 13:44:53 +00:00
]
2010-07-27 12:43:02 +00:00
2010-07-02 14:37:06 +00:00
def set_template ( self , cr , uid , ids , context = None ) :
2010-01-08 11:05:05 +00:00
res = self . setActive ( cr , uid , ids , value = False , context = context )
2008-09-23 06:14:43 +00:00
return res
2010-07-02 14:37:06 +00:00
def set_done ( self , cr , uid , ids , context = None ) :
2010-07-10 13:18:44 +00:00
task_obj = self . pool . get ( ' project.task ' )
2010-07-13 19:54:11 +00:00
task_ids = task_obj . search ( cr , uid , [ ( ' project_id ' , ' in ' , ids ) , ( ' state ' , ' not in ' , ( ' cancelled ' , ' done ' ) ) ] )
task_obj . write ( cr , uid , task_ids , { ' state ' : ' done ' , ' date_end ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) , ' remaining_hours ' : 0.0 } )
2010-01-22 06:25:43 +00:00
self . write ( cr , uid , ids , { ' state ' : ' close ' } , context = context )
2010-07-08 10:14:24 +00:00
for ( id , name ) in self . name_get ( cr , uid , ids ) :
2010-10-17 17:30:00 +00:00
message = _ ( " The project ' %s ' has been closed. " ) % name
2010-07-08 10:14:24 +00:00
self . log ( cr , uid , id , message )
2008-10-27 11:13:26 +00:00
return True
2010-07-02 14:37:06 +00:00
def set_cancel ( self , cr , uid , ids , context = None ) :
2010-07-13 19:54:11 +00:00
task_obj = self . pool . get ( ' project.task ' )
task_ids = task_obj . search ( cr , uid , [ ( ' project_id ' , ' in ' , ids ) , ( ' state ' , ' != ' , ' done ' ) ] )
task_obj . write ( cr , uid , task_ids , { ' state ' : ' cancelled ' , ' date_end ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) , ' remaining_hours ' : 0.0 } )
2008-10-27 11:13:26 +00:00
self . write ( cr , uid , ids , { ' state ' : ' cancelled ' } , context = context )
return True
2010-07-02 14:37:06 +00:00
def set_pending ( self , cr , uid , ids , context = None ) :
2008-10-27 11:13:26 +00:00
self . write ( cr , uid , ids , { ' state ' : ' pending ' } , context = context )
return True
2010-07-02 14:37:06 +00:00
def set_open ( self , cr , uid , ids , context = None ) :
2008-10-27 11:13:26 +00:00
self . write ( cr , uid , ids , { ' state ' : ' open ' } , context = context )
return True
2010-07-02 14:37:06 +00:00
def reset_project ( self , cr , uid , ids , context = None ) :
res = self . setActive ( cr , uid , ids , value = True , context = context )
2010-07-08 10:14:24 +00:00
for ( id , name ) in self . name_get ( cr , uid , ids ) :
2010-10-17 17:30:00 +00:00
message = _ ( " The project ' %s ' has been opened. " ) % name
2010-07-08 10:14:24 +00:00
self . log ( cr , uid , id , message )
2008-09-12 05:42:31 +00:00
return res
2008-09-23 06:14:43 +00:00
2010-07-02 14:37:06 +00:00
def copy ( self , cr , uid , id , default = { } , context = None ) :
if context is None :
context = { }
2010-07-27 12:43:02 +00:00
2009-05-05 21:14:47 +00:00
proj = self . browse ( cr , uid , id , context = context )
2008-09-23 06:14:43 +00:00
default = default or { }
2009-05-05 21:14:47 +00:00
context [ ' active_test ' ] = False
default [ ' state ' ] = ' open '
2009-06-19 06:29:13 +00:00
if not default . get ( ' name ' , False ) :
2010-09-22 14:00:24 +00:00
default [ ' name ' ] = proj . name + _ ( ' (copy) ' )
2009-05-05 21:14:47 +00:00
res = super ( project , self ) . copy ( cr , uid , id , default , context )
2010-07-27 12:43:02 +00:00
2009-05-05 21:14:47 +00:00
return res
2008-09-23 06:14:43 +00:00
2010-07-02 14:37:06 +00:00
def duplicate_template ( self , cr , uid , ids , context = None ) :
if context is None :
context = { }
2010-03-16 12:51:46 +00:00
project_obj = self . pool . get ( ' project.project ' )
2011-03-02 08:45:54 +00:00
task_pool = self . pool . get ( ' project.task ' )
2010-04-05 10:17:04 +00:00
data_obj = self . pool . get ( ' ir.model.data ' )
2010-01-22 07:41:48 +00:00
result = [ ]
2010-03-16 12:51:46 +00:00
for proj in self . browse ( cr , uid , ids , context = context ) :
2010-08-12 07:31:45 +00:00
parent_id = context . get ( ' parent_id ' , False )
2010-07-01 09:08:44 +00:00
context . update ( { ' analytic_project_copy ' : True } )
2010-08-12 07:31:45 +00:00
new_date_start = time . strftime ( ' % Y- % m- %d ' )
new_date_end = False
if proj . date_start and proj . date :
start_date = date ( * time . strptime ( proj . date_start , ' % Y- % m- %d ' ) [ : 3 ] )
end_date = date ( * time . strptime ( proj . date , ' % Y- % m- %d ' ) [ : 3 ] )
new_date_end = ( datetime ( * time . strptime ( new_date_start , ' % Y- % m- %d ' ) [ : 3 ] ) + ( end_date - start_date ) ) . strftime ( ' % Y- % m- %d ' )
2011-02-18 07:36:11 +00:00
context . update ( { ' copy ' : True } )
2010-03-16 12:51:46 +00:00
new_id = project_obj . copy ( cr , uid , proj . id , default = {
2010-01-22 07:41:48 +00:00
' name ' : proj . name + _ ( ' (copy) ' ) ,
' state ' : ' open ' ,
2010-08-12 07:31:45 +00:00
' date_start ' : new_date_start ,
' date ' : new_date_end ,
2010-03-16 12:51:46 +00:00
' parent_id ' : parent_id } , context = context )
2010-01-22 07:41:48 +00:00
result . append ( new_id )
2011-03-02 08:45:54 +00:00
new_project = self . browse ( cr , uid , new_id , context )
if new_project . tasks :
new_ids = [ task . id for task in new_project . tasks ]
old_ids = [ task . id for task in proj . tasks ]
task_pool . duplicate_task ( cr , uid , new_ids , old_ids , new_id , context )
2010-09-27 13:44:03 +00:00
2010-07-15 07:35:17 +00:00
child_ids = self . search ( cr , uid , [ ( ' parent_id ' , ' = ' , proj . analytic_account_id . id ) ] , context = context )
parent_id = self . read ( cr , uid , new_id , [ ' analytic_account_id ' ] ) [ ' analytic_account_id ' ] [ 0 ]
2010-01-22 06:25:43 +00:00
if child_ids :
2010-07-01 05:50:01 +00:00
self . duplicate_template ( cr , uid , child_ids , context = { ' parent_id ' : parent_id } )
2010-04-05 10:17:04 +00:00
if result and len ( result ) :
res_id = result [ 0 ]
form_view_id = data_obj . _get_id ( cr , uid , ' project ' , ' edit_project ' )
form_view = data_obj . read ( cr , uid , form_view_id , [ ' res_id ' ] )
2010-08-13 05:07:55 +00:00
tree_view_id = data_obj . _get_id ( cr , uid , ' project ' , ' view_project ' )
2010-04-05 10:17:04 +00:00
tree_view = data_obj . read ( cr , uid , tree_view_id , [ ' res_id ' ] )
search_view_id = data_obj . _get_id ( cr , uid , ' project ' , ' view_project_project_filter ' )
search_view = data_obj . read ( cr , uid , search_view_id , [ ' res_id ' ] )
return {
' name ' : _ ( ' Projects ' ) ,
' view_type ' : ' form ' ,
' view_mode ' : ' form,tree ' ,
' res_model ' : ' project.project ' ,
' view_id ' : False ,
2010-07-02 14:37:06 +00:00
' res_id ' : res_id ,
2010-04-05 10:17:04 +00:00
' views ' : [ ( form_view [ ' res_id ' ] , ' form ' ) , ( tree_view [ ' res_id ' ] , ' tree ' ) ] ,
' type ' : ' ir.actions.act_window ' ,
' search_view_id ' : search_view [ ' res_id ' ] ,
' nodestroy ' : True
2010-08-13 12:20:05 +00:00
}
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
# set active value for a project, its sub projects and its tasks
2010-07-02 14:37:06 +00:00
def setActive ( self , cr , uid , ids , value = True , context = None ) :
2010-03-16 12:51:46 +00:00
task_obj = self . pool . get ( ' project.task ' )
2010-07-02 14:37:06 +00:00
for proj in self . browse ( cr , uid , ids , context = None ) :
2008-09-12 05:42:31 +00:00
self . write ( cr , uid , [ proj . id ] , { ' state ' : value and ' open ' or ' template ' } , context )
2008-12-10 14:29:55 +00:00
cr . execute ( ' select id from project_task where project_id= %s ' , ( proj . id , ) )
2008-09-12 10:25:46 +00:00
tasks_id = [ x [ 0 ] for x in cr . fetchall ( ) ]
2008-09-12 05:42:31 +00:00
if tasks_id :
2010-03-16 12:51:46 +00:00
task_obj . write ( cr , uid , tasks_id , { ' active ' : value } , context = context )
2010-07-20 05:58:59 +00:00
child_ids = self . search ( cr , uid , [ ( ' parent_id ' , ' = ' , proj . analytic_account_id . id ) ] )
2010-01-22 06:25:43 +00:00
if child_ids :
2010-07-02 14:37:06 +00:00
self . setActive ( cr , uid , child_ids , value , context = None )
2008-07-22 15:11:28 +00:00
return True
2010-10-11 05:51:53 +00:00
2006-12-07 13:41:40 +00:00
project ( )
2010-08-19 06:17:31 +00:00
class users ( osv . osv ) :
_inherit = ' res.users '
_columns = {
' context_project_id ' : fields . many2one ( ' project.project ' , ' Project ' )
}
users ( )
2006-12-07 13:41:40 +00:00
class task ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " project.task "
2010-05-19 18:32:32 +00:00
_description = " Task "
2010-05-19 20:02:36 +00:00
_log_create = True
2008-07-22 15:11:28 +00:00
_date_name = " date_start "
2010-08-05 05:28:15 +00:00
2010-08-03 12:38:19 +00:00
def search ( self , cr , user , args , offset = 0 , limit = None , order = None , context = None , count = False ) :
2010-08-05 05:28:15 +00:00
obj_project = self . pool . get ( ' project.project ' )
2010-08-03 12:38:19 +00:00
for domain in args :
2010-08-04 12:11:24 +00:00
if domain [ 0 ] == ' project_id ' and ( not isinstance ( domain [ 2 ] , str ) ) :
2010-08-05 05:28:15 +00:00
id = isinstance ( domain [ 2 ] , list ) and domain [ 2 ] [ 0 ] or domain [ 2 ]
if id and isinstance ( id , ( long , int ) ) :
if obj_project . read ( cr , user , id , [ ' state ' ] ) [ ' state ' ] == ' template ' :
args . append ( ( ' active ' , ' = ' , False ) )
2010-08-03 12:38:19 +00:00
return super ( task , self ) . search ( cr , user , args , offset = offset , limit = limit , order = order , context = context , count = count )
2010-01-12 05:32:48 +00:00
2010-07-02 14:37:06 +00:00
def _str_get ( self , task , level = 0 , border = ' *** ' , context = None ) :
2008-07-22 15:11:28 +00:00
return border + ' ' + ( task . user_id and task . user_id . name . upper ( ) or ' ' ) + ( level and ( ' : L ' + str ( level ) ) or ' ' ) + ( ' - %.1f h / %.1f h ' % ( task . effective_hours or 0.0 , task . planned_hours ) ) + ' ' + border + ' \n ' + \
border [ 0 ] + ' ' + ( task . name or ' ' ) + ' \n ' + \
( task . description or ' ' ) + ' \n \n '
2010-08-05 05:28:15 +00:00
2010-01-31 21:29:06 +00:00
# Compute: effective_hours, total_hours, progress
2010-05-03 11:01:51 +00:00
def _hours_get ( self , cr , uid , ids , field_names , args , context = None ) :
res = { }
2010-06-10 13:34:19 +00:00
cr . execute ( " SELECT task_id, COALESCE(SUM(hours),0) FROM project_task_work WHERE task_id IN %s GROUP BY task_id " , ( tuple ( ids ) , ) )
2008-09-07 23:24:39 +00:00
hours = dict ( cr . fetchall ( ) )
for task in self . browse ( cr , uid , ids , context = context ) :
2010-10-17 20:31:19 +00:00
res [ task . id ] = { ' effective_hours ' : hours . get ( task . id , 0.0 ) , ' total_hours ' : ( task . remaining_hours or 0.0 ) + hours . get ( task . id , 0.0 ) }
res [ task . id ] [ ' delay_hours ' ] = res [ task . id ] [ ' total_hours ' ] - task . planned_hours
2010-05-27 09:57:03 +00:00
res [ task . id ] [ ' progress ' ] = 0.0
2008-09-07 23:24:39 +00:00
if ( task . remaining_hours + hours . get ( task . id , 0.0 ) ) :
2010-08-03 12:38:19 +00:00
res [ task . id ] [ ' progress ' ] = round ( min ( 100.0 * hours . get ( task . id , 0.0 ) / res [ task . id ] [ ' total_hours ' ] , 99.99 ) , 2 )
if task . state in ( ' done ' , ' cancelled ' ) :
2010-01-31 21:29:06 +00:00
res [ task . id ] [ ' progress ' ] = 100.0
2008-07-22 15:11:28 +00:00
return res
2006-12-07 13:41:40 +00:00
2010-08-03 12:38:19 +00:00
2010-10-04 18:47:57 +00:00
def onchange_remaining ( self , cr , uid , ids , remaining = 0.0 , planned = 0.0 ) :
if remaining and not planned :
return { ' value ' : { ' planned_hours ' : remaining } }
return { }
2010-03-02 13:27:02 +00:00
def onchange_planned ( self , cr , uid , ids , planned = 0.0 , effective = 0.0 ) :
2010-07-02 14:37:06 +00:00
return { ' value ' : { ' remaining_hours ' : planned - effective } }
2010-12-17 12:38:44 +00:00
def onchange_project ( self , cr , uid , id , project_id ) :
if not project_id :
return { }
data = self . pool . get ( ' project.project ' ) . browse ( cr , uid , [ project_id ] )
partner_id = data and data [ 0 ] . parent_id . partner_id
if partner_id :
return { ' value ' : { ' partner_id ' : partner_id . id } }
return { }
2010-07-02 14:37:06 +00:00
def _default_project ( self , cr , uid , context = None ) :
2010-07-09 07:27:50 +00:00
if context is None :
context = { }
2008-10-28 16:23:02 +00:00
if ' project_id ' in context and context [ ' project_id ' ] :
2010-02-02 13:33:38 +00:00
return int ( context [ ' project_id ' ] )
2008-10-28 16:23:02 +00:00
return False
2011-03-02 08:45:54 +00:00
def duplicate_task ( self , cr , uid , new_ids , old_ids , project_id , context = None ) :
project_pool = self . pool . get ( " project.project " )
new_proj = project_pool . browse ( cr , uid , project_id , context )
for new in new_ids :
task = self . browse ( cr , uid , new , context )
child_ids = [ ch . id for ch in task . child_ids ]
if task . child_ids :
for child in task . child_ids :
if child . id in old_ids :
relate = self . search ( cr , uid , [ ( ' project_id ' , ' = ' , project_id ) , ( ' name ' , ' = ' , child . name ) ] )
child_ids . remove ( child . id )
child_ids . extend ( relate )
self . write ( cr , uid , new , { ' child_ids ' : [ ( 6 , 0 , child_ids ) ] } )
parent_ids = [ ch . id for ch in task . parent_ids ]
if task . parent_ids :
for parent in task . parent_ids :
if parent . id in old_ids :
relate = self . search ( cr , uid , [ ( ' project_id ' , ' = ' , project_id ) , ( ' name ' , ' = ' , parent . name ) ] )
parent_ids . remove ( parent . id )
parent_ids . extend ( relate )
self . write ( cr , uid , new , { ' child_ids ' : [ ( 6 , 0 , child_ids ) ] } )
return True
2010-07-02 14:37:06 +00:00
def copy_data ( self , cr , uid , id , default = { } , context = None ) :
2008-11-28 10:21:42 +00:00
default = default or { }
2010-08-10 10:32:59 +00:00
default . update ( { ' work_ids ' : [ ] , ' date_start ' : False , ' date_end ' : False , ' date_deadline ' : False } )
if not default . get ( ' remaining_hours ' , False ) :
default [ ' remaining_hours ' ] = float ( self . read ( cr , uid , id , [ ' planned_hours ' ] ) [ ' planned_hours ' ] )
2010-07-18 19:43:57 +00:00
default [ ' active ' ] = True
2010-09-29 07:53:45 +00:00
default [ ' type_id ' ] = False
2010-09-18 05:52:11 +00:00
if not default . get ( ' name ' , False ) :
2011-02-18 07:36:11 +00:00
default [ ' name ' ] = self . browse ( cr , uid , id , context = context ) . name or ' '
if not context . get ( ' copy ' , False ) :
2011-02-18 10:31:07 +00:00
new_name = _ ( " %s (copy) " ) % default . get ( ' name ' , ' ' )
default . update ( { ' name ' : new_name } )
2009-02-04 13:12:10 +00:00
return super ( task , self ) . copy_data ( cr , uid , id , default , context )
2008-11-28 10:21:42 +00:00
2010-07-02 14:37:06 +00:00
def _check_dates ( self , cr , uid , ids , context = None ) :
task = self . read ( cr , uid , ids [ 0 ] , [ ' date_start ' , ' date_end ' ] )
if task [ ' date_start ' ] and task [ ' date_end ' ] :
2010-01-20 12:44:10 +00:00
if task [ ' date_start ' ] > task [ ' date_end ' ] :
return False
2010-07-02 14:37:06 +00:00
return True
2010-09-18 05:52:11 +00:00
2010-07-18 19:43:57 +00:00
def _is_template ( self , cr , uid , ids , field_name , arg , context = None ) :
res = { }
for task in self . browse ( cr , uid , ids , context = context ) :
res [ task . id ] = True
if task . project_id :
if task . project_id . active == False or task . project_id . state == ' template ' :
res [ task . id ] = False
return res
2010-08-05 07:20:23 +00:00
2010-10-12 11:43:30 +00:00
def _get_task ( self , cr , uid , ids , context = None ) :
result = { }
for work in self . pool . get ( ' project.task.work ' ) . browse ( cr , uid , ids , context = context ) :
if work . task_id : result [ work . task_id . id ] = True
return result . keys ( )
2011-03-02 08:45:54 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
2010-07-18 19:43:57 +00:00
' active ' : fields . function ( _is_template , method = True , store = True , string = ' Not a Template Task ' , type = ' boolean ' , help = " This field is computed automatically and have the same behavior than the boolean ' active ' field: if the task is linked to a template or unactivated project, it will be hidden unless specifically asked. " ) ,
2010-01-18 21:38:14 +00:00
' name ' : fields . char ( ' Task Summary ' , size = 128 , required = True ) ,
2008-07-22 15:11:28 +00:00
' description ' : fields . text ( ' Description ' ) ,
2011-02-21 14:05:10 +00:00
' priority ' : fields . selection ( [ ( ' 4 ' , ' Very Low ' ) , ( ' 3 ' , ' Low ' ) , ( ' 2 ' , ' Medium ' ) , ( ' 1 ' , ' Important ' ) , ( ' 0 ' , ' Very important ' ) ] , ' Priority ' ) ,
2009-12-21 13:14:12 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of tasks. " ) ,
2010-10-17 21:30:29 +00:00
' type_id ' : fields . many2one ( ' project.task.type ' , ' Stage ' ) ,
2009-11-23 13:52:38 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' Draft ' ) , ( ' open ' , ' In Progress ' ) , ( ' pending ' , ' Pending ' ) , ( ' cancelled ' , ' Cancelled ' ) , ( ' done ' , ' Done ' ) ] , ' State ' , readonly = True , required = True ,
2010-07-09 07:27:50 +00:00
help = ' If the task is created the state is \' Draft \' . \n If the task is started, the state becomes \' In Progress \' . \n If review is needed the task is in \' Pending \' state. \
2009-11-25 10:03:47 +00:00
\n If the task is over , the states is set to \' Done \' . ' ) ,
2011-01-17 13:12:57 +00:00
' create_date ' : fields . datetime ( ' Create Date ' , readonly = True , select = True ) ,
' date_start ' : fields . datetime ( ' Starting Date ' , select = True ) ,
' date_end ' : fields . datetime ( ' Ending Date ' , select = True ) ,
' date_deadline ' : fields . date ( ' Deadline ' , select = True ) ,
2010-09-20 06:11:01 +00:00
' project_id ' : fields . many2one ( ' project.project ' , ' Project ' , ondelete = ' cascade ' ) ,
2010-01-12 05:32:48 +00:00
' parent_ids ' : fields . many2many ( ' project.task ' , ' project_task_parent_rel ' , ' task_id ' , ' parent_id ' , ' Parent Tasks ' ) ,
2010-02-15 15:31:31 +00:00
' child_ids ' : fields . many2many ( ' project.task ' , ' project_task_parent_rel ' , ' parent_id ' , ' task_id ' , ' Delegated Tasks ' ) ,
2008-07-22 15:11:28 +00:00
' notes ' : fields . text ( ' Notes ' ) ,
2010-09-20 12:07:06 +00:00
' planned_hours ' : fields . float ( ' Planned Hours ' , help = ' Estimated time to do the task, usually set by the project manager when the task is in draft state. ' ) ,
2010-10-12 11:43:30 +00:00
' effective_hours ' : fields . function ( _hours_get , method = True , string = ' Hours Spent ' , multi = ' hours ' , help = " Computed using the sum of the task work done. " ,
store = {
2010-10-17 20:31:19 +00:00
' project.task ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' work_ids ' , ' remaining_hours ' , ' planned_hours ' ] , 10 ) ,
2010-10-12 11:43:30 +00:00
' project.task.work ' : ( _get_task , [ ' hours ' ] , 10 ) ,
} ) ,
2010-08-03 12:38:19 +00:00
' remaining_hours ' : fields . float ( ' Remaining Hours ' , digits = ( 16 , 2 ) , help = " Total remaining time, can be re-estimated periodically by the assignee of the task. " ) ,
2010-10-12 11:43:30 +00:00
' total_hours ' : fields . function ( _hours_get , method = True , string = ' Total Hours ' , multi = ' hours ' , help = " Computed as: Time Spent + Remaining Time. " ,
store = {
2010-10-17 20:31:19 +00:00
' project.task ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' work_ids ' , ' remaining_hours ' , ' planned_hours ' ] , 10 ) ,
2010-10-12 11:43:30 +00:00
' project.task.work ' : ( _get_task , [ ' hours ' ] , 10 ) ,
} ) ,
' progress ' : fields . function ( _hours_get , method = True , string = ' Progress ( % ) ' , multi = ' hours ' , group_operator = " avg " , help = " Computed as: Time Spent / Total Time. " ,
store = {
2010-10-17 20:31:19 +00:00
' project.task ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' work_ids ' , ' remaining_hours ' , ' planned_hours ' , ' state ' ] , 10 ) ,
2010-10-12 11:43:30 +00:00
' project.task.work ' : ( _get_task , [ ' hours ' ] , 10 ) ,
} ) ,
' delay_hours ' : fields . function ( _hours_get , method = True , string = ' Delay Hours ' , multi = ' hours ' , help = " Computed as difference of the time estimated by the project manager and the real time to close the task. " ,
store = {
2010-10-17 20:31:19 +00:00
' project.task ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' work_ids ' , ' remaining_hours ' , ' planned_hours ' ] , 10 ) ,
2010-10-12 11:43:30 +00:00
' project.task.work ' : ( _get_task , [ ' hours ' ] , 10 ) ,
} ) ,
2008-07-22 15:11:28 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Assigned to ' ) ,
2010-07-02 14:37:06 +00:00
' delegated_user_id ' : fields . related ( ' child_ids ' , ' user_id ' , type = ' many2one ' , relation = ' res.users ' , string = ' Delegated To ' ) ,
2008-09-04 17:16:05 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' ) ,
2009-02-26 10:33:11 +00:00
' work_ids ' : fields . one2many ( ' project.task.work ' , ' task_id ' , ' Work done ' ) ,
2010-07-15 07:35:17 +00:00
' manager_id ' : fields . related ( ' project_id ' , ' analytic_account_id ' , ' user_id ' , type = ' many2one ' , relation = ' res.users ' , string = ' Project Manager ' ) ,
2009-11-10 12:49:20 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' ) ,
2010-09-30 14:41:19 +00:00
' id ' : fields . integer ( ' ID ' ) ,
2008-07-22 15:11:28 +00:00
}
2010-07-27 12:43:02 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
2010-07-02 14:37:06 +00:00
' state ' : ' draft ' ,
' priority ' : ' 2 ' ,
' progress ' : 0 ,
' sequence ' : 10 ,
' active ' : True ,
2008-10-28 16:23:02 +00:00
' project_id ' : _default_project ,
2010-08-18 06:59:40 +00:00
' user_id ' : lambda obj , cr , uid , context : uid ,
2010-03-16 12:51:46 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' project.task ' , context = c )
2008-07-22 15:11:28 +00:00
}
2010-07-27 12:43:02 +00:00
2011-01-11 09:06:25 +00:00
_order = " sequence,priority, date_start, name, id "
2010-09-18 05:52:11 +00:00
2010-11-22 10:37:53 +00:00
def _check_recursion ( self , cr , uid , ids , context = None ) :
obj_task = self . browse ( cr , uid , ids [ 0 ] , context = context )
2010-09-14 11:02:02 +00:00
parent_ids = [ x . id for x in obj_task . parent_ids ]
children_ids = [ x . id for x in obj_task . child_ids ]
2010-09-18 05:52:11 +00:00
2010-09-14 11:02:02 +00:00
if ( obj_task . id in children_ids ) or ( obj_task . id in parent_ids ) :
return False
2010-09-18 05:52:11 +00:00
2010-09-14 11:02:02 +00:00
while ( ids ) :
cr . execute ( ' SELECT DISTINCT task_id ' \
' FROM project_task_parent_rel ' \
' WHERE parent_id IN %s ' , ( tuple ( ids ) , ) )
2010-09-20 13:59:21 +00:00
child_ids = map ( lambda x : x [ 0 ] , cr . fetchall ( ) )
2010-09-14 11:02:02 +00:00
c_ids = child_ids
if ( list ( set ( parent_ids ) . intersection ( set ( c_ids ) ) ) ) or ( obj_task . id in c_ids ) :
return False
while len ( c_ids ) :
s_ids = self . search ( cr , uid , [ ( ' parent_ids ' , ' in ' , c_ids ) ] )
if ( list ( set ( parent_ids ) . intersection ( set ( s_ids ) ) ) ) :
return False
c_ids = s_ids
ids = child_ids
return True
2006-12-07 13:41:40 +00:00
2011-01-18 06:57:42 +00:00
def _check_dates ( self , cr , uid , ids , context = None ) :
if context == None :
context = { }
obj_task = self . browse ( cr , uid , ids [ 0 ] , context = context )
start = obj_task . date_start or False
end = obj_task . date_end or False
if start and end :
if start > end :
return False
return True
2010-01-20 12:44:10 +00:00
_constraints = [
2011-01-18 06:57:42 +00:00
( _check_recursion , ' Error ! You cannot create recursive tasks. ' , [ ' parent_ids ' ] ) ,
( _check_dates , ' Error ! Task end-date must be greater then task start-date ' , [ ' date_start ' , ' date_end ' ] )
2010-01-20 12:44:10 +00:00
]
2008-09-07 23:24:39 +00:00
#
# Override view according to the company definition
#
2010-07-27 12:43:02 +00:00
2010-12-17 12:38:44 +00:00
2009-09-24 10:46:21 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' , context = None , toolbar = False , submenu = False ) :
2010-03-16 12:51:46 +00:00
users_obj = self . pool . get ( ' res.users ' )
2010-10-25 14:54:45 +00:00
# read uom as admin to avoid access rights issues, e.g. for portal/share users,
# this should be safe (no context passed to avoid side-effects)
2010-11-22 10:37:53 +00:00
obj_tm = users_obj . browse ( cr , 1 , uid , context = context ) . company_id . project_time_mode_id
2009-12-02 14:40:35 +00:00
tm = obj_tm and obj_tm . name or ' Hours '
2008-09-07 23:24:39 +00:00
2009-11-28 11:05:26 +00:00
res = super ( task , self ) . fields_view_get ( cr , uid , view_id , view_type , context , toolbar , submenu = submenu )
2009-12-02 14:40:35 +00:00
if tm in [ ' Hours ' , ' Hour ' ] :
2008-09-07 23:24:39 +00:00
return res
2009-12-02 14:40:35 +00:00
2008-09-07 23:24:39 +00:00
eview = etree . fromstring ( res [ ' arch ' ] )
2010-01-08 11:05:05 +00:00
2009-12-02 14:40:35 +00:00
def _check_rec ( eview ) :
if eview . attrib . get ( ' widget ' , ' ' ) == ' float_time ' :
2008-09-07 23:24:39 +00:00
eview . set ( ' widget ' , ' float ' )
for child in eview :
2009-12-02 14:40:35 +00:00
_check_rec ( child )
2008-09-07 23:24:39 +00:00
return True
2010-01-08 11:05:05 +00:00
2009-12-02 14:40:35 +00:00
_check_rec ( eview )
2010-01-08 11:05:05 +00:00
2008-09-07 23:24:39 +00:00
res [ ' arch ' ] = etree . tostring ( eview )
2010-01-08 11:05:05 +00:00
2008-09-07 23:24:39 +00:00
for f in res [ ' fields ' ] :
if ' Hours ' in res [ ' fields ' ] [ f ] [ ' string ' ] :
2009-09-24 10:46:21 +00:00
res [ ' fields ' ] [ f ] [ ' string ' ] = res [ ' fields ' ] [ f ] [ ' string ' ] . replace ( ' Hours ' , tm )
2008-09-07 23:24:39 +00:00
return res
2010-09-22 10:38:20 +00:00
def action_close ( self , cr , uid , ids , context = None ) :
# This action open wizard to send email to partner or project manager after close task.
project_id = len ( ids ) and ids [ 0 ] or False
if not project_id : return False
task = self . browse ( cr , uid , project_id , context = context )
project = task . project_id
2010-10-11 05:51:53 +00:00
res = self . do_close ( cr , uid , [ project_id ] , context = context )
2010-09-22 10:38:20 +00:00
if project . warn_manager or project . warn_customer :
return {
' name ' : _ ( ' Send Email after close task ' ) ,
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' project.task.close ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
' nodestroy ' : True ,
' context ' : { ' active_id ' : task . id }
2010-10-11 05:51:53 +00:00
}
2010-09-22 10:38:20 +00:00
return res
2010-08-27 10:56:11 +00:00
def do_close ( self , cr , uid , ids , context = None ) :
"""
Close Task
"""
2010-08-27 11:39:50 +00:00
request = self . pool . get ( ' res.request ' )
2010-08-27 10:56:11 +00:00
for task in self . browse ( cr , uid , ids , context = context ) :
2011-01-18 06:57:42 +00:00
vals = { }
2008-07-22 15:11:28 +00:00
project = task . project_id
if project :
2010-08-27 10:56:11 +00:00
# Send request to project manager
2010-01-12 05:32:48 +00:00
if project . warn_manager and project . user_id and ( project . user_id . id != uid ) :
2008-07-22 15:11:28 +00:00
request . create ( cr , uid , {
2009-03-16 14:07:32 +00:00
' name ' : _ ( " Task ' %s ' closed " ) % task . name ,
2008-07-22 15:11:28 +00:00
' state ' : ' waiting ' ,
' act_from ' : uid ,
2010-01-12 05:32:48 +00:00
' act_to ' : project . user_id . id ,
2008-07-22 15:11:28 +00:00
' ref_partner_id ' : task . partner_id . id ,
' ref_doc1 ' : ' project.task, %d ' % ( task . id , ) ,
' ref_doc2 ' : ' project.project, %d ' % ( project . id , ) ,
} )
2010-09-18 05:52:11 +00:00
2010-02-08 07:16:56 +00:00
for parent_id in task . parent_ids :
if parent_id . state in ( ' pending ' , ' draft ' ) :
reopen = True
for child in parent_id . child_ids :
if child . id != task . id and child . state not in ( ' done ' , ' cancelled ' ) :
reopen = False
if reopen :
self . do_reopen ( cr , uid , [ parent_id . id ] )
2011-01-18 06:57:42 +00:00
vals . update ( { ' state ' : ' done ' } )
vals . update ( { ' remaining_hours ' : 0.0 } )
if not task . date_end :
vals . update ( { ' date_end ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) } )
self . write ( cr , uid , [ task . id ] , vals )
2010-10-16 14:02:41 +00:00
message = _ ( " The task ' %s ' is done " ) % ( task . name , )
2010-08-27 10:56:11 +00:00
self . log ( cr , uid , task . id , message )
return True
2006-12-07 13:41:40 +00:00
2010-08-27 11:36:48 +00:00
def do_reopen ( self , cr , uid , ids , context = None ) :
2008-07-22 15:11:28 +00:00
request = self . pool . get ( ' res.request ' )
2010-10-04 13:10:33 +00:00
2010-08-27 11:36:48 +00:00
for task in self . browse ( cr , uid , ids , context = context ) :
2008-07-22 15:11:28 +00:00
project = task . project_id
2010-01-12 05:32:48 +00:00
if project and project . warn_manager and project . user_id . id and ( project . user_id . id != uid ) :
2008-07-22 15:11:28 +00:00
request . create ( cr , uid , {
2009-03-16 14:07:32 +00:00
' name ' : _ ( " Task ' %s ' set in progress " ) % task . name ,
2008-07-22 15:11:28 +00:00
' state ' : ' waiting ' ,
' act_from ' : uid ,
2010-01-12 05:32:48 +00:00
' act_to ' : project . user_id . id ,
2008-07-22 15:11:28 +00:00
' ref_partner_id ' : task . partner_id . id ,
' ref_doc1 ' : ' project.task, %d ' % task . id ,
' ref_doc2 ' : ' project.project, %d ' % project . id ,
} )
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
self . write ( cr , uid , [ task . id ] , { ' state ' : ' open ' } )
2010-10-04 13:10:33 +00:00
2008-07-22 15:11:28 +00:00
return True
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
def do_cancel ( self , cr , uid , ids , * args ) :
request = self . pool . get ( ' res.request ' )
tasks = self . browse ( cr , uid , ids )
for task in tasks :
project = task . project_id
2010-01-12 05:32:48 +00:00
if project . warn_manager and project . user_id and ( project . user_id . id != uid ) :
2008-07-22 15:11:28 +00:00
request . create ( cr , uid , {
2009-03-16 14:07:32 +00:00
' name ' : _ ( " Task ' %s ' cancelled " ) % task . name ,
2008-07-22 15:11:28 +00:00
' state ' : ' waiting ' ,
' act_from ' : uid ,
2010-01-12 05:32:48 +00:00
' act_to ' : project . user_id . id ,
2008-07-22 15:11:28 +00:00
' ref_partner_id ' : task . partner_id . id ,
' ref_doc1 ' : ' project.task, %d ' % task . id ,
' ref_doc2 ' : ' project.project, %d ' % project . id ,
} )
2010-10-16 14:02:41 +00:00
message = _ ( " The task ' %s ' is cancelled. " ) % ( task . name , )
2010-07-08 10:14:24 +00:00
self . log ( cr , uid , task . id , message )
2008-09-07 23:24:39 +00:00
self . write ( cr , uid , [ task . id ] , { ' state ' : ' cancelled ' , ' remaining_hours ' : 0.0 } )
2008-07-22 15:11:28 +00:00
return True
2007-05-11 11:04:02 +00:00
2008-07-22 15:11:28 +00:00
def do_open ( self , cr , uid , ids , * args ) :
tasks = self . browse ( cr , uid , ids )
for t in tasks :
2010-10-04 18:58:49 +00:00
data = { ' state ' : ' open ' }
if not t . date_start :
data [ ' date_start ' ] = time . strftime ( ' % Y- % m- %d % H: % M: % S ' )
self . write ( cr , uid , [ t . id ] , data )
2010-10-16 14:02:41 +00:00
message = _ ( " The task ' %s ' is opened. " ) % ( t . name , )
2010-07-08 10:14:24 +00:00
self . log ( cr , uid , t . id , message )
2008-07-22 15:11:28 +00:00
return True
2007-05-11 11:04:02 +00:00
2008-07-22 15:11:28 +00:00
def do_draft ( self , cr , uid , ids , * args ) :
self . write ( cr , uid , ids , { ' state ' : ' draft ' } )
return True
2007-05-11 11:04:02 +00:00
2010-08-27 12:05:25 +00:00
def do_delegate ( self , cr , uid , task_id , delegate_data = { } , context = None ) :
"""
Delegate Task to another users .
"""
task = self . browse ( cr , uid , task_id , context = context )
2011-01-11 05:27:10 +00:00
self . copy ( cr , uid , task . id , {
2010-08-27 12:05:25 +00:00
' name ' : delegate_data [ ' name ' ] ,
' user_id ' : delegate_data [ ' user_id ' ] ,
' planned_hours ' : delegate_data [ ' planned_hours ' ] ,
' remaining_hours ' : delegate_data [ ' planned_hours ' ] ,
' parent_ids ' : [ ( 6 , 0 , [ task . id ] ) ] ,
' state ' : ' draft ' ,
' description ' : delegate_data [ ' new_task_description ' ] or ' ' ,
' child_ids ' : [ ] ,
' work_ids ' : [ ]
2010-11-22 10:37:53 +00:00
} , context = context )
2010-08-27 12:05:25 +00:00
newname = delegate_data [ ' prefix ' ] or ' '
self . write ( cr , uid , [ task . id ] , {
' remaining_hours ' : delegate_data [ ' planned_hours_me ' ] ,
' planned_hours ' : delegate_data [ ' planned_hours_me ' ] + ( task . effective_hours or 0.0 ) ,
' name ' : newname ,
2010-11-22 10:37:53 +00:00
} , context = context )
2010-08-27 12:05:25 +00:00
if delegate_data [ ' state ' ] == ' pending ' :
self . do_pending ( cr , uid , [ task . id ] , context )
else :
2010-11-22 10:37:53 +00:00
self . do_close ( cr , uid , [ task . id ] , context = context )
2010-08-27 12:05:25 +00:00
user_pool = self . pool . get ( ' res.users ' )
2010-10-16 14:02:41 +00:00
delegate_user = user_pool . browse ( cr , uid , delegate_data [ ' user_id ' ] , context = context )
message = _ ( " The task ' %s ' has been delegated to %s . " ) % ( delegate_data [ ' name ' ] , delegate_user . name )
2010-08-27 12:05:25 +00:00
self . log ( cr , uid , task . id , message )
return True
2008-07-22 15:11:28 +00:00
def do_pending ( self , cr , uid , ids , * args ) :
self . write ( cr , uid , ids , { ' state ' : ' pending ' } )
2010-07-08 10:14:24 +00:00
for ( id , name ) in self . name_get ( cr , uid , ids ) :
2010-10-17 17:30:00 +00:00
message = _ ( " The task ' %s ' is pending. " ) % name
2010-07-08 10:14:24 +00:00
self . log ( cr , uid , id , message )
2008-07-22 15:11:28 +00:00
return True
2007-05-11 11:04:02 +00:00
2010-01-18 12:54:12 +00:00
def next_type ( self , cr , uid , ids , * args ) :
2010-07-15 08:25:10 +00:00
for task in self . browse ( cr , uid , ids ) :
typeid = task . type_id . id
types = map ( lambda x : x . id , task . project_id . type_ids or [ ] )
2010-01-19 11:32:55 +00:00
if types :
2010-01-21 08:21:47 +00:00
if not typeid :
2010-07-15 08:25:10 +00:00
self . write ( cr , uid , task . id , { ' type_id ' : types [ 0 ] } )
2010-10-11 05:51:53 +00:00
elif typeid and typeid in types and types . index ( typeid ) != len ( types ) - 1 :
2010-01-19 11:32:55 +00:00
index = types . index ( typeid )
2010-07-15 08:25:10 +00:00
self . write ( cr , uid , task . id , { ' type_id ' : types [ index + 1 ] } )
2010-01-18 12:54:12 +00:00
return True
def prev_type ( self , cr , uid , ids , * args ) :
2010-07-15 08:25:10 +00:00
for task in self . browse ( cr , uid , ids ) :
typeid = task . type_id . id
2010-10-04 23:57:26 +00:00
types = map ( lambda x : x . id , task . project_id and task . project_id . type_ids or [ ] )
2010-01-19 11:32:55 +00:00
if types :
2010-01-31 21:29:06 +00:00
if typeid and typeid in types :
2010-01-19 11:32:55 +00:00
index = types . index ( typeid )
2010-07-15 08:25:10 +00:00
self . write ( cr , uid , task . id , { ' type_id ' : index and types [ index - 1 ] or False } )
2010-01-18 12:54:12 +00:00
return True
2008-06-17 13:35:06 +00:00
2006-12-07 13:41:40 +00:00
task ( )
class project_work ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " project.task.work "
2010-08-05 07:20:23 +00:00
_description = " Project Task Work "
2008-07-22 15:11:28 +00:00
_columns = {
' name ' : fields . char ( ' Work summary ' , size = 128 ) ,
' date ' : fields . datetime ( ' Date ' ) ,
2008-09-07 23:24:39 +00:00
' task_id ' : fields . many2one ( ' project.task ' , ' Task ' , ondelete = ' cascade ' , required = True ) ,
2008-09-08 22:53:40 +00:00
' hours ' : fields . float ( ' Time Spent ' ) ,
2008-07-22 15:11:28 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Done by ' , required = True ) ,
2011-01-06 11:32:21 +00:00
' company_id ' : fields . related ( ' task_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True , readonly = True )
2008-07-22 15:11:28 +00:00
}
2010-07-27 12:43:02 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
2010-03-16 12:51:46 +00:00
' user_id ' : lambda obj , cr , uid , context : uid ,
2010-11-02 11:29:09 +00:00
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d % H: % M: % S ' )
2008-07-22 15:11:28 +00:00
}
2010-07-27 12:43:02 +00:00
2008-07-22 15:11:28 +00:00
_order = " date desc "
2010-10-17 23:33:01 +00:00
def create ( self , cr , uid , vals , * args , * * kwargs ) :
if ' hours ' in vals and ( not vals [ ' hours ' ] ) :
vals [ ' hours ' ] = 0.00
if ' task_id ' in vals :
cr . execute ( ' update project_task set remaining_hours=remaining_hours - %s where id= %s ' , ( vals . get ( ' hours ' , 0.0 ) , vals [ ' task_id ' ] ) )
return super ( project_work , self ) . create ( cr , uid , vals , * args , * * kwargs )
2010-11-22 10:37:53 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2010-10-17 23:33:01 +00:00
if ' hours ' in vals and ( not vals [ ' hours ' ] ) :
vals [ ' hours ' ] = 0.00
if ' hours ' in vals :
2010-11-22 10:37:53 +00:00
for work in self . browse ( cr , uid , ids , context = context ) :
2010-10-17 23:33:01 +00:00
cr . execute ( ' update project_task set remaining_hours=remaining_hours - %s + ( %s ) where id= %s ' , ( vals . get ( ' hours ' , 0.0 ) , work . hours , work . task_id . id ) )
return super ( project_work , self ) . write ( cr , uid , ids , vals , context )
2010-08-05 07:20:23 +00:00
2010-10-17 23:33:01 +00:00
def unlink ( self , cr , uid , ids , * args , * * kwargs ) :
for work in self . browse ( cr , uid , ids ) :
cr . execute ( ' update project_task set remaining_hours=remaining_hours + %s where id= %s ' , ( work . hours , work . task_id . id ) )
return super ( project_work , self ) . unlink ( cr , uid , ids , * args , * * kwargs )
2006-12-07 13:41:40 +00:00
project_work ( )
2010-07-01 09:08:44 +00:00
class account_analytic_account ( osv . osv ) :
_inherit = ' account.analytic.account '
_description = ' Analytic Account '
def create ( self , cr , uid , vals , context = None ) :
if context is None :
context = { }
if vals . get ( ' child_ids ' , False ) and context . get ( ' analytic_project_copy ' , False ) :
vals [ ' child_ids ' ] = [ ]
return super ( account_analytic_account , self ) . create ( cr , uid , vals , context = context )
account_analytic_account ( )
2010-07-02 14:37:06 +00:00
2010-01-18 21:38:14 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: