From add91c0a59b44dcea44799852c4515490cc81990 Mon Sep 17 00:00:00 2001
From: Kelvin Jayanoris <kjayanoris@gmail.com>
Date: Mon, 31 Jan 2022 15:33:06 +0300
Subject: [PATCH 1/3] Use models.DecimalField second

---
 small_small_hr/models.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

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."),
     )
 

From 5978d6e52cc605e0f2dd0a54048462ce8d5577f6 Mon Sep 17 00:00:00 2001
From: Kelvin Jayanoris <kjayanoris@gmail.com>
Date: Mon, 31 Jan 2022 16:51:35 +0300
Subject: [PATCH 2/3] Add new migration

---
 .../migrations/0011_auto_20220131_1640.py     | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 small_small_hr/migrations/0011_auto_20220131_1640.py

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'),
+        ),
+    ]

From f00aad296df6961d429149fff95ecb1e005a7f87 Mon Sep 17 00:00:00 2001
From: Kelvin Jayanoris <kjayanoris@gmail.com>
Date: Mon, 31 Jan 2022 16:55:22 +0300
Subject: [PATCH 3/3] Add test for decimals

---
 tests/test_forms.py | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

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("/")