Projet

Général

Profil

Télécharger (7,21 ko) Statistiques
| Branche: | Tag: | Révision:

calebasse / calebasse / dossiers / views_utils.py @ feffc421

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

    
3
from datetime import date
4

    
5
from django.db import models
6
from django.utils import formats
7

    
8
from calebasse.agenda.models import Event, EventWithAct
9
from calebasse.dossiers.states import STATES_BTN_MAPPER
10

    
11
def get_status(ctx, user):
12
    """
13
    Return status and hc_status
14
    """
15
    status = []
16
    close_btn = STATES_BTN_MAPPER['CLOS']
17
    if ctx.get('next_rdv'):
18
        close_btn = STATES_BTN_MAPPER['CLOS_RDV']
19
    if ctx['object'].service.slug == "cmpp":
20
        ctx['can_rediag'] = ctx['object'].create_diag_healthcare(user)
21
        status = ctx['object'].get_healthcare_status()
22
        highlight = False
23
        if status[0] == -1:
24
            status = 'Indéterminé.'
25
            highlight = True
26
        elif status[0] == 0:
27
            status = "Prise en charge de diagnostic en cours."
28
        elif status[0] == 1:
29
            status = 'Patient jamais pris en charge.'
30
        elif status[0] == 2:
31
            status = "Prise en charge de diagnostic complète, faire une demande de prise en charge de traitement."
32
            highlight = True
33
        elif status[0] == 3:
34
            if ctx['can_rediag']:
35
                status = "Prise en charge de traitement expirée. Patient élligible en rediagnostic."
36
                highlight = True
37
            else:
38
                status = "Prise en charge de traitement expirée. La demande d'un renouvellement est possible."
39
                highlight = True
40
        elif status[0] == 4:
41
            status = "Il existe une prise en charge de traitement mais qui ne prendra effet que le %s." % str(status[1])
42
        elif status[0] == 5:
43
            status = "Prise en charge de traitement en cours."
44
        elif status[0] == 6:
45
            status = "Prise en charge de traitement complète mais qui peut être prolongée."
46
            highlight = True
47
        elif status[0] == 7:
48
            status = "Prise en charge de traitement complète et déjà prolongée, se terminant le %s." % \
49
                formats.date_format(status[2], "SHORT_DATE_FORMAT")
50
        else:
51
            status = 'Statut inconnu.'
52
        hc_status = (status, highlight)
53
        if ctx['object'].last_state.status.type == "ACCUEIL":
54
            # Inscription automatique au premier acte facturable valide
55
            status = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
56
                    STATES_BTN_MAPPER['DIAGNOSTIC'],
57
                    STATES_BTN_MAPPER['TRAITEMENT']]
58
        elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
59
            # Passage automatique en diagnostic ou traitement
60
            status = [STATES_BTN_MAPPER['ACCUEIL'],
61
                    STATES_BTN_MAPPER['DIAGNOSTIC'],
62
                    STATES_BTN_MAPPER['TRAITEMENT']]
63
        elif ctx['object'].last_state.status.type == "DIAGNOSTIC":
64
            # Passage automatique en traitement
65
            status = [STATES_BTN_MAPPER['TRAITEMENT'],
66
                    close_btn,
67
                    STATES_BTN_MAPPER['ACCUEIL']]
68
        elif ctx['object'].last_state.status.type == "TRAITEMENT":
69
            # Passage automatique en diagnostic si on ajoute une prise en charge diagnostic,
70
            # ce qui est faisable dans l'onglet prise en charge par un bouton visible sous conditions
71
            status = [STATES_BTN_MAPPER['DIAGNOSTIC'],
72
                    close_btn,
73
                    STATES_BTN_MAPPER['ACCUEIL']]
74
        elif ctx['object'].last_state.status.type == "CLOS":
75
            # Passage automatique en diagnostic ou traitement
76
            status = [STATES_BTN_MAPPER['DIAGNOSTIC'],
77
                    STATES_BTN_MAPPER['TRAITEMENT'],
78
                    STATES_BTN_MAPPER['ACCUEIL']]
79
    elif ctx['object'].service.slug == "camsp":
80
        hc_status = None
81
        if ctx['object'].last_state.status.type == "ACCUEIL":
82
            status = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
83
                    STATES_BTN_MAPPER['BILAN']]
84
        elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
85
            status = [STATES_BTN_MAPPER['ACCUEIL'],
86
                    STATES_BTN_MAPPER['BILAN'],
87
                    STATES_BTN_MAPPER['SURVEILLANCE'],
88
                    STATES_BTN_MAPPER['SUIVI'],
89
                    close_btn]
90
        elif ctx['object'].last_state.status.type == "BILAN":
91
            status = [STATES_BTN_MAPPER['SURVEILLANCE'],
92
                    STATES_BTN_MAPPER['SUIVI'],
93
                    close_btn,
94
                    STATES_BTN_MAPPER['ACCUEIL']]
95
        elif ctx['object'].last_state.status.type == "SURVEILLANCE":
96
            status = [STATES_BTN_MAPPER['SUIVI'],
97
                    close_btn,
98
                    STATES_BTN_MAPPER['ACCUEIL'],
99
                    STATES_BTN_MAPPER['BILAN']]
100
        elif ctx['object'].last_state.status.type == "SUIVI":
101
            status = [close_btn,
102
                    STATES_BTN_MAPPER['ACCUEIL'],
103
                    STATES_BTN_MAPPER['BILAN'],
104
                    STATES_BTN_MAPPER['SURVEILLANCE']]
105
        elif ctx['object'].last_state.status.type == "CLOS":
106
            status = [STATES_BTN_MAPPER['ACCUEIL'],
107
                    STATES_BTN_MAPPER['BILAN'],
108
                    STATES_BTN_MAPPER['SURVEILLANCE'],
109
                    STATES_BTN_MAPPER['SUIVI']]
110
    elif ctx['object'].service.slug == "sessad-ted" or ctx['object'].service.slug == "sessad-dys":
111
        hc_status = None
112
        if ctx['object'].last_state.status.type == "ACCUEIL":
113
            status = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
114
                    STATES_BTN_MAPPER['TRAITEMENT']]
115
        elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
116
            status = [STATES_BTN_MAPPER['ACCUEIL'],
117
                    STATES_BTN_MAPPER['TRAITEMENT'],
118
                    close_btn]
119
        elif ctx['object'].last_state.status.type == "TRAITEMENT":
120
            status = [close_btn,
121
                    STATES_BTN_MAPPER['ACCUEIL']]
122
        elif ctx['object'].last_state.status.type == "CLOS":
123
            status = [STATES_BTN_MAPPER['ACCUEIL'],
124
                    STATES_BTN_MAPPER['TRAITEMENT']]
125
    return (status, hc_status)
126

    
127
def get_last_rdv(patient_record):
128
    last_rdv = {}
129
    event = Event.objects.last_appointment(patient_record)
130
    if event:
131
        last_rdv['start_datetime'] = event.start_datetime
132
        last_rdv['participants'] = event.participants.all()
133
        last_rdv['act_type'] = event.eventwithact.act_type
134
        last_rdv['act_state'] = event.act.get_state()
135
        last_rdv['is_absent'] = event.is_absent()
136
    return last_rdv
137

    
138
def get_next_rdv(patient_record):
139
    Q = models.Q
140
    today = date.today()
141
    qs = EventWithAct.objects.filter(patient=patient_record) \
142
            .filter(exception_to__isnull=True, canceled=False) \
143
            .filter(Q(start_datetime__gte=today) \
144
            |  Q(exceptions__isnull=False) \
145
            | ( Q(recurrence_periodicity__isnull=False) \
146
            & (Q(recurrence_end_date__gte=today) \
147
            | Q(recurrence_end_date__isnull=True) \
148
            ))) \
149
            .distinct() \
150
            .select_related() \
151
            .prefetch_related('participants', 'exceptions__eventwithact')
152
    occurrences = []
153
    for event in qs:
154
        occurrences.extend(filter(lambda e: e.start_datetime.date() >= today, event.all_occurences(limit=180)))
155
    occurrences = sorted(occurrences, key=lambda e: e.start_datetime)
156
    if occurrences:
157
        return occurrences[0]
158
    else:
159
        return None
(12-12/12)