Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 76974b6f

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

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

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

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

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

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

    
171
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
172

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

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

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