Proper migration to 11.0, and implementation of 'overtime rules' breakdown of hours/weeks.

This commit is contained in:
Jared Kipe
2018-05-09 16:01:49 -07:00
parent 1f51a7eaac
commit e12ec1290a
2 changed files with 66 additions and 27 deletions

View File

@@ -8,7 +8,7 @@
<field name="inherit_id" ref="hr_contract.hr_contract_view_form"/> <field name="inherit_id" ref="hr_contract.hr_contract_view_form"/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<data> <data>
<xpath expr="//field[@name='wage']" position="after"> <xpath expr="//field[@name='advantages']" position="after">
<field name="paid_hourly"/> <field name="paid_hourly"/>
</xpath> </xpath>
</data> </data>

View File

@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*- from datetime import datetime
from collections import defaultdict
from odoo import api, models from odoo import api, models
from odoo.exceptions import ValidationError
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
class HrPayslip(models.Model): class HrPayslip(models.Model):
@@ -7,44 +10,80 @@ class HrPayslip(models.Model):
@api.model @api.model
def get_worked_day_lines(self, contracts, date_from, date_to): def get_worked_day_lines(self, contracts, date_from, date_to):
def create_empty_worked_lines(employee_id, contract_id, date_from, date_to): def create_empty_worked_lines(employee, contract, date_from, date_to):
attendance = { attn = {
'name': 'Timesheet Attendance', 'name': 'Attendance',
'sequence': 10, 'sequence': 10,
'code': 'ATTN', 'code': 'ATTN',
'number_of_days': 0.0, 'number_of_days': 0.0,
'number_of_hours': 0.0, 'number_of_hours': 0.0,
'contract_id': contract_id, 'contract_id': contract.id,
} }
valid_days = [ valid_attn = [
('sheet_id.employee_id', '=', employee_id), ('employee_id', '=', employee.id),
('sheet_id.state', '=', 'done'), ('check_in', '>=', date_from),
('sheet_id.date_from', '>=', date_from), ('check_in', '<=', date_to),
('sheet_id.date_to', '<=', date_to),
] ]
return attendance, valid_days return attn, valid_attn
attendances = []
work = []
for contract in contracts: for contract in contracts:
attendance, valid_days = create_empty_worked_lines( worked_attn, valid_attn = create_empty_worked_lines(
contract.employee_id.id, contract.employee_id,
contract.id, contract,
date_from, date_from,
date_to date_to
) )
days = set()
for day in self.env['hr_timesheet_sheet.sheet.day'].search(valid_days): for attn in self.env['hr.attendance'].search(valid_attn):
if day.total_attendance >= 0.0: if not attn.check_out:
attendance['number_of_days'] += 1 raise ValidationError('This pay period must not have any open attendances.')
attendance['number_of_hours'] += day.total_attendance if attn.worked_hours:
# Avoid in/outs
# needed so that the shown hours matches any calculations you use them for attn_start_time = datetime.strptime(attn.check_in, DEFAULT_SERVER_DATETIME_FORMAT)
attendance['number_of_hours'] = round(attendance['number_of_hours'], 2) attn_iso = attn_start_time.isocalendar()
attendances.append(attendance) if not attn_iso in days:
worked_attn['number_of_days'] += 1
days.add(attn_iso)
worked_attn['number_of_hours'] += attn.worked_hours
worked_attn['number_of_hours'] = round(worked_attn['number_of_hours'], 2)
work.append(worked_attn)
res = super(HrPayslip, self).get_worked_day_lines(contracts, date_from, date_to) res = super(HrPayslip, self).get_worked_day_lines(contracts, date_from, date_to)
res.extend(attendances) res.extend(work)
return res return res
@api.multi
def hour_break_down(self, code):
"""
:param code: what kind of worked days you need aggregated
:return: dict: keys are isocalendar tuples, values are hours.
"""
self.ensure_one()
if code == 'ATTN':
attns = self.env['hr.attendance'].search([
('employee_id', '=', self.employee_id.id),
('check_in', '>=', self.date_from),
('check_in', '<=', self.date_to),
])
day_values = defaultdict(float)
for attn in attns:
if not attn.check_out:
raise ValidationError('This pay period must not have any open attendances.')
if attn.worked_hours:
# Avoid in/outs
attn_start_time = datetime.strptime(attn.check_in, DEFAULT_SERVER_DATETIME_FORMAT)
attn_iso = attn_start_time.isocalendar()
day_values[attn_iso] += attn.worked_hours
return day_values
elif hasattr(super(HrPayslip, self), 'hours'):
return super(HrPayslip, self).hours(code)
@api.multi
def hours_break_down_week(self, code):
days = self.hour_break_down(code)
weeks = defaultdict(float)
for isoday, hours in days.items():
weeks[isoday[1]] += hours
return weeks