1
|
# -*- coding: utf-8 -*-
|
2
|
import datetime
|
3
|
|
4
|
|
5
|
def get_acts_of_the_day(date):
|
6
|
from models import EventAct
|
7
|
return EventAct.objects.filter(date__year=date.year,
|
8
|
date__month=date.month, date__day=date.day).order_by('date')
|
9
|
|
10
|
|
11
|
def unlock_all_acts_of_the_day(date):
|
12
|
for act in get_acts_of_the_day(date):
|
13
|
act.validation_locked = False
|
14
|
|
15
|
|
16
|
def are_all_acts_of_the_day_locked(date):
|
17
|
for act in get_acts_of_the_day(date):
|
18
|
if not act.validation_locked:
|
19
|
return False
|
20
|
return True
|
21
|
|
22
|
|
23
|
def get_acts_not_locked_of_the_day(date):
|
24
|
acts = []
|
25
|
for act in get_acts_of_the_day(date):
|
26
|
if not act.validation_locked:
|
27
|
acts.append(act)
|
28
|
return acts
|
29
|
|
30
|
|
31
|
def get_days_with_acts_not_locked(start_day, end_day):
|
32
|
num_days = abs((start_day - end_day).days) + 1
|
33
|
days_list = [start_day + datetime.timedelta(days=x) \
|
34
|
for x in range(0, num_days)]
|
35
|
result = []
|
36
|
for day in days_list:
|
37
|
if not are_all_acts_of_the_day_locked(day):
|
38
|
result.append(day)
|
39
|
return result
|
40
|
|
41
|
|
42
|
def get_days_with_all_acts_locked(start_day, end_day):
|
43
|
num_days = abs((start_day - end_day).days) + 1
|
44
|
days_list = [start_day + datetime.timedelta(days=x) \
|
45
|
for x in range(0, num_days)]
|
46
|
result = []
|
47
|
for day in days_list:
|
48
|
if are_all_acts_of_the_day_locked(day):
|
49
|
result.append(day)
|
50
|
return result
|
51
|
|
52
|
|
53
|
def automated_validation(date, service, user):
|
54
|
nb_acts_double = 0
|
55
|
nb_acts_validated = 0
|
56
|
nb_acts_absence = 0
|
57
|
acts_of_the_day = get_acts_of_the_day(date)
|
58
|
nb_acts_total = len(acts_of_the_day)
|
59
|
patients = {}
|
60
|
if service.name == 'CMPP':
|
61
|
# Verification des actes en doubles
|
62
|
acts = [act for act in acts_of_the_day \
|
63
|
if act.get_state().state_name in ('VALIDE', 'NON_VALIDE')]
|
64
|
for act in acts:
|
65
|
if act.patient not in patients:
|
66
|
patients[act.patient] = []
|
67
|
patients[act.patient].append(act)
|
68
|
for patient, acts in patients.items():
|
69
|
if len(acts) > 1:
|
70
|
# Si plusieurs actes pour un même patient le même jour
|
71
|
# On valide le premier, s'il n'est pas déja validé.
|
72
|
# Les autres sont marqués actes en double
|
73
|
found_one = False
|
74
|
for act in acts:
|
75
|
if not found_one:
|
76
|
if not act.is_state('VALIDE'):
|
77
|
act.set_state('VALIDE', author=user, auto=True)
|
78
|
found_one = True
|
79
|
nb_acts_validated = nb_acts_validated + 1
|
80
|
else:
|
81
|
act.set_state('ACT_DOUBLE', author=user, auto=True)
|
82
|
nb_acts_double = nb_acts_double + 1
|
83
|
else:
|
84
|
if acts[0].is_state('NON_VALIDE'):
|
85
|
acts[0].set_state('VALIDE', author=user, auto=True)
|
86
|
nb_acts_validated = nb_acts_validated + 1
|
87
|
else:
|
88
|
acts = [act for act in acts_of_the_day if act.is_state('NON_VALIDE')]
|
89
|
for act in acts:
|
90
|
act.set_state('VALIDE', author=user, auto=True)
|
91
|
nb_acts_validated = nb_acts_validated + 1
|
92
|
nb_acts_absence = nb_acts_total - nb_acts_validated - nb_acts_double
|
93
|
# Acts locking
|
94
|
for act in acts_of_the_day:
|
95
|
act.validation_locked = True
|
96
|
act.save()
|
97
|
if service.name == 'CMPP':
|
98
|
for patient, _ in patients.items():
|
99
|
patient.create_diag_healthcare(user)
|
100
|
patient.automated_switch_state(user)
|
101
|
return (nb_acts_total, nb_acts_validated, nb_acts_absence, nb_acts_double)
|