Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 41dfa4b8

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):
14
        """ """
15
        self.title = title
16
        self.type = type
17
        self.length = length
18
        self.description = description
19
        self.room = room
20
        self.convocation_sent = None
21
        self.service_name = None
22
        self.patient_record_id = None
23
        self.event_id = None
24
        self.service = None
25
        self.workers_initial = None
26
        self.act_type = None
27
        self.__set_time(begin_time)
28

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

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

    
65
    def init_free_time(self, length, begin_time):
66
        """ """
67
        self.type = "free"
68
        self.length = length
69
        self.__set_time(begin_time)
70

    
71

    
72
    def init_start_stop(self, title, time):
73
        """
74
        title: Arrivee ou Depart
75
        """
76
        self.type = "info"
77
        self.title = title
78
        self.__set_time(time)
79

    
80
def _add_free_time(delta, appointments, begin_time):
81
    if delta > 0:
82
        delta_minutes = delta / 60
83
        appointment = Appointment()
84
        appointment.init_free_time(delta_minutes, begin_time)
85
        appointments.append(appointment)
86
    return appointments
87

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

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

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

    
158
    appointments = sorted(appointments, key=lambda app: app.begin_time)
159
    return appointments
160

    
(4-4/10)