Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 2ff5e689

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.act_type = None
29
        self.__set_time(begin_time)
30

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

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

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

    
73

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

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

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

    
111
    appointments = sorted(appointments, key=lambda app: app.begin_time)
112
    return appointments
113

    
(4-4/10)