Project

General

Profile

Download (6.33 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / agenda / appointments.py @ a87d6ad7

1
# -*- coding: utf-8 -*-
2

    
3
from django.db.models import Q
4
from datetime import datetime, time
5

    
6
from calebasse.actes.models import EventAct
7
from calebasse.agenda.models import Occurrence
8
from calebasse.personnes.models import TimeTable
9

    
10
class Appointment(object):
11

    
12
    def __init__(self, title=None, begin_time=None, type=None,
13
            length=None, description=None, room=None, convocation_sent=None,
14
            service_name=None, patient_record_id=None, event_id=None):
15
        """ """
16
        self.title = title
17
        self.type = type
18
        self.length = length
19
        self.description = description
20
        self.room = room
21
        self.convocation_sent = None
22
        self.service_name = None
23
        self.patient_record_id = None
24
        self.event_id = None
25
        self.__set_time(begin_time)
26

    
27
    def __set_time(self, time):
28
        self.begin_time = time
29
        if time:
30
            self.begin_hour = time.strftime("%H:%M")
31
        else:
32
            self.begin_hour = None
33

    
34
    def init_from_occurrence(self, occurrence, service):
35
        """ """
36
        delta = occurrence.end_time - occurrence.start_time
37
        self.length = delta.seconds / 60
38
        self.title = occurrence.title
39
        self.__set_time(time(occurrence.start_time.hour, occurrence.start_time.minute))
40
        if service in occurrence.event.services.all():
41
            self.type = "busy-here"
42
        else:
43
            self.type = "busy-elsewhere"
44
            self.service_name = service.name
45
        self.event_id = occurrence.event.id
46
        if occurrence.event.room:
47
            self.room = occurrence.event.room.name
48
        self.description = occurrence.event.description
49
        if occurrence.event.event_type.label == 'patient_appointment':
50
            event_act = occurrence.event.eventact
51
            self.convocation_sent = event_act.convocation_sent
52
            self.patient_record_id = event_act.patient.id
53

    
54
    def init_free_time(self, length, begin_time):
55
        """ """
56
        self.type = "free"
57
        self.length = length
58
        self.__set_time(begin_time)
59

    
60

    
61
    def init_start_stop(self, title, time):
62
        """
63
        title: Arrivee ou Depart
64
        """
65
        self.type = "info"
66
        self.title = title
67
        self.__set_time(time)
68

    
69

    
70
def get_daily_appointments(date, worker, service):
71
    """
72
    """
73
    appointments = []
74
    weekday_mapping = {
75
            '0': u'dimanche',
76
            '1': u'lundi',
77
            '2': u'mardi',
78
            '3': u'mercredi',
79
            '4': u'jeudi',
80
            '5': u'vendredi',
81
            '6': u'samedi'
82
            }
83
    weekday = weekday_mapping[date.strftime("%w")]
84
    time_tables = TimeTable.objects.filter(worker=worker).\
85
            filter(service=service).\
86
            filter(weekday=weekday).\
87
            filter(start_date__lte=date).\
88
            filter(Q(end_date=None) |Q(end_date__gte=date)).\
89
            order_by('start_date')
90

    
91
    appointments = []
92
    occurrences = Occurrence.objects.daily_occurrences(date, [worker]).order_by('start_time')
93
    for occurrence in occurrences:
94
        appointment = Appointment()
95
        appointment.init_from_occurrence(occurrence, service)
96
        appointments.append(appointment)
97
        # Find free times between occurrences in time_tables
98
        next_occurrences = Occurrence.objects.filter(start_time__gte=occurrence.end_time).order_by('start_time')
99
        if next_occurrences:
100
            next_occurrence = next_occurrences[0]
101
            if not Occurrence.objects.filter(end_time__gt=occurrence.end_time).filter(end_time__lt=next_occurrence.start_time):
102
                for time_table in time_tables:
103
                    start_time_table =  datetime(date.year, date.month, date.day,
104
                            time_table.start_time.hour, time_table.start_time.minute)
105
                    end_time_table =  datetime(date.year, date.month, date.day,
106
                            time_table.end_time.hour, time_table.end_time.minute)
107
                    if (occurrence.end_time >= start_time_table) and (next_occurrence.start_time < end_time_table):
108
                        delta = next_occurrence.start_time - occurrence.end_time
109
                        if delta.seconds > 0:
110
                            delta_minutes = delta.seconds / 60
111
                            appointment = Appointment()
112
                            appointment.init_free_time(delta_minutes, time(occurrence.end_time.hour, occurrence.end_time.minute))
113
                            appointments.append(appointment)
114

    
115
    for time_table in time_tables:
116
        appointment = Appointment()
117
        appointment.init_start_stop(u"Arrivée",
118
            time(time_table.start_time.hour, time_table.start_time.minute))
119
        appointments.append(appointment)
120
        appointment = Appointment()
121
        appointment.init_start_stop(u"Départ",
122
            time(time_table.end_time.hour, time_table.end_time.minute))
123
        appointments.append(appointment)
124
        start_datetime = datetime(date.year, date.month, date.day,
125
            time_table.start_time.hour, time_table.start_time.minute)
126
        end_datetime = datetime(date.year, date.month, date.day,
127
            time_table.end_time.hour, time_table.end_time.minute)
128
        smallest = Occurrence.objects.smallest_start_in_range(start_datetime, end_datetime, [worker])
129
        biggest = Occurrence.objects.biggest_end_in_range(start_datetime, end_datetime, [worker])
130
        if not smallest and not biggest:
131
            delta = end_datetime - start_datetime
132
            delta = delta.seconds / 60
133
            appointment = Appointment()
134
            appointment.init_free_time(delta,
135
                    time(start_datetime.hour, start_datetime.minute))
136
            appointments.append(appointment)
137
        if smallest:
138
            delta = smallest.start_time - start_datetime
139
            delta = delta.seconds / 60
140
            appointment = Appointment()
141
            appointment.init_free_time(delta,
142
                    time(start_datetime.hour, start_datetime.minute))
143
            appointments.append(appointment)
144
        if biggest:
145
            delta = end_datetime - biggest.end_time
146
            delta = delta.seconds / 60
147
            appointment = Appointment()
148
            appointment.init_free_time(delta,
149
                    time(biggest.end_time.hour, biggest.end_time.minute))
150
            appointments.append(appointment)
151

    
152
    appointments = sorted(appointments, key=lambda app: app.begin_time)
153
    return appointments
154

    
(4-4/10)