Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ db33aecd

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

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

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

    
48
    def init_from_event(self, event, service, validation_states=None):
49
        """ """
50
        delta = event.end_datetime - event.start_datetime
51
        self.event_id = event.id
52
        self.length = delta.seconds / 60
53
        self.title = event.title
54
        if hasattr(event, 'parent') and event.parent.recurrence_periodicity:
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
            if e_service != service:
61
                name = e_service.name.lower().replace(' ', '-')
62
                self.other_services_names.append(name)
63
        if service in services:
64
            self.type = "busy-here"
65
        else:
66
            self.type = "busy-elsewhere"
67
        self.event_id = event.id
68
        if event.room:
69
            self.room = event.room.name
70
        self.description = event.description
71
        self.workers_initial = ""
72
        self.workers_code = []
73
        if event.event_type.id == 1:
74
            self.workers = event.participants.all()
75
            self.convocation_sent = event.convocation_sent
76
            self.patient = event.patient
77
            self.patient_record_id = event.patient.id
78
            self.patient_record_paper_id = event.patient.paper_id
79
            self.act_type = event.act_type.name
80
            self.act_state = event.act.get_state().state_name
81
            if self.act_state not in ('NON_VALIDE', 'VALIDE', 'ACT_DOUBLE'):
82
                self.act_absence = VALIDATION_STATES.get(self.act_state)
83
            state = event.act.get_state()
84
            display_name = VALIDATION_STATES[state.state_name]
85
            if not state.previous_state and state.state_name == 'NON_VALIDE':
86
                state = None
87
            if not service in services:
88
                validation_states = None
89
            self.validation = (event.act, state, display_name, validation_states)
90
        else:
91
            self.event_type = event.event_type
92
            self.workers = event.participants.all()
93
        for worker in self.workers:
94
            self.workers_initial += " " + worker.get_initials()
95
            self.workers_code.append("%s-%s" % (worker.id, worker.last_name.upper()))
96

    
97
    def init_free_time(self, length, begin_time):
98
        """ """
99
        self.type = "free"
100
        self.length = length
101
        self.__set_time(begin_time)
102

    
103
    def init_busy_time(self, title, length, begin_time, description=None):
104
        self.title = title
105
        self.type = "busy-here"
106
        self.length = length
107
        self.__set_time(begin_time)
108
        self.description = description
109

    
110
    def init_holiday_time(self, title, length, begin_time, description=None):
111
        self.init_busy_time(title, length, begin_time, description)
112
        self.holiday = True
113

    
114
    def init_start_stop(self, title, time):
115
        """
116
        title: Arrivee ou Depart
117
        """
118
        self.type = "info"
119
        self.title = title
120
        self.__set_time(time)
121

    
122
def get_daily_appointments(date, worker, service, time_tables, events, holidays):
123
    """
124
    """
125
    appointments = []
126

    
127
    timetables_set = IntervalSet((t.to_interval(date) for t in time_tables))
128
    holidays_set = IntervalSet((h.to_interval(date) for h in holidays))
129
    busy_occurrences_set = IntervalSet((o.to_interval() for o in events if not o.is_event_absence()))
130
    for free_time in timetables_set - (busy_occurrences_set+holidays_set):
131
        if free_time:
132
            delta = free_time.upper_bound - free_time.lower_bound
133
            delta_minutes = delta.seconds / 60
134
            appointment = Appointment()
135
            appointment.init_free_time(delta_minutes,
136
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
137
            appointments.append(appointment)
138
    validation_states = dict(VALIDATION_STATES)
139
    if service.name != 'CMPP' and \
140
            'ACT_DOUBLE' in validation_states:
141
        validation_states.pop('ACT_DOUBLE')
142
    vs = [('VALIDE', 'Présent')]
143
    validation_states.pop('VALIDE')
144
    validation_states = vs + sorted(validation_states.items(), key=lambda tup: tup[0])
145
    for event in events:
146
        appointment = Appointment()
147
        appointment.init_from_event(event, service, validation_states)
148
        appointments.append(appointment)
149
    for holiday in holidays:
150
        interval = holiday.to_interval(date)
151
        delta = interval.upper_bound - interval.lower_bound
152
        delta_minutes = delta.seconds / 60
153
        appointment = Appointment()
154
        label = u"Congé (%s)" % holiday.holiday_type.name
155
        appointment.init_holiday_time(label,
156
                    delta_minutes,
157
                    time(interval.lower_bound.hour, interval.lower_bound.minute),
158
                    description=holiday.comment)
159
        appointments.append(appointment)
160
    for time_table in time_tables:
161
        interval_set = IntervalSet.between(time_table.to_interval(date).lower_bound.time(),
162
                                   time_table.to_interval(date).upper_bound.time())
163
        for holiday in holidays:
164
            holiday_interval_set = IntervalSet.between(holiday.to_interval(date).lower_bound.time(),
165
                                   holiday.to_interval(date).upper_bound.time())
166
            interval_set = interval_set - holiday_interval_set
167
        if not interval_set:
168
            continue
169
        start_time = interval_set.lower_bound()
170
        end_time = interval_set.upper_bound()
171
        appointment = Appointment()
172
        appointment.init_start_stop(u"Arrivée", start_time)
173
        appointment.weight = -1
174
        appointments.append(appointment)
175
        appointment = Appointment()
176
        appointment.init_start_stop(u"Départ", end_time)
177
        appointment.weight = 1
178
        appointments.append(appointment)
179

    
180
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
181

    
182
def get_daily_usage(date, ressource, service, occurrences):
183
    """
184
    """
185
    appointments = []
186

    
187
    start_time = datetime_time(8, 0)
188
    end_time = datetime_time(20, 0)
189
    all_day = Interval(datetime.combine(date, start_time), datetime.combine(date, end_time))
190
    timetables_set = IntervalSet([all_day])
191
    occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
192
    for free_time in timetables_set - occurrences_set:
193
        if free_time:
194
            delta = free_time.upper_bound - free_time.lower_bound
195
            delta_minutes = delta.seconds / 60
196
            appointment = Appointment()
197
            appointment.init_free_time(delta_minutes,
198
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
199
            appointments.append(appointment)
200
    for occurrence in occurrences:
201
        appointment = Appointment()
202
        appointment.init_from_occurrence(occurrence, service)
203
        appointments.append(appointment)
204

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