Project

General

Profile

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

calebasse / calebasse / facturation / views.py @ 6c3d1192

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

    
3
from calebasse import cbv
4
from calebasse.facturation import forms
5

    
6
from datetime import date, datetime
7

    
8
from django.http import HttpResponseRedirect, HttpResponse
9
from django.core.files import File
10

    
11
from calebasse.cbv import TemplateView, UpdateView
12

    
13
from models import Invoicing, Invoice
14
from calebasse.ressources.models import Service
15
from invoice_header import render_invoicing
16

    
17
def display_invoicing(request, *args, **kwargs):
18
    if request.method == 'POST':
19
        try:
20
            seq_id = request.POST.get('id', None)
21
            service_name = kwargs['service']
22
            service = Service.objects.get(slug=service_name)
23
            invoicing = Invoicing.objects.get(seq_id=seq_id, service=service)
24
            return HttpResponseRedirect('%s' % invoicing.id)
25
        except:
26
            pass
27
    return HttpResponseRedirect('.')
28

    
29
class FacturationHomepageView(TemplateView):
30

    
31
    template_name = 'facturation/index.html'
32

    
33
    def get_context_data(self, **kwargs):
34
        context = super(FacturationHomepageView, self).get_context_data(**kwargs)
35
        current = Invoicing.objects.current_for_service(self.service)
36
        last = Invoicing.objects.last_for_service(self.service)
37
        context['current'] = current
38
        context['last'] = last
39
        return context
40

    
41
class FacturationRebillView(cbv.FormView):
42
    template_name = 'facturation/rebill.html'
43
    form_class = forms.FacturationRebillForm
44
    success_url = '../..'
45

    
46
    def post(self, request, *args, **kwarg):
47
        return super(FacturationRebillView, self).post(request, *args, **kwarg)
48

    
49
    def form_valid(self, form):
50
        invoice = Invoice.objects.get(id=form.data['invoice_id'])
51
        for act in invoice.acts.all():
52
            act.is_billed = False
53
            act.healthcare = None
54
            act.save()
55
        invoice.rejected = True
56
        invoice.save()
57
        return super(FacturationRebillView, self).form_valid(form)
58

    
59
rebill_form = FacturationRebillView.as_view()
60

    
61
class FacturationDetailView(UpdateView):
62

    
63
    context_object_name = "invoicing"
64
    model = Invoicing
65
    template_name = 'facturation/detail.html'
66

    
67
    def post(self, request, *args, **kwargs):
68
        return HttpResponseRedirect('/')
69

    
70
    def get_context_data(self, **kwargs):
71
        context = super(FacturationDetailView, self).get_context_data(**kwargs)
72
        if self.service.name == 'CMPP':
73
            (len_patients, len_invoices, len_invoices_hors_pause,
74
            len_acts_invoiced, len_acts_invoiced_hors_pause,
75
            len_patient_invoiced, len_patient_invoiced_hors_pause,
76
            len_acts_lost, len_patient_with_lost_acts,
77
            patients_stats, days_not_locked, len_patient_acts_paused,
78
                len_acts_paused, len_acts_lost_missing_policy,
79
                len_patient_with_lost_acts_missing_policy) = \
80
            context['invoicing'].get_stats_or_validate()
81
            context['len_patients'] = len_patients
82
            context['len_invoices'] = len_invoices
83
            context['len_invoices_hors_pause'] = len_invoices_hors_pause
84
            context['len_invoices_pause'] = len_invoices - len_invoices_hors_pause
85
            context['len_acts_invoiced'] = len_acts_invoiced
86
            context['len_acts_invoiced_hors_pause'] = len_acts_invoiced_hors_pause
87
            context['len_acts_invoiced_pause'] = len_acts_invoiced - len_acts_invoiced_hors_pause
88
            context['len_patient_invoiced'] = len_patient_invoiced
89
            context['len_patient_invoiced_hors_pause'] = len_patient_invoiced_hors_pause
90
            context['len_patient_invoiced_pause'] = len_patient_invoiced - len_patient_invoiced_hors_pause
91
            context['len_acts_lost'] = len_acts_lost
92
            context['len_patient_with_lost_acts'] = len_patient_with_lost_acts
93
            context['patients_stats'] = patients_stats
94
            context['days_not_locked'] = days_not_locked
95
            context['len_patient_acts_paused'] = len_patient_acts_paused
96
            context['len_acts_paused'] = len_acts_paused
97
            context['len_acts_lost_missing_policy'] = len_acts_lost_missing_policy
98
            context['len_patient_with_lost_acts_missing_policy'] = len_patient_with_lost_acts_missing_policy
99
        elif self.service.name == 'CAMSP':
100
            (len_patient_pause, len_patient_hors_pause,
101
                len_acts_pause, len_acts_hors_pause, patients_stats,
102
                days_not_locked, len_patient_acts_paused,
103
                len_acts_paused, patients_missing_policy) = \
104
                    context['invoicing'].get_stats_or_validate()
105
            if context['invoicing'].status == Invoicing.STATUS.closed and \
106
                    date.today() > context['invoicing'].end_date:
107
                context['show_validation_btn'] = True
108
            context['len_patient_pause'] = len_patient_pause
109
            context['len_patient_hors_pause'] = len_patient_hors_pause
110
            context['len_acts_pause'] = len_acts_pause
111
            context['len_acts_hors_pause'] = len_acts_hors_pause
112
            context['patients_stats'] = patients_stats
113
            context['days_not_locked'] = days_not_locked
114
            context['len_patient_acts_paused'] = len_patient_acts_paused
115
            context['len_acts_paused'] = len_acts_paused
116
            context['patients_missing_policy'] = patients_missing_policy
117
        elif 'SESSAD' in self.service.name:
118
            (len_patient_pause, len_patient_hors_pause,
119
                len_acts_pause, len_acts_hors_pause,
120
                len_patient_missing_notif, len_acts_missing_notif,
121
                patients_stats, days_not_locked,
122
                len_patient_acts_paused, len_acts_paused,
123
                patients_missing_policy, patients_missing_notif) = \
124
                    context['invoicing'].get_stats_or_validate()
125
            if context['invoicing'].status == Invoicing.STATUS.closed and \
126
                    date.today() > context['invoicing'].end_date:
127
                context['show_validation_btn'] = True
128
            context['len_patient_pause'] = len_patient_pause
129
            context['len_patient_hors_pause'] = len_patient_hors_pause
130
            context['len_acts_pause'] = len_acts_pause
131
            context['len_acts_hors_pause'] = len_acts_hors_pause
132
            context['len_patient_missing_notif'] = len_patient_missing_notif
133
            context['len_acts_missing_notif'] = len_acts_missing_notif
134
            context['patients_stats'] = patients_stats
135
            context['days_not_locked'] = days_not_locked
136
            context['len_patient_acts_paused'] = len_patient_acts_paused
137
            context['len_acts_paused'] = len_acts_paused
138
            context['patients_missing_policy'] = patients_missing_policy
139
            context['patients_missing_notif'] = patients_missing_notif
140
        return context
141

    
142

    
143
class CloseInvoicingView(cbv.FormView):
144
    template_name = 'facturation/close.html'
145
    form_class = forms.CloseInvoicingForm
146
    success_url = '..'
147

    
148
    def post(self, request, *args, **kwarg):
149
        return super(CloseInvoicingView, self).post(request, *args, **kwarg)
150

    
151
    def form_valid(self, form):
152
        service = Service.objects.get(name=form.data['service_name'])
153
        invoicing = Invoicing.objects.get(id=form.data['invoicing_id'])
154
        date_selected = datetime.strptime(form.data['date'], "%d/%m/%Y")
155
        invoicing.close(end_date=date_selected)
156
        return super(CloseInvoicingView, self).form_valid(form)
157

    
158
close_form = CloseInvoicingView.as_view()
159

    
160

    
161
class ValidationFacturationView(UpdateView):
162

    
163
    context_object_name = "invoicing"
164
    model = Invoicing
165
    template_name = 'facturation/validation.html'
166

    
167
    def post(self, request, *args, **kwargs):
168
        pk = self.kwargs.get('pk', None)
169
        invoicing = None
170
        if pk is not None:
171
            invoicing = Invoicing.objects.get(pk=pk, service=self.service)
172
        if not invoicing or \
173
                invoicing.status != Invoicing.STATUS.closed:
174
            return HttpResponseRedirect('..')
175
        invoicing.get_stats_or_validate(commit=True)
176
        return HttpResponseRedirect('..')
177

    
178
    def get_context_data(self, **kwargs):
179
        context = super(ValidationFacturationView, self).get_context_data(**kwargs)
180
        return context
181

    
182
class FacturationDownloadView(cbv.DetailView):
183
    context_object_name = "invoicing"
184
    model = Invoicing
185

    
186
    def get(self, *args, **kwargs):
187
        invoicing = self.get_object()
188
        path = render_invoicing(invoicing, delete=False)
189
        content = File(file(path))
190
        response = HttpResponse(content,'application/pdf')
191
        response['Content-Length'] = content.size
192
        response['Content-Disposition'] = 'attachment; filename="facturation-%s.pdf"' % invoicing.seq_id
193
        return response
194

    
195
class FacturationExportView(cbv.DetailView):
196
    context_object_name = "invoicing"
197
    model = Invoicing
198

    
199
    def get(self, *args, **kwargs):
200
        invoicing = self.get_object()
201
        path = invoicing.export_for_accounting()
202
        content = File(file(path))
203
        response = HttpResponse(content,'text/plain')
204
        response['Content-Length'] = content.size
205
        response['Content-Disposition'] = 'attachment; filename="export-%s.txt"' % invoicing.seq_id
206
        return response
(14-14/14)