Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 7f73eb56

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.service = None
26
        self.__set_time(begin_time)
27

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

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

    
58
    def init_free_time(self, length, begin_time):
59
        """ """
60
        self.type = "free"
61
        self.length = length
62
        self.__set_time(begin_time)
63

    
64

    
65
    def init_start_stop(self, title, time):
66
        """
67
        title: Arrivee ou Depart
68
        """
69
        self.type = "info"
70
        self.title = title
71
        self.__set_time(time)
72

    
73
def _add_free_time(delta, appointments, begin_time):
74
    if delta > 0:
75
        delta_minutes = delta / 60
76
        appointment = Appointment()
77
        appointment.init_free_time(delta_minutes, begin_time)
78
        appointments.append(appointment)
79
    return appointments
80

    
81
def get_daily_appointments(date, worker, service):
82
    """
83
    """
84
    appointments = []
85
    weekday_mapping = {
86
            '0': u'dimanche',
87
            '1': u'lundi',
88
            '2': u'mardi',
89
            '3': u'mercredi',
90
            '4': u'jeudi',
91
            '5': u'vendredi',
92
            '6': u'samedi'
93
            }
94
    weekday = weekday_mapping[date.strftime("%w")]
95
    time_tables = TimeTable.objects.filter(worker=worker).\
96
            filter(service=service).\
97
            filter(weekday=weekday).\
98
            filter(start_date__lte=date).\
99
            filter(Q(end_date=None) |Q(end_date__gte=date)).\
100
            order_by('start_date')
101

    
102
    appointments = []
103
    occurrences = Occurrence.objects.daily_occurrences(date, [worker]).order_by('start_time')
104
    for occurrence in occurrences:
105
        appointment = Appointment()
106
        appointment.init_from_occurrence(occurrence, service)
107
        appointments.append(appointment)
108
        # Find free times between occurrences in time_tables
109
        next_occurrences = Occurrence.objects.filter(start_time__gte=occurrence.end_time).order_by('start_time')
110
        if next_occurrences:
111
            next_occurrence = next_occurrences[0]
112
            if not Occurrence.objects.filter(end_time__gt=occurrence.end_time).filter(end_time__lt=next_occurrence.start_time):
113
                for time_table in time_tables:
114
                    start_time_table =  datetime(date.year, date.month, date.day,
115
                            time_table.start_time.hour, time_table.start_time.minute)
116
                    end_time_table =  datetime(date.year, date.month, date.day,
117
                            time_table.end_time.hour, time_table.end_time.minute)
118
                    if (occurrence.end_time >= start_time_table) and (next_occurrence.start_time < end_time_table):
119
                        delta = next_occurrence.start_time - occurrence.end_time
120
                        appointments = _add_free_time(delta.seconds, appointments,
121
                                time(occurrence.end_time.hour, occurrence.end_time.minute))
122

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

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

    
(4-4/10)