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