From 9edfed086129f06b1e668e8e25d3d7d44b38be48 Mon Sep 17 00:00:00 2001
From: Rithvik Nishad <rithvik.nishad@egovernments.org>
Date: Tue, 28 May 2024 16:10:39 +0530
Subject: [PATCH] Adds support to specify and filter ration card category of a
 patient (#2201)

* Adds support to specify and filter ration card category of a patient

* Show ration card category in discharge summary

---------

Co-authored-by: Vignesh Hari <vichuhari100@gmail.com>
---
 care/facility/api/viewsets/patient.py         |  3 +-
 ...istration_ration_card_category_and_more.py | 38 +++++++++++++++++++
 care/facility/models/patient.py               | 11 +++++-
 .../patient_discharge_summary_pdf.html        |  3 ++
 care/utils/tests/test_utils.py                |  2 +
 5 files changed, 55 insertions(+), 2 deletions(-)
 create mode 100644 care/facility/migrations/0439_historicalpatientregistration_ration_card_category_and_more.py

diff --git a/care/facility/api/viewsets/patient.py b/care/facility/api/viewsets/patient.py
index 36748513ec..f61b7ffb0e 100644
--- a/care/facility/api/viewsets/patient.py
+++ b/care/facility/api/viewsets/patient.py
@@ -73,7 +73,7 @@
     ConditionVerificationStatus,
 )
 from care.facility.models.notification import Notification
-from care.facility.models.patient import PatientNotesEdit
+from care.facility.models.patient import PatientNotesEdit, RationCardCategory
 from care.facility.models.patient_base import (
     DISEASE_STATUS_DICT,
     NewDischargeReasonEnum,
@@ -124,6 +124,7 @@ class PatientFilterSet(filters.FilterSet):
         method="filter_by_category",
         choices=CATEGORY_CHOICES,
     )
+    ration_card_category = filters.ChoiceFilter(choices=RationCardCategory.choices)
 
     def filter_by_category(self, queryset, name, value):
         if value:
diff --git a/care/facility/migrations/0439_historicalpatientregistration_ration_card_category_and_more.py b/care/facility/migrations/0439_historicalpatientregistration_ration_card_category_and_more.py
new file mode 100644
index 0000000000..e40f20b2cc
--- /dev/null
+++ b/care/facility/migrations/0439_historicalpatientregistration_ration_card_category_and_more.py
@@ -0,0 +1,38 @@
+# Generated by Django 4.2.8 on 2024-05-28 05:52
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("facility", "0438_alter_dailyround_patient_category_and_more"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="historicalpatientregistration",
+            name="ration_card_category",
+            field=models.CharField(
+                choices=[
+                    ("NO_CARD", "Non-card holder"),
+                    ("BPL", "BPL"),
+                    ("APL", "APL"),
+                ],
+                max_length=8,
+                null=True,
+            ),
+        ),
+        migrations.AddField(
+            model_name="patientregistration",
+            name="ration_card_category",
+            field=models.CharField(
+                choices=[
+                    ("NO_CARD", "Non-card holder"),
+                    ("BPL", "BPL"),
+                    ("APL", "APL"),
+                ],
+                max_length=8,
+                null=True,
+            ),
+        ),
+    ]
diff --git a/care/facility/models/patient.py b/care/facility/models/patient.py
index 94a06f0f5f..9c30e76914 100644
--- a/care/facility/models/patient.py
+++ b/care/facility/models/patient.py
@@ -7,6 +7,7 @@
 from django.db.models import Case, F, Func, JSONField, Value, When
 from django.db.models.functions import Coalesce, Now
 from django.utils import timezone
+from django.utils.translation import gettext_lazy as _
 from simple_history.models import HistoricalRecords
 
 from care.abdm.models import AbhaNumber
@@ -43,6 +44,12 @@
 from care.utils.models.validators import mobile_or_landline_number_validator
 
 
+class RationCardCategory(models.TextChoices):
+    NON_CARD_HOLDER = "NO_CARD", _("Non-card holder")
+    BPL = "BPL", _("BPL")
+    APL = "APL", _("APL")
+
+
 class PatientRegistration(PatientBaseModel, PatientPermissionMixin):
     # fields in the PatientSearch model
     PATIENT_SEARCH_KEYS = [
@@ -140,7 +147,9 @@ class TestTypeEnum(enum.Enum):
         default="",
         verbose_name="Passport Number of Foreign Patients",
     )
-    # aadhar_no = models.CharField(max_length=255, default="", verbose_name="Aadhar Number of Patient")
+    ration_card_category = models.CharField(
+        choices=RationCardCategory.choices, null=True, max_length=8
+    )
 
     is_medical_worker = models.BooleanField(
         default=False, verbose_name="Is the Patient a Medical Worker"
diff --git a/care/templates/reports/patient_discharge_summary_pdf.html b/care/templates/reports/patient_discharge_summary_pdf.html
index 06c7969b10..b5b1594b64 100644
--- a/care/templates/reports/patient_discharge_summary_pdf.html
+++ b/care/templates/reports/patient_discharge_summary_pdf.html
@@ -66,6 +66,9 @@ <h3 class="text-lg text-gray-900">
       <p class="flex-grow">
         <span class="text-sm text-gray-500">Address:</span> {{patient.address}}
       </p>
+      <p class="flex-grow">
+        <span class="text-sm text-gray-500">Ration Card Category:</span> {{patient.get_ration_card_category_display|field_name_to_label}}
+      </p>
     </div>
   </div>
 
diff --git a/care/utils/tests/test_utils.py b/care/utils/tests/test_utils.py
index 465fa74c87..20fbfee7d2 100644
--- a/care/utils/tests/test_utils.py
+++ b/care/utils/tests/test_utils.py
@@ -31,6 +31,7 @@
     ConsultationDiagnosis,
     ICD11Diagnosis,
 )
+from care.facility.models.patient import RationCardCategory
 from care.users.models import District, State
 
 
@@ -275,6 +276,7 @@ def get_patient_data(cls, district, state) -> dict:
             "date_of_receipt_of_information": make_aware(
                 datetime(2020, 4, 1, 15, 30, 00)
             ),
+            "ration_card_category": RationCardCategory.NON_CARD_HOLDER,
         }
 
     @classmethod