To split out the cost by employee labor cost and workcenter labor cost, you need to modify your _cal_price method to separately calculate and store these costs. Here's how you can achieve this:
def _cal_price(self, consumed_moves):
"""Set a price unit on the finished move according to `consumed_moves`."""
work_center_cost_dict = {}
employee_labor_cost_dict = {}
for work_order in self.workorder_ids:
# Initialize costs for this work order
work_center_cost = work_center_cost_dict.get(work_order.workcenter_id.id, 0.0)
employee_labor_cost = 0.0
if work_order.workcenter_id.labor_expense_account_id:
# Filter time lines that have an end date (i.e., completed)
time_lines = work_order.time_ids.filtered(lambda x: x.date_end)
# Calculate work center labor cost
duration = sum(time_lines.mapped("duration")) # Duration in minutes
work_center_cost += (duration / 60.0) * work_order.workcenter_id.costs_hour # Cost per hour
work_center_cost_dict[work_order.workcenter_id.id] = work_center_cost
# Calculate employee labor cost
for time_line in time_lines:
employee = time_line.employee_id
if employee:
# Assume each employee has an 'hourly_rate' field
employee_rate = employee.hourly_rate or 0.0 # Default to 0.0 if not set
time_line_duration = time_line.duration # Duration in minutes
employee_cost = (time_line_duration / 60.0) * employee_rate
# Update the total cost for this employee
total_employee_cost = employee_labor_cost_dict.get(employee.id, 0.0)
total_employee_cost += employee_cost
employee_labor_cost_dict[employee.id] = total_employee_cost
else:
# Handle cases where employee is not set
pass
# Update the context with separate cost dictionaries
if work_center_cost_dict or employee_labor_cost_dict:
self = self.with_context(
work_center_cost_dict=work_center_cost_dict,
employee_labor_cost_dict=employee_labor_cost_dict
)
return super()._cal_price(consumed_moves)
Explanation:
-
Separate Cost Dictionaries: We introduced two dictionaries, work_center_cost_dict and employee_labor_cost_dict, to store costs separately.
-
Work Center Cost Calculation:
- We calculate the total duration for each work order.
- Multiply the duration by the
costs_hour of the workcenter_id to get the work center cost.
- Store the cost in
work_center_cost_dict with the work center ID as the key.
-
Employee Labor Cost Calculation:
- Iterate over each
time_line in time_lines.
- For each time line, get the associated
employee_id.
- Calculate the employee cost using the employee's
hourly_rate and the duration.
- Accumulate the cost for each employee in
employee_labor_cost_dict.
-
Context Update:
- Update the context with both
work_center_cost_dict and employee_labor_cost_dict so that these costs can be used later in the processing (e.g., in the super class's _cal_price method or for accounting entries).
Notes:
-
Assumptions:
- Each
time_line has an employee_id field linking to the employee.
- The
employee_id (employee record) has an hourly_rate field representing the employee's labor cost per hour.
- If an employee does not have an
hourly_rate set, it's treated as 0.0 to prevent calculation errors.
-
Error Handling:
- Included a check for the presence of
employee and handled cases where employee_id might not be set.
-
Context Management:
- Used
self.with_context() to safely update the context without affecting other operations.
By separating the costs into different dictionaries and updating the context, you can now handle employee labor costs and workcenter labor costs independently throughout your application.