diff --git a/small_small_hr/migrations/0011_auto_20220131_1640.py b/small_small_hr/migrations/0011_auto_20220131_1640.py new file mode 100644 index 0000000..6773113 --- /dev/null +++ b/small_small_hr/migrations/0011_auto_20220131_1640.py @@ -0,0 +1,30 @@ +# Generated by Django 3.1.2 on 2022-01-31 13:40 + +from decimal import Decimal +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('small_small_hr', '0010_auto_20200626_1313'), + ] + + operations = [ + migrations.AlterField( + model_name='annualleave', + name='allowed_days', + field=models.DecimalField(blank=True, decimal_places=1, default=21, help_text='Number of leave days allowed in a year.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0.1'))], verbose_name='Allowed Leave days'), + ), + migrations.AlterField( + model_name='annualleave', + name='carried_over_days', + field=models.DecimalField(blank=True, decimal_places=1, default=0, help_text='Number of leave days carried over into this year.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0.1'))], verbose_name='Carried Over Leave days'), + ), + migrations.AlterField( + model_name='annualleave', + name='year', + field=models.PositiveIntegerField(choices=[(2017, 2017), (2018, 2018), (2019, 2019), (2020, 2020), (2021, 2021), (2022, 2022), (2023, 2023), (2024, 2024), (2025, 2025), (2026, 2026), (2027, 2027), (2028, 2028), (2029, 2029), (2030, 2030), (2031, 2031)], db_index=True, default=2017, verbose_name='Year'), + ), + ] diff --git a/small_small_hr/models.py b/small_small_hr/models.py index e17ee61..89efee2 100644 --- a/small_small_hr/models.py +++ b/small_small_hr/models.py @@ -6,6 +6,7 @@ from django.conf import settings from django.contrib.postgres.fields import JSONField from django.db import models +from django.core.validators import MinValueValidator from django.db.models import Q from django.utils import timezone from django.utils.functional import cached_property @@ -421,16 +422,22 @@ class AnnualLeave(TimeStampedModel, models.Model): leave_type = models.CharField( _("Type"), max_length=1, choices=Leave.TYPE_CHOICES, db_index=True ) - allowed_days = models.PositiveIntegerField( + allowed_days = models.DecimalField( _("Allowed Leave days"), default=21, blank=True, + decimal_places=1, + max_digits=12, + validators=[MinValueValidator(Decimal('0.1'))], help_text=_("Number of leave days allowed in a year."), ) - carried_over_days = models.PositiveIntegerField( + carried_over_days = models.DecimalField( _("Carried Over Leave days"), default=0, blank=True, + decimal_places=1, + max_digits=12, + validators=[MinValueValidator(Decimal('0.1'))], help_text=_("Number of leave days carried over into this year."), ) diff --git a/tests/test_forms.py b/tests/test_forms.py index 649be1d..0196c2a 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -96,6 +96,50 @@ def test_annual_leave_form(self): self.assertEqual(5, annual_leave.carried_over_days) self.assertEqual(Leave.REGULAR, annual_leave.leave_type) + def test_annual_leave_form_decimals(self): + """Test AnnualLeaveForm with decimal days.""" + user = mommy.make("auth.User", first_name="Bob", last_name="Ndoe") + staffprofile = mommy.make("small_small_hr.StaffProfile", user=user) + + request = self.factory.get("/") + request.session = {} + request.user = AnonymousUser() + + data = { + "staff": staffprofile.id, + "year": 2018, + "leave_type": Leave.REGULAR, + "allowed_days": 16.5, + "carried_over_days": 8.5, + } + + form = AnnualLeaveForm(data=data) + self.assertTrue(form.is_valid()) + annual_leave = form.save() + self.assertEqual(staffprofile, annual_leave.staff) + self.assertEqual(2018, annual_leave.year) + self.assertEqual(16.5, annual_leave.allowed_days) + self.assertEqual(8.5, annual_leave.carried_over_days) + self.assertEqual(Leave.REGULAR, annual_leave.leave_type) + + data2 = { + "staff": staffprofile.id, + "year": 2017, + "leave_type": Leave.REGULAR, + "allowed_days": 21, + "carried_over_days": 5, + } + + form = AnnualLeaveForm(data=data2, instance=annual_leave) + self.assertTrue(form.is_valid()) + form.save() + annual_leave.refresh_from_db() + self.assertEqual(staffprofile, annual_leave.staff) + self.assertEqual(2017, annual_leave.year) + self.assertEqual(21, annual_leave.allowed_days) + self.assertEqual(5, annual_leave.carried_over_days) + self.assertEqual(Leave.REGULAR, annual_leave.leave_type) + def test_role_form(self): """Test RoleForm.""" request = self.factory.get("/")