Project

General

Profile

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

calebasse / calebasse / actes / models.py @ 831983bb

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
    CmppHealthCareDiagnostic, CmppHealthCareTreatment
9
from calebasse.actes.validation import are_all_acts_of_the_day_locked
10

    
11
from validation_states import VALIDATION_STATES
12

    
13

    
14
class ActValidationState(models.Model):
15

    
16
    class Meta:
17
        app_label = 'actes'
18

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

    
33
    def __unicode__(self):
34
        return self.state_name + ' ' + str(self.created)
35

    
36

    
37
class Act(models.Model):
38
    patient = models.ForeignKey('dossiers.PatientRecord')
39
    date = models.DateTimeField()
40
    act_type = models.ForeignKey('ressources.ActType',
41
            verbose_name=u'Type d\'acte')
42
    validation_locked = models.BooleanField(default=False,
43
            verbose_name=u'Vérouillage')
44
    is_billed = models.BooleanField(default=False,
45
            verbose_name=u'Facturé')
46
    switch_billable = models.BooleanField(default=False,
47
            verbose_name=u'Inverser type facturable')
48
    healthcare = models.ForeignKey('dossiers.HealthCare',
49
            blank=True,
50
            null=True,
51
            verbose_name=u'Prise en charge utilisée pour facturée (CMPP)')
52
    transport_company = models.ForeignKey('ressources.TransportCompany',
53
            blank=True,
54
            null=True,
55
            verbose_name=u'Compagnie de transport')
56
    transport_type = models.ForeignKey('ressources.TransportType',
57
            blank=True,
58
            null=True,
59
            verbose_name=u'Type de transport')
60
    doctors = models.ManyToManyField('personnes.Worker',
61
            limit_choices_to={'type__intervene': True},
62
            verbose_name=u'Thérapeutes')
63

    
64
    def is_absent(self):
65
        if self.get_state() in ('ABS_NON_EXC', 'ABS_EXC', 'ANNUL_NOUS',
66
                'ANNUL_FAMILLE', 'ABS_ESS_PPS', 'ENF_HOSP'):
67
            return True
68

    
69
    def get_state(self):
70
        return self.actvalidationstate_set.latest('created')
71

    
72
    def is_state(self, state_name):
73
        state = self.get_state()
74
        if state and state.state_name == state_name:
75
            return True
76
        return False
77

    
78
    def set_state(self, state_name, author, auto=False,
79
            change_state_check=True):
80
        if not author:
81
            raise Exception('Missing author to set state')
82
        if not state_name in VALIDATION_STATES.keys():
83
            raise Exception("Etat d'acte non existant %s")
84
        current_state = self.get_state()
85
        ActValidationState(act=self, state_name=state_name,
86
            author=author, previous_state=current_state).save()
87

    
88
    def is_billable(self):
89
        if (self.act_type.billable and not self.switch_billable) or \
90
                (not self.act_type.billable and self.switch_billable):
91
            return True
92

    
93
    # START Specific to sessad healthcare
94
    def was_covered_by_notification(self):
95
        notifications = \
96
            SessadHealthCareNotification.objects.filter(patient=self.patient,
97
            start_date__lte=self.date, end_date__gte=self.date)
98
        if notifications:
99
            return True
100
    # END Specific to sessad healthcare
101

    
102
    # START Specific to cmpp healthcare
103
    def is_act_covered_by_diagnostic_healthcare(self):
104
        # L'acte est déja pointé par une prise en charge
105
        if self.is_billed:
106
            # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
107
            # y est une healthcare non vide
108
            if self.healthcare and isinstance(self.healthcare,
109
                    CmppHealthCareDiagnostic):
110
                return (False, self.healthcare)
111
            elif self.healthcare:
112
                return (False, None)
113
        # L'acte doit être facturable
114
        if not (are_all_acts_of_the_day_locked(self.date) and \
115
                self.is_state('VALIDE') and self.is_billable()):
116
            return (False, None)
117
        # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
118
        # pris en charge sur une prise en charge précédente
119
        # Il peut arriver que l'on ait ajouté une prise en charge de
120
        # traitement alors que tous les actes pour le diag ne sont pas encore facturés
121
        try:
122
            hc = CmppHealthCareDiagnostic.objects.\
123
                filter(patient=self.patient).latest('start_date')
124
        except:
125
            return (False, None)
126
        if not hc:
127
            # On pourrait ici créer une prise en charge de diagnostic
128
            return (False, None)
129
        if self.date < hc.start_date:
130
            return (False, None)
131
        # Les acts facturés déja couvert par la prise en charge sont pointés
132
        # dans hc.act_set.all()
133
        nb_acts_billed = len(hc.act_set.all())
134
        if nb_acts_billed >= hc.get_act_number():
135
            return (False, None)
136
        # Il faut ajouter les actes facturables non encore facturés qui précède cet
137
        # acte
138
        acts_billable = [a for a in self.patient.act_set.\
139
            filter(is_billed=False).order_by('date') \
140
            if are_all_acts_of_the_day_locked(a.date) and \
141
            a.is_state('VALIDE') and a.is_billable()]
142
        count = 0
143
        for a in acts_billable:
144
            if nb_acts_billed + count >= hc.get_act_number():
145
                return (False, None)
146
            if a.date >= hc.start_date:
147
                if a.id == self.id:
148
                    return (True, hc)
149
                count = count + 1
150
        return (False, None)
151

    
152
    def is_act_covered_by_treatment_healthcare(self):
153
        # L'acte est déja pointé par une prise en charge
154
        if self.is_billed:
155
            # Sinon ce peut une refacturation, donc ne pas tenir compte qu'il
156
            # y est une healthcare non vide
157
            if self.healthcare and isinstance(self.healthcare,
158
                    CmppHealthCareTreatment):
159
                return (False, self.healthcare)
160
            elif self.healthcare:
161
                return (False, None)
162
        # L'acte doit être facturable
163
        if not (are_all_acts_of_the_day_locked(self.date) and \
164
                self.is_state('VALIDE') and self.is_billable()):
165
            return (False, None)
166
        # On prend la dernière prise en charge diagnostic, l'acte ne sera pas
167
        # pris en charge sur une prise en charge précédente
168
        # Il peut arriver que l'on ait ajouté une prise en charge de
169
        # traitement alors que tous les actes pour le diag ne sont pas encore facturés
170
        try:
171
            hc = CmppHealthCareTreatment.objects.\
172
                filter(patient=self.patient).latest('start_date')
173
        except:
174
            return (False, None)
175
        if not hc:
176
            return (False, None)
177
        if self.date < hc.start_date or self.date > hc.end_date:
178
            return (False, None)
179
        # Les acts facturés déja couvert par la prise en charge sont pointés
180
        # dans hc.act_set.all()
181
        nb_acts_billed = len(hc.act_set.all())
182
        if nb_acts_billed >= hc.get_act_number():
183
            return (False, None)
184
        # Il faut ajouter les actes facturables non encore facturés qui précède cet
185
        # acte
186
        acts_billable = [a for a in self.patient.act_set.\
187
            filter(is_billed=False).order_by('date') \
188
            if are_all_acts_of_the_day_locked(a.date) and \
189
            a.is_state('VALIDE') and a.is_billable()]
190
        count = 0
191
        for a in acts_billable:
192
            if nb_acts_billed + count >= hc.get_act_number():
193
                return (False, None)
194
            if a.date >= hc.start_date and a.date <= hc.end_date:
195
                if a.id == self.id:
196
                    return (True, hc)
197
                count = count + 1
198
        return (False, None)
199
    # END Specific to cmpp healthcare
200

    
201
    def __unicode__(self):
202
        return '{0} le {1} pour {2} avec {3}'.format(
203
                self.act_type, self.date, self.patient,
204
                ', '.join(map(unicode, self.doctors.all())))
205

    
206
    def __repr__(self):
207
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
208

    
209
    class Meta:
210
        verbose_name = u"Acte"
211
        verbose_name_plural = u"Actes"
212
        ordering = ['-date', 'patient']
213

    
214

    
215
class EventActManager(EventManager):
216

    
217
    def create_patient_appointment(self, creator, title, patient, participants,
218
            act_type, service, start_datetime, end_datetime, description='',
219
            room=None, note=None, **rrule_params):
220
        """
221
        This method allow you to create a new patient appointment quickly
222

    
223
        Args:
224
            title: patient appointment title (str)
225
            patient: Patient object
226
            participants: List of CalebasseUser (therapists)
227
            act_type: ActType object
228
            service: Service object. Use session service by defaut
229
            start_datetime: datetime with the start date and time
230
            end_datetime: datetime with the end date and time
231
            freq, count, until, byweekday, rrule_params:
232
            follow the ``dateutils`` API (see http://labix.org/python-dateutil)
233

    
234
        Example:
235
            Look at calebasse.agenda.tests.EventTest (test_create_appointments
236
            method)
237
        """
238

    
239
        event_type, created = EventType.objects.get_or_create(
240
                label=u"Rendez-vous patient"
241
                )
242

    
243
        history = HistoryAct.objects.create(
244
                date = datetime.now(),
245
                event_type = 'NR')
246
        act_event = EventAct.objects.create(
247
                title=title,
248
                event_type=event_type,
249
                patient=patient,
250
                act_type=act_type,
251
                date=start_datetime,
252
                histories = [history]
253
                )
254
        act_event.doctors = participants
255
        ActValidationState(act=act_event, state_name='NON_VALIDE',
256
            author=creator, previous_state=None).save()
257

    
258
        return self._set_event(act_event, participants, description,
259
                services=[service], start_datetime=start_datetime,
260
                end_datetime=end_datetime,
261
                room=room, note=note, **rrule_params)
262

    
263

    
264
class EventAct(Act, Event):
265
    objects = EventActManager()
266

    
267
    VALIDATION_CODE_CHOICES = (
268
            ('absent', u'Absent'),
269
            ('present', u'Présent'),
270
            )
271
    attendance = models.CharField(max_length=16,
272
            choices=VALIDATION_CODE_CHOICES,
273
            default='absent',
274
            verbose_name=u'Présence')
275
    convocation_sent = models.BooleanField(blank=True,
276
            verbose_name=u'Convoqué')
277

    
278
    def __unicode__(self):
279
        return 'Rdv le {0} de {1} avec {2} pour {3}'.format(
280
                self.occurrence_set.all()[0].start_time, self.patient,
281
                ', '.join(map(unicode, self.participants.all())),
282
                self.act_type)
283

    
284
    def __repr__(self):
285
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self),
286
            self.id)
287

    
288
    class Meta:
289
        verbose_name = 'Rendez-vous patient'
290
        verbose_name_plural = 'Rendez-vous patient'
291
        ordering = ['-date', 'patient']
(4-4/9)