Project

General

Profile

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

calebasse / calebasse / actes / models.py @ 3c5df84d

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

    
12
class ActValidationState(models.Model):
13

    
14
    class Meta:
15
        app_label = 'actes'
16
        ordering = ('-created',)
17

    
18
    act = models.ForeignKey('actes.Act',
19
        verbose_name=u'Acte', editable=False)
20
    state_name = models.CharField(max_length=150)
21
    created = models.DateTimeField(u'Création', auto_now_add=True)
22
    author = \
23
        models.ForeignKey(User,
24
        verbose_name=u'Auteur', editable=False, blank=True, null=True)
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
        states = self.actvalidationstate_set.all()
159
        states_len = len(states)
160
        return states_len == 0 or \
161
            (states_len == 1 and states[0].state_name == 'NON_VALIDE')
162

    
163
    def is_absent(self):
164
        state = self.get_state()
165
        if state and state.state_name in ('ABS_NON_EXC', 'ABS_EXC', 'ABS_INTER', 'ANNUL_NOUS',
166
                'ANNUL_FAMILLE', 'REPORTE', 'ABS_ESS_PPS', 'ENF_HOSP'):
167
            return True
168
        return False
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
        self.save()
204

    
205
    def is_billable(self):
206
        billable = self.act_type.billable
207
        if (billable and not self.switch_billable) or \
208
                (not billable and self.switch_billable):
209
            return True
210
        return False
211

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

    
222
    def save(self, *args, **kwargs):
223
        super(Act, self).save(*args, **kwargs)
224

    
225
    def duration(self):
226
        '''Return a displayable duration for this field.'''
227
        hours, remainder = divmod(self._duration, 60)
228
        return '%02d:%02d' % (hours, remainder)
229

    
230
    def __unicode__(self):
231
        if self.time:
232
            return u'{0} le {1} à {2} pour {3} avec {4}'.format(
233
                    self.act_type, self.date.strftime('%d/%m/%Y'),
234
                    self.time.strftime('%H:%M'), self.patient,
235
                    ', '.join(map(unicode, self.doctors.all())))
236
        return u'{0} le {1} pour {2} avec {3}'.format(
237
                self.act_type, self.date.strftime('%d/%m/%Y'), self.patient,
238
                ', '.join(map(unicode, self.doctors.all())))
239

    
240
    def __repr__(self):
241
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
242

    
243
    def cancel(self):
244
        '''Parent event is canceled completely, or partially, act upon it.
245
        '''
246
        new_act = self.actvalidationstate_set.count() > 1
247
        if self.date <= date.today():
248
            if new_act:
249
                self.set_state('ANNUL_NOUS', get_request().user)
250
            self.parent_event = None
251
            self.save()
252
        else:
253
            self.delete()
254

    
255
    def get_invoice_number(self):
256
        try:
257
            if self.is_billed:
258
                return self.invoice_set.order_by('-number')[0].number
259
        except:
260
            pass
261
        return None
262

    
263
    class Meta:
264
        verbose_name = u"Acte"
265
        verbose_name_plural = u"Actes"
266
        ordering = ['-date', 'patient']
267

    
268

    
269
class ValidationMessage(ServiceLinkedAbstractModel):
270
    validation_date = models.DateTimeField()
271
    what = models.CharField(max_length=256)
272
    who = models.ForeignKey(User)
273
    when = models.DateTimeField(auto_now_add=True)
(4-4/9)