1 |
3ed29cb7
|
Mikaël Ates
|
# -*- coding: utf-8 -*-
|
2 |
979b59ce
|
Benjamin Dauvergne
|
|
3 |
3ed29cb7
|
Mikaël Ates
|
import datetime
|
4 |
979b59ce
|
Benjamin Dauvergne
|
import models
|
5 |
bb3869d2
|
Mikaël Ates
|
from django.db import transaction
|
6 |
3ed29cb7
|
Mikaël Ates
|
|
7 |
0853d495
|
Mikaël Ates
|
def get_acts_of_the_day(date, service=None):
|
8 |
979b59ce
|
Benjamin Dauvergne
|
if not isinstance(date, datetime.date):
|
9 |
|
|
date = date.date()
|
10 |
|
|
qs = models.Act.objects.filter(date=date)
|
11 |
0853d495
|
Mikaël Ates
|
if service:
|
12 |
979b59ce
|
Benjamin Dauvergne
|
qs = qs.filter(patient__service=service)
|
13 |
|
|
return qs.order_by('date')
|
14 |
3ed29cb7
|
Mikaël Ates
|
|
15 |
|
|
|
16 |
0853d495
|
Mikaël Ates
|
def unlock_all_acts_of_the_day(date, service=None):
|
17 |
979b59ce
|
Benjamin Dauvergne
|
get_acts_of_the_day(date, service).update(validation_locked=False)
|
18 |
3ed29cb7
|
Mikaël Ates
|
|
19 |
|
|
|
20 |
0853d495
|
Mikaël Ates
|
def get_acts_not_locked_of_the_day(date, service=None):
|
21 |
979b59ce
|
Benjamin Dauvergne
|
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 |
3ed29cb7
|
Mikaël Ates
|
|
28 |
|
|
|
29 |
0853d495
|
Mikaël Ates
|
def get_days_with_acts_not_locked(start_day, end_day, service=None):
|
30 |
979b59ce
|
Benjamin Dauvergne
|
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 |
3ed29cb7
|
Mikaël Ates
|
|
42 |
|
|
|
43 |
0853d495
|
Mikaël Ates
|
def get_days_with_all_acts_locked(start_day, end_day, service=None):
|
44 |
979b59ce
|
Benjamin Dauvergne
|
locked_days = get_days_with_acts_not_locked(start_day, end_day,
|
45 |
|
|
service)
|
46 |
e1a725aa
|
Benjamin Dauvergne
|
return sorted(set(date_generator(start_day, end_day)) - set(locked_days))
|
47 |
3ed29cb7
|
Mikaël Ates
|
|
48 |
|
|
|
49 |
bb3869d2
|
Mikaël Ates
|
@transaction.commit_manually
|
50 |
93895776
|
Mikaël Ates
|
def automated_validation(date, service, user, commit=True):
|
51 |
3ed29cb7
|
Mikaël Ates
|
nb_acts_double = 0
|
52 |
|
|
nb_acts_validated = 0
|
53 |
93895776
|
Mikaël Ates
|
nb_acts_abs_non_exc = 0
|
54 |
|
|
nb_acts_abs_exc = 0
|
55 |
08bf3477
|
Mikaël Ates
|
nb_acts_abs_inter = 0
|
56 |
93895776
|
Mikaël Ates
|
nb_acts_annul_nous = 0
|
57 |
|
|
nb_acts_annul_famille = 0
|
58 |
6c9a3a0d
|
Mikaël Ates
|
nb_acts_reporte = 0
|
59 |
93895776
|
Mikaël Ates
|
nb_acts_abs_ess_pps = 0
|
60 |
|
|
nb_acts_enf_hosp = 0
|
61 |
08bf3477
|
Mikaël Ates
|
nb_acts_losts = 0
|
62 |
0853d495
|
Mikaël Ates
|
acts_of_the_day = get_acts_of_the_day(date, service)
|
63 |
93895776
|
Mikaël Ates
|
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 |
08bf3477
|
Mikaël Ates
|
if act.is_state('ABS_INTER'):
|
69 |
|
|
nb_acts_abs_inter = nb_acts_abs_inter + 1
|
70 |
93895776
|
Mikaël Ates
|
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 |
6c9a3a0d
|
Mikaël Ates
|
if act.is_state('REPORTE'):
|
75 |
|
|
nb_acts_reporte = nb_acts_reporte + 1
|
76 |
93895776
|
Mikaël Ates
|
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 |
740e0f07
|
Mikaël Ates
|
if act.is_state('ACT_LOST') or act.is_lost:
|
81 |
08bf3477
|
Mikaël Ates
|
nb_acts_losts = nb_acts_losts + 1
|
82 |
3fe7dd91
|
Mikaël Ates
|
if not act.get_state():
|
83 |
|
|
act.set_state('VALIDE', author=user, auto=True)
|
84 |
93895776
|
Mikaël Ates
|
|
85 |
3ed29cb7
|
Mikaël Ates
|
nb_acts_total = len(acts_of_the_day)
|
86 |
|
|
patients = {}
|
87 |
|
|
if service.name == 'CMPP':
|
88 |
|
|
# Verification des actes en doubles
|
89 |
|
|
acts = [act for act in acts_of_the_day \
|
90 |
b53f12e0
|
Mikaël Ates
|
if not act.is_lost and (act.is_state('VALIDE') or
|
91 |
|
|
act.is_state('NON_VALIDE') or act.is_state('ACT_DOUBLE'))]
|
92 |
3ed29cb7
|
Mikaël Ates
|
for act in acts:
|
93 |
|
|
if act.patient not in patients:
|
94 |
|
|
patients[act.patient] = []
|
95 |
|
|
patients[act.patient].append(act)
|
96 |
|
|
for patient, acts in patients.items():
|
97 |
|
|
if len(acts) > 1:
|
98 |
|
|
# Si plusieurs actes pour un même patient le même jour
|
99 |
|
|
# On valide le premier, s'il n'est pas déja validé.
|
100 |
|
|
# Les autres sont marqués actes en double
|
101 |
|
|
found_one = False
|
102 |
1aae39b5
|
Mikaël Ates
|
acts_t = []
|
103 |
3ed29cb7
|
Mikaël Ates
|
for act in acts:
|
104 |
1aae39b5
|
Mikaël Ates
|
if act.is_billed:
|
105 |
|
|
found_one = True
|
106 |
c756ec89
|
Mikaël Ates
|
nb_acts_validated = nb_acts_validated + 1
|
107 |
1aae39b5
|
Mikaël Ates
|
else:
|
108 |
|
|
acts_t.append(act)
|
109 |
|
|
for act in acts_t:
|
110 |
4e902357
|
Mikaël Ates
|
if not found_one or not act.is_billable():
|
111 |
93895776
|
Mikaël Ates
|
if not act.is_state('VALIDE') and commit:
|
112 |
3ed29cb7
|
Mikaël Ates
|
act.set_state('VALIDE', author=user, auto=True)
|
113 |
4e902357
|
Mikaël Ates
|
if act.is_billable():
|
114 |
|
|
found_one = True
|
115 |
3ed29cb7
|
Mikaël Ates
|
nb_acts_validated = nb_acts_validated + 1
|
116 |
|
|
else:
|
117 |
93895776
|
Mikaël Ates
|
if commit:
|
118 |
|
|
act.set_state('ACT_DOUBLE', author=user, auto=True)
|
119 |
3ed29cb7
|
Mikaël Ates
|
nb_acts_double = nb_acts_double + 1
|
120 |
|
|
else:
|
121 |
9f81a4a0
|
Mikaël Ates
|
if not acts[0].is_state('VALIDE') and commit:
|
122 |
3ed29cb7
|
Mikaël Ates
|
acts[0].set_state('VALIDE', author=user, auto=True)
|
123 |
93895776
|
Mikaël Ates
|
nb_acts_validated = nb_acts_validated + 1
|
124 |
3ed29cb7
|
Mikaël Ates
|
else:
|
125 |
3198d138
|
Mikaël Ates
|
acts = [act for act in acts_of_the_day if act.is_state('NON_VALIDE') or act.is_state('ACT_DOUBLE')]
|
126 |
3ed29cb7
|
Mikaël Ates
|
for act in acts:
|
127 |
93895776
|
Mikaël Ates
|
if commit:
|
128 |
|
|
act.set_state('VALIDE', author=user, auto=True)
|
129 |
3ed29cb7
|
Mikaël Ates
|
nb_acts_validated = nb_acts_validated + 1
|
130 |
93895776
|
Mikaël Ates
|
|
131 |
214ab9fb
|
Mikaël Ates
|
for act in acts_of_the_day:
|
132 |
e91381a0
|
Mikaël Ates
|
if commit and (act.is_lost or act.is_billed):
|
133 |
|
|
state = act.get_state()
|
134 |
bb3869d2
|
Mikaël Ates
|
if not state or state.state_name == 'NON_VALIDE':
|
135 |
e91381a0
|
Mikaël Ates
|
act.set_state('VALIDE', author=user, auto=True)
|
136 |
214ab9fb
|
Mikaël Ates
|
|
137 |
3ed29cb7
|
Mikaël Ates
|
# Acts locking
|
138 |
|
|
for act in acts_of_the_day:
|
139 |
93895776
|
Mikaël Ates
|
if commit:
|
140 |
|
|
act.validation_locked = True
|
141 |
|
|
act.save()
|
142 |
|
|
if service.name == 'CMPP' and commit:
|
143 |
3ed29cb7
|
Mikaël Ates
|
for patient, _ in patients.items():
|
144 |
|
|
patient.create_diag_healthcare(user)
|
145 |
a4b52050
|
Mikaël Ates
|
patient.automated_switch_state(user)
|
146 |
bb3869d2
|
Mikaël Ates
|
transaction.commit()
|
147 |
93895776
|
Mikaël Ates
|
return (nb_acts_total, nb_acts_validated, nb_acts_double,
|
148 |
08bf3477
|
Mikaël Ates
|
nb_acts_abs_non_exc, nb_acts_abs_exc, nb_acts_abs_inter, nb_acts_annul_nous,
|
149 |
6c9a3a0d
|
Mikaël Ates
|
nb_acts_annul_famille, nb_acts_reporte, nb_acts_abs_ess_pps,
|
150 |
08bf3477
|
Mikaël Ates
|
nb_acts_enf_hosp, nb_acts_losts)
|