1
|
# -*- coding: utf-8 -*-
|
2
|
from datetime import date, time
|
3
|
|
4
|
from django.db import models
|
5
|
from django.contrib.auth.models import User
|
6
|
|
7
|
import reversion
|
8
|
|
9
|
from calebasse.agenda.models import Event, EventType
|
10
|
from calebasse.agenda.managers import EventManager
|
11
|
from calebasse.ressources.models import ServiceLinkedAbstractModel
|
12
|
from ..middleware.request import get_request
|
13
|
|
14
|
from validation_states import VALIDATION_STATES, NON_VALIDE
|
15
|
|
16
|
|
17
|
class ActValidationState(models.Model):
|
18
|
|
19
|
class Meta:
|
20
|
app_label = 'actes'
|
21
|
ordering = ('-created',)
|
22
|
|
23
|
act = models.ForeignKey('actes.Act',
|
24
|
verbose_name=u'Acte', editable=False)
|
25
|
state_name = models.CharField(max_length=150)
|
26
|
created = models.DateTimeField(u'Création', auto_now_add=True)
|
27
|
author = \
|
28
|
models.ForeignKey(User,
|
29
|
verbose_name=u'Auteur', editable=False, blank=True, null=True)
|
30
|
previous_state = models.ForeignKey('ActValidationState',
|
31
|
verbose_name=u'Etat précédent',
|
32
|
editable=False, blank=True, null=True)
|
33
|
# To record if the validation has be done by the automated validation
|
34
|
auto = models.BooleanField(default=False,
|
35
|
verbose_name=u'Validaté automatiquement')
|
36
|
|
37
|
def __repr__(self):
|
38
|
return self.state_name + ' ' + str(self.created)
|
39
|
|
40
|
def __unicode__(self):
|
41
|
return VALIDATION_STATES[self.state_name]
|
42
|
|
43
|
|
44
|
class ActManager(models.Manager):
|
45
|
def for_today(self, today=None):
|
46
|
today = today or date.today()
|
47
|
return self.filter(date=today)
|
48
|
|
49
|
def create_act(self, author=None, **kwargs):
|
50
|
act = Act(**kwargs)
|
51
|
last_validation_state = ActValidationState.objects.create(
|
52
|
act=act,state_name='NON_VALIDE',
|
53
|
author=author, previous_state=None)
|
54
|
act.last_validation_state = last_validation_state
|
55
|
act.save()
|
56
|
return act
|
57
|
|
58
|
def next_acts(self, patient_record, today=None):
|
59
|
today = today or date.today()
|
60
|
return self.filter(date__gte=today) \
|
61
|
.filter(patient=patient_record) \
|
62
|
.order_by('date')
|
63
|
|
64
|
def last_acts(self, patient_record, today=None):
|
65
|
today = today or date.today()
|
66
|
return self.filter(date__lte=today) \
|
67
|
.filter(patient=patient_record) \
|
68
|
.order_by('-date')
|
69
|
|
70
|
|
71
|
class Act(models.Model):
|
72
|
objects = ActManager()
|
73
|
|
74
|
patient = models.ForeignKey('dossiers.PatientRecord')
|
75
|
date = models.DateField(u'Date', db_index=True)
|
76
|
time = models.TimeField(u'Heure', blank=True, null=True, default=time(), db_index=True)
|
77
|
_duration = models.IntegerField(u'Durée en minutes', blank=True, null=True, default=0)
|
78
|
act_type = models.ForeignKey('ressources.ActType',
|
79
|
verbose_name=u'Type d\'acte')
|
80
|
validation_locked = models.BooleanField(default=False,
|
81
|
verbose_name=u'Vérouillage', db_index=True)
|
82
|
is_billed = models.BooleanField(default=False,
|
83
|
verbose_name=u'Facturé', db_index=True)
|
84
|
is_lost = models.BooleanField(default=False,
|
85
|
verbose_name=u'Acte perdu', db_index=True)
|
86
|
last_validation_state = models.ForeignKey(ActValidationState,
|
87
|
related_name='+',
|
88
|
null=True, default=None,
|
89
|
on_delete=models.SET_NULL)
|
90
|
valide = models.BooleanField(default=False,
|
91
|
verbose_name=u'Validé', db_index=True)
|
92
|
switch_billable = models.BooleanField(default=False,
|
93
|
verbose_name=u'Inverser type facturable')
|
94
|
healthcare = models.ForeignKey('dossiers.HealthCare',
|
95
|
blank=True,
|
96
|
null=True,
|
97
|
on_delete=models.SET_NULL,
|
98
|
verbose_name=u'Prise en charge utilisée pour facturer (CMPP)')
|
99
|
transport_company = models.ForeignKey('ressources.TransportCompany',
|
100
|
blank=True,
|
101
|
null=True,
|
102
|
on_delete=models.SET_NULL,
|
103
|
verbose_name=u'Compagnie de transport')
|
104
|
transport_type = models.ForeignKey('ressources.TransportType',
|
105
|
blank=True,
|
106
|
null=True,
|
107
|
on_delete=models.SET_NULL,
|
108
|
verbose_name=u'Type de transport')
|
109
|
doctors = models.ManyToManyField('personnes.Worker',
|
110
|
limit_choices_to={'type__intervene': True},
|
111
|
verbose_name=u'Intervenants')
|
112
|
pause = models.BooleanField(default=False,
|
113
|
verbose_name=u'Pause facturation', db_index=True)
|
114
|
parent_event = models.ForeignKey('agenda.Event',
|
115
|
verbose_name=u'Rendez-vous lié',
|
116
|
blank=True, null=True,
|
117
|
on_delete=models.SET_NULL)
|
118
|
VALIDATION_CODE_CHOICES = (
|
119
|
('absent', u'Absent'),
|
120
|
('present', u'Présent'),
|
121
|
)
|
122
|
attendance = models.CharField(max_length=16,
|
123
|
choices=VALIDATION_CODE_CHOICES,
|
124
|
default='absent',
|
125
|
verbose_name=u'Présence')
|
126
|
comment = models.TextField(u'Commentaire', blank=True, null=True)
|
127
|
old_id = models.CharField(max_length=256,
|
128
|
verbose_name=u'Ancien ID', blank=True, null=True)
|
129
|
|
130
|
@property
|
131
|
def event(self):
|
132
|
if self.parent_event:
|
133
|
return self.parent_event.today_occurrence(self.date)
|
134
|
return None
|
135
|
|
136
|
@property
|
137
|
def start_datetime(self):
|
138
|
event = self.event
|
139
|
if event:
|
140
|
return event.start_datetime
|
141
|
return self.date
|
142
|
|
143
|
def get_hc_tag(self):
|
144
|
if self.healthcare:
|
145
|
beg = None
|
146
|
try:
|
147
|
self.healthcare.cmpphealthcaretreatment
|
148
|
beg = 'T'
|
149
|
except:
|
150
|
pass
|
151
|
try:
|
152
|
self.healthcare.cmpphealthcarediagnostic
|
153
|
beg = 'D'
|
154
|
except:
|
155
|
pass
|
156
|
if beg:
|
157
|
acts = list(self.healthcare.act_set.order_by('date').values_list('id', flat=True))
|
158
|
beg += str(acts.index(self.id) + 1)
|
159
|
return beg
|
160
|
return None
|
161
|
|
162
|
def is_new(self):
|
163
|
states = self.actvalidationstate_set.all()
|
164
|
states_len = len(states)
|
165
|
return states_len == 0 or \
|
166
|
(states_len == 1 and states[0].state_name == 'NON_VALIDE')
|
167
|
|
168
|
def is_absent(self):
|
169
|
state = self.get_state()
|
170
|
if state and state.state_name in ('ABS_NON_EXC', 'ABS_EXC', 'ABS_INTER', 'ANNUL_NOUS',
|
171
|
'ANNUL_FAMILLE', 'REPORTE', 'ABS_ESS_PPS', 'ENF_HOSP'):
|
172
|
return True
|
173
|
return False
|
174
|
|
175
|
def get_state(self):
|
176
|
return self.last_validation_state
|
177
|
|
178
|
def is_state(self, state_name):
|
179
|
state = self.get_state()
|
180
|
if state and state.state_name == state_name:
|
181
|
return True
|
182
|
return False
|
183
|
|
184
|
def set_state(self, state_name, author, auto=False,
|
185
|
change_state_check=True):
|
186
|
if not self.id:
|
187
|
self.save()
|
188
|
if not author:
|
189
|
raise Exception('Missing author to set state')
|
190
|
if not state_name in VALIDATION_STATES.keys():
|
191
|
raise Exception("Etat d'acte non existant %s")
|
192
|
current_state = self.get_state()
|
193
|
last_validation_state = ActValidationState.objects.create(
|
194
|
act=self,
|
195
|
state_name=state_name,
|
196
|
author=author,
|
197
|
previous_state=current_state)
|
198
|
self.last_validation_state = last_validation_state
|
199
|
if state_name == 'VALIDE':
|
200
|
self.valide = True
|
201
|
else:
|
202
|
self.valide = False
|
203
|
self.save()
|
204
|
|
205
|
def is_billable(self):
|
206
|
billable = self.act_type.billable
|
207
|
if (billable and not self.switch_billable) or \
|
208
|
(not billable and self.switch_billable):
|
209
|
return True
|
210
|
return False
|
211
|
|
212
|
# START Specific to sessad healthcare
|
213
|
def was_covered_by_notification(self):
|
214
|
from calebasse.dossiers.models import SessadHealthCareNotification
|
215
|
notifications = \
|
216
|
SessadHealthCareNotification.objects.filter(patient=self.patient,
|
217
|
start_date__lte=self.date, end_date__gte=self.date)
|
218
|
if notifications:
|
219
|
return True
|
220
|
# END Specific to sessad healthcare
|
221
|
|
222
|
# START Specific to cmpp healthcare
|
223
|
def is_act_covered_by_diagnostic_healthcare(self):
|
224
|
from calebasse.dossiers.models import CmppHealthCareDiagnostic
|
225
|
from validation import are_all_acts_of_the_day_locked
|
226
|
# L'acte est déja pointé par une prise en charge
|
227
|
if self.is_billed:
|
228
|
# Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
|
229
|
# y est une healthcare non vide
|
230
|
if self.healthcare and isinstance(self.healthcare,
|
231
|
CmppHealthCareDiagnostic):
|
232
|
return (False, self.healthcare)
|
233
|
elif self.healthcare:
|
234
|
return (False, None)
|
235
|
# L'acte doit être facturable
|
236
|
if not (are_all_acts_of_the_day_locked(self.date) and \
|
237
|
self.is_state('VALIDE') and self.is_billable()):
|
238
|
return (False, None)
|
239
|
# On prend la dernière prise en charge diagnostic, l'acte ne sera pas
|
240
|
# pris en charge sur une prise en charge précédente
|
241
|
# Il peut arriver que l'on ait ajouté une prise en charge de
|
242
|
# traitement alors que tous les actes pour le diag ne sont pas encore facturés
|
243
|
try:
|
244
|
hc = CmppHealthCareDiagnostic.objects.\
|
245
|
filter(patient=self.patient).latest('start_date')
|
246
|
except:
|
247
|
return (False, None)
|
248
|
if not hc:
|
249
|
# On pourrait ici créer une prise en charge de diagnostic
|
250
|
return (False, None)
|
251
|
if self.date < hc.start_date:
|
252
|
return (False, None)
|
253
|
# Les acts facturés déja couvert par la prise en charge sont pointés
|
254
|
# dans hc.act_set.all()
|
255
|
nb_acts_billed = len(hc.act_set.all())
|
256
|
if nb_acts_billed >= hc.get_act_number():
|
257
|
return (False, None)
|
258
|
# Il faut ajouter les actes facturables non encore facturés qui précède cet
|
259
|
# acte
|
260
|
acts_billable = [a for a in self.patient.act_set.\
|
261
|
filter(is_billed=False).order_by('date') \
|
262
|
if are_all_acts_of_the_day_locked(a.date) and \
|
263
|
a.is_state('VALIDE') and a.is_billable()]
|
264
|
count = 0
|
265
|
for a in acts_billable:
|
266
|
if nb_acts_billed + count >= hc.get_act_number():
|
267
|
return (False, None)
|
268
|
if a.date >= hc.start_date:
|
269
|
if a.id == self.id:
|
270
|
return (True, hc)
|
271
|
count = count + 1
|
272
|
return (False, None)
|
273
|
|
274
|
# def is_act_covered_by_treatment_healthcare(self):
|
275
|
# # L'acte est déja pointé par une prise en charge
|
276
|
# if self.is_billed:
|
277
|
# # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
|
278
|
# # y est une healthcare non vide
|
279
|
# if self.healthcare and isinstance(self.healthcare,
|
280
|
# CmppHealthCareTreatment):
|
281
|
# return (False, self.healthcare)
|
282
|
# elif self.healthcare:
|
283
|
# return (False, None)
|
284
|
# # L'acte doit être facturable
|
285
|
# if not (are_all_acts_of_the_day_locked(self.date) and \
|
286
|
# self.is_state('VALIDE') and self.is_billable()):
|
287
|
# return (False, None)
|
288
|
# # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
|
289
|
# # pris en charge sur une prise en charge précédente
|
290
|
# # Il peut arriver que l'on ait ajouté une prise en charge de
|
291
|
# # traitement alors que tous les actes pour le diag ne sont pas encore facturés
|
292
|
# try:
|
293
|
# hc = CmppHealthCareTreatment.objects.\
|
294
|
# filter(patient=self.patient).latest('start_date')
|
295
|
# except:
|
296
|
# return (False, None)
|
297
|
# if not hc:
|
298
|
# return (False, None)
|
299
|
# if self.date < hc.start_date or self.date > hc.end_date:
|
300
|
# return (False, None)
|
301
|
# # Les acts facturés déja couvert par la prise en charge sont pointés
|
302
|
# # dans hc.act_set.all()
|
303
|
# nb_acts_billed = len(hc.act_set.all())
|
304
|
# if nb_acts_billed >= hc.get_act_number():
|
305
|
# return (False, None)
|
306
|
# # Il faut ajouter les actes facturables non encore facturés qui précède cet
|
307
|
# # acte
|
308
|
# acts_billable = [a for a in self.patient.act_set.\
|
309
|
# filter(is_billed=False).order_by('date') \
|
310
|
# if are_all_acts_of_the_day_locked(a.date) and \
|
311
|
# a.is_state('VALIDE') and a.is_billable()]
|
312
|
# count = 0
|
313
|
# for a in acts_billable:
|
314
|
# if nb_acts_billed + count >= hc.get_act_number():
|
315
|
# return (False, None)
|
316
|
# if a.date >= hc.start_date and a.date <= hc.end_date:
|
317
|
# if a.id == self.id:
|
318
|
# return (True, hc)
|
319
|
# count = count + 1
|
320
|
# return (False, None)
|
321
|
# END Specific to cmpp healthcare
|
322
|
|
323
|
def save(self, *args, **kwargs):
|
324
|
super(Act, self).save(*args, **kwargs)
|
325
|
|
326
|
def duration(self):
|
327
|
'''Return a displayable duration for this field.'''
|
328
|
hours, remainder = divmod(self._duration, 60)
|
329
|
return '%02d:%02d' % (hours, remainder)
|
330
|
|
331
|
def __unicode__(self):
|
332
|
if self.time:
|
333
|
return u'{0} le {1} à {2} pour {3} avec {4}'.format(
|
334
|
self.act_type, self.date.strftime('%d/%m/%Y'),
|
335
|
self.time.strftime('%H:%M'), self.patient,
|
336
|
', '.join(map(unicode, self.doctors.all())))
|
337
|
return u'{0} le {1} pour {2} avec {3}'.format(
|
338
|
self.act_type, self.date.strftime('%d/%m/%Y'), self.patient,
|
339
|
', '.join(map(unicode, self.doctors.all())))
|
340
|
|
341
|
def __repr__(self):
|
342
|
return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
|
343
|
|
344
|
def cancel(self):
|
345
|
'''Parent event is canceled completely, or partially, act upon it.
|
346
|
'''
|
347
|
new_act = self.actvalidationstate_set.count() > 1
|
348
|
if self.date <= date.today():
|
349
|
if new_act:
|
350
|
self.set_state('ANNUL_NOUS', get_request().user)
|
351
|
self.parent_event = None
|
352
|
self.save()
|
353
|
else:
|
354
|
self.delete()
|
355
|
|
356
|
class Meta:
|
357
|
verbose_name = u"Acte"
|
358
|
verbose_name_plural = u"Actes"
|
359
|
ordering = ['-date', 'patient']
|
360
|
|
361
|
|
362
|
class ValidationMessage(ServiceLinkedAbstractModel):
|
363
|
validation_date = models.DateTimeField()
|
364
|
what = models.CharField(max_length=256)
|
365
|
who = models.ForeignKey(User)
|
366
|
when = models.DateTimeField(auto_now_add=True)
|