Project

General

Profile

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

calebasse / calebasse / actes / models.py @ 48a85f96

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
import reversion
8

    
9
from calebasse.agenda.models import Event, EventType
10
from calebasse.agenda.managers import EventManager
11
from calebasse.ressources.models import ServiceLinkedAbstractModel
12
from ..middleware.request import get_request
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
        ordering = ('-created',)
22

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

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

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

    
43

    
44
class ActManager(models.Manager):
45
    def for_today(self, today=None):
46
        today = today or date.today()
47
        return self.filter(date=today)
48

    
49
    def create_act(self, author=None, **kwargs):
50
        act = self.create(**kwargs)
51
        ActValidationState.objects.create(act=act,state_name='NON_VALIDE',
52
            author=author, previous_state=None)
53
        return act
54

    
55
    def next_acts(self, patient_record, today=None):
56
        today = today or date.today()
57
        return self.filter(date__gte=today) \
58
                .filter(patient=patient_record) \
59
                .order_by('date')
60

    
61
    def last_acts(self, patient_record, today=None):
62
        today = today or date.today()
63
        return self.filter(date__lte=today) \
64
                .filter(patient=patient_record) \
65
                .order_by('-date')
66

    
67

    
68
class Act(models.Model):
69
    objects = ActManager()
70

    
71
    patient = models.ForeignKey('dossiers.PatientRecord')
72
    date = models.DateField(u'Date', db_index=True)
73
    time = models.TimeField(u'Heure', blank=True, null=True, default=time(), db_index=True)
74
    _duration = models.IntegerField(u'Durée en minutes', blank=True, null=True, default=0)
75
    act_type = models.ForeignKey('ressources.ActType',
76
            verbose_name=u'Type d\'acte')
77
    validation_locked = models.BooleanField(default=False,
78
            verbose_name=u'Vérouillage', db_index=True)
79
    is_billed = models.BooleanField(default=False,
80
            verbose_name=u'Facturé', db_index=True)
81
    is_lost = models.BooleanField(default=False,
82
            verbose_name=u'Acte perdu', db_index=True)
83
    valide = models.BooleanField(default=False,
84
            verbose_name=u'Valide', db_index=True)
85
    switch_billable = models.BooleanField(default=False,
86
            verbose_name=u'Inverser type facturable')
87
    healthcare = models.ForeignKey('dossiers.HealthCare',
88
            blank=True,
89
            null=True,
90
            on_delete=models.SET_NULL,
91
            verbose_name=u'Prise en charge utilisée pour facturer (CMPP)')
92
    transport_company = models.ForeignKey('ressources.TransportCompany',
93
            blank=True,
94
            null=True,
95
            on_delete=models.SET_NULL,
96
            verbose_name=u'Compagnie de transport')
97
    transport_type = models.ForeignKey('ressources.TransportType',
98
            blank=True,
99
            null=True,
100
            on_delete=models.SET_NULL,
101
            verbose_name=u'Type de transport')
102
    doctors = models.ManyToManyField('personnes.Worker',
103
            limit_choices_to={'type__intervene': True},
104
            verbose_name=u'Intervenants')
105
    pause = models.BooleanField(default=False,
106
            verbose_name=u'Pause facturation', db_index=True)
107
    parent_event = models.ForeignKey('agenda.Event',
108
            verbose_name=u'Rendez-vous lié',
109
            blank=True, null=True,
110
            on_delete=models.SET_NULL)
111
    VALIDATION_CODE_CHOICES = (
112
            ('absent', u'Absent'),
113
            ('present', u'Présent'),
114
            )
115
    attendance = models.CharField(max_length=16,
116
            choices=VALIDATION_CODE_CHOICES,
117
            default='absent',
118
            verbose_name=u'Présence')
119
    comment = models.TextField(u'Commentaire', blank=True, null=True)
120
    old_id = models.CharField(max_length=256,
121
            verbose_name=u'Ancien ID', blank=True, null=True)
122

    
123
    @property
124
    def event(self):
125
        if self.parent_event:
126
            return self.parent_event.today_occurrence(self.date)
127
        return None
128

    
129
    @property
130
    def start_datetime(self):
131
        event = self.event
132
        if event:
133
            return event.start_datetime
134
        return self.date
135

    
136
    def get_hc_tag(self):
137
        if self.healthcare:
138
            beg = None
139
            try:
140
                self.healthcare.cmpphealthcaretreatment
141
                beg = 'T'
142
            except:
143
                pass
144
            try:
145
                self.healthcare.cmpphealthcarediagnostic
146
                beg = 'D'
147
            except:
148
                pass
149
            if beg:
150
                acts = list(self.healthcare.act_set.order_by('date').values_list('id', flat=True))
151
                beg += str(acts.index(self.id) + 1)
152
                return beg
153
        return None
154

    
155
    def is_new(self):
156
        states = self.actvalidationstate_set.all()
157
        states_len = len(states)
158
        return states_len == 0 or \
159
            (states_len == 1 and states[0].state_name == 'NON_VALIDE')
160

    
161
    def is_absent(self):
162
        state = self.get_state()
163
        if state and state.state_name in ('ABS_NON_EXC', 'ABS_EXC', 'ABS_INTER', 'ANNUL_NOUS',
164
                'ANNUL_FAMILLE', 'REPORTE', 'ABS_ESS_PPS', 'ENF_HOSP'):
165
            return True
166
        return False
167

    
168
    def get_state(self):
169
#        states = sorted(self.actvalidationstate_set.all(),
170
#                key=lambda avs: avs.created, reverse=True)
171
#        if states:
172
#            return states[0]
173
#        return None
174
        try:
175
            return self.actvalidationstate_set.latest('created')
176
        except:
177
            return None
178

    
179
    def is_state(self, state_name):
180
        state = self.get_state()
181
        if state and state.state_name == state_name:
182
            return True
183
        return False
184

    
185
    def set_state(self, state_name, author, auto=False,
186
            change_state_check=True):
187
        if not self.id:
188
            self.save()
189
        if not author:
190
            raise Exception('Missing author to set state')
191
        if not state_name in VALIDATION_STATES.keys():
192
            raise Exception("Etat d'acte non existant %s")
193
        current_state = self.get_state()
194
        ActValidationState(act=self, state_name=state_name,
195
            author=author, previous_state=current_state).save()
196
        if state_name == 'VALIDE':
197
            self.valide = True
198
        else:
199
            self.valide = False
200
        self.save()
201

    
202
    def is_billable(self):
203
        billable = self.act_type.billable
204
        if (billable and not self.switch_billable) or \
205
                (not billable and self.switch_billable):
206
            return True
207
        return False
208

    
209
    # START Specific to sessad healthcare
210
    def was_covered_by_notification(self):
211
        from calebasse.dossiers.models import SessadHealthCareNotification
212
        notifications = \
213
            SessadHealthCareNotification.objects.filter(patient=self.patient,
214
            start_date__lte=self.date, end_date__gte=self.date)
215
        if notifications:
216
            return True
217
    # END Specific to sessad healthcare
218

    
219
    # START Specific to cmpp healthcare
220
    def is_act_covered_by_diagnostic_healthcare(self):
221
        from calebasse.dossiers.models import CmppHealthCareDiagnostic
222
        from validation import are_all_acts_of_the_day_locked
223
        # L'acte est déja pointé par une prise en charge
224
        if self.is_billed:
225
            # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
226
            # y est une healthcare non vide
227
            if self.healthcare and isinstance(self.healthcare,
228
                    CmppHealthCareDiagnostic):
229
                return (False, self.healthcare)
230
            elif self.healthcare:
231
                return (False, None)
232
        # L'acte doit être facturable
233
        if not (are_all_acts_of_the_day_locked(self.date) and \
234
                self.is_state('VALIDE') and self.is_billable()):
235
            return (False, None)
236
        # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
237
        # pris en charge sur une prise en charge précédente
238
        # Il peut arriver que l'on ait ajouté une prise en charge de
239
        # traitement alors que tous les actes pour le diag ne sont pas encore facturés
240
        try:
241
            hc = CmppHealthCareDiagnostic.objects.\
242
                filter(patient=self.patient).latest('start_date')
243
        except:
244
            return (False, None)
245
        if not hc:
246
            # On pourrait ici créer une prise en charge de diagnostic
247
            return (False, None)
248
        if self.date < hc.start_date:
249
            return (False, None)
250
        # Les acts facturés déja couvert par la prise en charge sont pointés
251
        # dans hc.act_set.all()
252
        nb_acts_billed = len(hc.act_set.all())
253
        if nb_acts_billed >= hc.get_act_number():
254
            return (False, None)
255
        # Il faut ajouter les actes facturables non encore facturés qui précède cet
256
        # acte
257
        acts_billable = [a for a in self.patient.act_set.\
258
            filter(is_billed=False).order_by('date') \
259
            if are_all_acts_of_the_day_locked(a.date) and \
260
            a.is_state('VALIDE') and a.is_billable()]
261
        count = 0
262
        for a in acts_billable:
263
            if nb_acts_billed + count >= hc.get_act_number():
264
                return (False, None)
265
            if a.date >= hc.start_date:
266
                if a.id == self.id:
267
                    return (True, hc)
268
                count = count + 1
269
        return (False, None)
270

    
271
#    def is_act_covered_by_treatment_healthcare(self):
272
#        # L'acte est déja pointé par une prise en charge
273
#        if self.is_billed:
274
#            # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
275
#            # y est une healthcare non vide
276
#            if self.healthcare and isinstance(self.healthcare,
277
#                    CmppHealthCareTreatment):
278
#                return (False, self.healthcare)
279
#            elif self.healthcare:
280
#                return (False, None)
281
#        # L'acte doit être facturable
282
#        if not (are_all_acts_of_the_day_locked(self.date) and \
283
#                self.is_state('VALIDE') and self.is_billable()):
284
#            return (False, None)
285
#        # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
286
#        # pris en charge sur une prise en charge précédente
287
#        # Il peut arriver que l'on ait ajouté une prise en charge de
288
#        # traitement alors que tous les actes pour le diag ne sont pas encore facturés
289
#        try:
290
#            hc = CmppHealthCareTreatment.objects.\
291
#                filter(patient=self.patient).latest('start_date')
292
#        except:
293
#            return (False, None)
294
#        if not hc:
295
#            return (False, None)
296
#        if self.date < hc.start_date or self.date > hc.end_date:
297
#            return (False, None)
298
#        # Les acts facturés déja couvert par la prise en charge sont pointés
299
#        # dans hc.act_set.all()
300
#        nb_acts_billed = len(hc.act_set.all())
301
#        if nb_acts_billed >= hc.get_act_number():
302
#            return (False, None)
303
#        # Il faut ajouter les actes facturables non encore facturés qui précède cet
304
#        # acte
305
#        acts_billable = [a for a in self.patient.act_set.\
306
#            filter(is_billed=False).order_by('date') \
307
#            if are_all_acts_of_the_day_locked(a.date) and \
308
#            a.is_state('VALIDE') and a.is_billable()]
309
#        count = 0
310
#        for a in acts_billable:
311
#            if nb_acts_billed + count >= hc.get_act_number():
312
#                return (False, None)
313
#            if a.date >= hc.start_date and a.date <= hc.end_date:
314
#                if a.id == self.id:
315
#                    return (True, hc)
316
#                count = count + 1
317
#        return (False, None)
318
# END Specific to cmpp healthcare
319

    
320
    def save(self, *args, **kwargs):
321
        super(Act, self).save(*args, **kwargs)
322

    
323
    def duration(self):
324
        '''Return a displayable duration for this field.'''
325
        hours, remainder = divmod(self._duration, 60)
326
        return '%02d:%02d' % (hours, remainder)
327

    
328
    def __unicode__(self):
329
        if self.time:
330
            return u'{0} le {1} à {2} pour {3} avec {4}'.format(
331
                    self.act_type, self.date, self.time, self.patient,
332
                    ', '.join(map(unicode, self.doctors.all())))
333
        return u'{0} le {1} pour {2} avec {3}'.format(
334
                self.act_type, self.date, self.patient,
335
                ', '.join(map(unicode, self.doctors.all())))
336

    
337
    def __repr__(self):
338
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
339

    
340
    def cancel(self):
341
        '''Parent event is canceled completely, or partially, act upon it.
342
        '''
343
        new_act = self.actvalidationstate_set.count() > 1
344
        if self.date <= date.today():
345
            if new_act:
346
                self.set_state('ANNUL_NOUS', get_request().user)
347
            self.parent_event = None
348
            self.save()
349
        else:
350
            self.delete()
351

    
352
    class Meta:
353
        verbose_name = u"Acte"
354
        verbose_name_plural = u"Actes"
355
        ordering = ['-date', 'patient']
356

    
357

    
358
class EventActManager(EventManager):
359

    
360
    def create_patient_appointment(self, creator, title, patient, participants,
361
            act_type, service, start_datetime, end_datetime, description='',
362
            room=None, note=None, **rrule_params):
363
        """
364
        This method allow you to create a new patient appointment quickly
365

    
366
        Args:
367
            title: patient appointment title (str)
368
            patient: Patient object
369
            participants: List of CalebasseUser (therapists)
370
            act_type: ActType object
371
            service: Service object. Use session service by defaut
372
            start_datetime: datetime with the start date and time
373
            end_datetime: datetime with the end date and time
374
            freq, count, until, byweekday, rrule_params:
375
            follow the ``dateutils`` API (see http://labix.org/python-dateutil)
376

    
377
        Example:
378
            Look at calebasse.agenda.tests.EventTest (test_create_appointments
379
            method)
380
        """
381

    
382
        event_type, created = EventType.objects.get_or_create(
383
                label=u"Rendez-vous patient"
384
                )
385

    
386
        act_event = EventAct.objects.create(
387
                title=title,
388
                event_type=event_type,
389
                patient=patient,
390
                act_type=act_type,
391
                date=start_datetime,
392
                )
393
        act_event.doctors = participants
394
        ActValidationState(act=act_event, state_name='NON_VALIDE',
395
            author=creator, previous_state=None).save()
396

    
397
        return self._set_event(act_event, participants, description,
398
                services=[service], start_datetime=start_datetime,
399
                end_datetime=end_datetime,
400
                room=room, note=note, **rrule_params)
401

    
402

    
403
class ValidationMessage(ServiceLinkedAbstractModel):
404
    validation_date = models.DateTimeField()
405
    what = models.CharField(max_length=256)
406
    who = models.ForeignKey(User)
407
    when = models.DateTimeField(auto_now_add=True)
(4-4/9)