1
|
# -*- coding: utf-8 -*-
|
2
|
from django.db import models
|
3
|
from django.contrib.auth.models import User
|
4
|
|
5
|
from calebasse.agenda.models import Event, EventType
|
6
|
from calebasse.agenda.managers import EventManager
|
7
|
from calebasse.dossiers.models import SessadHealthCareNotification
|
8
|
|
9
|
from validation_states import VALIDATION_STATES
|
10
|
|
11
|
|
12
|
class ActValidationState(models.Model):
|
13
|
|
14
|
class Meta:
|
15
|
app_label = 'actes'
|
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)
|
24
|
previous_state = models.ForeignKey('ActValidationState',
|
25
|
verbose_name=u'Etat précédent',
|
26
|
editable=False, blank=True, null=True)
|
27
|
# To record if the validation has be done by the automated validation
|
28
|
auto = models.BooleanField(default=False,
|
29
|
verbose_name=u'Vérouillage')
|
30
|
|
31
|
def __unicode__(self):
|
32
|
return self.state_name + ' ' + str(self.created)
|
33
|
|
34
|
|
35
|
class Act(models.Model):
|
36
|
patient = models.ForeignKey('dossiers.PatientRecord')
|
37
|
date = models.DateTimeField()
|
38
|
act_type = models.ForeignKey('ressources.ActType',
|
39
|
verbose_name=u'Type d\'acte')
|
40
|
validation_locked = models.BooleanField(default=False,
|
41
|
verbose_name=u'Vérouillage')
|
42
|
is_billed = models.BooleanField(default=False,
|
43
|
verbose_name=u'Facturé')
|
44
|
switch_billable = models.BooleanField(default=False,
|
45
|
verbose_name=u'Inverser type facturable')
|
46
|
transport_company = models.ForeignKey('ressources.TransportCompany',
|
47
|
blank=True,
|
48
|
null=True,
|
49
|
verbose_name=u'Compagnie de transport')
|
50
|
transport_type = models.ForeignKey('ressources.TransportType',
|
51
|
blank=True,
|
52
|
null=True,
|
53
|
verbose_name=u'Type de transport')
|
54
|
doctors = models.ManyToManyField('personnes.Worker',
|
55
|
limit_choices_to={'type__intervene': True},
|
56
|
verbose_name=u'Thérapeutes')
|
57
|
histories = models.ManyToManyField('HistoryAct')
|
58
|
|
59
|
def is_absent(self):
|
60
|
if self.get_state() in ('ABS_NON_EXC', 'ABS_EXC', 'ANNUL_NOUS',
|
61
|
'ANNUL_FAMILLE', 'ABS_ESS_PPS', 'ENF_HOSP'):
|
62
|
return True
|
63
|
|
64
|
def get_state(self):
|
65
|
return self.actvalidationstate_set.latest('created')
|
66
|
|
67
|
def is_state(self, state_name):
|
68
|
state = self.get_state()
|
69
|
if state and state.state_name == state_name:
|
70
|
return True
|
71
|
return False
|
72
|
|
73
|
def set_state(self, state_name, author, auto=False,
|
74
|
change_state_check=True):
|
75
|
if not author:
|
76
|
raise Exception('Missing author to set state')
|
77
|
if not state_name in VALIDATION_STATES.keys():
|
78
|
raise Exception("Etat d'acte non existant %s")
|
79
|
current_state = self.get_state()
|
80
|
ActValidationState(act=self, state_name=state_name,
|
81
|
author=author, previous_state=current_state).save()
|
82
|
|
83
|
def is_billable(self):
|
84
|
if (self.act_type.billable and not self.switch_billable) or \
|
85
|
(not self.act_type.billable and self.switch_billable):
|
86
|
return True
|
87
|
|
88
|
# START Specific to sessad healthcare
|
89
|
def was_covered_by_notification(self):
|
90
|
notifications = \
|
91
|
SessadHealthCareNotification.objects.filter(patient=self.patient,
|
92
|
start_date__lte=self.date, end_date__gte=self.date)
|
93
|
if notifications:
|
94
|
return True
|
95
|
# END Specific to sessad healthcare
|
96
|
|
97
|
def __unicode__(self):
|
98
|
return '{0} le {1} pour {2} avec {3}'.format(
|
99
|
self.act_type, self.date, self.patient,
|
100
|
', '.join(map(unicode, self.doctors.all())))
|
101
|
|
102
|
def __repr__(self):
|
103
|
return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
|
104
|
|
105
|
class Meta:
|
106
|
verbose_name = u"Acte"
|
107
|
verbose_name_plural = u"Actes"
|
108
|
ordering = ['-date', 'patient']
|
109
|
|
110
|
|
111
|
class HistoryAct(models.Model):
|
112
|
EVENT_CHOICES = (
|
113
|
('NR', u'Nouveau rendez-vous'),
|
114
|
('AV', u'Acte validé'),
|
115
|
('CF', u'En cours de facturation'),
|
116
|
('AF', u'Acte facturé'),
|
117
|
)
|
118
|
date = models.DateTimeField()
|
119
|
event_type = models.CharField(max_length=2,
|
120
|
choices=EVENT_CHOICES)
|
121
|
description = models.TextField(default='')
|
122
|
|
123
|
|
124
|
class EventActManager(EventManager):
|
125
|
|
126
|
def create_patient_appointment(self, creator, title, patient, participants,
|
127
|
act_type, service, start_datetime, end_datetime, description='',
|
128
|
room=None, note=None, **rrule_params):
|
129
|
"""
|
130
|
This method allow you to create a new patient appointment quickly
|
131
|
|
132
|
Args:
|
133
|
title: patient appointment title (str)
|
134
|
patient: Patient object
|
135
|
participants: List of CalebasseUser (therapists)
|
136
|
act_type: ActType object
|
137
|
service: Service object. Use session service by defaut
|
138
|
start_datetime: datetime with the start date and time
|
139
|
end_datetime: datetime with the end date and time
|
140
|
freq, count, until, byweekday, rrule_params:
|
141
|
follow the ``dateutils`` API (see http://labix.org/python-dateutil)
|
142
|
|
143
|
Example:
|
144
|
Look at calebasse.agenda.tests.EventTest (test_create_appointments
|
145
|
method)
|
146
|
"""
|
147
|
|
148
|
event_type, created = EventType.objects.get_or_create(
|
149
|
label=u"Rendez-vous patient"
|
150
|
)
|
151
|
|
152
|
history = HistoryAct.objects.create(
|
153
|
date = datetime.now(),
|
154
|
event_type = 'NR')
|
155
|
act_event = EventAct.objects.create(
|
156
|
title=title,
|
157
|
event_type=event_type,
|
158
|
patient=patient,
|
159
|
act_type=act_type,
|
160
|
date=start_datetime,
|
161
|
histories = [history]
|
162
|
)
|
163
|
act_event.doctors = participants
|
164
|
ActValidationState(act=act_event, state_name='NON_VALIDE',
|
165
|
author=creator, previous_state=None).save()
|
166
|
|
167
|
return self._set_event(act_event, participants, description,
|
168
|
services=[service], start_datetime=start_datetime,
|
169
|
end_datetime=end_datetime,
|
170
|
room=room, note=note, **rrule_params)
|
171
|
|
172
|
|
173
|
class EventAct(Act, Event):
|
174
|
objects = EventActManager()
|
175
|
|
176
|
VALIDATION_CODE_CHOICES = (
|
177
|
('absent', u'Absent'),
|
178
|
('present', u'Présent'),
|
179
|
)
|
180
|
attendance = models.CharField(max_length=16,
|
181
|
choices=VALIDATION_CODE_CHOICES,
|
182
|
default='absent',
|
183
|
verbose_name=u'Présence')
|
184
|
convocation_sent = models.BooleanField(blank=True,
|
185
|
verbose_name=u'Convoqué')
|
186
|
|
187
|
def __unicode__(self):
|
188
|
return 'Rdv le {0} de {1} avec {2} pour {3}'.format(
|
189
|
self.occurrence_set.all()[0].start_time, self.patient,
|
190
|
', '.join(map(unicode, self.participants.all())),
|
191
|
self.act_type)
|
192
|
|
193
|
def __repr__(self):
|
194
|
return '<%s %r %r>' % (self.__class__.__name__, unicode(self),
|
195
|
self.id)
|
196
|
|
197
|
class Meta:
|
198
|
verbose_name = 'Rendez-vous patient'
|
199
|
verbose_name_plural = 'Rendez-vous patient'
|
200
|
ordering = ['-date', 'patient']
|