Project

General

Profile

Download (14.6 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / actes / models.py @ d694f19f

1
# -*- coding: utf-8 -*-
2
from django.db import models
3
from django.contrib.auth.models import User
4

    
5
import reversion
6

    
7
from calebasse.agenda.models import Event, EventType
8
from calebasse.agenda.managers import EventManager
9
from calebasse.dossiers.models import SessadHealthCareNotification, \
10
    CmppHealthCareDiagnostic, CmppHealthCareTreatment
11
from calebasse.actes.validation import are_all_acts_of_the_day_locked
12
from calebasse.ressources.models import ServiceLinkedAbstractModel
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

    
22
    act = models.ForeignKey('actes.Act',
23
        verbose_name=u'Acte', editable=False)
24
    state_name = models.CharField(max_length=150)
25
    created = models.DateTimeField(u'Création', auto_now_add=True)
26
    author = \
27
        models.ForeignKey(User,
28
        verbose_name=u'Auteur', editable=False)
29
    previous_state = models.ForeignKey('ActValidationState',
30
        verbose_name=u'Etat précédent',
31
        editable=False, blank=True, null=True)
32
    # To record if the validation has be done by the automated validation
33
    auto = models.BooleanField(default=False,
34
            verbose_name=u'Vérouillage')
35

    
36
    def __repr__(self):
37
        return self.state_name + ' ' + str(self.created)
38

    
39
    def __unicode__(self):
40
        return VALIDATION_STATES[self.state_name]
41

    
42

    
43
class Act(models.Model):
44
    patient = models.ForeignKey('dossiers.PatientRecord')
45
    date = models.DateTimeField()
46
    act_type = models.ForeignKey('ressources.ActType',
47
            verbose_name=u'Type d\'acte')
48
    validation_locked = models.BooleanField(default=False,
49
            verbose_name=u'Vérouillage')
50
    is_billed = models.BooleanField(default=False,
51
            verbose_name=u'Facturé')
52
    switch_billable = models.BooleanField(default=False,
53
            verbose_name=u'Inverser type facturable')
54
    healthcare = models.ForeignKey('dossiers.HealthCare',
55
            blank=True,
56
            null=True,
57
            verbose_name=u'Prise en charge utilisée pour facturer (CMPP)')
58
    transport_company = models.ForeignKey('ressources.TransportCompany',
59
            blank=True,
60
            null=True,
61
            verbose_name=u'Compagnie de transport')
62
    transport_type = models.ForeignKey('ressources.TransportType',
63
            blank=True,
64
            null=True,
65
            verbose_name=u'Type de transport')
66
    doctors = models.ManyToManyField('personnes.Worker',
67
            limit_choices_to={'type__intervene': True},
68
            verbose_name=u'Intervenants')
69
    pause = models.BooleanField(default=False,
70
            verbose_name=u'Pause facturation')
71

    
72
    def get_hc_tag(self):
73
        if self.healthcare:
74
            try:
75
                self.healthcare.cmpphealthcaretreatment
76
                return 'T'
77
            except:
78
                pass
79
            try:
80
                self.healthcare.cmpphealthcarediagnostic
81
                acts = self.healthcare.act_set.order_by('date')
82
                i = 1
83
                for act in acts:
84
                    if act.id == self.id:
85
                        return 'D' + str(i)
86
                    i = i + 1
87
            except:
88
                pass
89
        return None
90

    
91
    def is_absent(self):
92
        if self.get_state() in ('ABS_NON_EXC', 'ABS_EXC', 'ANNUL_NOUS',
93
                'ANNUL_FAMILLE', 'ABS_ESS_PPS', 'ENF_HOSP'):
94
            return True
95

    
96
    def get_state(self):
97
        return self.actvalidationstate_set.latest('created')
98

    
99
    def is_state(self, state_name):
100
        state = self.get_state()
101
        if state and state.state_name == state_name:
102
            return True
103
        return False
104

    
105
    def set_state(self, state_name, author, auto=False,
106
            change_state_check=True):
107
        if not author:
108
            raise Exception('Missing author to set state')
109
        if not state_name in VALIDATION_STATES.keys():
110
            raise Exception("Etat d'acte non existant %s")
111
        current_state = self.get_state()
112
        ActValidationState(act=self, state_name=state_name,
113
            author=author, previous_state=current_state).save()
114

    
115
    def is_billable(self):
116
        if (self.act_type.billable and not self.switch_billable) or \
117
                (not self.act_type.billable and self.switch_billable):
118
            return True
119

    
120
    # START Specific to sessad healthcare
121
    def was_covered_by_notification(self):
122
        notifications = \
123
            SessadHealthCareNotification.objects.filter(patient=self.patient,
124
            start_date__lte=self.date, end_date__gte=self.date)
125
        if notifications:
126
            return True
127
    # END Specific to sessad healthcare
128

    
129
    # START Specific to cmpp healthcare
130
    def is_act_covered_by_diagnostic_healthcare(self):
131
        # L'acte est déja pointé par une prise en charge
132
        if self.is_billed:
133
            # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
134
            # y est une healthcare non vide
135
            if self.healthcare and isinstance(self.healthcare,
136
                    CmppHealthCareDiagnostic):
137
                return (False, self.healthcare)
138
            elif self.healthcare:
139
                return (False, None)
140
        # L'acte doit être facturable
141
        if not (are_all_acts_of_the_day_locked(self.date) and \
142
                self.is_state('VALIDE') and self.is_billable()):
143
            return (False, None)
144
        # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
145
        # pris en charge sur une prise en charge précédente
146
        # Il peut arriver que l'on ait ajouté une prise en charge de
147
        # traitement alors que tous les actes pour le diag ne sont pas encore facturés
148
        try:
149
            hc = CmppHealthCareDiagnostic.objects.\
150
                filter(patient=self.patient).latest('start_date')
151
        except:
152
            return (False, None)
153
        if not hc:
154
            # On pourrait ici créer une prise en charge de diagnostic
155
            return (False, None)
156
        if self.date.date() < hc.start_date:
157
            return (False, None)
158
        # Les acts facturés déja couvert par la prise en charge sont pointés
159
        # dans hc.act_set.all()
160
        nb_acts_billed = len(hc.act_set.all())
161
        if nb_acts_billed >= hc.get_act_number():
162
            return (False, None)
163
        # Il faut ajouter les actes facturables non encore facturés qui précède cet
164
        # acte
165
        acts_billable = [a for a in self.patient.act_set.\
166
            filter(is_billed=False).order_by('date') \
167
            if are_all_acts_of_the_day_locked(a.date) and \
168
            a.is_state('VALIDE') and a.is_billable()]
169
        count = 0
170
        for a in acts_billable:
171
            if nb_acts_billed + count >= hc.get_act_number():
172
                return (False, None)
173
            if a.date.date() >= hc.start_date:
174
                if a.id == self.id:
175
                    return (True, hc)
176
                count = count + 1
177
        return (False, None)
178

    
179
#    def is_act_covered_by_treatment_healthcare(self):
180
#        # L'acte est déja pointé par une prise en charge
181
#        if self.is_billed:
182
#            # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
183
#            # y est une healthcare non vide
184
#            if self.healthcare and isinstance(self.healthcare,
185
#                    CmppHealthCareTreatment):
186
#                return (False, self.healthcare)
187
#            elif self.healthcare:
188
#                return (False, None)
189
#        # L'acte doit être facturable
190
#        if not (are_all_acts_of_the_day_locked(self.date) and \
191
#                self.is_state('VALIDE') and self.is_billable()):
192
#            return (False, None)
193
#        # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
194
#        # pris en charge sur une prise en charge précédente
195
#        # Il peut arriver que l'on ait ajouté une prise en charge de
196
#        # traitement alors que tous les actes pour le diag ne sont pas encore facturés
197
#        try:
198
#            hc = CmppHealthCareTreatment.objects.\
199
#                filter(patient=self.patient).latest('start_date')
200
#        except:
201
#            return (False, None)
202
#        if not hc:
203
#            return (False, None)
204
#        if self.date < hc.start_date or self.date > hc.end_date:
205
#            return (False, None)
206
#        # Les acts facturés déja couvert par la prise en charge sont pointés
207
#        # dans hc.act_set.all()
208
#        nb_acts_billed = len(hc.act_set.all())
209
#        if nb_acts_billed >= hc.get_act_number():
210
#            return (False, None)
211
#        # Il faut ajouter les actes facturables non encore facturés qui précède cet
212
#        # acte
213
#        acts_billable = [a for a in self.patient.act_set.\
214
#            filter(is_billed=False).order_by('date') \
215
#            if are_all_acts_of_the_day_locked(a.date) and \
216
#            a.is_state('VALIDE') and a.is_billable()]
217
#        count = 0
218
#        for a in acts_billable:
219
#            if nb_acts_billed + count >= hc.get_act_number():
220
#                return (False, None)
221
#            if a.date >= hc.start_date and a.date <= hc.end_date:
222
#                if a.id == self.id:
223
#                    return (True, hc)
224
#                count = count + 1
225
#        return (False, None)
226
    # END Specific to cmpp healthcare
227

    
228
    def __unicode__(self):
229
        return u'{0} le {1} pour {2} avec {3}'.format(
230
                self.act_type, self.date, self.patient,
231
                ', '.join(map(unicode, self.doctors.all())))
232

    
233
    def __repr__(self):
234
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
235

    
236
    class Meta:
237
        verbose_name = u"Acte"
238
        verbose_name_plural = u"Actes"
239
        ordering = ['-date', 'patient']
240

    
241

    
242
class EventActManager(EventManager):
243

    
244
    def create_patient_appointment(self, creator, title, patient, participants,
245
            act_type, service, start_datetime, end_datetime, description='',
246
            room=None, note=None, **rrule_params):
247
        """
248
        This method allow you to create a new patient appointment quickly
249

    
250
        Args:
251
            title: patient appointment title (str)
252
            patient: Patient object
253
            participants: List of CalebasseUser (therapists)
254
            act_type: ActType object
255
            service: Service object. Use session service by defaut
256
            start_datetime: datetime with the start date and time
257
            end_datetime: datetime with the end date and time
258
            freq, count, until, byweekday, rrule_params:
259
            follow the ``dateutils`` API (see http://labix.org/python-dateutil)
260

    
261
        Example:
262
            Look at calebasse.agenda.tests.EventTest (test_create_appointments
263
            method)
264
        """
265

    
266
        event_type, created = EventType.objects.get_or_create(
267
                label=u"Rendez-vous patient"
268
                )
269

    
270
        act_event = EventAct.objects.create(
271
                title=title,
272
                event_type=event_type,
273
                patient=patient,
274
                act_type=act_type,
275
                date=start_datetime,
276
                )
277
        act_event.doctors = participants
278
        ActValidationState(act=act_event, state_name='NON_VALIDE',
279
            author=creator, previous_state=None).save()
280

    
281
        return self._set_event(act_event, participants, description,
282
                services=[service], start_datetime=start_datetime,
283
                end_datetime=end_datetime,
284
                room=room, note=note, **rrule_params)
285

    
286
    def modify_patient_appointment(self, creator, title, patient, participants,
287
            act_type, service, start_datetime, end_datetime, description='',
288
            room=None, note=None, **rrule_params):
289
        """
290
        This method allow you to create a new patient appointment quickly
291

    
292
        Args:
293
            creator: author of the modification
294
            title: patient appointment title (str)
295
            patient: Patient object
296
            participants: List of CalebasseUser (therapists)
297
            act_type: ActType object
298
            service: Service object. Use session service by defaut
299
            start_datetime: datetime with the start date and time
300
            end_datetime: datetime with the end date and time
301
            description: description of the event
302
            room: room where the event will take place
303
            freq, count, until, byweekday, rrule_params:
304
            follow the ``dateutils`` API (see http://labix.org/python-dateutil)
305

    
306
        Example:
307
            Look at calebasse.agenda.tests.EventTest (test_create_appointments
308
            method)
309
        """
310

    
311
        event_type, created = EventType.objects.get_or_create(
312
                label=u"Rendez-vous patient"
313
                )
314

    
315
        act_event = EventAct.objects.create(
316
                title=title,
317
                event_type=event_type,
318
                patient=patient,
319
                act_type=act_type,
320
                date=start_datetime,
321
                )
322
        act_event.doctors = participants
323
        ActValidationState(act=act_event, state_name=NON_VALIDE,
324
            author=creator, previous_state=None).save()
325

    
326
        return self._set_event(act_event, participants, description,
327
                services=[service], start_datetime=start_datetime,
328
                end_datetime=end_datetime,
329
                room=room, note=note, **rrule_params)
330

    
331
class EventAct(Act, Event):
332
    objects = EventActManager()
333

    
334
    VALIDATION_CODE_CHOICES = (
335
            ('absent', u'Absent'),
336
            ('present', u'Présent'),
337
            )
338
    attendance = models.CharField(max_length=16,
339
            choices=VALIDATION_CODE_CHOICES,
340
            default='absent',
341
            verbose_name=u'Présence')
342
    convocation_sent = models.BooleanField(blank=True,
343
            verbose_name=u'Convoqué')
344

    
345
    def __unicode__(self):
346
        return u'Rdv le {0} de {1} avec {2} pour {3}'.format(
347
                self.occurrence_set.all()[0].start_time, self.patient,
348
                ', '.join(map(unicode, self.participants.all())),
349
                self.act_type)
350

    
351
    def __repr__(self):
352
        return (u'<%s %r %r>' % (self.__class__.__name__, unicode(self),
353
            self.id)).encode('utf-8')
354

    
355
    def start_time(self):
356
        return self.occurrence_set.all()[0].start_time
357

    
358
    def duration(self):
359
        o = self.occurrence_set.all()[0]
360
        td = o.end_time - o.start_time
361
        hours, remainder = divmod(td.seconds, 3600)
362
        minutes, remainder = divmod(remainder, 60)
363
        return '%02d:%02d' % (hours, minutes)
364

    
365
    class Meta:
366
        verbose_name = 'Rendez-vous patient'
367
        verbose_name_plural = 'Rendez-vous patient'
368
        ordering = ['-date', 'patient']
369

    
370
reversion.register(EventAct, follow=['act_ptr', 'event_ptr'])
371

    
372
class ValidationMessage(ServiceLinkedAbstractModel):
373
    validation_date = models.DateTimeField()
374
    what = models.CharField(max_length=256)
375
    who = models.ForeignKey(User)
376
    when = models.DateTimeField(auto_now_add=True)
(4-4/9)