-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from moshthepitt/issue-1
Add utils to create annual leave objects
- Loading branch information
Showing
9 changed files
with
258 additions
and
10 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
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,57 @@ | ||
""" | ||
Utils module for small small hr | ||
""" | ||
from django.conf import settings | ||
|
||
from small_small_hr.models import AnnualLeave, Leave | ||
|
||
MAX_CARRY_OVER = getattr(settings, 'SSHR_MAX_CARRY_OVER', 10) | ||
|
||
|
||
def get_carry_over(staffprofile: object, year: int, leave_type: str): | ||
""" | ||
Get carried over leave days | ||
""" | ||
# pylint: disable=no-member | ||
if leave_type == Leave.REGULAR: | ||
previous_obj = AnnualLeave.objects.filter( | ||
staff=staffprofile, year=year-1, leave_type=leave_type).first() | ||
if previous_obj: | ||
remaining = previous_obj.get_available_leave_days() | ||
max_carry_over = MAX_CARRY_OVER | ||
if remaining > max_carry_over: | ||
carry_over = max_carry_over | ||
else: | ||
carry_over = remaining | ||
|
||
return carry_over | ||
|
||
return 0 | ||
|
||
|
||
def create_annual_leave(staffprofile: object, year: int, leave_type: str): | ||
""" | ||
Creates an annuall leave object for the staff member | ||
""" | ||
# pylint: disable=no-member | ||
try: | ||
annual_leave = AnnualLeave.objects.get( | ||
staff=staffprofile, year=year, leave_type=leave_type) | ||
except AnnualLeave.DoesNotExist: | ||
carry_over = get_carry_over(staffprofile, year, leave_type) | ||
|
||
if leave_type == Leave.REGULAR: | ||
allowed_days = staffprofile.leave_days | ||
elif leave_type == Leave.SICK: | ||
allowed_days = staffprofile.sick_days | ||
|
||
annual_leave = AnnualLeave( | ||
staff=staffprofile, | ||
year=year, | ||
leave_type=leave_type, | ||
allowed_days=allowed_days, | ||
carried_over_days=carry_over | ||
) | ||
annual_leave.save() | ||
|
||
return annual_leave |
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
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,104 @@ | ||
""" | ||
Module to test small_small_hr Signals | ||
""" | ||
from datetime import datetime | ||
|
||
from django.conf import settings | ||
from django.test import TestCase | ||
|
||
import pytz | ||
from model_mommy import mommy | ||
|
||
from small_small_hr.models import Leave, StaffProfile | ||
from small_small_hr.utils import create_annual_leave, get_carry_over | ||
|
||
|
||
class TestUtils(TestCase): | ||
""" | ||
Test class for utils | ||
""" | ||
|
||
def test_get_carry_over(self): | ||
""" | ||
Test get_carry_over | ||
""" | ||
user = mommy.make('auth.User', id=23) | ||
StaffProfile.objects.all().delete() | ||
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) | ||
|
||
self.assertEqual( | ||
0, | ||
get_carry_over(staffprofile, 2017, Leave.REGULAR) | ||
) | ||
|
||
create_annual_leave(staffprofile, 2016, Leave.REGULAR) | ||
|
||
# carry over should be 10 because the balance is 21 | ||
self.assertEqual( | ||
10, | ||
get_carry_over(staffprofile, 2017, Leave.REGULAR) | ||
) | ||
|
||
# 12 days of leave | ||
start = datetime( | ||
2016, 6, 5, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE)) | ||
end = datetime( | ||
2016, 6, 16, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE)) | ||
|
||
mommy.make('small_small_hr.Leave', leave_type=Leave.REGULAR, | ||
start=start, end=end, status=Leave.APPROVED, | ||
staff=staffprofile) | ||
|
||
# carry over should be 9 => 21-12 | ||
self.assertEqual( | ||
9, | ||
get_carry_over(staffprofile, 2017, Leave.REGULAR) | ||
) | ||
|
||
# no sick leave carry over | ||
create_annual_leave(staffprofile, 2016, Leave.SICK) | ||
|
||
self.assertEqual( | ||
0, | ||
get_carry_over(staffprofile, 2017, Leave.SICK) | ||
) | ||
|
||
def test_create_annual_leave(self): | ||
""" | ||
Test create_annual_leave | ||
""" | ||
user = mommy.make('auth.User', id=56) | ||
StaffProfile.objects.all().delete() | ||
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) | ||
|
||
obj = create_annual_leave(staffprofile, 2016, Leave.REGULAR) | ||
|
||
self.assertEqual(staffprofile, obj.staff) | ||
self.assertEqual(2016, obj.year) | ||
self.assertEqual(0, obj.carried_over_days) | ||
self.assertEqual(21, obj.allowed_days) | ||
self.assertEqual(Leave.REGULAR, obj.leave_type) | ||
|
||
# 12 days of leave | ||
start = datetime( | ||
2016, 6, 5, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE)) | ||
end = datetime( | ||
2016, 6, 16, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE)) | ||
|
||
mommy.make('small_small_hr.Leave', leave_type=Leave.REGULAR, | ||
start=start, end=end, status=Leave.APPROVED, | ||
staff=staffprofile) | ||
|
||
obj2 = create_annual_leave(staffprofile, 2017, Leave.REGULAR) | ||
self.assertEqual(staffprofile, obj2.staff) | ||
self.assertEqual(2017, obj2.year) | ||
self.assertEqual(9, obj2.carried_over_days) | ||
self.assertEqual(21, obj2.allowed_days) | ||
self.assertEqual(Leave.REGULAR, obj2.leave_type) | ||
|
||
obj3 = create_annual_leave(staffprofile, 2018, Leave.SICK) | ||
self.assertEqual(staffprofile, obj3.staff) | ||
self.assertEqual(2018, obj3.year) | ||
self.assertEqual(0, obj3.carried_over_days) | ||
self.assertEqual(10, obj3.allowed_days) | ||
self.assertEqual(Leave.SICK, obj3.leave_type) |