Project

General

Profile

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

calebasse / calebasse / actes / views.py @ 1edcbebd

1
# -*- coding: utf-8 -*-
2

    
3
import datetime
4

    
5
from django.http import HttpResponseRedirect, HttpResponse
6
from django.db.models import Q
7
from django.shortcuts import redirect
8

    
9
from calebasse.cbv import ListView, UpdateView, DeleteView
10
from calebasse.agenda.views import NewAppointmentView
11
from calebasse.agenda.models import EventWithAct
12
from calebasse.agenda.forms import UpdateAppointmentForm, NewAppointmentForm
13

    
14
import copy
15
import models
16
import forms
17

    
18
def redirect_today(request, service):
19
    '''If not date is given we redirect on the agenda for today'''
20
    return redirect(act_listing, date=datetime.date.today().strftime('%Y-%m-%d'),
21
            service=service)
22

    
23
class ActListingView(ListView):
24
    model = models.Act
25
    template_name = 'actes/act_listing.html'
26

    
27
    def get_queryset(self):
28
        qs = super(ActListingView, self).get_queryset()
29
        qs = qs.filter(patient__service=self.service)
30
        qs = qs.filter(date=self.date)
31
        self.search_form = forms.ActSearchForm(data=self.request.GET or None)
32
        last_name = self.request.GET.get('last_name')
33
        group = self.request.GET.get('group')
34
        patient_record_id = self.request.GET.get('patient_record_id')
35
        social_security_number = self.request.GET.get('social_security_number')
36
        doctor_name = self.request.GET.get('doctor_name')
37
        filters = self.request.GET.getlist('filters')
38
        if last_name:
39
            qs = qs.filter(patient__last_name__istartswith=last_name)
40
        if patient_record_id:
41
            qs = qs.filter(patient__id=int(patient_record_id))
42
        if doctor_name:
43
            qs = qs.filter(doctors__last_name__icontains=doctor_name)
44
        if 'valide' in filters:
45
            qs = qs.exclude(last_validation_state__state_name__exact='VALIDE')
46
        if 'group' in filters:
47
            qs = qs.filter(act_type__group=True)
48
        if 'pointe' in filters:
49
            qs = qs.filter(last_validation_state__isnull=False). \
50
                    exclude(last_validation_state__state_name__exact='NON_VALIDE')
51
        if 'non-pointe' in filters:
52
            qs = qs.filter(Q(last_validation_state__isnull=True) | \
53
                    Q(last_validation_state__state_name__exact='NON_VALIDE'))
54
        if 'absent-or-canceled' in filters:
55
            qs = qs.filter(last_validation_state__state_name__in=('ABS_NON_EXC',
56
                'ABS_EXC', 'ABS_INTER', 'ANNUL_NOUS',
57
                'ANNUL_FAMILLE', 'REPORTE', 'ABS_ESS_PPS', 'ENF_HOSP'))
58
        if 'is-billable' in filters:
59
            qs = qs.filter(
60
                    (Q(act_type__billable=True) & Q(switch_billable=False)) | \
61
                    (Q(act_type__billable=False) & Q(switch_billable=True))
62
                    )
63
        if 'switch-billable' in filters:
64
            qs = qs.filter(switch_billable=True)
65
        if 'lost' in filters:
66
            qs = qs.filter(is_lost=True)
67
        if 'pause-invoicing' in filters:
68
            qs = qs.filter(pause=True)
69
        if 'invoiced' in filters:
70
            qs = qs.filter(is_billed=True)
71

    
72
        return qs.select_related()
73

    
74
    def get_context_data(self, **kwargs):
75
        ctx = super(ActListingView, self).get_context_data(**kwargs)
76
        ctx['search_form'] = self.search_form
77
        self.request.record('acts-view', 'act listing by {user} from {ip}')
78
        return ctx
79

    
80
class NewAct(NewAppointmentView):
81
    success_url = '.'
82
    success_msg = u'Acte enregistré avec succès.'
83
    model = EventWithAct
84
    form_class = UpdateAppointmentForm
85

    
86
    def form_valid(self, form):
87
        result = super(NewAct, self).form_valid(form)
88
        self.object.act.save()
89
        return result
90

    
91
act_listing = ActListingView.as_view()
92
act_new = NewAct.as_view()
93

    
94
class DeleteActView(DeleteView):
95
    model = models.Act
96
    template_name = 'actes/confirm_delete.html'
97
    success_url = '..'
98

    
99
    def delete(self, request, *args, **kwargs):
100
        self.object = self.get_object()
101
        if self.object.event:
102
            self.object.event.delete()
103
        if not self.object.is_billed:
104
            self.object.delete()
105

    
106
        return HttpResponse(status=204)
107

    
108
delete_act = DeleteActView.as_view()
109

    
110
class UpdateActView(UpdateView):
111
    model = models.Act
112
    form_class = forms.ActUpdate
113
    template_name = 'actes/act_update.html'
114
    success_url = '..'
115

    
116
    def form_valid(self, form):
117
        result = super(UpdateView, self).form_valid(form)
118
        if self.object.event:
119
            doctors = copy.copy(self.object.doctors.all())
120
            self.object.event.participants =  doctors
121
            self.object.event.act_type =  self.object.act_type
122
            self.object.event.save()
123
            self.request.record('act-update',
124
                                '{obj_id} updated by {user} from {ip} with: {changes}',
125
                                obj_id=self.object.id,
126
                                changes={'participants': doctors, 'act_type': self.object.act_type})
127
        return result
128

    
129
update_act = UpdateActView.as_view()
130

    
131
class RebillActView(UpdateView):
132
    model = models.Act
133
    template_name = 'actes/act_rebill.html'
134
    success_url = '..'
135

    
136
    def post(self, request, *args, **kwarg):
137
        act = models.Act.objects.get(pk=kwarg['pk'])
138
        act.is_billed = False
139
        act.healthcare = None
140
        act.save()
141
        self.request.record('rebill-act', '{obj_id} rebilled by {user} from {ip}', obj_id=act.id)
142
        return super(RebillActView, self).post(request, *args, **kwarg)
143

    
144
rebill_act = RebillActView.as_view()
(9-9/9)