From 21338460b5a8b50757e55add9eea8bf12d0a436d Mon Sep 17 00:00:00 2001
From: Ashesh <3626859+Ashesh3@users.noreply.github.com>
Date: Thu, 11 Jan 2024 14:53:26 +0530
Subject: [PATCH] Fix patient discharge_reason filter and add it to export
 (#1818)

---
 care/facility/api/viewsets/patient.py | 8 ++------
 care/facility/models/patient.py       | 7 ++++---
 care/facility/models/patient_base.py  | 1 +
 3 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/care/facility/api/viewsets/patient.py b/care/facility/api/viewsets/patient.py
index 7608d177fe..73b2563a48 100644
--- a/care/facility/api/viewsets/patient.py
+++ b/care/facility/api/viewsets/patient.py
@@ -180,12 +180,8 @@ def filter_by_bed_type(self, queryset, name, value):
         field_name="last_consultation__current_bed__bed__bed_type",
         choice_dict=REVERSE_BED_TYPES,
     )
-    last_consultation_discharge_reason = filters.ChoiceFilter(
-        field_name="last_consultation__discharge_reason",
-        choices=DISCHARGE_REASON_CHOICES,
-    )
-    last_consultation_discharge_reason = filters.ChoiceFilter(
-        field_name="last_consultation__discharge_reason",
+    last_consultation__new_discharge_reason = filters.ChoiceFilter(
+        field_name="last_consultation__new_discharge_reason",
         choices=NewDischargeReasonEnum.choices,
     )
     last_consultation_assigned_to = filters.NumberFilter(
diff --git a/care/facility/models/patient.py b/care/facility/models/patient.py
index 0852d392b5..4085a5da5b 100644
--- a/care/facility/models/patient.py
+++ b/care/facility/models/patient.py
@@ -31,7 +31,7 @@
     BLOOD_GROUP_CHOICES,
     DISEASE_STATUS_CHOICES,
     REVERSE_CATEGORY_CHOICES,
-    REVERSE_DISCHARGE_REASON_CHOICES,
+    REVERSE_NEW_DISCHARGE_REASON_CHOICES,
     REVERSE_ROUTE_TO_FACILITY_CHOICES,
 )
 from care.facility.models.patient_consultation import PatientConsultation
@@ -527,6 +527,7 @@ def annotate_diagnosis_ids(*args, **kwargs):
         # Last Consultation Details
         "last_consultation__suggestion": "Decision after consultation",
         "last_consultation__category": "Category",
+        "last_consultation__new_discharge_reason": "Reason for discharge",
         "last_consultation__discharge_date": "Date of discharge",
         "last_consultation__discharge_date__time": "Time of discharge",
     }
@@ -559,8 +560,8 @@ def format_diagnoses(diagnosis_ids):
             lambda x: REVERSE_ROUTE_TO_FACILITY_CHOICES.get(x, "-")
         ),
         "last_consultation__category": lambda x: REVERSE_CATEGORY_CHOICES.get(x, "-"),
-        "last_consultation__discharge_reason": (
-            lambda x: REVERSE_DISCHARGE_REASON_CHOICES.get(x, "-")
+        "last_consultation__new_discharge_reason": (
+            lambda x: REVERSE_NEW_DISCHARGE_REASON_CHOICES.get(x, "-")
         ),
         "last_consultation__discharge_date": format_as_date,
         "last_consultation__discharge_date__time": format_as_time,
diff --git a/care/facility/models/patient_base.py b/care/facility/models/patient_base.py
index cc05c59766..dd6c6b73bd 100644
--- a/care/facility/models/patient_base.py
+++ b/care/facility/models/patient_base.py
@@ -146,3 +146,4 @@ class BedType(enum.Enum):
 REVERSE_BED_TYPE_CHOICES = reverse_choices(BedTypeChoices)
 REVERSE_ROUTE_TO_FACILITY_CHOICES = reverse_choices(RouteToFacility.choices)
 REVERSE_DISCHARGE_REASON_CHOICES = reverse_choices(DISCHARGE_REASON_CHOICES)
+REVERSE_NEW_DISCHARGE_REASON_CHOICES = reverse_choices(NEW_DISCHARGE_REASON_CHOICES)