2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-03-12 07:04:55 +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-03-12 07:04:55 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
import time
2010-03-12 07:04:55 +00:00
2006-12-07 13:41:40 +00:00
from osv import fields
from osv import osv
2009-03-06 22:18:24 +00:00
from tools . translate import _
2006-12-07 13:41:40 +00:00
class hr_employee ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " hr.employee "
_inherit = " hr.employee "
_columns = {
2009-12-22 13:08:10 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' , help = " Specifies employee ' s designation as a product with type ' service ' . " ) ,
2011-08-03 12:13:32 +00:00
' journal_id ' : fields . many2one ( ' account.analytic.journal ' , ' Analytic Journal ' ) ,
2012-04-25 07:32:57 +00:00
' uom_id ' : fields . related ( ' product_id ' , ' uom_id ' , type = ' many2one ' , relation = ' product.uom ' , string = ' Unit of Measure ' , store = True , readonly = True )
2008-07-22 15:11:28 +00:00
}
2010-10-27 12:49:59 +00:00
2010-10-12 09:40:42 +00:00
def _getAnalyticJournal ( self , cr , uid , context = None ) :
2010-10-12 17:27:25 +00:00
md = self . pool . get ( ' ir.model.data ' )
try :
result = md . get_object_reference ( cr , uid , ' hr_timesheet ' , ' analytic_journal ' )
return result [ 1 ]
2010-10-17 22:45:35 +00:00
except ValueError :
2010-10-12 17:27:25 +00:00
pass
2010-10-12 09:40:42 +00:00
return False
2010-10-12 17:27:25 +00:00
2010-10-12 09:40:42 +00:00
def _getEmployeeProduct ( self , cr , uid , context = None ) :
2010-10-12 17:27:25 +00:00
md = self . pool . get ( ' ir.model.data ' )
try :
2011-10-18 15:31:52 +00:00
result = md . get_object_reference ( cr , uid , ' product ' , ' product_consultant ' )
2010-10-12 17:27:25 +00:00
return result [ 1 ]
2010-10-17 22:45:35 +00:00
except ValueError :
2010-10-12 17:27:25 +00:00
pass
2010-10-12 09:40:42 +00:00
return False
2010-10-12 17:27:25 +00:00
2010-10-12 09:40:42 +00:00
_defaults = {
2010-10-27 12:49:59 +00:00
' journal_id ' : _getAnalyticJournal ,
' product_id ' : _getEmployeeProduct
2010-10-12 09:40:42 +00:00
}
2006-12-07 13:41:40 +00:00
hr_employee ( )
class hr_analytic_timesheet ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " hr.analytic.timesheet "
_table = ' hr_analytic_timesheet '
2010-05-19 18:32:32 +00:00
_description = " Timesheet Line "
2008-07-22 15:11:28 +00:00
_inherits = { ' account.analytic.line ' : ' line_id ' }
_order = " id desc "
_columns = {
2011-09-10 12:43:55 +00:00
' line_id ' : fields . many2one ( ' account.analytic.line ' , ' Analytic Line ' , ondelete = ' cascade ' , required = True ) ,
' partner_id ' : fields . related ( ' account_id ' , ' partner_id ' , type = ' many2one ' , string = ' Partner ' , relation = ' res.partner ' , store = True ) ,
2008-07-22 15:11:28 +00:00
}
2010-07-26 06:15:27 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2008-07-22 15:11:28 +00:00
toremove = { }
2010-05-19 13:07:03 +00:00
for obj in self . browse ( cr , uid , ids , context = context ) :
2008-07-22 15:11:28 +00:00
toremove [ obj . line_id . id ] = True
2010-05-19 13:07:03 +00:00
self . pool . get ( ' account.analytic.line ' ) . unlink ( cr , uid , toremove . keys ( ) , context = context )
return super ( hr_analytic_timesheet , self ) . unlink ( cr , uid , ids , context = context )
2008-07-22 15:11:28 +00:00
2010-10-04 23:23:08 +00:00
def on_change_unit_amount ( self , cr , uid , id , prod_id , unit_amount , company_id , unit = False , journal_id = False , context = None ) :
2010-08-19 09:38:02 +00:00
res = { ' value ' : { } }
2010-05-06 12:29:54 +00:00
if prod_id and unit_amount :
2009-12-17 14:13:32 +00:00
# find company
2010-05-19 13:07:03 +00:00
company_id = self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.analytic.line ' , context = context )
2010-10-17 22:08:23 +00:00
r = self . pool . get ( ' account.analytic.line ' ) . on_change_unit_amount ( cr , uid , id , prod_id , unit_amount , company_id , unit , journal_id , context = context )
if r :
res . update ( r )
2010-08-19 09:38:02 +00:00
# update unit of measurement
if prod_id :
uom = self . pool . get ( ' product.product ' ) . browse ( cr , uid , prod_id , context = context )
if uom . uom_id :
res [ ' value ' ] . update ( { ' product_uom_id ' : uom . uom_id . id } )
else :
res [ ' value ' ] . update ( { ' product_uom_id ' : False } )
2008-07-22 15:11:28 +00:00
return res
2010-07-26 06:15:27 +00:00
def _getEmployeeProduct ( self , cr , uid , context = None ) :
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
emp_obj = self . pool . get ( ' hr.employee ' )
2010-07-26 06:15:27 +00:00
emp_id = emp_obj . search ( cr , uid , [ ( ' user_id ' , ' = ' , context . get ( ' user_id ' , uid ) ) ] , context = context )
2008-07-22 15:11:28 +00:00
if emp_id :
2010-08-27 09:17:39 +00:00
emp = emp_obj . browse ( cr , uid , emp_id [ 0 ] , context = context )
2008-07-22 15:11:28 +00:00
if emp . product_id :
return emp . product_id . id
return False
2010-05-19 13:07:03 +00:00
def _getEmployeeUnit ( self , cr , uid , context = None ) :
2008-07-22 15:11:28 +00:00
emp_obj = self . pool . get ( ' hr.employee ' )
2010-05-19 13:07:03 +00:00
if context is None :
context = { }
2010-07-26 06:15:27 +00:00
emp_id = emp_obj . search ( cr , uid , [ ( ' user_id ' , ' = ' , context . get ( ' user_id ' , uid ) ) ] , context = context )
2008-07-22 15:11:28 +00:00
if emp_id :
2010-08-27 09:17:39 +00:00
emp = emp_obj . browse ( cr , uid , emp_id [ 0 ] , context = context )
2008-07-22 15:11:28 +00:00
if emp . product_id :
return emp . product_id . uom_id . id
return False
2010-05-19 13:07:03 +00:00
def _getGeneralAccount ( self , cr , uid , context = None ) :
2008-07-22 15:11:28 +00:00
emp_obj = self . pool . get ( ' hr.employee ' )
2010-05-19 13:07:03 +00:00
if context is None :
context = { }
2010-07-26 06:15:27 +00:00
emp_id = emp_obj . search ( cr , uid , [ ( ' user_id ' , ' = ' , context . get ( ' user_id ' , uid ) ) ] , context = context )
2008-07-22 15:11:28 +00:00
if emp_id :
2010-03-12 07:04:55 +00:00
emp = emp_obj . browse ( cr , uid , emp_id [ 0 ] , context = context )
2008-07-22 15:11:28 +00:00
if bool ( emp . product_id ) :
2012-07-27 01:52:02 +00:00
a = emp . product_id . property_account_expense . id
2008-07-22 15:11:28 +00:00
if not a :
a = emp . product_id . categ_id . property_account_expense_categ . id
if a :
return a
return False
2010-05-19 13:07:03 +00:00
def _getAnalyticJournal ( self , cr , uid , context = None ) :
2008-07-22 15:11:28 +00:00
emp_obj = self . pool . get ( ' hr.employee ' )
2010-05-19 13:07:03 +00:00
if context is None :
context = { }
2010-07-26 06:15:27 +00:00
emp_id = emp_obj . search ( cr , uid , [ ( ' user_id ' , ' = ' , context . get ( ' user_id ' , uid ) ) ] , context = context )
2008-07-22 15:11:28 +00:00
if emp_id :
2010-03-12 07:04:55 +00:00
emp = emp_obj . browse ( cr , uid , emp_id [ 0 ] , context = context )
2008-07-22 15:11:28 +00:00
if emp . journal_id :
return emp . journal_id . id
return False
_defaults = {
2010-10-27 12:49:59 +00:00
' product_uom_id ' : _getEmployeeUnit ,
' product_id ' : _getEmployeeProduct ,
' general_account_id ' : _getGeneralAccount ,
' journal_id ' : _getAnalyticJournal ,
2012-02-14 12:25:20 +00:00
' date ' : lambda self , cr , uid , ctx : ctx . get ( ' date ' , fields . date . context_today ( self , cr , uid , context = ctx ) ) ,
2010-10-27 12:49:59 +00:00
' user_id ' : lambda obj , cr , uid , ctx : ctx . get ( ' user_id ' , uid ) ,
2008-07-22 15:11:28 +00:00
}
2008-11-20 16:50:39 +00:00
def on_change_account_id ( self , cr , uid , ids , account_id ) :
return { ' value ' : { } }
2010-03-12 07:04:55 +00:00
2009-05-01 07:21:09 +00:00
def on_change_date ( self , cr , uid , ids , date ) :
if ids :
2010-06-16 11:51:39 +00:00
new_date = self . read ( cr , uid , ids [ 0 ] , [ ' date ' ] ) [ ' date ' ]
2009-05-01 07:21:09 +00:00
if date != new_date :
warning = { ' title ' : ' User Alert! ' , ' message ' : ' Changing the date will let this entry appear in the timesheet of the new date. ' }
return { ' value ' : { } , ' warning ' : warning }
return { ' value ' : { } }
2010-03-12 07:04:55 +00:00
2010-07-26 06:15:27 +00:00
def create ( self , cr , uid , vals , context = None ) :
if context is None :
context = { }
2010-03-12 07:04:55 +00:00
emp_obj = self . pool . get ( ' hr.employee ' )
emp_id = emp_obj . search ( cr , uid , [ ( ' user_id ' , ' = ' , context . get ( ' user_id ' , uid ) ) ] , context = context )
ename = ' '
if emp_id :
ename = emp_obj . browse ( cr , uid , emp_id [ 0 ] , context = context ) . name
if not vals . get ( ' journal_id ' , False ) :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' Analytic journal is not defined for employee %s \n Define an employee for the selected user and assign an analytic journal! ' ) % ( ename , ) )
if not vals . get ( ' account_id ' , False ) :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' No analytic account defined on the project. \n Please set one or we can not automatically fill the timesheet. ' ) )
return super ( hr_analytic_timesheet , self ) . create ( cr , uid , vals , context = context )
2008-07-22 15:11:28 +00:00
def on_change_user_id ( self , cr , uid , ids , user_id ) :
if not user_id :
return { }
2010-08-27 09:17:39 +00:00
context = { ' user_id ' : user_id }
2010-10-27 12:49:59 +00:00
return { ' value ' : {
' product_id ' : self . _getEmployeeProduct ( cr , uid , context ) ,
' product_uom_id ' : self . _getEmployeeUnit ( cr , uid , context ) ,
' general_account_id ' : self . _getGeneralAccount ( cr , uid , context ) ,
' journal_id ' : self . _getAnalyticJournal ( cr , uid , context ) ,
2008-08-19 22:38:20 +00:00
} }
2010-10-27 12:49:59 +00:00
2006-12-07 13:41:40 +00:00
hr_analytic_timesheet ( )
2008-07-23 14:41:47 +00:00
2012-05-28 13:31:58 +00:00
class account_analytic_account ( osv . osv ) :
_inherit = ' account.analytic.account '
_description = ' Analytic Account '
_columns = {
2012-05-31 09:27:54 +00:00
' use_timesheets ' : fields . boolean ( ' Timesheets ' , help = " Check this field if this project manages timesheets " ) ,
2012-05-28 13:31:58 +00:00
}
2012-07-23 12:10:29 +00:00
def on_change_template ( self , cr , uid , ids , template_id , context = None ) :
res = super ( account_analytic_account , self ) . on_change_template ( cr , uid , ids , template_id , context = context )
if template_id and ' value ' in res :
template = self . browse ( cr , uid , template_id , context = context )
res [ ' value ' ] [ ' use_timesheets ' ] = template . use_timesheets
return res
2012-05-28 13:31:58 +00:00
account_analytic_account ( )
2010-08-12 19:29:33 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: