Projet

Général

Profil

Télécharger (5,71 ko) Statistiques
| Branche: | Tag: | Révision:

calebasse / calebasse / actes / validation.py @ a9520794

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

    
3
import datetime
4
import models
5
from django.db import transaction
6

    
7
def get_acts_of_the_day(date, service=None):
8
    if not isinstance(date, datetime.date):
9
        date = date.date()
10
    qs = models.Act.objects.filter(date=date)
11
    if service:
12
        qs = qs.filter(patient__service=service)
13
    return qs.order_by('date')
14

    
15

    
16
def unlock_all_acts_of_the_day(date, service=None):
17
    get_acts_of_the_day(date, service).update(validation_locked=False)
18

    
19

    
20
def get_acts_not_locked_of_the_day(date, service=None):
21
    return get_acts_of_the_day(date, service) \
22
            .filter(validation_locked=False)
23

    
24

    
25
def are_all_acts_of_the_day_locked(date, service=None):
26
    return not get_acts_not_locked_of_the_day(date, service).exists()
27

    
28

    
29
def get_days_with_acts_not_locked(start_day, end_day, service=None):
30
    qs = models.Act.objects.filter(date__gte=start_day,
31
            date__lte=end_day, validation_locked=False)
32
    if service:
33
        qs = qs.filter(patient__service=service)
34
    return sorted(set(qs.values_list('date', flat=True)))
35

    
36

    
37
def date_generator(from_date, to_date):
38
    while from_date < to_date:
39
        yield from_date
40
        from_date += datetime.timedelta(days=1)
41

    
42

    
43
def get_days_with_all_acts_locked(start_day, end_day, service=None):
44
    locked_days = get_days_with_acts_not_locked(start_day, end_day,
45
            service)
46
    return sorted(set(date_generator(start_day, end_day)) - set(locked_days))
47

    
48

    
49
@transaction.commit_manually
50
def automated_validation(date, service, user, commit=True):
51
    nb_acts_double = 0
52
    nb_acts_validated = 0
53
    nb_acts_abs_non_exc = 0
54
    nb_acts_abs_exc = 0
55
    nb_acts_abs_inter = 0
56
    nb_acts_annul_nous = 0
57
    nb_acts_annul_famille = 0
58
    nb_acts_reporte = 0
59
    nb_acts_abs_ess_pps = 0
60
    nb_acts_enf_hosp = 0
61
    nb_acts_losts = 0
62
    acts_of_the_day = get_acts_of_the_day(date, service)
63
    for act in acts_of_the_day:
64
        if act.is_state('ABS_NON_EXC'):
65
            nb_acts_abs_non_exc = nb_acts_abs_non_exc + 1
66
        if act.is_state('ABS_EXC'):
67
            nb_acts_abs_exc = nb_acts_abs_exc + 1
68
        if act.is_state('ABS_INTER'):
69
            nb_acts_abs_inter = nb_acts_abs_inter + 1
70
        if act.is_state('ANNUL_NOUS'):
71
            nb_acts_annul_nous = nb_acts_annul_nous + 1
72
        if act.is_state('ANNUL_FAMILLE'):
73
            nb_acts_annul_famille = nb_acts_annul_famille + 1
74
        if act.is_state('REPORTE'):
75
            nb_acts_reporte = nb_acts_reporte + 1
76
        if act.is_state('ABS_ESS_PPS'):
77
            nb_acts_abs_ess_pps = nb_acts_abs_ess_pps + 1
78
        if act.is_state('ENF_HOSP'):
79
            nb_acts_enf_hosp = nb_acts_enf_hosp + 1
80
        if act.is_state('ACT_LOST') or act.is_lost:
81
            nb_acts_losts = nb_acts_losts + 1
82
        if not act.get_state():
83
            act.set_state('VALIDE', author=user, auto=True)
84

    
85
    nb_acts_total = len(acts_of_the_day)
86
    patients = {}
87
    if service.name == 'CMPP':
88
        # Verification des actes en doubles
89
        acts = [act for act in acts_of_the_day \
90
            if not act.is_lost and (act.is_state('VALIDE') or
91
                act.is_state('NON_VALIDE') or act.is_state('ACT_DOUBLE'))]
92
        for act in acts:
93
            if act.patient not in patients:
94
                patients[act.patient] = []
95
            patients[act.patient].append(act)
96
        for patient, acts in patients.items():
97
            if len(acts) > 1:
98
                # Si plusieurs actes pour un même patient le même jour
99
                # On valide le premier, s'il n'est pas déja validé.
100
                # Les autres sont marqués actes en double
101
                found_one = False
102
                acts_t = []
103
                for act in acts:
104
                    if act.is_billed:
105
                        found_one = True
106
                        nb_acts_validated = nb_acts_validated + 1
107
                    else:
108
                        acts_t.append(act)
109
                for act in acts_t:
110
                    if not found_one or not act.is_billable():
111
                        if not act.is_state('VALIDE') and commit:
112
                            act.set_state('VALIDE', author=user, auto=True)
113
                        if act.is_billable():
114
                            found_one = True
115
                        nb_acts_validated = nb_acts_validated + 1
116
                    else:
117
                        if commit:
118
                            act.set_state('ACT_DOUBLE', author=user, auto=True)
119
                        nb_acts_double = nb_acts_double + 1
120
            else:
121
                if not acts[0].is_state('VALIDE') and commit:
122
                    acts[0].set_state('VALIDE', author=user, auto=True)
123
                nb_acts_validated = nb_acts_validated + 1
124
    else:
125
        acts = [act for act in acts_of_the_day if act.is_state('NON_VALIDE') or act.is_state('ACT_DOUBLE')]
126
        for act in acts:
127
            if commit:
128
                act.set_state('VALIDE', author=user, auto=True)
129
            nb_acts_validated = nb_acts_validated + 1
130

    
131
    for act in acts_of_the_day:
132
        if commit and (act.is_lost or act.is_billed):
133
            state = act.get_state()
134
            if not state or state.state_name == 'NON_VALIDE':
135
                act.set_state('VALIDE', author=user, auto=True)
136

    
137
    # Acts locking
138
    for act in acts_of_the_day:
139
        if commit:
140
            act.validation_locked = True
141
            act.save()
142
    if service.name == 'CMPP' and commit:
143
        for patient, _ in patients.items():
144
            patient.create_diag_healthcare(user)
145
            patient.automated_switch_state(user)
146
    transaction.commit()
147
    return (nb_acts_total, nb_acts_validated, nb_acts_double,
148
        nb_acts_abs_non_exc, nb_acts_abs_exc, nb_acts_abs_inter, nb_acts_annul_nous,
149
        nb_acts_annul_famille, nb_acts_reporte, nb_acts_abs_ess_pps,
150
        nb_acts_enf_hosp, nb_acts_losts)
(7-7/9)