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-07-13 19:54:11 +00:00
from datetime import date , datetime
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-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-19 12:17:16 +00:00
def get_all_child_projects ( self , cr , uid , ids , context = None ) :
2010-08-05 07:20:23 +00:00
# Calculate child project for Given project id => For progress rate + planned time + Time spent
2010-08-10 10:32:59 +00:00
cr . execute ( ''' SELECT prpc.id AS id from account_analytic_account AS p
JOIN account_analytic_account AS c ON p . id = c . parent_id
JOIN project_project AS prp ON prp . analytic_account_id = p . id
JOIN project_project AS prpc ON prpc . analytic_account_id = c . id
WHERE prp . id IN % s ''' ,(tuple(ids),))
2010-07-27 12:43:02 +00:00
2010-07-19 12:17:16 +00:00
child_ids = cr . fetchall ( )
if child_ids :
2010-08-05 07:20:23 +00:00
child_ids = [ x [ 0 ] for x in child_ids ]
child_ids = self . get_all_child_projects ( cr , uid , child_ids )
return ids + child_ids
2010-07-27 12:43:02 +00:00
2010-08-03 12:38:19 +00:00
def _get_user_and_default_uom_ids ( self , cr , uid ) :
users_obj = self . pool . get ( ' res.users ' )
model_data_obj = self . pool . get ( ' ir.model.data ' )
model_data_id = model_data_obj . _get_id ( cr , uid , ' product ' , ' uom_hour ' )
default_uom = user_uom = model_data_obj . read ( cr , uid , [ model_data_id ] , [ ' res_id ' ] ) [ 0 ] [ ' res_id ' ]
obj_tm = users_obj . browse ( cr , uid , uid ) . company_id . project_time_mode_id
if obj_tm :
user_uom = obj_tm . id
return user_uom , default_uom
2010-08-05 07:20:23 +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-27 12:43:02 +00:00
2010-07-19 12:17:16 +00:00
par_child_projects = { }
all_projects = list ( ids )
2010-07-27 12:43:02 +00:00
2010-07-19 12:17:16 +00:00
for id in ids :
child_projects = self . get_all_child_projects ( cr , uid , [ id ] , context )
child_projects = [ x for x in child_projects ]
par_child_projects [ id ] = child_projects
all_projects . extend ( child_projects )
2010-07-27 12:43:02 +00:00
all_projects = dict . fromkeys ( all_projects ) . keys ( )
2010-07-19 12:17:16 +00:00
cr . execute ( ''' SELECT
project_id , sum ( planned_hours ) , sum ( total_hours ) , sum ( effective_hours )
FROM
project_task
WHERE
project_id IN % s AND
state < > ' cancelled '
GROUP BY
project_id ''' ,(tuple(all_projects),))
progress = dict ( map ( lambda x : ( x [ 0 ] , ( x [ 1 ] , x [ 2 ] , x [ 3 ] ) ) , cr . fetchall ( ) ) )
2010-07-27 12:43:02 +00:00
2010-07-19 12:17:16 +00:00
for project in self . browse ( cr , uid , par_child_projects . keys ( ) , context = context ) :
2010-03-16 12:51:46 +00:00
s = [ 0.0 , 0.0 , 0.0 ]
2010-07-19 12:17:16 +00:00
tocompute = par_child_projects [ project . id ]
2008-11-23 10:18:15 +00:00
while tocompute :
p = tocompute . pop ( )
for i in range ( 3 ) :
2010-07-19 12:17:16 +00:00
s [ i ] + = progress . get ( p , ( 0.0 , 0.0 , 0.0 ) ) [ i ]
2010-08-05 07:20:23 +00:00
uom_obj = self . pool . get ( ' product.uom ' )
2010-08-03 12:38:19 +00:00
user_uom , def_uom = self . _get_user_and_default_uom_ids ( cr , uid )
if user_uom != def_uom :
2010-08-04 12:11:24 +00:00
s [ 0 ] = uom_obj . _compute_qty ( cr , uid , user_uom , s [ 0 ] , def_uom )
s [ 1 ] = uom_obj . _compute_qty ( cr , uid , user_uom , s [ 1 ] , def_uom )
s [ 2 ] = uom_obj . _compute_qty ( cr , uid , user_uom , s [ 2 ] , def_uom )
2010-08-05 07:20:23 +00:00
2010-07-19 12:17:16 +00:00
if project . state == ' close ' :
progress_rate = 100.0
else :
progress_rate = s [ 1 ] and round ( min ( 100.0 * s [ 2 ] / s [ 1 ] , 99.99 ) , 2 )
2010-08-05 07:20:23 +00:00
2008-11-23 10:18:15 +00:00
res [ project . id ] = {
' planned_hours ' : s [ 0 ] ,
' effective_hours ' : s [ 2 ] ,
' total_hours ' : s [ 1 ] ,
2010-07-19 12:17:16 +00:00
' progress_rate ' : progress_rate
2008-11-23 10:18:15 +00:00
}
2008-08-27 13:48:40 +00:00
return res
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 ) ,
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 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-07-15 07:35:17 +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. " ) ,
2010-06-08 12:34:12 +00:00
' priority ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of task " ) ,
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 ) ] } ) ,
' members ' : fields . many2many ( ' res.users ' , ' project_user_rel ' , ' project_id ' , ' uid ' , ' Project Members ' , help = " Project ' s member. Not used in any computation, just for information purpose. " , 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-07-19 12:17:16 +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. " ) ,
' 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. " ) ,
' 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. " ) ,
2008-11-23 10:18:15 +00:00
' progress_rate ' : fields . function ( _progress_rate , multi = " progress " , method = True , string = ' Progress ' , type = ' float ' , 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-07-27 12:43:02 +00:00
2010-02-11 05:34:49 +00:00
_order = " sequence "
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
' active ' : True ,
' priority ' : 1 ,
' sequence ' : 10 ,
' warn_manager ' : True ,
2008-07-22 15:11:28 +00:00
}
2010-07-27 12:43:02 +00:00
2010-01-18 13:44:53 +00:00
def _check_dates ( self , cr , uid , ids ) :
2010-07-02 14:37:06 +00:00
leave = self . read ( cr , uid , ids [ 0 ] , [ ' date_start ' , ' date ' ] )
2010-01-18 13:44:53 +00:00
if leave [ ' date_start ' ] and leave [ ' date ' ] :
if leave [ ' date_start ' ] > leave [ ' date ' ] :
return False
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 ) :
message = _ ( ' Project ' ) + " ' " + name + " ' " + _ ( " is Closed. " )
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 ) :
message = _ ( ' Project ' ) + " ' " + name + " ' " + _ ( " is Open. " )
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
2010-07-10 13:18:44 +00:00
task_obj = self . pool . get ( ' project.task ' )
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-07-27 12:43:02 +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 ' )
2010-04-05 10:17:04 +00:00
data_obj = self . pool . get ( ' ir.model.data ' )
2010-03-16 12:51:46 +00:00
task_obj = self . pool . get ( ' project.task ' )
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-07-02 14:37:06 +00:00
parent_id = context . get ( ' parent_id ' , False ) # check me where to pass context for parent id ??
2010-07-01 09:08:44 +00:00
context . update ( { ' analytic_project_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-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 )
2009-09-24 10:46:21 +00:00
cr . execute ( ' select id from project_task where project_id= %s ' , ( proj . id , ) )
res = cr . fetchall ( )
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 ' ] )
tree_view_id = data_obj . _get_id ( cr , uid , ' project ' , ' view_project_list ' )
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
}
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-07-27 12:43:02 +00:00
2006-12-07 13:41:40 +00:00
project ( )
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 ) :
2010-08-03 12:38:19 +00:00
project_obj = self . pool . get ( ' project.project ' )
2010-05-03 11:01:51 +00:00
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 ( ) )
2010-08-05 05:28:15 +00:00
2010-08-04 12:11:24 +00:00
uom_obj = self . pool . get ( ' product.uom ' )
2010-08-03 12:38:19 +00:00
user_uom , default_uom = project_obj . _get_user_and_default_uom_ids ( cr , uid )
if user_uom != default_uom :
for task in self . browse ( cr , uid , ids , context = context ) :
if hours . get ( task . id , False ) :
2010-08-04 12:11:24 +00:00
dur_in_user_uom = uom_obj . _compute_qty ( cr , uid , default_uom , hours . get ( task . id , 0.0 ) , user_uom )
2010-08-03 12:38:19 +00:00
hours [ task . id ] = dur_in_user_uom
2010-08-05 07:20:23 +00:00
2008-09-07 23:24:39 +00:00
for task in self . browse ( cr , uid , ids , context = context ) :
2010-05-03 11:01:51 +00:00
res [ task . id ] = { ' effective_hours ' : hours . get ( task . id , 0.0 ) , ' total_hours ' : task . remaining_hours + hours . get ( task . id , 0.0 ) }
2010-08-03 12:38:19 +00:00
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-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-01-12 05:32:48 +00:00
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
2008-09-07 23:24:39 +00:00
#_sql_constraints = [
# ('remaining_hours', 'CHECK (remaining_hours>=0)', 'Please increase and review remaining hours ! It can not be smaller than 0.'),
#]
2010-01-08 11:05:05 +00:00
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
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-02-09 10:20:13 +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
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 ' ) ,
' priority ' : fields . selection ( [ ( ' 4 ' , ' Very Low ' ) , ( ' 3 ' , ' Low ' ) , ( ' 2 ' , ' Medium ' ) , ( ' 1 ' , ' Urgent ' ) , ( ' 0 ' , ' Very urgent ' ) ] , ' Importance ' ) ,
2009-12-21 13:14:12 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of tasks. " ) ,
2010-07-15 08:25:10 +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 \' . ' ) ,
2010-07-15 04:44:12 +00:00
' create_date ' : fields . datetime ( ' Create Date ' , readonly = True ) ,
2008-10-29 13:03:53 +00:00
' date_start ' : fields . datetime ( ' Starting Date ' ) ,
2010-01-12 05:32:48 +00:00
' date_end ' : fields . datetime ( ' Ending Date ' ) ,
2010-01-31 21:29:06 +00:00
' date_deadline ' : fields . date ( ' Deadline ' ) ,
2009-01-27 21:25:44 +00:00
' project_id ' : fields . many2one ( ' project.project ' , ' Project ' , ondelete = ' cascade ' ,
help = " If you have [?] in the project name, it means there are no analytic account linked to this project. " ) ,
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 ' ) ,
2009-02-26 10:33:11 +00:00
' planned_hours ' : fields . float ( ' Planned Hours ' , required = True , help = ' Estimated time to do the task, usually set by the project manager when the task is in draft state. ' ) ,
2008-10-27 11:26:07 +00:00
' effective_hours ' : fields . function ( _hours_get , method = True , string = ' Hours Spent ' , multi = ' hours ' , store = True , help = " Computed using the sum of the task work done. " ) ,
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. " ) ,
2008-10-27 11:26:07 +00:00
' total_hours ' : fields . function ( _hours_get , method = True , string = ' Total Hours ' , multi = ' hours ' , store = True , help = " Computed as: Time Spent + Remaining Time. " ) ,
' progress ' : fields . function ( _hours_get , method = True , string = ' Progress ( % ) ' , multi = ' hours ' , store = True , help = " Computed as: Time Spent / Total Time. " ) ,
2010-03-08 07:17:31 +00:00
' delay_hours ' : fields . function ( _hours_get , method = True , string = ' Delay Hours ' , multi = ' hours ' , store = True , help = " Computed as difference of the time estimated by the project manager and the real time to close the task. " ) ,
2008-09-07 23:24:39 +00:00
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 ' ) ,
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-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
2010-01-31 21:29:06 +00:00
_order = " sequence, priority, date_start, id "
2006-12-07 13:41:40 +00:00
2010-01-20 12:44:10 +00:00
_constraints = [
2010-08-05 07:20:23 +00:00
( _check_dates , ' Error! Task start-date must be lower then task end-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
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 ' )
obj_tm = users_obj . browse ( cr , uid , uid , 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
2008-07-22 15:11:28 +00:00
def do_close ( self , cr , uid , ids , * args ) :
2010-04-05 08:44:37 +00:00
mail_send = False
mod_obj = self . pool . get ( ' ir.model.data ' )
2008-07-22 15:11:28 +00:00
request = self . pool . get ( ' res.request ' )
tasks = self . browse ( cr , uid , ids )
2010-07-09 07:27:50 +00:00
task_id = ids [ 0 ]
cntx = { }
if len ( args ) :
cntx = args [ 0 ]
2008-07-22 15:11:28 +00:00
for task in tasks :
project = task . project_id
if project :
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-07-29 05:31:04 +00:00
elif ( project . warn_manager or project . warn_customer ) and cntx . get ( ' mail_send ' , True ) :
cntx . update ( { ' send_manager ' : project . warn_manager , ' send_partner ' : project . warn_customer } )
2010-04-05 08:44:37 +00:00
mail_send = True
2010-07-08 10:14:24 +00:00
message = _ ( ' Task ' ) + " ' " + task . name + " ' " + _ ( " is Done. " )
self . log ( cr , uid , task . id , message )
2010-07-15 04:44:12 +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 ] )
2010-04-05 08:44:37 +00:00
if mail_send :
model_data_ids = mod_obj . search ( cr , uid , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_project_close_task ' ) ] )
resource_id = mod_obj . read ( cr , uid , model_data_ids , fields = [ ' res_id ' ] ) [ 0 ] [ ' res_id ' ]
2010-07-09 07:27:50 +00:00
cntx . update ( { ' task_id ' : task_id } )
2010-04-05 08:44:37 +00:00
return {
' name ' : _ ( ' Email Send to Customer ' ) ,
' view_type ' : ' form ' ,
2010-07-09 07:27:50 +00:00
' context ' : cntx , # improve me
2010-04-05 08:44:37 +00:00
' view_mode ' : ' tree,form ' ,
' res_model ' : ' close.task ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
' nodestroy ' : True
}
2010-07-09 07:27:50 +00:00
else :
self . write ( cr , uid , [ task_id ] , { ' state ' : ' done ' , ' date_end ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) , ' remaining_hours ' : 0.0 } )
return False
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
def do_reopen ( 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 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 ' } )
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-07-08 10:14:24 +00:00
message = _ ( ' Task ' ) + " ' " + task . name + " ' " + _ ( " is Cancelled. " )
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-07-15 04:44:12 +00:00
self . write ( cr , uid , [ t . id ] , { ' state ' : ' open ' , ' date_start ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) , } )
2010-07-08 10:14:24 +00:00
message = _ ( ' Task ' ) + " ' " + t . name + " ' " + _ ( " is Open. " )
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
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 ) :
message = _ ( ' Task ' ) + " ' " + name + " ' " + _ ( " is Pending. " )
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-01-21 08:21:47 +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
types = map ( lambda x : x . id , task . project_id . type_ids )
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 ) ,
2010-07-02 14:37:06 +00:00
' company_id ' : fields . related ( ' task_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = 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-07-02 14:37:06 +00:00
' date ' : 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-08-05 07:20:23 +00:00
2008-12-04 06:47:26 +00:00
def create ( self , cr , uid , vals , * args , * * kwargs ) :
2010-08-05 07:20:23 +00:00
project_obj = self . pool . get ( ' project.project ' )
uom_obj = self . pool . get ( ' product.uom ' )
2010-08-09 11:57:35 +00:00
if vals . get ( ' hours ' , False ) :
user_uom , default_uom = project_obj . _get_user_and_default_uom_ids ( cr , uid )
duration = vals [ ' hours ' ]
if user_uom != default_uom :
duration = uom_obj . _compute_qty ( cr , uid , default_uom , duration , user_uom )
cr . execute ( ' update project_task set remaining_hours=remaining_hours - %s where id= %s ' , ( duration , vals [ ' task_id ' ] ) )
return super ( project_work , self ) . create ( cr , uid , vals , * args , * * kwargs )
2008-12-04 06:47:26 +00:00
2010-07-02 14:37:06 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2010-08-05 07:20:23 +00:00
project_obj = self . pool . get ( ' project.project ' )
uom_obj = self . pool . get ( ' product.uom ' )
2010-08-09 11:57:35 +00:00
if vals . get ( ' hours ' , False ) :
old_hours = self . browse ( cr , uid , ids , context = context )
user_uom , default_uom = project_obj . _get_user_and_default_uom_ids ( cr , uid )
duration = vals [ ' hours ' ]
for old in old_hours :
if vals . get ( ' hours ' ) != old . hours :
# this code is only needed when we update the hours of the project
# TODO: it may still a second calculation if the task.id is changed
# at this task.
if user_uom == default_uom :
for work in self . browse ( cr , uid , ids , context = context ) :
cr . execute ( ' update project_task set remaining_hours=remaining_hours - %s + ( %s ) where id= %s ' , ( duration , work . hours , work . task_id . id ) )
else :
for work in self . browse ( cr , uid , ids , context = context ) :
duration = uom_obj . _compute_qty ( cr , uid , default_uom , duration , user_uom )
del_work = uom_obj . _compute_qty ( cr , uid , default_uom , work . hours , user_uom )
cr . execute ( ' update project_task set remaining_hours=remaining_hours - %s + ( %s ) where id= %s ' , ( duration , del_work , work . task_id . id ) )
return super ( project_work , self ) . write ( cr , uid , ids , vals , context = context )
2008-09-07 23:24:39 +00:00
2008-12-04 06:47:26 +00:00
def unlink ( self , cr , uid , ids , * args , * * kwargs ) :
2010-08-03 12:38:19 +00:00
context = kwargs . get ( ' context ' , { } )
project_obj = self . pool . get ( ' project.project ' )
2010-08-11 04:10:17 +00:00
uom_obj = self . pool . get ( ' product.uom ' )
2010-08-05 07:20:23 +00:00
user_uom , default_uom = project_obj . _get_user_and_default_uom_ids ( cr , uid )
2010-08-03 12:38:19 +00:00
if user_uom == default_uom :
for work in self . browse ( cr , uid , ids , context ) :
cr . execute ( ' update project_task set remaining_hours=remaining_hours + %s where id= %s ' , ( work . hours , work . task_id . id ) )
else :
for work in self . browse ( cr , uid , ids , context ) :
2010-08-04 12:11:24 +00:00
duration = uom_obj . _compute_qty ( cr , uid , default_uom , work . hours , user_uom )
2010-08-03 12:38:19 +00:00
cr . execute ( ' update project_task set remaining_hours=remaining_hours + %s where id= %s ' , ( duration , work . task_id . id ) )
2010-08-11 04:10:17 +00:00
return super ( project_work , self ) . unlink ( cr , uid , ids , * args , * * kwargs )
2010-07-27 12:43:02 +00:00
2006-12-07 13:41:40 +00:00
project_work ( )
2008-12-04 06:47:26 +00:00
class config_compute_remaining ( osv . osv_memory ) :
_name = ' config.compute.remaining '
2010-07-27 12:43:02 +00:00
2010-08-05 07:20:23 +00:00
def _get_remaining ( self , cr , uid , context = None ) :
if context and ' active_id ' in context :
2010-08-05 09:54:18 +00:00
return self . pool . get ( ' project.task ' ) . browse ( cr , uid , context [ ' active_id ' ] , context = context ) . remaining_hours
2008-12-04 06:47:26 +00:00
return False
_columns = {
2010-05-18 13:20:07 +00:00
' remaining_hours ' : fields . float ( ' Remaining Hours ' , digits = ( 16 , 2 ) , help = " Put here the remaining hours required to close the task. " ) ,
}
2010-07-27 12:43:02 +00:00
2008-12-04 06:47:26 +00:00
_defaults = {
' remaining_hours ' : _get_remaining
2010-05-18 13:20:07 +00:00
}
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' , context = None , toolbar = False , submenu = False ) :
users_obj = self . pool . get ( ' res.users ' )
obj_tm = users_obj . browse ( cr , uid , uid , context ) . company_id . project_time_mode_id
tm = obj_tm and obj_tm . name or ' Hours '
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
res = super ( config_compute_remaining , self ) . fields_view_get ( cr , uid , view_id , view_type , context , toolbar , submenu = submenu )
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
if tm in [ ' Hours ' , ' Hour ' ] :
return res
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
eview = etree . fromstring ( res [ ' arch ' ] )
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
def _check_rec ( eview ) :
if eview . attrib . get ( ' widget ' , ' ' ) == ' float_time ' :
eview . set ( ' widget ' , ' float ' )
for child in eview :
_check_rec ( child )
return True
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
_check_rec ( eview )
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
res [ ' arch ' ] = etree . tostring ( eview )
2010-08-05 07:20:23 +00:00
2010-08-04 12:11:24 +00:00
for f in res [ ' fields ' ] :
if ' Hours ' in res [ ' fields ' ] [ f ] [ ' string ' ] :
res [ ' fields ' ] [ f ] [ ' string ' ] = res [ ' fields ' ] [ f ] [ ' string ' ] . replace ( ' Hours ' , tm )
return res
2010-08-05 07:20:23 +00:00
2008-12-04 06:47:26 +00:00
def compute_hours ( self , cr , uid , ids , context = None ) :
2010-05-27 09:57:03 +00:00
if context is None :
context = { }
2010-03-16 12:51:46 +00:00
task_obj = self . pool . get ( ' project.task ' )
request = self . pool . get ( ' res.request ' )
2008-12-04 06:47:26 +00:00
if ' active_id ' in context :
2010-08-05 07:20:23 +00:00
remaining_hrs = self . browse ( cr , uid , ids ) [ 0 ] . remaining_hours
2010-03-16 12:51:46 +00:00
task_obj . write ( cr , uid , context [ ' active_id ' ] , { ' remaining_hours ' : remaining_hrs } )
if context . get ( ' button_reactivate ' , False ) :
tasks = task_obj . browse ( cr , uid , [ context [ ' active_id ' ] ] , context = context )
for task in tasks :
project = task . project_id
if project and project . warn_manager and project . user_id . id and ( project . user_id . id != uid ) :
request . create ( cr , uid , {
' name ' : _ ( " Task ' %s ' set in progress " ) % task . name ,
' state ' : ' waiting ' ,
' act_from ' : uid ,
' act_to ' : project . user_id . id ,
' ref_partner_id ' : task . partner_id . id ,
' ref_doc1 ' : ' project.task, %d ' % task . id ,
' ref_doc2 ' : ' project.project, %d ' % project . id ,
} )
task_obj . write ( cr , uid , [ task . id ] , { ' state ' : ' open ' } )
2008-12-04 06:47:26 +00:00
return {
' type ' : ' ir.actions.act_window_close ' ,
}
2010-08-05 07:20:23 +00:00
2008-12-04 06:47:26 +00:00
config_compute_remaining ( )
2010-01-08 11:05:05 +00:00
class message ( osv . osv ) :
_name = " project.message "
_description = " Message "
2010-07-27 12:43:02 +00:00
2010-01-08 11:05:05 +00:00
_columns = {
2010-02-03 13:47:51 +00:00
' subject ' : fields . char ( ' Subject ' , size = 128 , required = " True " ) ,
2010-01-08 11:05:05 +00:00
' project_id ' : fields . many2one ( ' project.project ' , ' Project ' , ondelete = ' cascade ' ) ,
2010-02-28 10:57:24 +00:00
' date ' : fields . date ( ' Date ' , required = 1 ) ,
2010-02-03 13:47:51 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User ' , required = " True " ) ,
2010-02-28 10:57:24 +00:00
' description ' : fields . text ( ' Description ' ) ,
}
2010-02-11 06:35:49 +00:00
2010-07-02 14:37:06 +00:00
def _default_project ( self , cr , uid , context = None ) :
if context is None :
context = { }
2010-02-02 13:33:38 +00:00
if ' project_id ' in context and context [ ' project_id ' ] :
return int ( context [ ' project_id ' ] )
2010-02-03 13:47:51 +00:00
return False
2010-02-02 13:33:38 +00:00
_defaults = {
2010-07-02 14:37:06 +00:00
' user_id ' : lambda self , cr , uid , ctx : uid ,
2010-07-09 07:27:50 +00:00
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
2010-07-02 14:37:06 +00:00
' project_id ' : _default_project
2010-02-28 10:57:24 +00:00
}
2010-02-03 13:47:51 +00:00
2010-01-08 11:05:05 +00:00
message ( )
2008-07-23 14:41:47 +00:00
2010-01-08 11:05:05 +00:00
class users ( osv . osv ) :
_inherit = ' res.users '
2010-01-11 07:26:51 +00:00
_description = " Users "
2010-01-08 11:05:05 +00:00
_columns = {
2010-02-09 13:48:22 +00:00
' context_project_id ' : fields . many2one ( ' project.project ' , ' Project ' )
}
2010-07-27 12:43:02 +00:00
2010-01-08 11:05:05 +00:00
users ( )
2008-07-23 14:41:47 +00:00
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: