[NEW] hr_attendance_work_entry: for Odoo 13

This commit is contained in:
Jared Kipe
2020-09-18 11:28:39 -07:00
parent 01e60f6e35
commit 87ac2897f3
9 changed files with 180 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
from . import models
def attn_payroll_pre_init_hook(cr):
"""
This module installs a Work Entry Type with code "ATTN"
If you have undergone a migration (either for this module
or even your own Payslip Work Entry lines with code "ATTN")
then the uniqueness constraint will prevent this module
from installing.
"""
cr.execute("UPDATE hr_work_entry_type "
"SET code = 'ATTN-PRE-INSTALL' "
"WHERE code = 'ATTN';"
)

View File

@@ -0,0 +1,17 @@
{
'name': 'Attendance Work Entry Type',
'description': 'Set work types on attendance records.',
'version': '13.0.1.0.0',
'website': 'https://hibou.io/',
'author': 'Hibou Corp. <hello@hibou.io>',
'license': 'AGPL-3',
'category': 'Human Resources',
'data': [
'data/hr_attendance_work_entry_data.xml',
],
'depends': [
'hr_attendance',
'hr_work_entry',
],
'pre_init_hook': 'attn_payroll_pre_init_hook',
}

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="work_input_attendance" model="hr.work.entry.type">
<field name="name">Attendance</field>
<field name="code">ATTN</field>
<field name="allow_attendance" eval="True"/>
<field name="attendance_state">checked_in</field>
<field name="attendance_icon">fa-sign-in</field>
</record>
<!-- Rename Stock "Attendance" type -->
<record id="hr_work_entry.work_entry_type_attendance" model="hr.work.entry.type">
<field name="name">Work Calendar</field>
</record>
</odoo>

View File

@@ -0,0 +1,3 @@
from . import attendance
from . import employee
from . import work_entry

View File

@@ -0,0 +1,9 @@
from odoo import fields, models
class HrAttendance(models.Model):
_inherit = 'hr.attendance'
work_type_id = fields.Many2one('hr.work.entry.type', string='Work Type',
default=lambda self: self.env.ref('hr_attendance_work_entry.work_input_attendance',
raise_if_not_found=False))

View File

@@ -0,0 +1,58 @@
from odoo import api, fields, models, _
from odoo.exceptions import UserError
class HrEmployee(models.Model):
_inherit = 'hr.employee'
attendance_state = fields.Selection(selection_add=[('break', 'Break')])
@api.depends('last_attendance_id.work_type_id')
def _compute_attendance_state(self):
for employee in self:
att = employee.last_attendance_id.sudo()
if not att or att.check_out:
employee.attendance_state = 'checked_out'
elif employee.last_attendance_id.work_type_id.attendance_state:
employee.attendance_state = employee.last_attendance_id.work_type_id.attendance_state
else:
employee.attendance_state = 'checked_in'
def attendance_manual(self, next_action, entered_pin=None, work_type_id=None):
self = self.with_context(work_type_id=work_type_id)
return super(HrEmployee, self).attendance_manual(next_action, entered_pin=entered_pin)
def _attendance_action_change(self):
""" Check In/Check Out action
Check In: create a new attendance record
Check Out: modify check_out field of appropriate attendance record
"""
self.ensure_one()
action_date = fields.Datetime.now()
work_type_id = self._context.get('work_type_id', False)
if self.attendance_state == 'checked_out':
vals = {
'employee_id': self.id,
'check_in': action_date,
}
if work_type_id:
# if we don't have a work_type_id, we want the default
vals['work_type_id'] = work_type_id
return self.env['hr.attendance'].create(vals)
attendance = self.env['hr.attendance'].search([('employee_id', '=', self.id), ('check_out', '=', False)], limit=1)
if attendance and work_type_id:
# work_type_id is the "next" attendance type
attendance.check_out = action_date
vals = {
'employee_id': self.id,
'check_in': action_date,
'work_type_id': work_type_id,
}
return self.env['hr.attendance'].create(vals)
if attendance:
attendance.check_out = action_date
else:
raise UserError(_('Cannot perform check out on %(empl_name)s, could not find corresponding check in. '
'Your attendances have probably been modified manually by human resources.') % {'empl_name': self.sudo().name, })
return attendance

View File

@@ -0,0 +1,13 @@
from odoo import fields, models
class HrWorkEntryType(models.Model):
_inherit = 'hr.work.entry.type'
allow_attendance = fields.Boolean(string='Allow in Attendance')
attendance_icon = fields.Char(string='Attendance Icon', default='fa-sign-in')
attendance_state = fields.Selection([
# ('checked_out', "Checked out"), # reserved for detecting new punch in
('checked_in', "Checked in"),
('break', 'Break'),
], string='Attendance State', default='checked_in')

View File

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

View File

@@ -0,0 +1,47 @@
from odoo.tests import common
class TestAttendanceWorkType(common.TransactionCase):
def setUp(self):
super().setUp()
self.employee = self.env.ref('hr.employee_hne')
self.default_work_type = self.env.ref('hr_attendance_work_entry.work_input_attendance')
def test_01_work_type(self):
attendance = self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': '2020-01-06 10:00:00', # Monday
'check_out': '2020-01-06 19:00:00',
})
self.assertTrue(attendance.work_type_id)
self.assertEqual(attendance.work_type_id, self.default_work_type)
def test_11_employee_clock_in(self):
self.assertEqual(self.employee.attendance_state, 'checked_out')
attendance = self.employee._attendance_action_change()
self.assertEqual(attendance.work_type_id, self.default_work_type)
self.assertEqual(self.employee.attendance_state, 'checked_in')
# check out
self.employee._attendance_action_change()
self.assertEqual(self.employee.attendance_state, 'checked_out')
def test_12_employee_clock_in_break(self):
# check in with non-standard work type
break_type = self.env['hr.work.entry.type'].create({
'name': 'Test Break',
'code': 'TESTBREAK',
'allow_attendance': True,
'attendance_state': 'break',
})
self.employee = self.employee.with_context(work_type_id=break_type.id)
attendance = self.employee._attendance_action_change()
self.assertEqual(attendance.work_type_id, break_type)
self.assertEqual(self.employee.attendance_state, 'break')
# check back in immediately with default
self.employee = self.employee.with_context(work_type_id=self.default_work_type.id)
attendance = self.employee._attendance_action_change()
self.assertEqual(attendance.work_type_id, self.default_work_type)
self.assertEqual(attendance.work_type_id.attendance_state, 'checked_in')
self.assertEqual(self.employee.attendance_state, 'checked_in')