Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ e969d52a

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
        else:
98
            if event.event_type.label == 'Autre' and event.title:
99
                self.title = event.title
100
            else:
101
                self.title = '%s' % event.event_type.label
102
                if event.title:
103
                    self.title += ' - %s' % event.title
104
            self.event_type = event.event_type
105
        for worker in self.workers:
106
            self.workers_code.append("%s-%s" % (worker.id, worker.last_name.upper()))
107
        if not self.description:
108
            self.description = ''
109

    
110
    def init_free_time(self, length, begin_time):
111
        self.type = "free"
112
        self.length = length
113
        self.__set_time(begin_time)
114

    
115
    def init_busy_time(self, title, length, begin_time, description=None):
116
        self.title = title
117
        self.length = length
118
        self.__set_time(begin_time)
119
        self.description = description
120

    
121
    def init_holiday_time(self, title, length, begin_time, description=None):
122
        self.init_busy_time(title, length, begin_time, description)
123
        self.holiday = True
124

    
125
    def init_start_stop(self, title, time):
126
        """
127
        title: Arrivee ou Depart
128
        """
129
        self.type = "info"
130
        self.title = title
131
        self.__set_time(time)
132

    
133
def get_daily_appointments(date, worker, service, time_tables, events, holidays):
134
    """
135
    """
136
    appointments = []
137

    
138
    timetables_set = IntervalSet((t.to_interval(date) for t in time_tables))
139
    holidays_set = IntervalSet((h.to_interval(date) for h in holidays))
140
    busy_occurrences_set = IntervalSet((o.to_interval() for o in events if not o.is_event_absence()))
141
    for free_time in timetables_set - (busy_occurrences_set+holidays_set):
142
        if free_time:
143
            delta = free_time.upper_bound - free_time.lower_bound
144
            delta_minutes = delta.seconds / 60
145
            appointment = Appointment()
146
            appointment.init_free_time(delta_minutes,
147
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
148
            appointments.append(appointment)
149
    validation_states = dict(VALIDATION_STATES)
150
    if service.name != 'CMPP' and \
151
            'ACT_DOUBLE' in validation_states:
152
        validation_states.pop('ACT_DOUBLE')
153
    vs = [('VALIDE', 'Présent')]
154
    validation_states.pop('VALIDE')
155
    validation_states.pop('ACT_LOST')
156
    validation_states = vs + sorted(validation_states.items(), key=lambda tup: tup[0])
157
    for event in events:
158
        appointment = Appointment()
159
        appointment.init_from_event(event, service, validation_states)
160
        appointments.append(appointment)
161
    for holiday in holidays:
162
        interval = holiday.to_interval(date)
163
        delta = interval.upper_bound - interval.lower_bound
164
        delta_minutes = delta.seconds / 60
165
        appointment = Appointment()
166
        appointment.type = 'busy-here'
167
        label = None
168
        if not holiday.worker:
169
            label = u"Absence de groupe : %s" % holiday.holiday_type.name
170
        else:
171
            label = u"Absence indiv. : %s" % holiday.holiday_type.name
172
        appointment.init_holiday_time(label,
173
                    delta_minutes,
174
                    time(interval.lower_bound.hour, interval.lower_bound.minute),
175
                    description=holiday.comment)
176
        services = holiday.services.all()
177
        if service not in services:
178
            appointment.type = 'busy-elsewhere'
179
        appointment.other_services_names = [s.slug for s in services if s != service]
180
        appointments.append(appointment)
181
    for time_table in time_tables:
182
        interval_set = IntervalSet.between(time_table.to_interval(date).lower_bound.time(),
183
                                   time_table.to_interval(date).upper_bound.time())
184
        for holiday in holidays:
185
            holiday_interval_set = IntervalSet.between(holiday.to_interval(date).lower_bound.time(),
186
                                   holiday.to_interval(date).upper_bound.time())
187
            interval_set = interval_set - holiday_interval_set
188
        if not interval_set:
189
            continue
190
        start_time = interval_set.lower_bound()
191
        end_time = interval_set.upper_bound()
192
        appointment = Appointment()
193
        appointment.init_start_stop(u"Arrivée", start_time)
194
        appointment.weight = -1
195
        appointments.append(appointment)
196
        appointment = Appointment()
197
        appointment.init_start_stop(u"Départ", end_time)
198
        appointment.weight = 1
199
        appointments.append(appointment)
200

    
201
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight, app.event_id))
202

    
203
def get_daily_usage(date, ressource, service, occurrences):
204
    """
205
    """
206
    appointments = []
207

    
208
    start_time = datetime_time(8, 0)
209
    end_time = datetime_time(20, 0)
210
    all_day = Interval(datetime.combine(date, start_time), datetime.combine(date, end_time))
211
    timetables_set = IntervalSet([all_day])
212
    occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
213
    for free_time in timetables_set - occurrences_set:
214
        if free_time:
215
            delta = free_time.upper_bound - free_time.lower_bound
216
            delta_minutes = delta.seconds / 60
217
            appointment = Appointment()
218
            appointment.init_free_time(delta_minutes,
219
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
220
            appointments.append(appointment)
221
    for occurrence in occurrences:
222
        appointment = Appointment()
223
        appointment.init_from_event(occurrence, service)
224
        appointments.append(appointment)
225

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