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
|
from calebasse.actes.validation_states import VALIDATION_STATES
|
8
|
from calebasse.ressources.models import ServiceLinkedAbstractModel
|
9
|
from ..middleware.request import get_request
|
10
|
|
11
|
class ActValidationState(models.Model):
|
12
|
|
13
|
class Meta:
|
14
|
app_label = 'actes'
|
15
|
ordering = ('-created',)
|
16
|
|
17
|
act = models.ForeignKey('actes.Act',
|
18
|
verbose_name=u'Acte', editable=False)
|
19
|
state_name = models.CharField(max_length=150)
|
20
|
created = models.DateTimeField(u'Création', auto_now_add=True)
|
21
|
author = \
|
22
|
models.ForeignKey(User,
|
23
|
verbose_name=u'Auteur', editable=False, blank=True, null=True,
|
24
|
on_delete=models.SET_NULL)
|
25
|
previous_state = models.ForeignKey('ActValidationState',
|
26
|
verbose_name=u'Etat précédent',
|
27
|
editable=False, blank=True, null=True)
|
28
|
# To record if the validation has be done by the automated validation
|
29
|
auto = models.BooleanField(default=False,
|
30
|
verbose_name=u'Validaté automatiquement')
|
31
|
|
32
|
def __repr__(self):
|
33
|
return self.state_name + ' ' + str(self.created)
|
34
|
|
35
|
def __unicode__(self):
|
36
|
return VALIDATION_STATES[self.state_name]
|
37
|
|
38
|
|
39
|
class ActManager(models.Manager):
|
40
|
def for_today(self, today=None):
|
41
|
today = today or date.today()
|
42
|
return self.filter(date=today)
|
43
|
|
44
|
def create_act(self, author=None, **kwargs):
|
45
|
act = Act(**kwargs)
|
46
|
last_validation_state = ActValidationState.objects.create(
|
47
|
act=act,state_name='NON_VALIDE',
|
48
|
author=author, previous_state=None)
|
49
|
act.last_validation_state = last_validation_state
|
50
|
act.save()
|
51
|
return act
|
52
|
|
53
|
def next_acts(self, patient_record, today=None):
|
54
|
today = today or date.today()
|
55
|
return self.filter(date__gte=today) \
|
56
|
.filter(patient=patient_record) \
|
57
|
.order_by('date')
|
58
|
|
59
|
def last_acts(self, patient_record, today=None):
|
60
|
today = today or date.today()
|
61
|
return self.filter(date__lte=today) \
|
62
|
.filter(patient=patient_record) \
|
63
|
.order_by('-date')
|
64
|
|
65
|
|
66
|
class Act(models.Model):
|
67
|
objects = ActManager()
|
68
|
|
69
|
patient = models.ForeignKey('dossiers.PatientRecord')
|
70
|
date = models.DateField(u'Date', db_index=True)
|
71
|
time = models.TimeField(u'Heure', blank=True, null=True, default=time(), db_index=True)
|
72
|
_duration = models.IntegerField(u'Durée en minutes', blank=True, null=True, default=0)
|
73
|
act_type = models.ForeignKey('ressources.ActType',
|
74
|
verbose_name=u'Type d\'acte')
|
75
|
validation_locked = models.BooleanField(default=False,
|
76
|
verbose_name=u'Vérouillage', db_index=True)
|
77
|
is_billed = models.BooleanField(default=False,
|
78
|
verbose_name=u'Facturé', db_index=True)
|
79
|
already_billed = models.BooleanField(default=False,
|
80
|
verbose_name=u'A déjà été facturé', db_index=True)
|
81
|
is_lost = models.BooleanField(default=False,
|
82
|
verbose_name=u'Acte perdu', db_index=True)
|
83
|
last_validation_state = models.ForeignKey(ActValidationState,
|
84
|
related_name='+',
|
85
|
null=True, default=None,
|
86
|
on_delete=models.SET_NULL)
|
87
|
valide = models.BooleanField(default=False,
|
88
|
verbose_name=u'Validé', db_index=True)
|
89
|
switch_billable = models.BooleanField(default=False,
|
90
|
verbose_name=u'Inverser type facturable')
|
91
|
healthcare = models.ForeignKey('dossiers.HealthCare',
|
92
|
blank=True,
|
93
|
null=True,
|
94
|
on_delete=models.SET_NULL,
|
95
|
verbose_name=u'Prise en charge utilisée pour facturer (CMPP)')
|
96
|
transport_company = models.ForeignKey('ressources.TransportCompany',
|
97
|
blank=True,
|
98
|
null=True,
|
99
|
on_delete=models.SET_NULL,
|
100
|
verbose_name=u'Compagnie de transport')
|
101
|
transport_type = models.ForeignKey('ressources.TransportType',
|
102
|
blank=True,
|
103
|
null=True,
|
104
|
on_delete=models.SET_NULL,
|
105
|
verbose_name=u'Type de transport')
|
106
|
doctors = models.ManyToManyField('personnes.Worker',
|
107
|
limit_choices_to={'type__intervene': True},
|
108
|
verbose_name=u'Intervenants')
|
109
|
pause = models.BooleanField(default=False,
|
110
|
verbose_name=u'Pause facturation', db_index=True)
|
111
|
parent_event = models.ForeignKey('agenda.Event',
|
112
|
verbose_name=u'Rendez-vous lié',
|
113
|
blank=True, null=True,
|
114
|
on_delete=models.SET_NULL)
|
115
|
VALIDATION_CODE_CHOICES = (
|
116
|
('absent', u'Absent'),
|
117
|
('present', u'Présent'),
|
118
|
)
|
119
|
attendance = models.CharField(max_length=16,
|
120
|
choices=VALIDATION_CODE_CHOICES,
|
121
|
default='absent',
|
122
|
verbose_name=u'Présence')
|
123
|
old_id = models.CharField(max_length=256,
|
124
|
verbose_name=u'Ancien ID', blank=True, null=True)
|
125
|
|
126
|
@property
|
127
|
def event(self):
|
128
|
if self.parent_event:
|
129
|
return self.parent_event.today_occurrence(self.date)
|
130
|
return None
|
131
|
|
132
|
@property
|
133
|
def start_datetime(self):
|
134
|
event = self.event
|
135
|
if event:
|
136
|
return event.start_datetime
|
137
|
return self.date
|
138
|
|
139
|
@property
|
140
|
def comment(self):
|
141
|
if self.parent_event:
|
142
|
return self.parent_event.description
|
143
|
return None
|
144
|
|
145
|
@property
|
146
|
def rebilling(self):
|
147
|
if self.already_billed and not self.is_billed and not self.is_lost:
|
148
|
return True
|
149
|
return False
|
150
|
|
151
|
def get_hc_tag(self):
|
152
|
if self.healthcare:
|
153
|
beg = None
|
154
|
try:
|
155
|
self.healthcare.cmpphealthcaretreatment
|
156
|
beg = 'T'
|
157
|
except:
|
158
|
pass
|
159
|
try:
|
160
|
self.healthcare.cmpphealthcarediagnostic
|
161
|
beg = 'D'
|
162
|
except:
|
163
|
pass
|
164
|
if beg:
|
165
|
acts = list(self.healthcare.act_set.order_by('date').values_list('id', flat=True))
|
166
|
beg += str(acts.index(self.id) + 1)
|
167
|
return beg
|
168
|
return None
|
169
|
|
170
|
def is_new(self):
|
171
|
return (not self.get_state() or self.is_state('NON_VALIDE'))
|
172
|
|
173
|
def is_absent(self):
|
174
|
state = self.get_state()
|
175
|
if state and state.state_name in ('ABS_NON_EXC', 'ABS_EXC', 'ABS_INTER', 'ANNUL_NOUS',
|
176
|
'ANNUL_FAMILLE', 'REPORTE', 'ABS_ESS_PPS', 'ENF_HOSP'):
|
177
|
return True
|
178
|
return False
|
179
|
|
180
|
def is_present(self):
|
181
|
return (not self.is_new() and not self.is_absent())
|
182
|
|
183
|
def get_state(self):
|
184
|
try:
|
185
|
return self.last_validation_state
|
186
|
except:
|
187
|
return None
|
188
|
|
189
|
def is_state(self, state_name):
|
190
|
state = self.get_state()
|
191
|
if state and state.state_name == state_name:
|
192
|
return True
|
193
|
return False
|
194
|
|
195
|
def set_state(self, state_name, author, auto=False,
|
196
|
change_state_check=True):
|
197
|
if not self.id:
|
198
|
self.save()
|
199
|
if self.is_billed:
|
200
|
raise Exception('Billed act state can not be modified')
|
201
|
if not author:
|
202
|
raise Exception('Missing author to set state')
|
203
|
if not state_name in VALIDATION_STATES.keys():
|
204
|
raise Exception("Etat d'acte non existant %s")
|
205
|
current_state = self.get_state()
|
206
|
last_validation_state = ActValidationState.objects.create(
|
207
|
act=self,
|
208
|
state_name=state_name,
|
209
|
author=author,
|
210
|
previous_state=current_state)
|
211
|
self.last_validation_state = last_validation_state
|
212
|
if state_name == 'VALIDE':
|
213
|
self.valide = True
|
214
|
else:
|
215
|
self.valide = False
|
216
|
try:
|
217
|
get_request().record('act-update', '{obj_id} state changed to {state} by {user} from {ip}',
|
218
|
obj_id=self.id, state=last_validation_state, user=author)
|
219
|
except:
|
220
|
pass
|
221
|
self.save()
|
222
|
|
223
|
def is_billable(self):
|
224
|
billable = self.act_type.billable
|
225
|
if (billable and not self.switch_billable) or \
|
226
|
(not billable and self.switch_billable):
|
227
|
return True
|
228
|
return False
|
229
|
|
230
|
# START Specific to sessad healthcare
|
231
|
def was_covered_by_notification(self):
|
232
|
from calebasse.dossiers.models import SessadHealthCareNotification
|
233
|
notifications = \
|
234
|
SessadHealthCareNotification.objects.filter(patient=self.patient,
|
235
|
start_date__lte=self.date, end_date__gte=self.date)
|
236
|
if notifications:
|
237
|
return True
|
238
|
# END Specific to sessad healthcare
|
239
|
|
240
|
def save(self, *args, **kwargs):
|
241
|
if self.is_billed:
|
242
|
self.already_billed = True
|
243
|
super(Act, self).save(*args, **kwargs)
|
244
|
|
245
|
def duration(self):
|
246
|
'''Return a displayable duration for this field.'''
|
247
|
hours, remainder = divmod(self._duration, 60)
|
248
|
return '%02d:%02d' % (hours, remainder)
|
249
|
|
250
|
def __unicode__(self):
|
251
|
if self.time:
|
252
|
return u'{0} le {1} à {2} pour {3} avec {4}'.format(
|
253
|
self.act_type, self.date.strftime('%d/%m/%Y'),
|
254
|
self.time.strftime('%H:%M'), self.patient,
|
255
|
', '.join(map(unicode, self.doctors.all())))
|
256
|
return u'{0} le {1} pour {2} avec {3}'.format(
|
257
|
self.act_type, self.date.strftime('%d/%m/%Y'), self.patient,
|
258
|
', '.join(map(unicode, self.doctors.all())))
|
259
|
|
260
|
def __repr__(self):
|
261
|
return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
|
262
|
|
263
|
def cancel(self):
|
264
|
'''Parent event is canceled completely, or partially, act upon it.
|
265
|
'''
|
266
|
new_act = self.actvalidationstate_set.count() > 1
|
267
|
if self.date <= date.today():
|
268
|
if new_act:
|
269
|
self.set_state('ANNUL_NOUS', get_request().user)
|
270
|
self.parent_event = None
|
271
|
self.save()
|
272
|
else:
|
273
|
self.delete()
|
274
|
|
275
|
def get_invoice_number(self):
|
276
|
try:
|
277
|
if self.is_billed:
|
278
|
return self.invoice_set.order_by('-number')[0].number
|
279
|
except:
|
280
|
pass
|
281
|
return None
|
282
|
|
283
|
def delete(self):
|
284
|
obj_id = self.id
|
285
|
super(Act, self).delete()
|
286
|
|
287
|
class Meta:
|
288
|
verbose_name = u"Acte"
|
289
|
verbose_name_plural = u"Actes"
|
290
|
ordering = ['-date', 'patient']
|
291
|
|
292
|
|
293
|
class ValidationMessage(ServiceLinkedAbstractModel):
|
294
|
validation_date = models.DateTimeField()
|
295
|
what = models.CharField(max_length=256)
|
296
|
who = models.ForeignKey(User)
|
297
|
when = models.DateTimeField(auto_now_add=True)
|