Skip to content

Commit

Permalink
Merge beed906 into f28ac36
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevhe18 committed Aug 27, 2018
2 parents f28ac36 + beed906 commit 41eeb00
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 0 deletions.
44 changes: 44 additions & 0 deletions hr_holiday_code/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

===============
HR Holiday Code
===============

This module extends the functionality of hr_holiday that allows
"Leave Request" and "Allocation Request" has a code.
The Code will be automatically filled in accordance with the sequence
which can be configured on (1) Leave Type (2) Company or (3) By Default.

Configuration
=============


Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/open-synergy/opnsynid-hr/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed
and welcomed feedback.


Credits
=======

Contributors
------------

* Andhitia Rama <andhitia.r@gmail.com>
* Michael Viriyananda <viriyananda.michael@gmail.com>

Maintainer
----------

.. image:: https://opensynergy-indonesia.com/logo.png
:alt: OpenSynergy Indonesia
:target: https://opensynergy-indonesia.com

This module is maintained by the OpenSynergy Indonesia.
7 changes: 7 additions & 0 deletions hr_holiday_code/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2018 OpenSynergy Indonesia
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
from .hooks import create_code_equal_to_id
from .hooks import assign_old_sequences
22 changes: 22 additions & 0 deletions hr_holiday_code/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright 2018 OpenSynergy Indonesia
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "HR Holiday Code",
"version": "8.0.1.0.0",
"website": "https://opensynergy-indonesia.com",
"author": "OpenSynergy Indonesia",
"license": "AGPL-3",
"installable": True,
"depends": [
"hr_holidays",
],
"data": [
"data/hr_holidays_sequence.xml",
"views/hr_holidays_view.xml",
"views/hr_holidays_status_view.xml",
"views/res_company_views.xml"
],
"pre_init_hook": "create_code_equal_to_id",
"post_init_hook": "assign_old_sequences",
}
27 changes: 27 additions & 0 deletions hr_holiday_code/data/hr_holidays_sequence.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data noupdate="1">
<record id="leave_request_sequence_type" model="ir.sequence.type">
<field name="name">Leave Request Type</field>
<field name="code">hr.holidays.lvr</field>
</record>
<record id="leave_request_sequence" model="ir.sequence">
<field name="name">Leave Request</field>
<field name="code">hr.holidays.lvr</field>
<field eval="4" name="padding" />
<field name="prefix">LVR</field>
</record>

<record id="allocation_request_sequence_type" model="ir.sequence.type">
<field name="name">Allocation Request Type</field>
<field name="code">hr.holidays.alr</field>
</record>
<record id="allocation_request_sequence" model="ir.sequence">
<field name="name">Allocation Request</field>
<field name="code">hr.holidays.alr</field>
<field eval="4" name="padding" />
<field name="prefix">ALR</field>
</record>

</data>
</openerp>
57 changes: 57 additions & 0 deletions hr_holiday_code/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
# Copyright 2018 OpenSynergy Indonesia
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import SUPERUSER_ID


def create_code_equal_to_id(cr):
"""
With this pre-init-hook we want to avoid error when creating the UNIQUE
code constraint when the module is installed and before the post-init-hook
is launched.
"""
cr.execute("ALTER TABLE hr_holidays "
"ADD COLUMN holiday_code character varying;")
cr.execute("UPDATE hr_holidays "
"SET holiday_code = id;")


def assign_old_sequences(cr, registry):
"""
This post-init-hook will update all existing issue assigning them the
corresponding sequence code.
"""
hr_holidays_obj =\
registry["hr.holidays"]
sequence_obj = registry["ir.sequence"]

criteria_lvr = [
("type", "=", "remove")
]

lvr_ids =\
hr_holidays_obj.search(
cr, SUPERUSER_ID, criteria_lvr, order="id")

for lvr in lvr_ids:
cr.execute("UPDATE hr_holidays "
"SET holiday_code = '%s' "
"WHERE id = %d;" %
(sequence_obj.get(cr, SUPERUSER_ID, "hr.holidays.lvr"),
lvr))

criteria_alr = [
("type", "=", "add")
]

alr_ids =\
hr_holidays_obj.search(
cr, SUPERUSER_ID, criteria_alr, order="id")

for alr in alr_ids:
cr.execute("UPDATE hr_holidays "
"SET holiday_code = '%s' "
"WHERE id = %d;" %
(sequence_obj.get(cr, SUPERUSER_ID, "hr.holidays.alr"),
alr))
7 changes: 7 additions & 0 deletions hr_holiday_code/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2018 OpenSynergy Indonesia
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import hr_holidays
from . import hr_holidays_status
from . import res_company
129 changes: 129 additions & 0 deletions hr_holiday_code/models/hr_holidays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
# Copyright 2018 OpenSynergy Indonesia
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import api, fields, models, _
from openerp.exceptions import Warning as UserError


class HrHolidays(models.Model):
_inherit = "hr.holidays"

holiday_code = fields.Char(
string="Code",
required=True,
default="/"
)

@api.multi
def _get_holidays_sequence_by_type(self, default_type):
self.ensure_one()
lvr_sequence_id = False
alr_sequence_id = False

status_id =\
self.holiday_status_id
if status_id:
lvr_sequence_id =\
status_id.leave_request_sequence_id
alr_sequence_id =\
status_id.allocation_request_sequence_id

if lvr_sequence_id and default_type == "remove":
result = self.env["ir.sequence"].\
next_by_id(lvr_sequence_id.id)
elif alr_sequence_id and default_type == "add":
result = self.env["ir.sequence"].\
next_by_id(alr_sequence_id.id)
else:
result = False
return result

@api.multi
def _get_holidays_sequence_by_company(self, default_type):
self.ensure_one()
obj_res_company =\
self.env["res.company"]

company_id =\
self.env.user.company_id.id

lvr_sequence_id = False
alr_sequence_id = False

company = obj_res_company.browse([company_id])[0]
if company:
lvr_sequence_id =\
company.leave_request_sequence_id
alr_sequence_id =\
company.allocation_request_sequence_id

if lvr_sequence_id and default_type == "remove":
result = self.env["ir.sequence"].\
next_by_id(lvr_sequence_id.id)
elif alr_sequence_id and default_type == "add":
result = self.env["ir.sequence"].\
next_by_id(alr_sequence_id.id)
else:
result = False
return result

@api.multi
def _get_holidays_sequence_by_default(self, default_type):
self.ensure_one()

if default_type == "remove":
result =\
self.env["ir.sequence"].next_by_code("hr.holidays.lvr")
elif default_type == "add":
result =\
self.env["ir.sequence"].next_by_code("hr.holidays.alr")
else:
result = False
return result

@api.multi
def _get_holidays_sequence(self):
self.ensure_one()
context = self._context

default_type =\
context.get("default_type", False)

if default_type:
by_type =\
self._get_holidays_sequence_by_type(default_type)
by_default =\
self._get_holidays_sequence_by_default(default_type)
by_company =\
self._get_holidays_sequence_by_company(default_type)
if by_type:
result = by_type
elif by_company:
result = by_company
elif by_default:
result = by_default
else:
result = False
else:
result = False
return result

@api.multi
def _prepare_create_data(self):
self.ensure_one()
result = {}
sequence =\
self._get_holidays_sequence()
if sequence:
result.update({"holiday_code": sequence})
else:
raise UserError(_("Sequence Is Not Valid"))
return result

@api.model
def create(self, values):
_super = super(HrHolidays, self)
result = _super.create(values)
result.write(result._prepare_create_data())
return result
21 changes: 21 additions & 0 deletions hr_holiday_code/models/hr_holidays_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright 2018 OpenSynergy Indonesia
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import fields, models


class HrHolidaysStatus(models.Model):
_inherit = "hr.holidays.status"

leave_request_sequence_id = fields.Many2one(
string="Leave Request Sequence",
comodel_name="ir.sequence",
company_dependent=True,
)

allocation_request_sequence_id = fields.Many2one(
string="Allocation Request Sequence",
comodel_name="ir.sequence",
company_dependent=True,
)
21 changes: 21 additions & 0 deletions hr_holiday_code/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright 2018 OpenSynergy Indonesia
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import models, fields


class ResCompany(models.Model):
_inherit = "res.company"

leave_request_sequence_id = fields.Many2one(
string="Leave Request Sequence",
comodel_name="ir.sequence",
company_dependent=True,
)

allocation_request_sequence_id = fields.Many2one(
string="Allocation Request Sequence",
comodel_name="ir.sequence",
company_dependent=True,
)
Binary file added hr_holiday_code/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions hr_holiday_code/views/hr_holidays_status_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="hr_holidays_status_form_view">
<field name="name">hr.holidays.status.form</field>
<field name="model">hr.holidays.status</field>
<field name="inherit_id" ref="hr_holidays.edit_holiday_status_form" />
<field name="arch" type="xml">
<xpath expr="//notebook/page/group[last()]" position="after">
<group name="sequence" string="Sequence">
<field name="leave_request_sequence_id"/>
<field name="allocation_request_sequence_id"/>
</group>
</xpath>
</field>
</record>
</data>
</openerp>
Loading

0 comments on commit 41eeb00

Please sign in to comment.