Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ d1b103ea

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

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

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

    
46
    def init_from_event(self, event, service):
47
        """ """
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
        services = event.services.all()
53
        self.date = event.start_datetime.date()
54
        self.__set_time(time(event.start_datetime.hour, event.start_datetime.minute))
55
        for e_service in services:
56
            if e_service != service:
57
                name = e_service.name.lower().replace(' ', '-')
58
                self.other_services_names.append(name)
59
        if service in services:
60
            self.type = "busy-here"
61
        else:
62
            self.type = "busy-elsewhere"
63
        self.event_id = event.id
64
        if event.room:
65
            self.room = event.room.name
66
        self.description = event.description
67
        self.workers_initial = ""
68
        self.workers_code = []
69
        if event.event_type.id == 1:
70
            event_act = event.act
71
            self.workers = event_act.doctors.all()
72
            self.convocation_sent = event.convocation_sent
73
            self.patient = event_act.patient
74
            self.patient_record_id = event_act.patient.id
75
            self.patient_record_paper_id = event_act.patient.paper_id
76
            self.act_type = event_act.act_type.name
77
            self.act_state = event_act.get_state().state_name
78
            if self.act_state not in ('NON_VALIDE', 'VALIDE', 'ACT_DOUBLE'):
79
                self.act_absence = VALIDATION_STATES.get(self.act_state)
80
            state = event_act.get_state()
81
            display_name = VALIDATION_STATES[state.state_name]
82
            if not state.previous_state and state.state_name == 'NON_VALIDE':
83
                state = None
84
            validation_states = None
85
            if service in services:
86
                validation_states = dict(VALIDATION_STATES)
87
                if not 'CMPP' in [s.name for s in services] and \
88
                        'ACT_DOUBLE' in validation_states:
89
                    validation_states.pop('ACT_DOUBLE')
90
            self.validation = (event_act, state, display_name, validation_states)
91
        else:
92
            self.event_type = event.event_type
93
            self.workers = event.participants.all()
94
        for worker in self.workers:
95
            if worker.first_name:
96
                self.workers_initial += " " + worker.first_name.upper()[0]
97
            self.workers_initial += worker.last_name.upper()[0]
98
            self.workers_code.append("%s-%s" % (worker.id, worker.last_name.upper()))
99

    
100
    def init_free_time(self, length, begin_time):
101
        """ """
102
        self.type = "free"
103
        self.length = length
104
        self.__set_time(begin_time)
105

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

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

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

    
126
    timetables_set = IntervalSet((t.to_interval(date) for t in time_tables))
127
    events_set = IntervalSet((o.to_interval() for o in events))
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
    for event in events:
139
        appointment = Appointment()
140
        appointment.init_from_event(event, service)
141
        appointments.append(appointment)
142
    for holiday in holidays:
143
        interval = holiday.to_interval(date)
144
        delta = interval.upper_bound - interval.lower_bound
145
        delta_minutes = delta.seconds / 60
146
        appointment = Appointment()
147
        label = u"Congé (%s)" % holiday.holiday_type.name
148
        appointment.init_busy_time(label,
149
                    delta_minutes,
150
                    time(interval.lower_bound.hour, interval.lower_bound.minute),
151
                    description=holiday.comment)
152
        appointments.append(appointment)
153
    for time_table in time_tables:
154
        interval_set = IntervalSet.between(time_table.to_interval(date).lower_bound.time(),
155
                                   time_table.to_interval(date).upper_bound.time())
156
        for holiday in holidays:
157
            holiday_interval_set = IntervalSet.between(holiday.to_interval(date).lower_bound.time(),
158
                                   holiday.to_interval(date).upper_bound.time())
159
            interval_set = interval_set - holiday_interval_set
160
        if not interval_set:
161
            continue
162
        start_time = interval_set.lower_bound()
163
        end_time = interval_set.upper_bound()
164
        appointment = Appointment()
165
        appointment.init_start_stop(u"Arrivée", start_time)
166
        appointment.weight = -1
167
        appointments.append(appointment)
168
        appointment = Appointment()
169
        appointment.init_start_stop(u"Départ", end_time)
170
        appointment.weight = 1
171
        appointments.append(appointment)
172

    
173
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
174

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

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

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