[FIX] sale_timesheet: record employee cost if no SO line
When a user records an activity or a timesheet entry on an analytic account not linked to a SO, the employee's cost is not taken into account. This is due to the fact that the search of the timesheet cost is only performed if there is a SO line. This fix uncouples the SO line search with the timesheet cost search, since there is no reason to link them. opw-657899
This commit is contained in:
@@ -21,6 +21,7 @@ class ResCompany(models.Model):
|
||||
|
||||
class HrEmployee(models.Model):
|
||||
_inherit = 'hr.employee'
|
||||
# FIXME: this field should be in module hr_timesheet, not sale_timesheet
|
||||
timesheet_cost = fields.Float(string='Timesheet Cost', default=0.0)
|
||||
|
||||
|
||||
@@ -43,7 +44,10 @@ class AccountAnalyticLine(models.Model):
|
||||
def _get_sale_order_line(self, vals=None):
|
||||
result = dict(vals or {})
|
||||
if self.is_timesheet:
|
||||
sol = result.get('so_line', False) or self.so_line
|
||||
if result.get('so_line'):
|
||||
sol = self.env['sale.order.line'].browse([result['so_line']])
|
||||
else:
|
||||
sol = self.so_line
|
||||
if not sol and self.account_id:
|
||||
sol = self.env['sale.order.line'].search([
|
||||
('order_id.project_id', '=', self.account_id.id),
|
||||
@@ -51,27 +55,59 @@ class AccountAnalyticLine(models.Model):
|
||||
('product_id.track_service', '=', 'timesheet'),
|
||||
('product_id.type', '=', 'service')],
|
||||
limit=1)
|
||||
else:
|
||||
sol = self.so_line
|
||||
if sol:
|
||||
emp = self.env['hr.employee'].search([('user_id', '=', self.user_id.id)], limit=1)
|
||||
if result.get('amount', False):
|
||||
amount = result['amount']
|
||||
elif emp and emp.timesheet_cost:
|
||||
amount = -self.unit_amount * emp.timesheet_cost
|
||||
elif self.product_id and self.product_id.standard_price:
|
||||
amount = -self.unit_amount * self.product_id.standard_price
|
||||
else:
|
||||
amount = self.amount or 0.0
|
||||
result.update({
|
||||
'product_id': sol.product_id.id,
|
||||
'product_uom_id': self.env.user.company_id.project_time_mode_id.id or sol.product_id.uom_id.id,
|
||||
'amount': amount,
|
||||
'so_line': sol.id,
|
||||
'product_id': sol.product_id.id,
|
||||
})
|
||||
result = self._get_timesheet_cost(result)
|
||||
|
||||
result = super(AccountAnalyticLine, self)._get_sale_order_line(vals=result)
|
||||
return result
|
||||
|
||||
def _get_timesheet_cost(self, vals=None):
|
||||
result = dict(vals or {})
|
||||
if result.get('is_timesheet') or self.is_timesheet:
|
||||
if result.get('amount'):
|
||||
return result
|
||||
|
||||
unit_amount = result.get('unit_amount', 0.0) or self.unit_amount
|
||||
user_id = result.get('user_id', False) or self.user_id.id
|
||||
emp = self.env['hr.employee'].search([('user_id', '=', user_id)], limit=1)
|
||||
|
||||
if emp and emp.timesheet_cost:
|
||||
# Most common case: cost obtained on employee
|
||||
result.update({
|
||||
'amount': -unit_amount * emp.timesheet_cost,
|
||||
'product_uom_id': emp.user_id.company_id.project_time_mode_id.id,
|
||||
})
|
||||
elif result.get('product_id'):
|
||||
# Only useful for _get_sale_order_line since it can add a product_id.
|
||||
product = self.env['product.product'].browse([result.get('product_id')])
|
||||
result.update({
|
||||
'amount': -unit_amount * product.standard_price,
|
||||
'product_uom_id': product.uom_id.id,
|
||||
})
|
||||
elif self.product_id:
|
||||
# Only use case where this should be useful:
|
||||
# - no cost on employee
|
||||
# - modification of the unit amount of an existing entry
|
||||
result.update({
|
||||
'amount': -unit_amount * self.product_id.standard_price,
|
||||
'product_uom_id': self.product_id.uom_id.id,
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def write(self, values):
|
||||
values = self._get_timesheet_cost(vals=values)
|
||||
return super(AccountAnalyticLine, self).write(values)
|
||||
|
||||
@api.model
|
||||
def create(self, values):
|
||||
values = self._get_timesheet_cost(vals=values)
|
||||
return super(AccountAnalyticLine, self).create(values)
|
||||
|
||||
class SaleOrder(models.Model):
|
||||
_inherit = 'sale.order'
|
||||
|
||||
Reference in New Issue
Block a user