[REF] Refactored hr_timesheet: Refactored the "timesheet" model in several modules. hr.analytic.timesheet is removed, and account.analytic.line is used directly to represent timesheets.

Also removed worklogs on tasks and replaced them by generic timesheets, i.e. account.analytic.line(s)
This commit is contained in:
odoo-tac
2015-04-28 10:31:18 +02:00
parent eb91b14837
commit 14183ba2be
46 changed files with 915 additions and 1273 deletions
@@ -19,22 +19,24 @@
#
##############################################################################
from openerp.osv import fields,osv
from openerp.osv import fields, osv
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
from openerp.exceptions import UserError
class analytic_user_funct_grid(osv.osv):
_name="analytic.user.funct.grid"
_description= "Price per User"
_rec_name="user_id"
_columns={
_name = "analytic.user.funct.grid"
_description = "Price per User"
_rec_name = "user_id"
_columns = {
'user_id': fields.many2one("res.users", "User", required=True,),
'product_id': fields.many2one("product.product", "Service", required=True,),
'account_id': fields.many2one("account.analytic.account", "Analytic Account", required=True,),
'uom_id': fields.related("product_id", "uom_id", relation="product.uom", string="Unit of Measure", type="many2one", readonly=True),
'price': fields.float('Price', digits_compute=dp.get_precision('Product Price'), help="Price per hour for this user.", required=True),
}
def onchange_user_product_id(self, cr, uid, ids, user_id, product_id, context=None):
if not user_id:
return {}
@@ -46,7 +48,7 @@ class analytic_user_funct_grid(osv.osv):
value = {}
prod = False
if product_id:
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
if emp.product_id and not product_id:
value['product_id'] = emp.product_id.id
@@ -64,92 +66,73 @@ class account_analytic_account(osv.osv):
}
class hr_analytic_timesheet(osv.osv):
_inherit = "hr.analytic.timesheet"
# Look in account, if no value for the user => look in parent until there is no more parent to look
# Take the first found... if nothing found => return False
def _get_related_user_account_recursiv(self, cr, uid, user_id, account_id):
temp=self.pool.get('analytic.user.funct.grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
account=self.pool.get('account.analytic.account').browse(cr, uid, account_id)
if temp:
return temp
else:
if account.parent_id:
return self._get_related_user_account_recursiv(cr, uid, user_id, account.parent_id.id)
else:
return False
def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0):
res = {}
if not (account_id):
#avoid a useless call to super
return res
if not (user_id):
return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)
#get the browse record related to user_id and account_id
temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id)
if not temp:
#if there isn't any record for this user_id and account_id
return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)
else:
#get the old values from super and add the value from the new relation analytic_user_funct_grid
r = self.pool.get('analytic.user.funct.grid').browse(cr, uid, temp)[0]
res.setdefault('value',{})
res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)['value']
res['value']['product_id'] = r.product_id.id
res['value']['product_uom_id'] = r.product_id.uom_id.id
#the change of product has to impact the amount, uom and general_account_id
a = r.product_id.property_account_expense.id
if not a:
a = r.product_id.categ_id.property_account_expense_categ.id
if not a:
raise UserError(_('There is no expense account defined for this product: "%s" (id:%d)') % (r.product_id.name, r.product_id.id,))
# Compute based on pricetype
if unit_amount:
amount_unit = self.on_change_unit_amount(cr, uid, ids,
r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
amount = unit_amount * amount_unit
res ['value']['amount']= - round(amount, 2)
res ['value']['general_account_id']= a
return res
def on_change_user_id(self, cr, uid, ids, user_id, account_id, unit_amount=0):
res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids, user_id)
if account_id:
#get the browse record related to user_id and account_id
temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id)
if temp:
#add the value from the new relation analytic_user_funct_grid
r = self.pool.get('analytic.user.funct.grid').browse(cr, uid, temp)[0]
res['value']['product_id'] = r.product_id.id
#the change of product has to impact the amount, uom and general_account_id
a = r.product_id.property_account_expense.id
if not a:
a = r.product_id.categ_id.property_account_expense_categ.id
if not a:
raise UserError(
_('There is no expense account defined ' \
'for this product: "%s" (id:%d)') % \
(r.product_id.name, r.product_id.id,))
# Compute based on pricetype
if unit_amount:
amount_unit = self.on_change_unit_amount(cr, uid, ids,
r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
amount = unit_amount * amount_unit
res ['value']['amount']= - round(amount, 2)
res ['value']['general_account_id']= a
return res
class account_analytic_line(osv.osv):
_inherit = "account.analytic.line"
def _get_invoice_price(self, cr, uid, account, product_id, user_id, qty, context = {}):
# Look in account, if no value for the user => look in parent until there is no more parent to look
# Take the first found... if nothing found => return False
def _get_related_user_account_recursiv(self, cr, uid, user_id, account_id, context):
user_function_id = self.pool.get('analytic.user.funct.grid').search(cr, uid, [('user_id', '=', user_id), ('account_id', '=', account_id)])
account = self.pool['account.analytic.account'].browse(cr, uid, account_id)
if user_function_id:
return user_function_id
elif account.parent_id:
return self._get_related_user_account_recursiv(cr, uid, user_id, account.parent_id.id, context)
else:
return False
def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0, is_timesheet=False, context=None):
res = {}
if not (account_id):
return res
if not (user_id):
return super(account_analytic_line, self).on_change_account_id(cr, uid, ids, account_id, user_id, is_timesheet, context)
# get the browse record related to user_id and account_id
user_function_id = self._get_related_user_account_recursiv(cr, uid, user_id, account_id, context)
if not user_function_id:
# if there isn't any record for this user_id and account_id
return super(account_analytic_line, self).on_change_account_id(cr, uid, ids, account_id, user_id, is_timesheet, context)
else:
# get the old values from super and add the value from the new relation analytic_user_funct_grid
res['value'] = super(account_analytic_line, self).on_change_account_id(cr, uid, ids, account_id, user_id, is_timesheet, context)['value']
res['value'].update(self._get_values_based_on_user_function(cr, uid, ids, user_function_id, unit_amount, context=context))
return res
def on_change_user_id(self, cr, uid, ids, user_id, unit_amount=0, account_id=None, is_timesheet=False, context=None):
res = {}
res = super(account_analytic_line, self).on_change_user_id(cr, uid, ids, user_id, is_timesheet, context=context)
if account_id:
# get the browse record related to user_id and account_id
user_function_id = self._get_related_user_account_recursiv(cr, uid, user_id, account_id, context)
if user_function_id:
res['value'].update(self._get_values_based_on_user_function(cr, uid, ids, user_function_id, unit_amount, context=context))
return res
# Returns appropriate values for an analytic line if the user has a specific user_function
def _get_values_based_on_user_function(self, cr, uid, ids, user_function_id, unit_amount=0, context=None):
res = {}
user_function = self.pool['analytic.user.funct.grid'].browse(cr, uid, user_function_id, context=context)
product = user_function.product_id
res['product_id'] = product.id
res['uom_id'] = product.uom_id.id
expense_account = product.property_account_expense.id
if not expense_account:
expense_account = product.categ_id.property_account_expense_categ.id
if not expense_account:
raise UserError(_('There is no expense account defined for this product: "%s" (id:%d)') % (product.name, product.id,))
res['general_account_id'] = expense_account
if unit_amount:
new_amount = self.on_change_unit_amount(cr, uid, ids, product.id, unit_amount, False, product.uom_id.id)
res['amount'] = new_amount['value']['amount']
return res
def _get_invoice_price(self, cr, uid, account, product_id, user_id, qty, context={}):
for grid in account.user_product_ids:
if grid.user_id.id==user_id:
if grid.user_id.id == user_id:
return grid.price
return super(account_analytic_line, self)._get_invoice_price(cr, uid, account, product_id, user_id, qty, context)