Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 0cbf950f

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_state = None
32
        self.act_absence = None
33
        self.weight = 0
34
        self.act_type = None
35
        self.validation = None
36
        self.holiday = False
37
        self.services_names = []
38
        self.__set_time(begin_time)
39

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

    
47
    def init_from_event(self, event, service, validation_states=None):
48
        delta = event.end_datetime - event.start_datetime
49
        self.event_id = event.id
50
        self.length = delta.seconds / 60
51
        self.title = event.title
52
        if hasattr(event, 'parent') and event.parent.recurrence_periodicity:
53
            self.is_recurrent = True
54
        services = event.services.all()
55
        self.date = event.start_datetime.date()
56
        self.__set_time(time(event.start_datetime.hour, event.start_datetime.minute))
57
        for e_service in services:
58
            name = e_service.name.lower().replace(' ', '-')
59
            if e_service != service:
60
                self.other_services_names.append(name)
61
            self.services_names.append(name)
62
        if service in services:
63
            self.type = "busy-here"
64
        else:
65
            self.type = "busy-elsewhere"
66
        self.event_id = event.id
67
        if event.room:
68
            self.room = event.room.name
69
        self.description = event.description
70
        self.workers_initial = ""
71
        self.workers_code = []
72
        self.workers = event.participants.all()
73
        self.len_workers = event.participants.count()
74
        self.workers_absent = event.get_missing_participants()
75
        if event.event_type.id == 1:
76
            self.convocation_sent = event.convocation_sent
77
            self.patient = event.patient
78
            self.patient_record_id = event.patient.id
79
            self.patient_record_paper_id = event.patient.paper_id
80
            self.act_type = event.act_type.name
81
            self.is_billed = event.act.is_billed
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
        for worker in self.workers:
101
            self.workers_code.append("%s-%s" % (worker.id, worker.last_name.upper()))
102

    
103
    def init_free_time(self, length, begin_time):
104
        self.type = "free"
105
        self.length = length
106
        self.__set_time(begin_time)
107

    
108
    def init_busy_time(self, title, length, begin_time, description=None):
109
        self.title = title
110
        self.type = "busy-here"
111
        self.length = length
112
        self.__set_time(begin_time)
113
        self.description = description
114

    
115
    def init_holiday_time(self, title, length, begin_time, description=None):
116
        self.init_busy_time(title, length, begin_time, description)
117
        self.holiday = True
118

    
119
    def init_start_stop(self, title, time):
120
        """
121
        title: Arrivee ou Depart
122
        """
123
        self.type = "info"
124
        self.title = title
125
        self.__set_time(time)
126

    
127
def get_daily_appointments(date, worker, service, time_tables, events, holidays):
128
    """
129
    """
130
    appointments = []
131

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

    
186
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight, app.event_id))
187

    
188
def get_daily_usage(date, ressource, service, occurrences):
189
    """
190
    """
191
    appointments = []
192

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

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