Project

General

Profile

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

calebasse / calebasse / actes / validation.py @ d3eeac81

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

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

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

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