Skip to content

Commit

Permalink
[9.0] [MIG] hr_worked_days_from_timesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
LoisRForgeFlow committed Mar 30, 2017
1 parent 6400571 commit b4108ad
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 73 deletions.
25 changes: 12 additions & 13 deletions hr_worked_days_from_timesheet/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

==========================
Worked Days From Timesheet
==========================
=============================
HR Worked Days From Timesheet
=============================

* Adds a button to import worked days from timesheet
This module allow you to automatically fill the 'Worked Days' of payslips with
the information of each employee's timesheets for the payslip's period.

Installation
============
Expand All @@ -16,26 +17,23 @@ To install this module, you need to:
* clone the branch 8.0 of the repository https://github.com/OCA/hr
* add the path to this repository in your configuration (addons-path)
* update the module list
* search for "Worked Days From Timesheet" in your addons
* search for "HR Worked Days From Timesheet" in your addons
* install the module

Usage
=====

To use this module, you need to:

* hr_payroll
* hr_timesheet_sheet
#. Go to 'Payroll > Employee Payslips'.
#. Open a draft payslip.
#. In 'Worked Days' section click on 'Import from Timesheets' button.


.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/116/8.0
:target: https://runbot.odoo-community.org/runbot/116/9.0

Known issues / Roadmap
======================

* Not known issues

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

Expand All @@ -54,6 +52,7 @@ Contributors
* Pierre Lamarche <pierre.lamarche@savoirfairelinux.com>
* Ignacio Ibeas <ignacio@acysos.com>
* OpenSynergy Indonesia <https://opensynergy-indonesia.com>
* Lois Rilo Antelo <lois.rilo@eficent.com>

Maintainer
----------
Expand Down
3 changes: 2 additions & 1 deletion hr_worked_days_from_timesheet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# © 2012 Odoo Canada
# © 2015 Acysos S.L.
# © 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from . import models
from . import tests
13 changes: 8 additions & 5 deletions hr_worked_days_from_timesheet/__openerp__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# -*- coding: utf-8 -*-
# © 2012 Odoo Canada
# © 2015 Acysos S.L.
# © 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

{
'name': 'Worked Days From Timesheet',
'version': '8.0.1.0.0',
'name': 'HR Worked Days From Timesheet',
'summary': 'Adds a button to import worked days from timesheet.',
'version': '9.0.1.0.0',
'license': 'AGPL-3',
'category': 'Generic Modules/Human Resources',
'author': "Savoir-faire Linux, Acysos S.L.,"
"Odoo Community Association (OCA)",
'author': "Savoir-faire Linux, Acysos S.L., Eficent, "
"Odoo Community Association (OCA)",
'website': 'https://www.savoirfairelinux.com/',
'depends': [
'hr_payroll',
Expand All @@ -17,5 +20,5 @@
'data': [
'views/hr_payslip_view.xml'
],
'installable': False,
'installable': True,
}
39 changes: 19 additions & 20 deletions hr_worked_days_from_timesheet/models/hr_payslip.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
# © 2012 Odoo Canada
# © 2015 Acysos S.L.
# © 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

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


class HrPayslip(models.Model):
Expand All @@ -18,23 +18,17 @@ def _timesheet_mapping(self, timesheet_sheets, payslip, date_from,
module and creates a dict of worked days to be created in the payslip.
"""
# Create one worked days record for each timesheet sheet
wd_model = self.env['hr.payslip.worked_days']
uom_obj = self.env['product.uom']
uom_hours = self.env.ref('product.product_uom_hour')
for ts_sheet in timesheet_sheets:
# Get formated date from the timesheet sheet
date_from_formated = fields.Date.to_string(
fields.Datetime.from_string(ts_sheet.date_from))
number_of_hours = 0
for ts in ts_sheet.timesheet_ids:
if date_from <= ts.date <= date_to:
unit_amount = uom_obj._compute_qty_obj(
ts.product_uom_id,
ts.unit_amount, uom_hours)
unit_amount = ts.unit_amount
number_of_hours += unit_amount

if number_of_hours > 0:
wd_model.create({
self.env['hr.payslip.worked_days'].create({
'name': _('Timesheet %s') % date_from_formated,
'number_of_hours': number_of_hours,
'contract_id': payslip.contract_id.id,
Expand All @@ -44,11 +38,22 @@ def _timesheet_mapping(self, timesheet_sheets, payslip, date_from,
'payslip_id': payslip.id,
})

@api.multi
def _check_contract(self):
"""Contract is not required field for payslips, yet it is for
payslips.worked_days."""
for payslip in self:
if not payslip.contract_id:
raise UserError(
_("Contract is not defined for one or more payslips."),
)

@api.multi
def import_worked_days(self):
"""This method retreives the employee's timesheets for a payslip period
and creates worked days records from the imported timesheets
"""
self._check_contract()
for payslip in self:

date_from = payslip.date_from
Expand All @@ -57,8 +62,7 @@ def import_worked_days(self):
# Delete old imported worked_days
# The reason to delete these records is that the user may make
# corrections to his timesheets and then reimport these.
wd_model = self.env['hr.payslip.worked_days']
wd_model.search(
self.env['hr.payslip.worked_days'].search(
[('payslip_id', '=', payslip.id),
('imported_from_timesheet', '=', True)]).unlink()

Expand All @@ -71,17 +75,12 @@ def import_worked_days(self):
]
ts_model = self.env['hr_timesheet_sheet.sheet']
timesheet_sheets = ts_model.search(criteria)

if not timesheet_sheets:
raise UserError(
_("Sorry, but there is no approved Timesheets for the \
entire Payslip period"),
)

# The reason to call this method is for other modules to modify it.
self._timesheet_mapping(
timesheet_sheets,
payslip,
date_from,
date_to
)
self._timesheet_mapping(timesheet_sheets, payslip,
date_from, date_to)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
# © 2012 Odoo Canada
# © 2015 Acysos S.L.
# © 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from openerp import models, fields
from openerp import fields, models


class HrPayslipWorkedDays(models.Model):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
# © 2016 OpenSynergy Indonesia <https://opensynergy-indonesia.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# © 2012 Odoo Canada
# © 2015 Acysos S.L.
# © 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from openerp.tests.common import TransactionCase

Expand All @@ -11,7 +13,6 @@ def setUp(self):
super(TestComputeWorkdays, self).setUp()

self.company = self.env.ref('base.main_company')
self.analytic_journal = self.env.ref('hr_timesheet.analytic_journal')
self.user_admin = self.env.ref('base.partner_root')

user_dict = {
Expand All @@ -24,7 +25,6 @@ def setUp(self):
employee_dict = {
'name': 'Employee 1',
'user_id': self.user_test.id,
'journal_id': self.analytic_journal.id,
'address_id': self.user_test.partner_id.id,
}
self.employee = self.env['hr.employee'].create(employee_dict)
Expand Down Expand Up @@ -79,9 +79,9 @@ def setUp(self):
'name': 'ddd',
'account_id': self.analytic.id,
'unit_amount': 3.0,
'journal_id': self.analytic_journal.id,
'is_timesheet': True
}
self.analytic_ts1 = self.env['hr.analytic.timesheet'].with_context(
self.analytic_ts1 = self.env['account.analytic.line'].with_context(
user_id=self.user_test.id).create(
analytic_ts1_dict)

Expand All @@ -91,10 +91,10 @@ def setUp(self):
'name': 'ddd',
'account_id': self.analytic.id,
'unit_amount': 5.0,
'journal_id': self.analytic_journal.id,
'is_timesheet': True,
'sheet_id': self.ts2.id,
}
self.analytic_ts2 = self.env['hr.analytic.timesheet'].with_context(
self.analytic_ts2 = self.env['account.analytic.line'].with_context(
user_id=self.user_test.id).create(
analytic_ts2_dict)

Expand All @@ -105,9 +105,9 @@ def setUp(self):
'name': 'ddd',
'account_id': self.analytic.id,
'unit_amount': 7.0,
'journal_id': self.analytic_journal.id,
'is_timesheet': True
}
self.analytic_ts3 = self.env['hr.analytic.timesheet'].with_context(
self.analytic_ts3 = self.env['account.analytic.line'].with_context(
user_id=self.user_test.id).create(
analytic_ts3_dict)

Expand All @@ -118,9 +118,9 @@ def setUp(self):
'name': 'ddd',
'account_id': self.analytic.id,
'unit_amount': 11.5,
'journal_id': self.analytic_journal.id,
'is_timesheet': True
}
self.analytic_ts4 = self.env['hr.analytic.timesheet'].with_context(
self.analytic_ts4 = self.env['account.analytic.line'].with_context(
user_id=self.user_test.id).create(
analytic_ts4_dict)

Expand All @@ -131,9 +131,9 @@ def setUp(self):
'name': 'ddd',
'account_id': self.analytic.id,
'unit_amount': 13,
'journal_id': self.analytic_journal.id,
'is_timesheet': True
}
self.analytic_ts5 = self.env['hr.analytic.timesheet'].with_context(
self.analytic_ts5 = self.env['account.analytic.line'].with_context(
user_id=self.user_test.id).create(
analytic_ts5_dict)

Expand Down
39 changes: 20 additions & 19 deletions hr_worked_days_from_timesheet/views/hr_payslip_view.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_hr_payslip_form" model="ir.ui.view">
<field name="name">hr.payslip.form</field>
<field name="model">hr.payslip</field>
<field name="inherit_id" ref="hr_payroll.view_hr_payslip_form"/>
<field name="arch" type="xml">
<separator string="Worked Days" position="replace"/>
<field name="worked_days_line_ids" position="before">
<group>
<separator string="Worked Days"/>
<div class="oe_right oe_button_box">
<button string="Import from Timesheets" name="import_worked_days" type="object" states="draft" />
</div>
</group>
</field>
<odoo>

<record id="view_hr_payslip_form" model="ir.ui.view">
<field name="name">hr.payslip.form</field>
<field name="model">hr.payslip</field>
<field name="inherit_id" ref="hr_payroll.view_hr_payslip_form"/>
<field name="arch" type="xml">
<xpath expr="//sheet/notebook/page[1]/separator[1]"
position="replace"/>
<field name="worked_days_line_ids" position="before">
<group>
<separator string="Worked Days"/>
<div class="oe_right oe_button_box">
<button string="Import from Timesheets" name="import_worked_days" type="object" states="draft" />
</div>
</group>
</field>
</record>
</data>
</openerp>
</field>
</record>

</odoo>

0 comments on commit b4108ad

Please sign in to comment.