- The code will compare if the dates from attendance fit the date_to and date_from of payslip and sum their lates, undertime and overtime.
- class awb_hr_payslip_extension(osv.osv):
- _name = 'hr.payslip'
- _inherit = 'hr.payslip'
- def create(self, cr, uid, vals, context=None):
- # get other input model
- attendance_model = self.pool.get('hr.attendance')
- payslip_model = self.pool.get('hr.payslip')
- #payslip = payslip_model.browse(cr, uid, vals['payslip_id'].id, context=context)
- # convert date_to
- date_to = vals['date_to'] + " 23:59:59"
- # filtering / get records depending on filter: name, date from, date to
- # 'name' is datetime field from attendance model
- # vals['field name from hr.payslip']
- attendance_ids = attendance_model.search(cr, uid, [('employee_id', '=', vals['employee_id']), ('name', '>=', vals['date_from']), ('name', "<=", date_to)], count=False)
- records_list = attendance_model.browse(cr, uid, attendance_ids)
- total_late = 0
- total_undertime = 0
- total_overtime = 0
- for record in records_list:
- total_late = total_late + record.late
- total_undertime = total_undertime + record.undertime
- total_overtime = total_overtime + record.overtime
- # insert to Other Inputs
- other_input_model = self.pool.get('hr.payslip.input')
- other_input_model.create(cr, uid, ['name': 'Total Late'], 'code': 'late_deduction', 'amount': total_late, 'contract_id': vals['contract_id']}, context=None)
- other_input.create(cr, uid, {'name': 'Total Undertime', 'code' : 'undertime_deduction', 'amount': total_undertime, 'contract_id': vals['contract_id']}, context=None)
- other_input.create(cr, uid, {'name': 'Total Overtime', 'code': 'overtime_pay', 'amount': total_overtime, 'contract_id': vals['contract_id']}, context=None)
- return super(awb_hr_payslip_extension, self).create(cr, uid, vals, context=context)
- awb_hr_payslip_extension()
this is my error from this code:(
Integrity Error
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[object with reference: payslip_id - payslip.id]