Hello Emilio!
The answer to this question could be as detailed as the business case, depending on case by case. So, to achieve this, you need to create a new salary rule or modify an existing one.
The computation should be based on Python Code:
hours = 0
Line explanation: setting the variable hours to 0.
lines = payslip.env[ 'account.analytic.line' ].search( [ ('employee_id' , '=', payslip.employee_id.id), ('validated_status', '=', 'validate'), ('date', '>=', payslip.date_from), ('date', '
Line explanation: This code retrieves all the analytic lines (account.analytic.line) for the employee on the payslip that has been validated and is within the current payroll period.
-
payslip.env['account.analytic.line']:
This accesses the account.analytic.line model, which refers to the analytic lines or timesheet records in Odoo.
- .search: The search() function looks for records that meet the criteria specified in the list of tuples (search criteria).
-
('employee_id', '=', payslip.employee_id.id): Filters the analytic lines to get only those associated with the specific employee that appears on the payslip (payslip.employee_id.id).
- ('validated_status', '=', 'validate'): Filters the analytic lines to get only those that have been validated, meaning they have been approved or confirmed.
- ('date', '>=', payslip.date_from): Filters the analytic lines to get only those that were recorded starting from the payslip start date (payslip.date_from).
- ('date', ': Filters the analytic lines to get only those that were recorded up to the payslip end date (payslip.date_to).
for line in lines:
hours += line.unit_amount
result_qty = hours
result = contract.hourly_wage
Explanation: The for loop iterates over each analytic line stored in "Lines," so each "Line" represents a record of time worked by the employee. Then, the code sums all the hours and inputs the total hours in result_qty, which will result in the hourly wage in the employee contract.
Disclaimer: the code may depend on what or how you want to impact your payroll calculations.
Before applying my solution, please think about other important factors that may be relevant to your payroll calculation:
- As you may have seen, this does not generate work entries such as overtime. When someone works more than the agreed-upon amount, you may need to create another salary rule to calculate that.
- And if Time Off generates Timesheets, consider this when applying this solution.
Disclaimer: This is only a general solution to the business case. Additional specifications are needed to cover it in more detail, which may apply depending on the specific case, as I mentioned earlier.