Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 6aa3310c

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
from .models import EventWithAct
10

    
11
class Appointment(object):
12

    
13
    def __init__(self, title=None, begin_time=None, type=None,
14
            length=None, description=None, ressource=None):
15
        self.title = title
16
        self.type = type
17
        self.length = length
18
        self.description = description
19
        self.ressource = ressource
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_id = None
33
        self.act_state = None
34
        self.act_absence = None
35
        self.weight = 0
36
        self.act_type = None
37
        self.validation = None
38
        self.holiday = False
39
        self.services_names = []
40
        self.event = False
41
        self.timetable_type = None
42
        self.__set_time(begin_time)
43

    
44
    def __set_time(self, time):
45
        self.begin_time = time
46
        if time:
47
            self.begin_hour = time.strftime("%H:%M")
48
        else:
49
            self.begin_hour = None
50

    
51
    def init_from_event(self, event, service, validation_states=None):
52
        delta = event.end_datetime - event.start_datetime
53
        self.event = not isinstance(event, EventWithAct)
54
        self.event_id = event.id
55
        self.length = delta.seconds / 60
56
        self.title = event.title
57
        if (hasattr(event, 'parent') and event.parent.recurrence_periodicity) or \
58
                event.exception_to:
59
            self.is_recurrent = True
60
        services = event.services.all()
61
        self.date = event.start_datetime.date()
62
        self.__set_time(time(event.start_datetime.hour, event.start_datetime.minute))
63
        for e_service in services:
64
            name = e_service.name.lower().replace(' ', '-')
65
            if e_service != service:
66
                self.other_services_names.append(name)
67
            self.services_names.append(name)
68
        if service in services:
69
            self.type = "busy-here"
70
        else:
71
            self.type = "busy-elsewhere"
72
        self.event_id = event.id
73
        if event.ressource:
74
            self.ressource = event.ressource.name
75
        self.description = event.description
76
        self.workers_initial = ""
77
        self.workers_code = []
78
        self.workers = event.participants.all()
79
        self.len_workers = event.participants.count()
80
        self.workers_absent = event.get_missing_participants()
81
        if event.event_type.id == 1:
82
            self.act_id = event.act.id
83
            self.convocation_sent = event.convocation_sent
84
            self.patient = event.patient
85
            self.patient_record_id = event.patient.id
86
            self.patient_record_paper_id = event.patient.paper_id
87
            self.act_type = event.act_type.name
88
            self.is_billed = event.act.is_billed
89
            self.already_billed = event.act.already_billed
90
            state = event.get_state()
91
            state_name = state.state_name if state else 'NON_VALIDE'
92
            display_name = VALIDATION_STATES[state_name]
93
            if event.is_absent():
94
                self.act_absence = VALIDATION_STATES.get(state_name)
95
            if state and not state.previous_state and state.state_name == 'NON_VALIDE':
96
                state = None
97
            if not service in services:
98
                validation_states = None
99
            self.validation = (event.act, state, display_name, validation_states)
100
            self.title = event.patient.display_name
101
        else:
102
            if event.event_type.label == 'Autre' and event.title:
103
                self.title = event.title
104
            else:
105
                self.title = '%s' % event.event_type.label
106
                if event.title:
107
                    self.title += ' - %s' % event.title
108
            self.event_type = event.event_type
109
        for worker in self.workers:
110
            self.workers_code.append("%s-%s" % (worker.id, worker.last_name.upper()))
111
        if not self.description:
112
            self.description = ''
113

    
114
    def init_free_time(self, length, begin_time):
115
        self.type = "free"
116
        self.length = length
117
        self.__set_time(begin_time)
118

    
119
    def init_busy_time(self, title, length, begin_time, description=None):
120
        self.title = title
121
        self.length = length
122
        self.__set_time(begin_time)
123
        self.description = description
124

    
125
    def init_holiday_time(self, title, length, begin_time, description=None):
126
        self.init_busy_time(title, length, begin_time, description)
127
        self.holiday = True
128

    
129
    def init_start_stop(self, title, time, type, kind):
130
        """
131
        title: Arrivee ou Depart
132
        """
133
        self.type = kind
134
        self.title = title
135
        self.__set_time(time)
136
        self.timetable_type = type
137

    
138
def get_daily_appointments(date, worker, service, time_tables, events, holidays):
139
    """
140
    """
141
    appointments = []
142
    activity = {'absences': []}
143

    
144
    service_time_tables = [tt for tt in time_tables if service in tt.services.all()]
145
    timetables_set = IntervalSet((t.to_interval(date) for t in service_time_tables))
146
    holidays_set = IntervalSet((h.to_interval(date) for h in holidays))
147
    busy_occurrences_set = IntervalSet((o.to_interval() for o in events if not o.is_event_absence() and service in o.services.all()))
148
    for free_time in timetables_set - (busy_occurrences_set+holidays_set):
149
        if free_time:
150
            delta = free_time.upper_bound - free_time.lower_bound
151
            delta_minutes = delta.seconds / 60
152
            appointment = Appointment()
153
            appointment.init_free_time(delta_minutes,
154
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
155
            appointments.append(appointment)
156
    validation_states = dict(VALIDATION_STATES)
157
    if service.name != 'CMPP' and \
158
            'ACT_DOUBLE' in validation_states:
159
        validation_states.pop('ACT_DOUBLE')
160
    vs = [('VALIDE', 'Présent')]
161
    validation_states.pop('VALIDE')
162
    validation_states.pop('ACT_LOST')
163
    validation_states = vs + sorted(validation_states.items(), key=lambda tup: tup[0])
164

    
165
    events.sort(key=lambda event: event.start_datetime)
166

    
167
    # get first and last events start times
168
    if events:
169
        activity['first_appointment'], activity['last_appointment'] = (e.start_datetime.time() for e in (events[0], events[-1]))
170

    
171
    for event in events:
172
        appointment = Appointment()
173
        appointment.init_from_event(event, service, validation_states)
174
        appointments.append(appointment)
175
    for holiday in holidays:
176
        interval = holiday.to_interval(date)
177
        delta = interval.upper_bound - interval.lower_bound
178
        delta_minutes = delta.seconds / 60
179
        appointment = Appointment()
180
        appointment.type = 'busy-here'
181
        label = None
182
        if not holiday.worker:
183
            label = u"Absence de groupe : %s" % holiday.holiday_type.name
184
        else:
185
            label = u"Absence indiv. : %s" % holiday.holiday_type.name
186

    
187
        appointment.init_holiday_time(label,
188
                    delta_minutes,
189
                    time(interval.lower_bound.hour, interval.lower_bound.minute),
190
                    description=holiday.comment)
191
        activity['absences'].append(label)
192
        services = holiday.services.all()
193
        if service not in services:
194
            appointment.type = 'busy-elsewhere'
195
        appointment.other_services_names = [s.slug for s in services if s != service]
196
        appointments.append(appointment)
197
    for time_table in time_tables:
198
        interval_set = IntervalSet.between(time_table.to_interval(date).lower_bound.time(),
199
                                   time_table.to_interval(date).upper_bound.time())
200
        for holiday in holidays:
201
            holiday_interval_set = IntervalSet.between(holiday.to_interval(date).lower_bound.time(),
202
                                   holiday.to_interval(date).upper_bound.time())
203
            interval_set = interval_set - holiday_interval_set
204
        if not interval_set:
205
            continue
206
        start_time = interval_set.lower_bound()
207
        end_time = interval_set.upper_bound()
208

    
209
        services = time_table.services.all()
210
        common_service = service in services
211
        services = [s.slug for s in services]
212
        appointment_kind = 'timetable '
213
        if common_service:
214
            appointment_kind += 'info'
215
        else:
216
            appointment_kind += 'busy-elsewhere'
217
        appointment = Appointment()
218
        appointment.other_services_names = services
219
        appointment.init_start_stop(u"Arrivée", start_time, 'arrival',
220
                                    appointment_kind)
221
        activity['arrival'] = start_time
222
        appointment.weight = -1
223
        appointments.append(appointment)
224
        appointment = Appointment()
225
        appointment.init_start_stop(u"Départ", end_time, 'departure',
226
                                    appointment_kind)
227
        appointment.other_services_names = services
228
        activity['departure'] = end_time
229
        appointment.weight = -1
230
        appointments.append(appointment)
231

    
232
    return activity, sorted(appointments, key=lambda app: (app.begin_time, app.weight, app.event_id))
233

    
234
def get_daily_usage(date, ressource, service, occurrences):
235
    """
236
    """
237
    appointments = []
238

    
239
    start_time = datetime_time(8, 0)
240
    end_time = datetime_time(20, 0)
241
    all_day = Interval(datetime.combine(date, start_time), datetime.combine(date, end_time))
242
    timetables_set = IntervalSet([all_day])
243
    occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
244
    for free_time in timetables_set - occurrences_set:
245
        if free_time:
246
            delta = free_time.upper_bound - free_time.lower_bound
247
            delta_minutes = delta.seconds / 60
248
            appointment = Appointment()
249
            appointment.init_free_time(delta_minutes,
250
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
251
            appointments.append(appointment)
252
    for occurrence in occurrences:
253
        appointment = Appointment()
254
        appointment.init_from_event(occurrence, service)
255
        appointments.append(appointment)
256

    
257
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
(4-4/10)