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
|
is_lost = models.BooleanField(default=False,
|
80
|
verbose_name=u'Acte perdu', db_index=True)
|
81
|
last_validation_state = models.ForeignKey(ActValidationState,
|
82
|
related_name='+',
|
83
|
null=True, default=None,
|
84
|
on_delete=models.SET_NULL)
|
85
|
valide = models.BooleanField(default=False,
|
86
|
verbose_name=u'Validé', db_index=True)
|
87
|
switch_billable = models.BooleanField(default=False,
|
88
|
verbose_name=u'Inverser type facturable')
|
89
|
healthcare = models.ForeignKey('dossiers.HealthCare',
|
90
|
blank=True,
|
91
|
null=True,
|
92
|
on_delete=models.SET_NULL,
|
93
|
verbose_name=u'Prise en charge utilisée pour facturer (CMPP)')
|
94
|
transport_company = models.ForeignKey('ressources.TransportCompany',
|
95
|
blank=True,
|
96
|
null=True,
|
97
|
on_delete=models.SET_NULL,
|
98
|
verbose_name=u'Compagnie de transport')
|
99
|
transport_type = models.ForeignKey('ressources.TransportType',
|
100
|
blank=True,
|
101
|
null=True,
|
102
|
on_delete=models.SET_NULL,
|
103
|
verbose_name=u'Type de transport')
|
104
|
doctors = models.ManyToManyField('personnes.Worker',
|
105
|
limit_choices_to={'type__intervene': True},
|
106
|
verbose_name=u'Intervenants')
|
107
|
pause = models.BooleanField(default=False,
|
108
|
verbose_name=u'Pause facturation', db_index=True)
|
109
|
parent_event = models.ForeignKey('agenda.Event',
|
110
|
verbose_name=u'Rendez-vous lié',
|
111
|
blank=True, null=True,
|
112
|
on_delete=models.SET_NULL)
|
113
|
VALIDATION_CODE_CHOICES = (
|
114
|
('absent', u'Absent'),
|
115
|
('present', u'Présent'),
|
116
|
)
|
117
|
attendance = models.CharField(max_length=16,
|
118
|
choices=VALIDATION_CODE_CHOICES,
|
119
|
default='absent',
|
120
|
verbose_name=u'Présence')
|
121
|
comment = models.TextField(u'Commentaire', blank=True, null=True)
|
122
|
old_id = models.CharField(max_length=256,
|
123
|
verbose_name=u'Ancien ID', blank=True, null=True)
|
124
|
|
125
|
@property
|
126
|
def event(self):
|
127
|
if self.parent_event:
|
128
|
return self.parent_event.today_occurrence(self.date)
|
129
|
return None
|
130
|
|
131
|
@property
|
132
|
def start_datetime(self):
|
133
|
event = self.event
|
134
|
if event:
|
135
|
return event.start_datetime
|
136
|
return self.date
|
137
|
|
138
|
def get_hc_tag(self):
|
139
|
if self.healthcare:
|
140
|
beg = None
|
141
|
try:
|
142
|
self.healthcare.cmpphealthcaretreatment
|
143
|
beg = 'T'
|
144
|
except:
|
145
|
pass
|
146
|
try:
|
147
|
self.healthcare.cmpphealthcarediagnostic
|
148
|
beg = 'D'
|
149
|
except:
|
150
|
pass
|
151
|
if beg:
|
152
|
acts = list(self.healthcare.act_set.order_by('date').values_list('id', flat=True))
|
153
|
beg += str(acts.index(self.id) + 1)
|
154
|
return beg
|
155
|
return None
|
156
|
|
157
|
def is_new(self):
|
158
|
return (not self.get_state() or self.is_state('NON_VALIDE'))
|
159
|
|
160
|
def is_absent(self):
|
161
|
state = self.get_state()
|
162
|
if state and state.state_name in ('ABS_NON_EXC', 'ABS_EXC', 'ABS_INTER', 'ANNUL_NOUS',
|
163
|
'ANNUL_FAMILLE', 'REPORTE', 'ABS_ESS_PPS', 'ENF_HOSP'):
|
164
|
return True
|
165
|
return False
|
166
|
|
167
|
def is_present(self):
|
168
|
return (not self.is_new() and not self.is_absent())
|
169
|
|
170
|
def get_state(self):
|
171
|
try:
|
172
|
return self.last_validation_state
|
173
|
except:
|
174
|
return None
|
175
|
|
176
|
def is_state(self, state_name):
|
177
|
state = self.get_state()
|
178
|
if state and state.state_name == state_name:
|
179
|
return True
|
180
|
return False
|
181
|
|
182
|
def set_state(self, state_name, author, auto=False,
|
183
|
change_state_check=True):
|
184
|
if not self.id:
|
185
|
self.save()
|
186
|
if self.is_billed:
|
187
|
raise Exception('Billed act state can not be modified')
|
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
|
get_request().record('act-update', '{obj_id} state changed to {state} by {user} from {ip}',
|
204
|
obj_id=self.id, state=last_validation_state, user=author)
|
205
|
self.save()
|
206
|
|
207
|
def is_billable(self):
|
208
|
billable = self.act_type.billable
|
209
|
if (billable and not self.switch_billable) or \
|
210
|
(not billable and self.switch_billable):
|
211
|
return True
|
212
|
return False
|
213
|
|
214
|
# START Specific to sessad healthcare
|
215
|
def was_covered_by_notification(self):
|
216
|
from calebasse.dossiers.models import SessadHealthCareNotification
|
217
|
notifications = \
|
218
|
SessadHealthCareNotification.objects.filter(patient=self.patient,
|
219
|
start_date__lte=self.date, end_date__gte=self.date)
|
220
|
if notifications:
|
221
|
return True
|
222
|
# END Specific to sessad healthcare
|
223
|
|
224
|
def save(self, *args, **kwargs):
|
225
|
if self.parent_event and not self.parent_event.canceled:
|
226
|
super(Act, self).save(*args, **kwargs)
|
227
|
|
228
|
def duration(self):
|
229
|
'''Return a displayable duration for this field.'''
|
230
|
hours, remainder = divmod(self._duration, 60)
|
231
|
return '%02d:%02d' % (hours, remainder)
|
232
|
|
233
|
def __unicode__(self):
|
234
|
if self.time:
|
235
|
return u'{0} le {1} à {2} pour {3} avec {4}'.format(
|
236
|
self.act_type, self.date.strftime('%d/%m/%Y'),
|
237
|
self.time.strftime('%H:%M'), self.patient,
|
238
|
', '.join(map(unicode, self.doctors.all())))
|
239
|
return u'{0} le {1} pour {2} avec {3}'.format(
|
240
|
self.act_type, self.date.strftime('%d/%m/%Y'), self.patient,
|
241
|
', '.join(map(unicode, self.doctors.all())))
|
242
|
|
243
|
def __repr__(self):
|
244
|
return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
|
245
|
|
246
|
def cancel(self):
|
247
|
'''Parent event is canceled completely, or partially, act upon it.
|
248
|
'''
|
249
|
new_act = self.actvalidationstate_set.count() > 1
|
250
|
if self.date <= date.today():
|
251
|
if new_act:
|
252
|
self.set_state('ANNUL_NOUS', get_request().user)
|
253
|
self.parent_event = None
|
254
|
self.save()
|
255
|
else:
|
256
|
self.delete()
|
257
|
|
258
|
def get_invoice_number(self):
|
259
|
try:
|
260
|
if self.is_billed:
|
261
|
return self.invoice_set.order_by('-number')[0].number
|
262
|
except:
|
263
|
pass
|
264
|
return None
|
265
|
|
266
|
def delete(self):
|
267
|
obj_id = self.id
|
268
|
super(Act, self).delete()
|
269
|
|
270
|
class Meta:
|
271
|
verbose_name = u"Acte"
|
272
|
verbose_name_plural = u"Actes"
|
273
|
ordering = ['-date', 'patient']
|
274
|
|
275
|
|
276
|
class ValidationMessage(ServiceLinkedAbstractModel):
|
277
|
validation_date = models.DateTimeField()
|
278
|
what = models.CharField(max_length=256)
|
279
|
who = models.ForeignKey(User)
|
280
|
when = models.DateTimeField(auto_now_add=True)
|