1
|
# -*- coding: utf-8 -*-
|
2
|
import datetime
|
3
|
|
4
|
def get_acts_of_the_day(date, service=None):
|
5
|
from models import EventAct
|
6
|
if service:
|
7
|
return EventAct.objects.filter(date__year=date.year,
|
8
|
date__month=date.month, date__day=date.day,
|
9
|
services__pk__icontains=service.pk).order_by('date')
|
10
|
return EventAct.objects.filter(date__year=date.year,
|
11
|
date__month=date.month, date__day=date.day).order_by('date')
|
12
|
|
13
|
|
14
|
def unlock_all_acts_of_the_day(date, service=None):
|
15
|
for act in get_acts_of_the_day(date, service):
|
16
|
if not act.is_billed:
|
17
|
act.validation_locked = False
|
18
|
act.save()
|
19
|
|
20
|
def are_all_acts_of_the_day_locked(date, service=None):
|
21
|
for act in get_acts_of_the_day(date, service):
|
22
|
if not act.validation_locked:
|
23
|
return False
|
24
|
return True
|
25
|
|
26
|
|
27
|
def get_acts_not_locked_of_the_day(date, service=None):
|
28
|
acts = []
|
29
|
for act in get_acts_of_the_day(date, service):
|
30
|
if not act.validation_locked:
|
31
|
acts.append(act)
|
32
|
return acts
|
33
|
|
34
|
|
35
|
def get_days_with_acts_not_locked(start_day, end_day, service=None):
|
36
|
num_days = abs((start_day - end_day).days) + 1
|
37
|
days_list = [start_day + datetime.timedelta(days=x) \
|
38
|
for x in range(0, num_days)]
|
39
|
result = []
|
40
|
for day in days_list:
|
41
|
if not are_all_acts_of_the_day_locked(day, service):
|
42
|
result.append(day)
|
43
|
return result
|
44
|
|
45
|
|
46
|
def get_days_with_all_acts_locked(start_day, end_day, service=None):
|
47
|
num_days = abs((start_day - end_day).days) + 1
|
48
|
days_list = [start_day + datetime.timedelta(days=x) \
|
49
|
for x in range(0, num_days)]
|
50
|
result = []
|
51
|
for day in days_list:
|
52
|
if are_all_acts_of_the_day_locked(day, service):
|
53
|
result.append(day)
|
54
|
return result
|
55
|
|
56
|
|
57
|
def automated_validation(date, service, user, commit=True):
|
58
|
nb_acts_double = 0
|
59
|
nb_acts_validated = 0
|
60
|
nb_acts_abs_non_exc = 0
|
61
|
nb_acts_abs_exc = 0
|
62
|
nb_acts_annul_nous = 0
|
63
|
nb_acts_annul_famille = 0
|
64
|
nb_acts_abs_ess_pps = 0
|
65
|
nb_acts_enf_hosp = 0
|
66
|
acts_of_the_day = get_acts_of_the_day(date, service)
|
67
|
for act in acts_of_the_day:
|
68
|
if act.is_state('ABS_NON_EXC'):
|
69
|
nb_acts_abs_non_exc = nb_acts_abs_non_exc + 1
|
70
|
if act.is_state('ABS_EXC'):
|
71
|
nb_acts_abs_exc = nb_acts_abs_exc + 1
|
72
|
if act.is_state('ANNUL_NOUS'):
|
73
|
nb_acts_annul_nous = nb_acts_annul_nous + 1
|
74
|
if act.is_state('ANNUL_FAMILLE'):
|
75
|
nb_acts_annul_famille = nb_acts_annul_famille + 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
|
|
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
|
else:
|
103
|
acts_t.append(act)
|
104
|
for act in acts_t:
|
105
|
if not found_one:
|
106
|
if not act.is_state('VALIDE') and commit:
|
107
|
act.set_state('VALIDE', author=user, auto=True)
|
108
|
found_one = True
|
109
|
nb_acts_validated = nb_acts_validated + 1
|
110
|
else:
|
111
|
if commit:
|
112
|
act.set_state('ACT_DOUBLE', author=user, auto=True)
|
113
|
nb_acts_double = nb_acts_double + 1
|
114
|
else:
|
115
|
if not acts[0].is_state('VALIDE') and commit:
|
116
|
acts[0].set_state('VALIDE', author=user, auto=True)
|
117
|
nb_acts_validated = nb_acts_validated + 1
|
118
|
else:
|
119
|
acts = [act for act in acts_of_the_day if act.is_state('NON_VALIDE')]
|
120
|
for act in acts:
|
121
|
if commit:
|
122
|
act.set_state('VALIDE', author=user, auto=True)
|
123
|
nb_acts_validated = nb_acts_validated + 1
|
124
|
|
125
|
# Acts locking
|
126
|
for act in acts_of_the_day:
|
127
|
if commit:
|
128
|
act.validation_locked = True
|
129
|
act.save()
|
130
|
if service.name == 'CMPP' and commit:
|
131
|
for patient, _ in patients.items():
|
132
|
patient.create_diag_healthcare(user)
|
133
|
patient.automated_switch_state(user)
|
134
|
return (nb_acts_total, nb_acts_validated, nb_acts_double,
|
135
|
nb_acts_abs_non_exc, nb_acts_abs_exc, nb_acts_annul_nous,
|
136
|
nb_acts_annul_famille, nb_acts_abs_ess_pps,
|
137
|
nb_acts_enf_hosp)
|