1
|
# -*- coding: utf-8 -*-
|
2
|
|
3
|
from datetime import datetime, time
|
4
|
from datetime import time as datetime_time
|
5
|
|
6
|
from interval import Interval, IntervalSet
|
7
|
|
8
|
from calebasse.actes.validation_states import VALIDATION_STATES
|
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.is_recurrent = False
|
21
|
self.is_billed = False
|
22
|
self.convocation_sent = None
|
23
|
self.other_services_names = []
|
24
|
self.patient = None
|
25
|
self.patient_record_id = None
|
26
|
self.patient_record_paper_id = None
|
27
|
self.event_id = None
|
28
|
self.event_type = None
|
29
|
self.workers = None
|
30
|
self.workers_initial = None
|
31
|
self.workers_codes = None
|
32
|
self.act_state = None
|
33
|
self.act_absence = None
|
34
|
self.weight = 0
|
35
|
self.act_type = None
|
36
|
self.validation = None
|
37
|
self.holiday = False
|
38
|
self.services_names = []
|
39
|
self.__set_time(begin_time)
|
40
|
|
41
|
def __set_time(self, time):
|
42
|
self.begin_time = time
|
43
|
if time:
|
44
|
self.begin_hour = time.strftime("%H:%M")
|
45
|
else:
|
46
|
self.begin_hour = None
|
47
|
|
48
|
def init_from_event(self, event, service, validation_states=None):
|
49
|
delta = event.end_datetime - event.start_datetime
|
50
|
self.event_id = event.id
|
51
|
self.length = delta.seconds / 60
|
52
|
self.title = event.title
|
53
|
if hasattr(event, 'parent') and event.parent.recurrence_periodicity:
|
54
|
self.is_recurrent = True
|
55
|
services = event.services.all()
|
56
|
self.date = event.start_datetime.date()
|
57
|
self.__set_time(time(event.start_datetime.hour, event.start_datetime.minute))
|
58
|
for e_service in services:
|
59
|
name = e_service.name.lower().replace(' ', '-')
|
60
|
if e_service != service:
|
61
|
self.other_services_names.append(name)
|
62
|
self.services_names.append(name)
|
63
|
if service in services:
|
64
|
self.type = "busy-here"
|
65
|
else:
|
66
|
self.type = "busy-elsewhere"
|
67
|
self.event_id = event.id
|
68
|
if event.room:
|
69
|
self.room = event.room.name
|
70
|
self.description = event.description
|
71
|
self.workers_initial = ""
|
72
|
self.workers_code = []
|
73
|
if event.event_type.id == 1:
|
74
|
self.workers = event.participants.all()
|
75
|
self.convocation_sent = event.convocation_sent
|
76
|
self.patient = event.patient
|
77
|
self.patient_record_id = event.patient.id
|
78
|
self.patient_record_paper_id = event.patient.paper_id
|
79
|
self.act_type = event.act_type.name
|
80
|
self.is_billed = event.act.is_billed
|
81
|
state = event.get_state()
|
82
|
state_name = state.state_name if state else 'NON_VALIDE'
|
83
|
display_name = VALIDATION_STATES[state_name]
|
84
|
if event.is_absent():
|
85
|
self.act_absence = VALIDATION_STATES.get(state_name)
|
86
|
if state and not state.previous_state and state.state_name == 'NON_VALIDE':
|
87
|
state = None
|
88
|
if not service in services:
|
89
|
validation_states = None
|
90
|
self.validation = (event.act, state, display_name, validation_states)
|
91
|
else:
|
92
|
if event.event_type.label == 'Autre' and event.title:
|
93
|
self.title = event.title
|
94
|
else:
|
95
|
self.title = '%s' % event.event_type.label
|
96
|
if event.title:
|
97
|
self.title += ' - %s' % event.title
|
98
|
self.event_type = event.event_type
|
99
|
self.workers = event.participants.all()
|
100
|
if len(self.workers) > 4 :
|
101
|
self.workers_initial = '%d inter.' % len(self.workers)
|
102
|
else:
|
103
|
for worker in self.workers:
|
104
|
self.workers_initial += " " + worker.worker.initials
|
105
|
for worker in self.workers:
|
106
|
self.workers_code.append("%s-%s" % (worker.id, worker.last_name.upper()))
|
107
|
|
108
|
def init_free_time(self, length, begin_time):
|
109
|
self.type = "free"
|
110
|
self.length = length
|
111
|
self.__set_time(begin_time)
|
112
|
|
113
|
def init_busy_time(self, title, length, begin_time, description=None):
|
114
|
self.title = title
|
115
|
self.type = "busy-here"
|
116
|
self.length = length
|
117
|
self.__set_time(begin_time)
|
118
|
self.description = description
|
119
|
|
120
|
def init_holiday_time(self, title, length, begin_time, description=None):
|
121
|
self.init_busy_time(title, length, begin_time, description)
|
122
|
self.holiday = True
|
123
|
|
124
|
def init_start_stop(self, title, time):
|
125
|
"""
|
126
|
title: Arrivee ou Depart
|
127
|
"""
|
128
|
self.type = "info"
|
129
|
self.title = title
|
130
|
self.__set_time(time)
|
131
|
|
132
|
def get_daily_appointments(date, worker, service, time_tables, events, holidays):
|
133
|
"""
|
134
|
"""
|
135
|
appointments = []
|
136
|
|
137
|
timetables_set = IntervalSet((t.to_interval(date) for t in time_tables))
|
138
|
holidays_set = IntervalSet((h.to_interval(date) for h in holidays))
|
139
|
busy_occurrences_set = IntervalSet((o.to_interval() for o in events if not o.is_event_absence()))
|
140
|
for free_time in timetables_set - (busy_occurrences_set+holidays_set):
|
141
|
if free_time:
|
142
|
delta = free_time.upper_bound - free_time.lower_bound
|
143
|
delta_minutes = delta.seconds / 60
|
144
|
appointment = Appointment()
|
145
|
appointment.init_free_time(delta_minutes,
|
146
|
time(free_time.lower_bound.hour, free_time.lower_bound.minute))
|
147
|
appointments.append(appointment)
|
148
|
validation_states = dict(VALIDATION_STATES)
|
149
|
if service.name != 'CMPP' and \
|
150
|
'ACT_DOUBLE' in validation_states:
|
151
|
validation_states.pop('ACT_DOUBLE')
|
152
|
vs = [('VALIDE', 'Présent')]
|
153
|
validation_states.pop('VALIDE')
|
154
|
validation_states.pop('ACT_LOST')
|
155
|
validation_states = vs + sorted(validation_states.items(), key=lambda tup: tup[0])
|
156
|
for event in events:
|
157
|
appointment = Appointment()
|
158
|
appointment.init_from_event(event, service, validation_states)
|
159
|
appointments.append(appointment)
|
160
|
for holiday in holidays:
|
161
|
interval = holiday.to_interval(date)
|
162
|
delta = interval.upper_bound - interval.lower_bound
|
163
|
delta_minutes = delta.seconds / 60
|
164
|
appointment = Appointment()
|
165
|
label = u"Congé (%s)" % holiday.holiday_type.name
|
166
|
appointment.init_holiday_time(label,
|
167
|
delta_minutes,
|
168
|
time(interval.lower_bound.hour, interval.lower_bound.minute),
|
169
|
description=holiday.comment)
|
170
|
appointments.append(appointment)
|
171
|
for time_table in time_tables:
|
172
|
interval_set = IntervalSet.between(time_table.to_interval(date).lower_bound.time(),
|
173
|
time_table.to_interval(date).upper_bound.time())
|
174
|
for holiday in holidays:
|
175
|
holiday_interval_set = IntervalSet.between(holiday.to_interval(date).lower_bound.time(),
|
176
|
holiday.to_interval(date).upper_bound.time())
|
177
|
interval_set = interval_set - holiday_interval_set
|
178
|
if not interval_set:
|
179
|
continue
|
180
|
start_time = interval_set.lower_bound()
|
181
|
end_time = interval_set.upper_bound()
|
182
|
appointment = Appointment()
|
183
|
appointment.init_start_stop(u"Arrivée", start_time)
|
184
|
appointment.weight = -1
|
185
|
appointments.append(appointment)
|
186
|
appointment = Appointment()
|
187
|
appointment.init_start_stop(u"Départ", end_time)
|
188
|
appointment.weight = 1
|
189
|
appointments.append(appointment)
|
190
|
|
191
|
return sorted(appointments, key=lambda app: (app.begin_time, app.weight, app.event_id))
|
192
|
|
193
|
def get_daily_usage(date, ressource, service, occurrences):
|
194
|
"""
|
195
|
"""
|
196
|
appointments = []
|
197
|
|
198
|
start_time = datetime_time(8, 0)
|
199
|
end_time = datetime_time(20, 0)
|
200
|
all_day = Interval(datetime.combine(date, start_time), datetime.combine(date, end_time))
|
201
|
timetables_set = IntervalSet([all_day])
|
202
|
occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
|
203
|
for free_time in timetables_set - occurrences_set:
|
204
|
if free_time:
|
205
|
delta = free_time.upper_bound - free_time.lower_bound
|
206
|
delta_minutes = delta.seconds / 60
|
207
|
appointment = Appointment()
|
208
|
appointment.init_free_time(delta_minutes,
|
209
|
time(free_time.lower_bound.hour, free_time.lower_bound.minute))
|
210
|
appointments.append(appointment)
|
211
|
for occurrence in occurrences:
|
212
|
appointment = Appointment()
|
213
|
appointment.init_from_event(occurrence, service)
|
214
|
appointments.append(appointment)
|
215
|
|
216
|
return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
|