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_annul_nous = 0
|
54
|
nb_acts_annul_famille = 0
|
55
|
nb_acts_reporte = 0
|
56
|
nb_acts_abs_ess_pps = 0
|
57
|
nb_acts_enf_hosp = 0
|
58
|
acts_of_the_day = get_acts_of_the_day(date, service)
|
59
|
for act in acts_of_the_day:
|
60
|
if act.is_state('ABS_NON_EXC'):
|
61
|
nb_acts_abs_non_exc = nb_acts_abs_non_exc + 1
|
62
|
if act.is_state('ABS_EXC'):
|
63
|
nb_acts_abs_exc = nb_acts_abs_exc + 1
|
64
|
if act.is_state('ANNUL_NOUS'):
|
65
|
nb_acts_annul_nous = nb_acts_annul_nous + 1
|
66
|
if act.is_state('ANNUL_FAMILLE'):
|
67
|
nb_acts_annul_famille = nb_acts_annul_famille + 1
|
68
|
if act.is_state('REPORTE'):
|
69
|
nb_acts_reporte = nb_acts_reporte + 1
|
70
|
if act.is_state('ABS_ESS_PPS'):
|
71
|
nb_acts_abs_ess_pps = nb_acts_abs_ess_pps + 1
|
72
|
if act.is_state('ENF_HOSP'):
|
73
|
nb_acts_enf_hosp = nb_acts_enf_hosp + 1
|
74
|
|
75
|
nb_acts_total = len(acts_of_the_day)
|
76
|
patients = {}
|
77
|
if service.name == 'CMPP':
|
78
|
# Verification des actes en doubles
|
79
|
acts = [act for act in acts_of_the_day \
|
80
|
if act.get_state().state_name in ('VALIDE', 'NON_VALIDE',
|
81
|
'ACT_DOUBLE')]
|
82
|
for act in acts:
|
83
|
if act.patient not in patients:
|
84
|
patients[act.patient] = []
|
85
|
patients[act.patient].append(act)
|
86
|
for patient, acts in patients.items():
|
87
|
if len(acts) > 1:
|
88
|
# Si plusieurs actes pour un même patient le même jour
|
89
|
# On valide le premier, s'il n'est pas déja validé.
|
90
|
# Les autres sont marqués actes en double
|
91
|
found_one = False
|
92
|
acts_t = []
|
93
|
for act in acts:
|
94
|
if act.is_billed:
|
95
|
found_one = True
|
96
|
nb_acts_validated = nb_acts_validated + 1
|
97
|
else:
|
98
|
acts_t.append(act)
|
99
|
for act in acts_t:
|
100
|
if not found_one:
|
101
|
if not act.is_state('VALIDE') and commit:
|
102
|
act.set_state('VALIDE', author=user, auto=True)
|
103
|
found_one = True
|
104
|
nb_acts_validated = nb_acts_validated + 1
|
105
|
else:
|
106
|
if commit:
|
107
|
act.set_state('ACT_DOUBLE', author=user, auto=True)
|
108
|
nb_acts_double = nb_acts_double + 1
|
109
|
else:
|
110
|
if not acts[0].is_state('VALIDE') and commit:
|
111
|
acts[0].set_state('VALIDE', author=user, auto=True)
|
112
|
nb_acts_validated = nb_acts_validated + 1
|
113
|
else:
|
114
|
acts = [act for act in acts_of_the_day if act.is_state('NON_VALIDE')]
|
115
|
for act in acts:
|
116
|
if commit:
|
117
|
act.set_state('VALIDE', author=user, auto=True)
|
118
|
nb_acts_validated = nb_acts_validated + 1
|
119
|
|
120
|
# Acts locking
|
121
|
for act in acts_of_the_day:
|
122
|
if commit:
|
123
|
act.validation_locked = True
|
124
|
act.save()
|
125
|
if service.name == 'CMPP' and commit:
|
126
|
for patient, _ in patients.items():
|
127
|
patient.create_diag_healthcare(user)
|
128
|
patient.automated_switch_state(user)
|
129
|
return (nb_acts_total, nb_acts_validated, nb_acts_double,
|
130
|
nb_acts_abs_non_exc, nb_acts_abs_exc, nb_acts_annul_nous,
|
131
|
nb_acts_annul_famille, nb_acts_reporte, nb_acts_abs_ess_pps,
|
132
|
nb_acts_enf_hosp)
|