Projet

Général

Profil

Télécharger (10,1 ko) Statistiques
| Branche: | Tag: | Révision:

calebasse / calebasse / agenda / appointments.py @ 01b95bad

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.__set_time(begin_time)
42

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

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

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

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

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

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

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

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

    
162
    events.sort(key=lambda event: event.start_datetime)
163

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

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

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

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

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

    
229
def get_daily_usage(date, ressource, service, occurrences):
230
    """
231
    """
232
    appointments = []
233

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

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