From f9f7f6000414d596dcecf8763b06bf0782c01e52 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 13 Mar 2017 15:33:39 +0100
Subject: [PATCH] vist has field with information about post mail sent

---
 smash/web/forms.py  |  2 ++
 smash/web/models.py |  6 +++++-
 smash/web/views.py  | 20 ++++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/smash/web/forms.py b/smash/web/forms.py
index 663f6886..2522e94b 100644
--- a/smash/web/forms.py
+++ b/smash/web/forms.py
@@ -135,6 +135,8 @@ class VisitDetailForm(ModelForm):
         widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d")
     )
 
+    post_mail_sent = forms.RadioSelect()
+
     class Meta:
         model = Visit
         exclude = ['is_finished']
diff --git a/smash/web/models.py b/smash/web/models.py
index 3567021a..18f2f5ae 100644
--- a/smash/web/models.py
+++ b/smash/web/models.py
@@ -479,7 +479,11 @@ class Visit(models.Model):
         verbose_name='Has ended',
         default=False
     )
-
+    BOOL_CHOICES = ((True, 'Yes'), (False, 'No'))
+    post_mail_sent  = models.BooleanField(choices=BOOL_CHOICES,
+        verbose_name='Post mail sent',
+        default=False
+    )
     appointment_types = models.ManyToManyField(AppointmentType,
         verbose_name='Requested appointments',
         blank=True,
diff --git a/smash/web/views.py b/smash/web/views.py
index f8604523..7d0707ee 100644
--- a/smash/web/views.py
+++ b/smash/web/views.py
@@ -185,6 +185,14 @@ def get_approaching_visits_without_appointments_count(user):
 		type = 'web.views.approaching_visits_without_appointments')
 	return notification
 
+def get_approaching_visits_for_mail_contact_count(user):
+	notification = NotificationCount(
+		title = "post mail for approaching visits",
+		count = get_approaching_visits_for_mail_contact(user).count(),
+		style = "fa fa-users text-aqua",
+		type = 'web.views.approaching_visits_for_mail_contact')
+	return notification
+
 def get_approaching_visits_without_appointments(user):
 	today = get_today_midnight_date()
 	today_plus_two_months =today+datetime.timedelta(days=62)
@@ -195,6 +203,18 @@ def get_approaching_visits_without_appointments(user):
 		subject__default_location__in = get_filter_locations(user),
 		my_count=0)
 
+def get_approaching_visits_for_mail_contact(user):
+	today = get_today_midnight_date()
+	today_plus_three_months =today+datetime.timedelta(days=91)
+	today_plus_six_months =today+datetime.timedelta(days=183)
+	return Visit.objects.annotate(my_count=Count(Case(When(appointment__status=Appointment.APPOINTMENT_STATUS_SCHEDULED, then=1)))).filter(
+		datetime_begin__gt = today_plus_three_months,
+		datetime_begin__lt = today_plus_six_months,
+		is_finished = False,
+		post_mail_sent = False,
+		subject__default_location__in = get_filter_locations(user),
+		my_count=0)
+
 def get_unfinished_appointments_count(user):
 	return NotificationCount(
 		title = "unfinished appointments ",
-- 
GitLab