Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ b5f052b7

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.convocation_sent = None
22
        self.other_services_names = []
23
        self.patient_record_id = None
24
        self.patient_record_paper_id = None
25
        self.event_id = None
26
        self.event_type = None
27
        self.workers = None
28
        self.workers_initial = None
29
        self.workers_codes = None
30
        self.act_state = None
31
        self.act_absence = None
32
        self.weight = 0
33
        self.act_type = None
34
        self.validation = None
35
        self.holiday = False
36
        self.services_names = []
37
        self.__set_time(begin_time)
38

    
39
    def __set_time(self, time):
40
        self.begin_time = time
41
        if time:
42
            self.begin_hour = time.strftime("%H:%M")
43
        else:
44
            self.begin_hour = None
45

    
46
    def __get_initials(self, personns):
47
        pass
48

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

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

    
114
    def init_busy_time(self, title, length, begin_time, description=None):
115
        self.title = title
116
        self.type = "busy-here"
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
        label = u"Congé (%s)" % holiday.holiday_type.name
167
        appointment.init_holiday_time(label,
168
                    delta_minutes,
169
                    time(interval.lower_bound.hour, interval.lower_bound.minute),
170
                    description=holiday.comment)
171
        appointments.append(appointment)
172
    for time_table in time_tables:
173
        interval_set = IntervalSet.between(time_table.to_interval(date).lower_bound.time(),
174
                                   time_table.to_interval(date).upper_bound.time())
175
        for holiday in holidays:
176
            holiday_interval_set = IntervalSet.between(holiday.to_interval(date).lower_bound.time(),
177
                                   holiday.to_interval(date).upper_bound.time())
178
            interval_set = interval_set - holiday_interval_set
179
        if not interval_set:
180
            continue
181
        start_time = interval_set.lower_bound()
182
        end_time = interval_set.upper_bound()
183
        appointment = Appointment()
184
        appointment.init_start_stop(u"Arrivée", start_time)
185
        appointment.weight = -1
186
        appointments.append(appointment)
187
        appointment = Appointment()
188
        appointment.init_start_stop(u"Départ", end_time)
189
        appointment.weight = 1
190
        appointments.append(appointment)
191

    
192
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
193

    
194
def get_daily_usage(date, ressource, service, occurrences):
195
    """
196
    """
197
    appointments = []
198

    
199
    start_time = datetime_time(8, 0)
200
    end_time = datetime_time(20, 0)
201
    all_day = Interval(datetime.combine(date, start_time), datetime.combine(date, end_time))
202
    timetables_set = IntervalSet([all_day])
203
    occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
204
    for free_time in timetables_set - occurrences_set:
205
        if free_time:
206
            delta = free_time.upper_bound - free_time.lower_bound
207
            delta_minutes = delta.seconds / 60
208
            appointment = Appointment()
209
            appointment.init_free_time(delta_minutes,
210
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
211
            appointments.append(appointment)
212
    for occurrence in occurrences:
213
        appointment = Appointment()
214
        appointment.init_from_occurrence(occurrence, service)
215
        appointments.append(appointment)
216

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