Project

General

Profile

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

calebasse / calebasse / actes / validation.py @ 08bf3477

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

    
3
import datetime
4
import models
5

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

    
14

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

    
18

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

    
23

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

    
27

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

    
35

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

    
41

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

    
47

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

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

    
126
    # Acts locking
127
    for act in acts_of_the_day:
128
        if commit:
129
            act.validation_locked = True
130
            act.save()
131
    if service.name == 'CMPP' and commit:
132
        for patient, _ in patients.items():
133
            patient.create_diag_healthcare(user)
134
            patient.automated_switch_state(user)
135
    return (nb_acts_total, nb_acts_validated, nb_acts_double,
136
        nb_acts_abs_non_exc, nb_acts_abs_exc, nb_acts_abs_inter, nb_acts_annul_nous,
137
        nb_acts_annul_famille, nb_acts_reporte, nb_acts_abs_ess_pps,
138
        nb_acts_enf_hosp, nb_acts_losts)
(7-7/9)