Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
from . import company | ||
from . import hr_holidays | ||
from . import hr_holidays_status | ||
from . import hr_timesheet_sheet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding: utf-8 -*- | ||
# © 2018 Therp BV <https://therp.nl> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
from openerp import api, models | ||
|
||
|
||
class HrTimesheetSheet(models.Model): | ||
_inherit = 'hr_timesheet_sheet.sheet' | ||
|
||
@api.multi | ||
def write(self, vals): | ||
result = super(HrTimesheetSheet, self).write(vals) | ||
if vals.get('state') == 'done': | ||
self._timesheet_approved() | ||
return result | ||
|
||
@api.multi | ||
def _timesheet_approved(self): | ||
requests = [] | ||
for this in self: | ||
holiday2hours = {} | ||
for line in this.mapped('timesheet_ids'): | ||
holiday = line.account_id.holiday_status_ids[:1] | ||
if not holiday or line.leave_id: | ||
continue | ||
holiday2hours.setdefault(holiday, 0.0) | ||
holiday2hours[holiday] += line.unit_amount | ||
for holiday, hours in holiday2hours.iteritems(): | ||
request = self.env['hr.holidays'].sudo(this.user_id).create({ | ||
'name': '%s: %s' % (this.display_name, holiday.name), | ||
'user_id': this.user_id.id, | ||
'date_from': this.date_from, | ||
'date_to': this.date_to, | ||
'holiday_status_id': holiday.id, | ||
'number_of_days_temp': | ||
hours / self.env['hr.holidays']._get_hours_per_day( | ||
this.employee_id.company_id, this.employee_id, | ||
), | ||
'type': 'remove', | ||
'holiday_type': 'employee', | ||
}) | ||
request.signal_workflow('confirm') | ||
request.sudo(self.env.user).signal_workflow('validate') | ||
request.sudo(self.env.user).signal_workflow('second_validate') | ||
# circumvent all constraints here | ||
self.env.cr.execute( | ||
'update hr_analytic_timesheet ' | ||
'set leave_id=%s where id in %s', | ||
(request.id, tuple(this.mapped('timesheet_ids').ids)), | ||
) | ||
requests.append(request) | ||
return requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,127 @@ | ||
# -*- coding: utf-8 -*- | ||
# © 2016 Sunflower IT (http://sunflowerweb.nl) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
import time | ||
|
||
from openerp.tests.common import TransactionCase | ||
from openerp.exceptions import ValidationError | ||
|
||
|
||
class TimesheetHolidayTest(TransactionCase): | ||
def setUp(self): | ||
super(TimesheetHolidayTest, self).setUp() | ||
self.leave = self.env['hr.holidays'] | ||
self.account = self.env['account.analytic.account'] | ||
|
||
# Create a test customer | ||
def test_all(self): | ||
# Working day is 7 hours per day | ||
self.env.ref('base.main_company') \ | ||
.timesheet_hours_per_day = 7.0 | ||
|
||
# Create analytic account | ||
account = self.account.create({ | ||
self.account = self.env['account.analytic.account'].create({ | ||
'name': 'Sick Leaves', | ||
'is_leave_account': True | ||
'is_leave_account': True, | ||
'type': 'normal', | ||
}) | ||
|
||
# Link sick leave to analytic account | ||
sl = self.env.ref('hr_holidays.holiday_status_sl') | ||
sl.write({ | ||
'analytic_account_id': account.id | ||
self.sl = self.env.ref('hr_holidays.holiday_status_sl') | ||
self.sl.write({ | ||
'analytic_account_id': self.account.id | ||
}) | ||
|
||
def test_leave(self): | ||
# Create sick leave for Pieter Parker | ||
leave = self.leave.create({ | ||
leave = self.env['hr.holidays'].create({ | ||
'name': 'One week sick leave', | ||
'holiday_status_id': sl.id, | ||
'holiday_status_id': self.sl.id, | ||
'date_from': time.strftime('%Y-%m-06'), | ||
'date_to': time.strftime('%Y-%m-12'), | ||
'number_of_days_temp': 7.0, | ||
'employee_id': self.env.ref('hr.employee_fp').id, | ||
}) | ||
|
||
# Confirm leave and check hours added to account | ||
hours_before = sum(account.line_ids.mapped('amount')) | ||
hours_before = sum(self.account.line_ids.mapped('amount')) | ||
leave.signal_workflow('confirm') | ||
leave.signal_workflow('validate') | ||
leave.signal_workflow('second_validate') | ||
hours_after = sum(account.line_ids.mapped('unit_amount')) | ||
hours_after = sum(self.account.line_ids.mapped('unit_amount')) | ||
self.assertEqual(hours_after - hours_before, 35.0) | ||
|
||
# Test editing of lines forbidden | ||
self.assertRaises(ValidationError, account.line_ids[0].write, { | ||
self.assertRaises(ValidationError, self.account.line_ids[0].write, { | ||
'unit_amount': 5.0 | ||
}) | ||
|
||
# Test force editing of lines allowed | ||
account.line_ids[0].with_context(force_write=True).write({ | ||
self.account.line_ids[0].with_context(force_write=True).write({ | ||
'unit_amount': 5.0 | ||
}) | ||
hours_after = sum(account.line_ids.mapped('unit_amount')) | ||
hours_after = sum(self.account.line_ids.mapped('unit_amount')) | ||
self.assertEqual(hours_after - hours_before, 33.0) | ||
|
||
# Refuse leave and check hours removed from account | ||
leave.signal_workflow('refuse') | ||
hours_final = sum(account.line_ids.mapped('unit_amount')) | ||
hours_final = sum(self.account.line_ids.mapped('unit_amount')) | ||
self.assertEqual(hours_final, hours_before) | ||
|
||
def test_analytic(self): | ||
manager = self.env.ref('hr.employee_fp') | ||
employee = self.env.ref('hr.employee_qdp') | ||
employee.write(employee.default_get(['product_id', 'journal_id'])) | ||
|
||
# create timesheet line with 6.0 hours of sick leave | ||
line = self.env['hr.analytic.timesheet'].sudo( | ||
employee.user_id, | ||
).default_get([ | ||
'general_account_id', 'journal_id', 'name', 'product_id', | ||
'product_uom_id', | ||
]) | ||
line.update(self.env['hr.analytic.timesheet'].on_change_account_id( | ||
self.account.id, employee.user_id.id | ||
)['value']) | ||
line.update( | ||
account_id=self.account.id, | ||
date=time.strftime('%Y-%m-06'), | ||
name='/', | ||
unit_amount=6, | ||
) | ||
|
||
# create and confirm a timesheet with the 6.0 hours line | ||
sheet = self.env['hr_timesheet_sheet.sheet'].sudo( | ||
employee.user_id | ||
).create({ | ||
'employee_id': employee.id, | ||
'date_from': time.strftime('%Y-%m-06'), | ||
'date_to': time.strftime('%Y-%m-12'), | ||
'timesheet_ids': [(0, 0, line)], | ||
}) | ||
sheet.signal_workflow('confirm') | ||
self.assertEqual(sheet.state, 'confirm') | ||
|
||
# fully approve timesheet | ||
leaves_taken_before = self.account.holiday_status_ids.sudo( | ||
employee.user_id | ||
).leaves_taken | ||
sheet.sudo(manager.user_id).signal_workflow('done') | ||
self.assertEqual(sheet.state, 'done') | ||
|
||
# assert that total leave has increases | ||
leaves_taken_after = self.account.holiday_status_ids.sudo( | ||
employee.user_id | ||
).leaves_taken | ||
self.assertAlmostEqual( | ||
leaves_taken_before + 6.0 / 7.0, | ||
leaves_taken_after, | ||
2 | ||
) | ||
|
||
# resubmit timesheet with 8.0 hours of leave | ||
# sheet.create_workflow() | ||
# self.assertEquals(sheet.state, 'draft') | ||
# sheet.timesheet_ids.write(dict(unit_amount=8)) | ||
# sheet.signal_workflow('confirm') | ||
# sheet.signal_workflow('done') | ||
# self.assertEqual(sheet.state, 'done') | ||
# self.assertAlmostEqual( | ||
# leaves_taken_before + 8.0 / 7.0, | ||
# leaves_taken_after, | ||
# 2 | ||
# ) |