Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 6b26af85

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

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

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

    
47
    def init_from_event(self, event, service, validation_states=None):
48
        """ """
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:
54
            self.is_recurrent = True
55
        services = event.services.all()
56
        self.date = event.start_datetime.date()
57
        self.__set_time(time(event.start_datetime.hour, event.start_datetime.minute))
58
        for e_service in services:
59
            if e_service != service:
60
                name = e_service.name.lower().replace(' ', '-')
61
                self.other_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
        if event.event_type.id == 1:
73
            self.workers = event.participants.all()
74
            self.convocation_sent = event.convocation_sent
75
            self.patient = event.patient
76
            self.patient_record_id = event.patient.id
77
            self.patient_record_paper_id = event.patient.paper_id
78
            self.act_type = event.act_type.name
79
            self.act_state = event.act.get_state().state_name
80
            if self.act_state not in ('NON_VALIDE', 'VALIDE', 'ACT_DOUBLE'):
81
                self.act_absence = VALIDATION_STATES.get(self.act_state)
82
            state = event.act.get_state()
83
            display_name = VALIDATION_STATES[state.state_name]
84
            if not state.previous_state and state.state_name == 'NON_VALIDE':
85
                state = None
86
            if not service in services:
87
                validation_states = None
88
            self.validation = (event.act, state, display_name, validation_states)
89
        else:
90
            self.event_type = event.event_type
91
            self.workers = event.participants.all()
92
        for worker in self.workers:
93
            self.workers_initial += " " + worker.get_initials()
94
            self.workers_code.append("%s-%s" % (worker.id, worker.last_name.upper()))
95

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

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

    
109
    def init_start_stop(self, title, time):
110
        """
111
        title: Arrivee ou Depart
112
        """
113
        self.type = "info"
114
        self.title = title
115
        self.__set_time(time)
116

    
117
def get_daily_appointments(date, worker, service, time_tables, events, holidays):
118
    """
119
    """
120
    appointments = []
121

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

    
175
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
176

    
177
def get_daily_usage(date, ressource, service, occurrences):
178
    """
179
    """
180
    appointments = []
181

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

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