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