Project

General

Profile

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

calebasse / calebasse / actes / models.py @ 08bf3477

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

    
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 ActManager(models.Manager):
44
    def for_today(self, today=None):
45
        today = today or date.today()
46
        return self.filter(date=today)
47

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

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

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

    
66

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

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

    
111
    @property
112
    def event(self):
113
        if self.parent_event:
114
            return self.parent_event.today_occurrence(self.date)
115
        return None
116

    
117
    @property
118
    def start_datetime(self):
119
        event = self.event
120
        if event:
121
            return event.start_datetime
122
        return self.date
123

    
124
    def get_hc_tag(self):
125
        if self.healthcare:
126
            try:
127
                self.healthcare.cmpphealthcaretreatment
128
                return 'T'
129
            except:
130
                pass
131
            try:
132
                self.healthcare.cmpphealthcarediagnostic
133
                acts = self.healthcare.act_set.order_by('date')
134
                i = 1
135
                for act in acts:
136
                    if act.id == self.id:
137
                        return 'D' + str(i)
138
                    i = i + 1
139
            except:
140
                pass
141
        return None
142

    
143
    def is_absent(self):
144
        state = self.get_state()
145
        if state and state.state_name in ('ABS_NON_EXC', 'ABS_EXC', 'ABS_INTER', 'ANNUL_NOUS',
146
                'ANNUL_FAMILLE', 'REPORTE', 'ABS_ESS_PPS', 'ENF_HOSP'):
147
            return True
148
        return False
149

    
150
    def get_state(self):
151
        return self.actvalidationstate_set.latest('created')
152

    
153
    def is_state(self, state_name):
154
        state = self.get_state()
155
        if state and state.state_name == state_name:
156
            return True
157
        return False
158

    
159
    def set_state(self, state_name, author, auto=False,
160
            change_state_check=True):
161
        if not author:
162
            raise Exception('Missing author to set state')
163
        if not state_name in VALIDATION_STATES.keys():
164
            raise Exception("Etat d'acte non existant %s")
165
        current_state = self.get_state()
166
        ActValidationState(act=self, state_name=state_name,
167
            author=author, previous_state=current_state).save()
168

    
169
    def is_billable(self):
170
        if (self.act_type.billable and not self.switch_billable) or \
171
                (not self.act_type.billable and self.switch_billable):
172
            return True
173

    
174
    # START Specific to sessad healthcare
175
    def was_covered_by_notification(self):
176
        from calebasse.dossiers.models import SessadHealthCareNotification
177
        notifications = \
178
            SessadHealthCareNotification.objects.filter(patient=self.patient,
179
            start_date__lte=self.date, end_date__gte=self.date)
180
        if notifications:
181
            return True
182
    # END Specific to sessad healthcare
183

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

    
236
#    def is_act_covered_by_treatment_healthcare(self):
237
#        # L'acte est déja pointé par une prise en charge
238
#        if self.is_billed:
239
#            # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
240
#            # y est une healthcare non vide
241
#            if self.healthcare and isinstance(self.healthcare,
242
#                    CmppHealthCareTreatment):
243
#                return (False, self.healthcare)
244
#            elif self.healthcare:
245
#                return (False, None)
246
#        # L'acte doit être facturable
247
#        if not (are_all_acts_of_the_day_locked(self.date) and \
248
#                self.is_state('VALIDE') and self.is_billable()):
249
#            return (False, None)
250
#        # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
251
#        # pris en charge sur une prise en charge précédente
252
#        # Il peut arriver que l'on ait ajouté une prise en charge de
253
#        # traitement alors que tous les actes pour le diag ne sont pas encore facturés
254
#        try:
255
#            hc = CmppHealthCareTreatment.objects.\
256
#                filter(patient=self.patient).latest('start_date')
257
#        except:
258
#            return (False, None)
259
#        if not hc:
260
#            return (False, None)
261
#        if self.date < hc.start_date or self.date > hc.end_date:
262
#            return (False, None)
263
#        # Les acts facturés déja couvert par la prise en charge sont pointés
264
#        # dans hc.act_set.all()
265
#        nb_acts_billed = len(hc.act_set.all())
266
#        if nb_acts_billed >= hc.get_act_number():
267
#            return (False, None)
268
#        # Il faut ajouter les actes facturables non encore facturés qui précède cet
269
#        # acte
270
#        acts_billable = [a for a in self.patient.act_set.\
271
#            filter(is_billed=False).order_by('date') \
272
#            if are_all_acts_of_the_day_locked(a.date) and \
273
#            a.is_state('VALIDE') and a.is_billable()]
274
#        count = 0
275
#        for a in acts_billable:
276
#            if nb_acts_billed + count >= hc.get_act_number():
277
#                return (False, None)
278
#            if a.date >= hc.start_date and a.date <= hc.end_date:
279
#                if a.id == self.id:
280
#                    return (True, hc)
281
#                count = count + 1
282
#        return (False, None)
283
# END Specific to cmpp healthcare
284

    
285
    def duration(self):
286
        '''Return a displayable duration for this field.'''
287
        hours, remainder = divmod(self._duration, 60)
288
        return '%02d:%02d' % (hours, remainder)
289

    
290
    def __unicode__(self):
291
        return u'{0} le {1} pour {2} avec {3}'.format(
292
                self.act_type, self.date, self.patient,
293
                ', '.join(map(unicode, self.doctors.all())))
294

    
295
    def __repr__(self):
296
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
297

    
298
    def cancel(self):
299
        '''Parent event is canceled completely, or partially, act upon it.
300
        '''
301
        new_act = self.actvalidationstate_set.count() > 1
302
        if self.date <= date.today():
303
            if new_act:
304
                self.set_state('ANNUL_NOUS', get_request().user)
305
            self.parent_event = None
306
            self.save()
307
        else:
308
            self.delete()
309

    
310
    class Meta:
311
        verbose_name = u"Acte"
312
        verbose_name_plural = u"Actes"
313
        ordering = ['-date', 'patient']
314

    
315

    
316
class EventActManager(EventManager):
317

    
318
    def create_patient_appointment(self, creator, title, patient, participants,
319
            act_type, service, start_datetime, end_datetime, description='',
320
            room=None, note=None, **rrule_params):
321
        """
322
        This method allow you to create a new patient appointment quickly
323

    
324
        Args:
325
            title: patient appointment title (str)
326
            patient: Patient object
327
            participants: List of CalebasseUser (therapists)
328
            act_type: ActType object
329
            service: Service object. Use session service by defaut
330
            start_datetime: datetime with the start date and time
331
            end_datetime: datetime with the end date and time
332
            freq, count, until, byweekday, rrule_params:
333
            follow the ``dateutils`` API (see http://labix.org/python-dateutil)
334

    
335
        Example:
336
            Look at calebasse.agenda.tests.EventTest (test_create_appointments
337
            method)
338
        """
339

    
340
        event_type, created = EventType.objects.get_or_create(
341
                label=u"Rendez-vous patient"
342
                )
343

    
344
        act_event = EventAct.objects.create(
345
                title=title,
346
                event_type=event_type,
347
                patient=patient,
348
                act_type=act_type,
349
                date=start_datetime,
350
                )
351
        act_event.doctors = participants
352
        ActValidationState(act=act_event, state_name='NON_VALIDE',
353
            author=creator, previous_state=None).save()
354

    
355
        return self._set_event(act_event, participants, description,
356
                services=[service], start_datetime=start_datetime,
357
                end_datetime=end_datetime,
358
                room=room, note=note, **rrule_params)
359

    
360

    
361
class ValidationMessage(ServiceLinkedAbstractModel):
362
    validation_date = models.DateTimeField()
363
    what = models.CharField(max_length=256)
364
    who = models.ForeignKey(User)
365
    when = models.DateTimeField(auto_now_add=True)
(4-4/9)