From 9f34a65a068e2a7920722143e604698930a92c65 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 12 Oct 2017 14:33:20 +0200
Subject: [PATCH] work in progress

---
 smash/web/api_views/appointment.py      |  1 -
 smash/web/api_views/subject.py          | 76 +++++++++++++++-----
 smash/web/templates/subjects/index.html | 95 ++++++++++++++++++++++++-
 3 files changed, 153 insertions(+), 19 deletions(-)

diff --git a/smash/web/api_views/appointment.py b/smash/web/api_views/appointment.py
index 227d8353..b8a7ce93 100644
--- a/smash/web/api_views/appointment.py
+++ b/smash/web/api_views/appointment.py
@@ -1,5 +1,4 @@
 import logging
-import traceback
 from datetime import datetime
 
 from django.contrib.auth.decorators import login_required
diff --git a/smash/web/api_views/subject.py b/smash/web/api_views/subject.py
index c1394a0d..bbda3f35 100644
--- a/smash/web/api_views/subject.py
+++ b/smash/web/api_views/subject.py
@@ -3,12 +3,14 @@ import logging
 from django.contrib.auth.decorators import login_required
 from django.http import JsonResponse
 
-from web.models import Subject
+from web.models import Subject, Visit, Appointment
 from web.models.constants import SUBJECT_TYPE_CHOICES
 from web.views import e500_error
-from web.views.notifications import get_subjects_with_no_visit, get_subjects_with_reminder
+from web.views.notifications import get_subjects_with_no_visit, get_subjects_with_reminder, get_today_midnight_date
 from web.views.subject import SUBJECT_LIST_GENERIC, SUBJECT_LIST_NO_VISIT, SUBJECT_LIST_REQUIRE_CONTACT
 
+from django.db.models import Count, Case, When, Min
+
 logger = logging.getLogger(__name__)
 
 
@@ -48,37 +50,45 @@ def get_subjects(request, type):
         raise TypeError("Unknown query type: " + type)
 
 
-def get_subjects_order(subjects, order_column, order_direction):
-    result = subjects
+def get_subjects_order(subjects_to_be_ordered, order_column, order_direction):
+    result = subjects_to_be_ordered
     if order_direction == "asc":
         order_direction = ""
     else:
         order_direction = "-"
     if order_column == "first_name":
-        result = subjects.order_by(order_direction + 'first_name')
+        result = subjects_to_be_ordered.order_by(order_direction + 'first_name')
     elif order_column == "last_name":
-        result = subjects.order_by(order_direction + 'last_name')
+        result = subjects_to_be_ordered.order_by(order_direction + 'last_name')
     elif order_column == "nd_number":
-        result = subjects.order_by(order_direction + 'nd_number')
+        result = subjects_to_be_ordered.order_by(order_direction + 'nd_number')
     elif order_column == "screening_number":
-        result = subjects.order_by(order_direction + 'screening_number')
+        result = subjects_to_be_ordered.order_by(order_direction + 'screening_number')
     elif order_column == "default_location":
-        result = subjects.order_by(order_direction + 'default_location')
+        result = subjects_to_be_ordered.order_by(order_direction + 'default_location')
     elif order_column == "dead":
-        result = subjects.order_by(order_direction + 'dead')
+        result = subjects_to_be_ordered.order_by(order_direction + 'dead')
     elif order_column == "resigned":
-        result = subjects.order_by(order_direction + 'resigned')
+        result = subjects_to_be_ordered.order_by(order_direction + 'resigned')
     elif order_column == "information_sent":
-        result = subjects.order_by(order_direction + 'information_sent')
+        result = subjects_to_be_ordered.order_by(order_direction + 'information_sent')
     elif order_column == "postponed":
-        result = subjects.order_by(order_direction + 'postponed')
+        result = subjects_to_be_ordered.order_by(order_direction + 'postponed')
     elif order_column == "type":
-        result = subjects.order_by(order_direction + 'type')
+        result = subjects_to_be_ordered.order_by(order_direction + 'type')
+    elif order_column == "visit_0":
+        result = subjects_to_be_ordered.annotate(visit_0=Min('visit__datetime_begin')).order_by(
+            order_direction + 'visit_0')
+    elif order_column == "visit_1":
+        result = subjects_to_be_ordered.annotate(visit_0=Min('visit__datetime_begin'))
+        result = result.filter(visit__datetime_begin__gt=Min('visit__datetime_begin'))
+        result = result.annotate(visit_1=Min('visit__datetime_begin')).order_by(order_direction + 'visit_1')
+        logger.info(result.query)
     return result
 
 
-def get_subjects_filtered(subjects, filters):
-    result = subjects
+def get_subjects_filtered(subjects_to_be_filtered, filters):
+    result = subjects_to_be_filtered
     for row in filters:
         column = row[0]
         value = row[1]
@@ -177,10 +187,43 @@ def get_yes_no(val):
         return "NO"
 
 
+def serialize_subject_visit(visit):
+    status = "---"
+    appointments = visit.appointment_set.filter()
+    pass
+
+
 def serialize_subject(subject):
     location = ""
     if subject.default_location is not None:
         location = subject.default_location.name
+    visits = Visit.objects.filter(subject=subject)
+    serialized_visits = []
+    for visit in visits:
+        if visit.datetime_begin < get_today_midnight_date():
+            if visit.is_finished:
+                finished_appointments_count = visit.appointment_set.filter(
+                    status=Appointment.APPOINTMENT_STATUS_FINISHED).count()
+                if finished_appointments_count > 0:
+                    status = "DONE"
+                else:
+                    status = "MISSED"
+            elif visit.datetime_end < get_today_midnight_date():
+                status = "EXCEEDED"
+            else:
+                scheduled_appointments_count = visit.appointment_set.filter(
+                    status=Appointment.APPOINTMENT_STATUS_SCHEDULED).count()
+                if scheduled_appointments_count > 0:
+                    status = "IN_PROGRESS"
+                else:
+                    status = "SHOULD_BE_IN_PROGRESS"
+        else:
+            status = "UPCOMING"
+        serialized_visits.append({
+            "status": status,
+            "datetime_start": visit.datetime_begin.strftime('%Y-%m-%d'),
+            "datetime_end": visit.datetime_end.strftime('%Y-%m-%d'),
+        })
 
     result = {
         "first_name": subject.first_name,
@@ -195,5 +238,6 @@ def serialize_subject(subject):
         "information_sent": get_yes_no(subject.information_sent),
         "type": subject.get_type_display(),
         "id": subject.id,
+        "visits": serialized_visits,
     }
     return result
diff --git a/smash/web/templates/subjects/index.html b/smash/web/templates/subjects/index.html
index 99bc0553..145672aa 100644
--- a/smash/web/templates/subjects/index.html
+++ b/smash/web/templates/subjects/index.html
@@ -32,8 +32,7 @@
             <tr>
                 <th>ND</th>
                 <th>Screening</th>
-                <th>First name
-                </th>
+                <th>First name</th>
                 <th>Last name</th>
                 <th>Default location</th>
                 <th>Deceased</th>
@@ -41,6 +40,14 @@
                 <th>Postponed</th>
                 <th>Info sent</th>
                 <th>Type</th>
+                <th>Visit 1</th>
+                <th>Visit 2</th>
+                <th>Visit 3</th>
+                <th>Visit 4</th>
+                <th>Visit 5</th>
+                <th>Visit 6</th>
+                <th>Visit 7</th>
+                <th>Visit 8</th>
                 <th>Edit</th>
             </tr>
             </thead>
@@ -76,6 +83,14 @@
                 <th>
                     <div name="type_filter">---</div>
                 </th>
+                <th/>
+                <th/>
+                <th/>
+                <th/>
+                <th/>
+                <th/>
+                <th/>
+                <th/>
             </tr>
             </tfoot>
 
@@ -128,6 +143,34 @@
 
         });
 
+        function create_visit_row(visit) {
+            var color = "white";
+            var text = "---";
+            if (visit !== undefined && visit !== null) {
+                if (visit.status === "DONE") {
+                    color = "green";
+                    text = "OK";
+                } else if (visit.status === "MISSED") {
+                    color = "pink";
+                    text = "MISSED";
+                } else if (visit.status === "UPCOMING") {
+                    color = "blue";
+                    text = "UPCOMING";
+                } else if (visit.status === "EXCEEDED") {
+                    color = "orange";
+                    text = "EXCEEDED";
+                } else if (visit.status === "SHOULD_BE_IN_PROGRESS") {
+                    color = "orange";
+                    text = "IN PROGRESS (NO APPOINTMENTS)";
+                } else if (visit.status === "IN_PROGRESS") {
+                    color = "lightgreen";
+                    text = "IN PROGRESS";
+                }
+                text += "<br/>" + visit.datetime_start + " - " + visit.datetime_end;
+            }
+            return "<div style='background-color:" + color + "';width:100%;height:100%>" + text + "</div>";
+        }
+
         $(function () {
             table = $('#table').DataTable({
                 pageLength: 25,
@@ -146,10 +189,58 @@
                     {"data": "postponed"},
                     {"data": "information_sent"},
                     {"data": "type"},
+                    {"data": "visit_0"},
+                    {"data": "visit_1"},
+                    {"data": null},
+                    {"data": null},
+                    {"data": null},
+                    {"data": null},
+                    {"data": null},
+                    {"data": null},
                     {"data": null}
                 ],
                 columnDefs: [{
                     "targets": 10,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[0]);
+                    }
+                }, {
+                    "targets": 11,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[1]);
+                    }
+                }, {
+                    "targets": 12,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[2]);
+                    }
+                }, {
+                    "targets": 13,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[3]);
+                    }
+                }, {
+                    "targets": 14,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[4]);
+                    }
+                }, {
+                    "targets": 15,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[5]);
+                    }
+                }, {
+                    "targets": 16,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[6]);
+                    }
+                }, {
+                    "targets": 17,
+                    "render": function (data, type, row, meta) {
+                        return create_visit_row(row.visits[7]);
+                    }
+                }, {
+                    "targets": 18,
                     "data": "id",
                     "defaultContent": '<a href="#" type="button" class="btn btn-block btn-default">Edit</a>'
                 }],
-- 
GitLab