Project

General

Profile

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

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

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

    
12
import copy
13
import models
14
import forms
15

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

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

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

    
67
        return qs.select_related()
68

    
69
    def get_context_data(self, **kwargs):
70
        ctx = super(ActListingView, self).get_context_data(**kwargs)
71
        ctx['search_form'] = self.search_form
72
        return ctx
73

    
74
class NewAct(NewAppointmentView):
75
    success_url = '.'
76
    success_msg = u'Acte enregistré avec succès.'
77

    
78
    def form_valid(self, form):
79
        result = super(NewAct, self).form_valid(form)
80
        self.object.act.save()
81
        return result
82

    
83
act_listing = ActListingView.as_view()
84
act_new = NewAct.as_view()
85

    
86
class DeleteActView(DeleteView):
87
    model = models.Act
88
    template_name = 'actes/confirm_delete.html'
89
    success_url = '..'
90

    
91
    def delete(self, request, *args, **kwargs):
92
        self.object = self.get_object()
93
        if self.object.event:
94
            self.object.event.delete()
95
        if not self.object.is_billed:
96
            self.object.delete()
97

    
98
        return HttpResponse(status=204)
99

    
100
delete_act = DeleteActView.as_view()
101

    
102
class UpdateActView(UpdateView):
103
    model = models.Act
104
    form_class = forms.ActUpdate
105
    template_name = 'actes/act_update.html'
106
    success_url = '..'
107

    
108
    def form_valid(self, form):
109
        result = super(UpdateView, self).form_valid(form)
110
        if self.object.event:
111
            doctors = copy.copy(self.object.doctors.all())
112
            self.object.event.participants =  doctors
113
            self.object.event.act_type =  self.object.act_type
114
            self.object.event.save()
115
        return result
116

    
117
update_act = UpdateActView.as_view()
118

    
119
class RebillActView(UpdateView):
120
    model = models.Act
121
    template_name = 'actes/act_rebill.html'
122
    success_url = '..'
123

    
124
    def post(self, request, *args, **kwarg):
125
        act = models.Act.objects.get(pk=kwarg['pk'])
126
        act.is_billed = False
127
        act.healthcare = None
128
        act.save()
129
        return super(RebillActView, self).post(request, *args, **kwarg)
130

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