Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ e5d0f8c5

1
# -*- coding: utf-8 -*-
2

    
3
from django.db.models import Q
4
from datetime import datetime, time
5

    
6
from interval import IntervalSet
7

    
8
from calebasse.actes.models import EventAct
9
from calebasse.agenda.models import Occurrence
10
from calebasse.personnes.models import TimeTable
11

    
12
class Appointment(object):
13

    
14
    def __init__(self, title=None, begin_time=None, type=None,
15
            length=None, description=None, room=None):
16
        """ """
17
        self.title = title
18
        self.type = type
19
        self.length = length
20
        self.description = description
21
        self.room = room
22
        self.convocation_sent = None
23
        self.service_name = None
24
        self.patient_record_id = None
25
        self.event_id = None
26
        self.service = None
27
        self.workers_initial = None
28
        self.weight = 0
29
        self.act_type = None
30
        self.__set_time(begin_time)
31

    
32
    def __set_time(self, time):
33
        self.begin_time = time
34
        if time:
35
            self.begin_hour = time.strftime("%H:%M")
36
        else:
37
            self.begin_hour = None
38

    
39
    def init_from_occurrence(self, occurrence, service):
40
        """ """
41
        delta = occurrence.end_time - occurrence.start_time
42
        self.length = delta.seconds / 60
43
        self.title = occurrence.title
44
        services = occurrence.event.services.all()
45
        if services:
46
            self.service = services[0].name.lower()
47
        self.__set_time(time(occurrence.start_time.hour, occurrence.start_time.minute))
48
        if service in occurrence.event.services.all():
49
            self.type = "busy-here"
50
        else:
51
            self.type = "busy-elsewhere"
52
            self.service_name = service.name
53
        self.event_id = occurrence.event.id
54
        if occurrence.event.room:
55
            self.room = occurrence.event.room.name
56
        self.description = occurrence.event.description
57
        if occurrence.event.event_type.label == 'patient_appointment':
58
            event_act = occurrence.event.eventact
59
            workers = event_act.participants.all()
60
            self.convocation_sent = event_act.convocation_sent
61
            self.patient_record_id = event_act.patient.id
62
            self.workers_initial = ""
63
            for worker in workers:
64
                self.workers_initial += " " + worker.first_name.upper()[0]
65
                self.workers_initial += worker.last_name.upper()[0]
66
            self.act_type = event_act.act_type.name
67

    
68
    def init_free_time(self, length, begin_time):
69
        """ """
70
        self.type = "free"
71
        self.length = length
72
        self.__set_time(begin_time)
73

    
74

    
75
    def init_start_stop(self, title, time):
76
        """
77
        title: Arrivee ou Depart
78
        """
79
        self.type = "info"
80
        self.title = title
81
        self.__set_time(time)
82

    
83
def get_daily_appointments(date, worker, service, time_tables, occurrences):
84
    """
85
    """
86
    appointments = []
87

    
88
    timetables_set = IntervalSet((t.to_interval(date) for t in time_tables))
89
    occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
90
    for free_time in timetables_set - occurrences_set:
91
        if free_time:
92
            delta = free_time.upper_bound - free_time.lower_bound
93
            delta_minutes = delta.seconds / 60
94
            appointment = Appointment()
95
            appointment.init_free_time(delta_minutes,
96
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
97
            appointments.append(appointment)
98
    for occurrence in occurrences:
99
        appointment = Appointment()
100
        appointment.init_from_occurrence(occurrence, service)
101
        appointments.append(appointment)
102
    for time_table in time_tables:
103
        appointment = Appointment()
104
        appointment.init_start_stop(u"Arrivée",
105
            time(time_table.start_time.hour, time_table.start_time.minute))
106
        appointment.weight = -1
107
        appointments.append(appointment)
108
        appointment = Appointment()
109
        appointment.init_start_stop(u"Départ",
110
            time(time_table.end_time.hour, time_table.end_time.minute))
111
        appointment.weight = 1
112
        appointments.append(appointment)
113

    
114
    s = sorted(appointments, key=lambda app: app.weight)
115
    return sorted(s, key=lambda app: app.begin_time)
116

    
(4-4/10)