Initial commit of *new* hr_payroll_timesheet and hr_payroll_timesheet_old for 11.0

This commit is contained in:
Jared Kipe
2018-10-31 09:58:09 -07:00
parent cd1880aa12
commit 19fa0dba34
11 changed files with 220 additions and 124 deletions

4
hr_payroll_timesheet/__init__.py Executable file → Normal file
View File

@@ -1,3 +1 @@
# -*- coding: utf-8 -*- from . import models
from . import hr_payslip
from . import hr_contract

View File

@@ -1,18 +1,16 @@
# -*- coding: utf-8 -*-
{ {
'name': 'Timesheets on Payslips', 'name': 'Timesheets on Payslips',
'description': 'Get Timesheet and Attendence numbers onto Employee Payslips.', 'description': 'Get Timesheet hours onto Employee Payslips.',
'version': '11.0.0.0.0', 'version': '11.0.1.0.0',
'website': 'https://hibou.io/', 'website': 'https://hibou.io/',
'author': 'Hibou Corp. <hello@hibou.io>', 'author': 'Hibou Corp. <hello@hibou.io>',
'license': 'AGPL-3', 'license': 'AGPL-3',
'category': 'Human Resources', 'category': 'Human Resources',
'data': [ 'data': [
'hr_contract_view.xml', 'views/hr_contract_view.xml',
], ],
'depends': [ 'depends': [
'hr_payroll', 'hr_payroll',
'hr_timesheet_attendance', 'hr_timesheet',
], ],
} }

View File

@@ -1,8 +0,0 @@
# -*- coding: utf-8 -*-
from odoo import models, fields
class HrContract(models.Model):
_inherit = 'hr.contract'
paid_hourly = fields.Boolean(string="Paid Hourly", default=False)

View File

@@ -1,18 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="hr_contract_form_inherit" model="ir.ui.view">
<field name="name">hr.contract.form.inherit</field>
<field name="model">hr.contract</field>
<field name="priority">20</field>
<field name="inherit_id" ref="hr_contract.hr_contract_view_form"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='advantages']" position="after">
<field name="paid_hourly"/>
</xpath>
</data>
</field>
</record>
</data>
</odoo>

View File

@@ -1,89 +0,0 @@
from datetime import datetime
from collections import defaultdict
from odoo import api, models
from odoo.exceptions import ValidationError
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
class HrPayslip(models.Model):
_inherit = 'hr.payslip'
@api.model
def get_worked_day_lines(self, contracts, date_from, date_to):
def create_empty_worked_lines(employee, contract, date_from, date_to):
attn = {
'name': 'Attendance',
'sequence': 10,
'code': 'ATTN',
'number_of_days': 0.0,
'number_of_hours': 0.0,
'contract_id': contract.id,
}
valid_attn = [
('employee_id', '=', employee.id),
('check_in', '>=', date_from),
('check_in', '<=', date_to),
]
return attn, valid_attn
work = []
for contract in contracts:
worked_attn, valid_attn = create_empty_worked_lines(
contract.employee_id,
contract,
date_from,
date_to
)
days = set()
for attn in self.env['hr.attendance'].search(valid_attn):
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()
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.extend(work)
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

View File

@@ -0,0 +1,2 @@
from . import hr_contract
from . import hr_payslip

View File

@@ -0,0 +1,7 @@
from odoo import fields, models
class HrContract(models.Model):
_inherit = 'hr.contract'
paid_hourly_timesheet = fields.Boolean(string="Paid Hourly Timesheet", default=False)

View File

@@ -0,0 +1,99 @@
from datetime import datetime
from collections import defaultdict
from odoo import api, models
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
class HrPayslip(models.Model):
_inherit = 'hr.payslip'
@api.model
def get_worked_day_lines(self, contracts, date_from, date_to):
work = []
for contract in contracts.filtered(lambda c: c.paid_hourly_timesheet):
# Only run on 'paid hourly timesheet' contracts.
res = self._get_worked_day_lines_hourly_timesheet(contract, date_from, date_to)
if res:
work.append(res)
res = super(HrPayslip, self).get_worked_day_lines(contracts.filtered(lambda c: not c.paid_hourly_timesheet), date_from, date_to)
res.extend(work)
return res
def _get_worked_day_lines_hourly_timesheet(self, contract, date_from, date_to):
"""
This would be a common hook to extend or break out more functionality, like pay rate based on project.
Note that you will likely need to aggregate similarly in hour_break_down() and hour_break_down_week()
:param contract: `hr.contract`
:param date_from: str
:param date_to: str
:return: dict of values for `hr.payslip.worked_days`
"""
values = {
'name': 'Timesheet',
'sequence': 15,
'code': 'TS',
'number_of_days': 0.0,
'number_of_hours': 0.0,
'contract_id': contract.id,
}
valid_ts = [
# ('is_timesheet', '=', True),
# 'is_timesheet' is computed if there is a project_id associated with the entry
('project_id', '!=', False),
('employee_id', '=', contract.employee_id.id),
('date', '>=', date_from),
('date', '<=', date_to),
]
days = set()
for ts in self.env['account.analytic.line'].search(valid_ts):
if ts.unit_amount:
ts_date = datetime.strptime(ts.date, DEFAULT_SERVER_DATE_FORMAT)
ts_iso = ts_date.isocalendar()
if ts_iso not in days:
values['number_of_days'] += 1
days.add(ts_iso)
values['number_of_hours'] += ts.unit_amount
values['number_of_hours'] = round(values['number_of_hours'], 2)
return values
@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 == 'TS':
timesheets = self.env['account.analytic.line'].search([
# ('is_timesheet', '=', True),
# 'is_timesheet' is computed if there is a project_id associated with the entry
('project_id', '!=', False),
('employee_id', '=', self.employee_id.id),
('date', '>=', self.date_from),
('date', '<=', self.date_to),
])
day_values = defaultdict(float)
for ts in timesheets:
if ts.unit_amount:
ts_date = datetime.strptime(ts.date, DEFAULT_SERVER_DATE_FORMAT)
ts_iso = ts_date.isocalendar()
day_values[ts_iso] += ts.unit_amount
return day_values
elif hasattr(super(HrPayslip, self), 'hour_break_down'):
return super(HrPayslip, self).hour_break_down(code)
@api.multi
def hours_break_down_week(self, code):
"""
:param code: hat kind of worked days you need aggregated
:return: dict: keys are isocalendar weeks, values are hours.
"""
days = self.hour_break_down(code)
weeks = defaultdict(float)
for isoday, hours in days.items():
weeks[isoday[1]] += hours
return weeks

View File

@@ -0,0 +1 @@
from . import test_payslip_timesheet

View File

@@ -0,0 +1,87 @@
from odoo.tests import common
from odoo import fields
class TestPayslipTimesheet(common.TransactionCase):
def setUp(self):
super(TestPayslipTimesheet, self).setUp()
self.employee = self.env['hr.employee'].create({
'birthday': '1985-03-14',
'country_id': self.ref('base.us'),
'department_id': self.ref('hr.dep_rd'),
'gender': 'male',
'name': 'Jared'
})
self.contract = self.env['hr.contract'].create({
'name': 'test',
'employee_id': self.employee.id,
'type_id': self.ref('hr_contract.hr_contract_type_emp'),
'struct_id': self.ref('hr_payroll.structure_base'),
'resource_calendar_id': self.ref('resource.resource_calendar_std'),
'wage': 21.50,
'date_start': '2018-01-01',
'state': 'open',
'paid_hourly_timesheet': True,
'schedule_pay': 'monthly',
})
self.project = self.env['project.project'].create({
'name': 'Timesheets',
})
def test_payslip_timesheet(self):
self.assertTrue(self.contract.paid_hourly_timesheet)
from_date = '2018-01-01'
to_date = '2018-01-31'
# Day 1
self.env['account.analytic.line'].create({
'employee_id': self.employee.id,
'project_id': self.project.id,
'date': '2018-01-01',
'unit_amount': 5.0,
})
self.env['account.analytic.line'].create({
'employee_id': self.employee.id,
'project_id': self.project.id,
'date': '2018-01-01',
'unit_amount': 3.0,
})
# Day 2
self.env['account.analytic.line'].create({
'employee_id': self.employee.id,
'project_id': self.project.id,
'date': '2018-01-02',
'unit_amount': 1.0,
})
# Make one that should be excluded.
self.env['account.analytic.line'].create({
'employee_id': self.employee.id,
'project_id': self.project.id,
'date': '2017-01-01',
'unit_amount': 5.0,
})
# Create slip like a batch run.
slip_data = self.env['hr.payslip'].onchange_employee_id(from_date, to_date, self.employee.id, contract_id=False)
res = {
'employee_id': self.employee.id,
'name': slip_data['value'].get('name'),
'struct_id': slip_data['value'].get('struct_id'),
'contract_id': slip_data['value'].get('contract_id'),
'input_line_ids': [(0, 0, x) for x in slip_data['value'].get('input_line_ids')],
'worked_days_line_ids': [(0, 0, x) for x in slip_data['value'].get('worked_days_line_ids')],
'date_from': from_date,
'date_to': to_date,
'company_id': self.employee.company_id.id,
}
payslip = self.env['hr.payslip'].create(res)
payslip.compute_sheet()
self.assertTrue(payslip.worked_days_line_ids)
timesheet_line = payslip.worked_days_line_ids.filtered(lambda l: l.code == 'TS')
self.assertTrue(timesheet_line)
self.assertEqual(timesheet_line.number_of_days, 2.0)
self.assertEqual(timesheet_line.number_of_hours, 9.0)

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="hr_contract_form_inherit" model="ir.ui.view">
<field name="name">hr.contract.form.inherit</field>
<field name="model">hr.contract</field>
<field name="inherit_id" ref="hr_contract.hr_contract_view_form"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='advantages']" position="after">
<field name="paid_hourly_timesheet"/>
</xpath>
<xpath expr="//div[@name='wage']/span" position="replace">
<span attrs="{'invisible': [('paid_hourly_timesheet', '=', True)]}">/ pay period</span>
<span attrs="{'invisible': [('paid_hourly_timesheet', '=', False)]}">/ hour</span>
</xpath>
</data>
</field>
</record>
</odoo>