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
|
from b2 import b2_is_configured, get_all_infos, buildall, sendmail
|
17
|
from noemie import noemie_mails, noemie_from_mail
|
18
|
|
19
|
def display_invoicing(request, *args, **kwargs):
|
20
|
if request.method == 'POST':
|
21
|
try:
|
22
|
seq_id = request.POST.get('id', None)
|
23
|
service_name = kwargs['service']
|
24
|
service = Service.objects.get(slug=service_name)
|
25
|
invoicing = Invoicing.objects.get(seq_id=seq_id, service=service)
|
26
|
return HttpResponseRedirect('%s' % invoicing.id)
|
27
|
except:
|
28
|
pass
|
29
|
return HttpResponseRedirect('.')
|
30
|
|
31
|
class FacturationHomepageView(TemplateView):
|
32
|
|
33
|
template_name = 'facturation/index.html'
|
34
|
|
35
|
def get_context_data(self, **kwargs):
|
36
|
context = super(FacturationHomepageView, self).get_context_data(**kwargs)
|
37
|
current = Invoicing.objects.current_for_service(self.service)
|
38
|
last = Invoicing.objects.last_for_service(self.service)
|
39
|
context['current'] = current
|
40
|
context['last'] = last
|
41
|
return context
|
42
|
|
43
|
class FacturationRebillView(cbv.FormView):
|
44
|
template_name = 'facturation/rebill.html'
|
45
|
form_class = forms.FacturationRebillForm
|
46
|
success_url = '../..'
|
47
|
|
48
|
def post(self, request, *args, **kwarg):
|
49
|
return super(FacturationRebillView, self).post(request, *args, **kwarg)
|
50
|
|
51
|
def form_valid(self, form):
|
52
|
invoice = Invoice.objects.get(id=form.data['invoice_id'])
|
53
|
for act in invoice.acts.all():
|
54
|
act.is_billed = False
|
55
|
act.healthcare = None
|
56
|
act.save()
|
57
|
invoice.rejected = True
|
58
|
invoice.save()
|
59
|
return super(FacturationRebillView, self).form_valid(form)
|
60
|
|
61
|
rebill_form = FacturationRebillView.as_view()
|
62
|
|
63
|
class FacturationDetailView(UpdateView):
|
64
|
|
65
|
context_object_name = "invoicing"
|
66
|
model = Invoicing
|
67
|
template_name = 'facturation/detail.html'
|
68
|
|
69
|
def post(self, request, *args, **kwargs):
|
70
|
return HttpResponseRedirect('/')
|
71
|
|
72
|
def get_context_data(self, **kwargs):
|
73
|
context = super(FacturationDetailView, self).get_context_data(**kwargs)
|
74
|
if self.service.name == 'CMPP':
|
75
|
(len_patients, len_invoices, len_invoices_hors_pause,
|
76
|
len_acts_invoiced, len_acts_invoiced_hors_pause,
|
77
|
len_patient_invoiced, len_patient_invoiced_hors_pause,
|
78
|
len_acts_lost, len_patient_with_lost_acts, patients_stats,
|
79
|
days_not_locked, len_patient_acts_paused,
|
80
|
len_acts_paused, len_acts_losts_missing_policy,
|
81
|
len_patient_with_lost_acts_missing_policy,
|
82
|
len_acts_losts_missing_birthdate,
|
83
|
len_patient_with_lost_acts_missing_birthdate) = \
|
84
|
context['invoicing'].get_stats_or_validate()
|
85
|
context['len_patients'] = len_patients
|
86
|
context['len_invoices'] = len_invoices
|
87
|
context['len_invoices_hors_pause'] = len_invoices_hors_pause
|
88
|
context['len_invoices_pause'] = len_invoices - len_invoices_hors_pause
|
89
|
context['len_acts_invoiced'] = len_acts_invoiced
|
90
|
context['len_acts_invoiced_hors_pause'] = len_acts_invoiced_hors_pause
|
91
|
context['len_acts_invoiced_pause'] = len_acts_invoiced - len_acts_invoiced_hors_pause
|
92
|
context['len_patient_invoiced'] = len_patient_invoiced
|
93
|
context['len_patient_invoiced_hors_pause'] = len_patient_invoiced_hors_pause
|
94
|
context['len_patient_invoiced_pause'] = len_patient_invoiced - len_patient_invoiced_hors_pause
|
95
|
context['len_acts_lost'] = len_acts_lost
|
96
|
context['len_patient_with_lost_acts'] = len_patient_with_lost_acts
|
97
|
context['patients_stats'] = patients_stats
|
98
|
context['days_not_locked'] = days_not_locked
|
99
|
context['len_patient_acts_paused'] = len_patient_acts_paused
|
100
|
context['len_acts_paused'] = len_acts_paused
|
101
|
context['len_acts_losts_missing_policy'] = len_acts_losts_missing_policy
|
102
|
context['len_patient_with_lost_acts_missing_policy'] = len_patient_with_lost_acts_missing_policy
|
103
|
context['len_acts_losts_missing_birthdate'] = len_acts_losts_missing_birthdate
|
104
|
context['len_patient_with_lost_acts_missing_birthdate'] = len_patient_with_lost_acts_missing_birthdate
|
105
|
context['some_stats'] = context['invoicing'].get_stats_per_price_per_year()
|
106
|
context['batches'] = context['invoicing'].get_batches()
|
107
|
previous_seq_id = context['invoicing'].seq_id - 1
|
108
|
try:
|
109
|
previous = Invoicing.objects.get(service= self.service, seq_id=previous_seq_id)
|
110
|
if previous.status == Invoicing.STATUS.validated:
|
111
|
context['previous_validated'] = True
|
112
|
except:
|
113
|
pass
|
114
|
elif self.service.name == 'CAMSP':
|
115
|
(len_patient_pause, len_patient_hors_pause,
|
116
|
len_acts_pause, len_acts_hors_pause, patients_stats,
|
117
|
days_not_locked, len_patient_acts_paused,
|
118
|
len_acts_paused, patients_missing_policy) = \
|
119
|
context['invoicing'].get_stats_or_validate()
|
120
|
if context['invoicing'].status == Invoicing.STATUS.closed and \
|
121
|
date.today() > context['invoicing'].end_date:
|
122
|
context['show_validation_btn'] = True
|
123
|
context['len_patient_pause'] = len_patient_pause
|
124
|
context['len_patient_hors_pause'] = len_patient_hors_pause
|
125
|
context['len_acts_pause'] = len_acts_pause
|
126
|
context['len_acts_hors_pause'] = len_acts_hors_pause
|
127
|
context['patients_stats'] = patients_stats
|
128
|
context['days_not_locked'] = days_not_locked
|
129
|
context['len_patient_acts_paused'] = len_patient_acts_paused
|
130
|
context['len_acts_paused'] = len_acts_paused
|
131
|
context['patients_missing_policy'] = patients_missing_policy
|
132
|
elif 'SESSAD' in self.service.name:
|
133
|
(len_patient_pause, len_patient_hors_pause,
|
134
|
len_acts_pause, len_acts_hors_pause,
|
135
|
len_patient_missing_notif, len_acts_missing_notif,
|
136
|
patients_stats, days_not_locked,
|
137
|
len_patient_acts_paused, len_acts_paused,
|
138
|
patients_missing_policy, patients_missing_notif) = \
|
139
|
context['invoicing'].get_stats_or_validate()
|
140
|
if context['invoicing'].status == Invoicing.STATUS.closed and \
|
141
|
date.today() > context['invoicing'].end_date:
|
142
|
context['show_validation_btn'] = True
|
143
|
context['len_patient_pause'] = len_patient_pause
|
144
|
context['len_patient_hors_pause'] = len_patient_hors_pause
|
145
|
context['len_acts_pause'] = len_acts_pause
|
146
|
context['len_acts_hors_pause'] = len_acts_hors_pause
|
147
|
context['len_patient_missing_notif'] = len_patient_missing_notif
|
148
|
context['len_acts_missing_notif'] = len_acts_missing_notif
|
149
|
context['patients_stats'] = patients_stats
|
150
|
context['days_not_locked'] = days_not_locked
|
151
|
context['len_patient_acts_paused'] = len_patient_acts_paused
|
152
|
context['len_acts_paused'] = len_acts_paused
|
153
|
context['patients_missing_policy'] = patients_missing_policy
|
154
|
context['patients_missing_notif'] = patients_missing_notif
|
155
|
return context
|
156
|
|
157
|
|
158
|
class CloseInvoicingView(cbv.FormView):
|
159
|
template_name = 'facturation/close.html'
|
160
|
form_class = forms.CloseInvoicingForm
|
161
|
success_url = '..'
|
162
|
|
163
|
def post(self, request, *args, **kwarg):
|
164
|
return super(CloseInvoicingView, self).post(request, *args, **kwarg)
|
165
|
|
166
|
def form_valid(self, form):
|
167
|
service = Service.objects.get(name=form.data['service_name'])
|
168
|
invoicing = Invoicing.objects.get(id=form.data['invoicing_id'])
|
169
|
date_selected = datetime.strptime(form.data['date'], "%d/%m/%Y")
|
170
|
invoicing.close(end_date=date_selected)
|
171
|
return super(CloseInvoicingView, self).form_valid(form)
|
172
|
|
173
|
close_form = CloseInvoicingView.as_view()
|
174
|
|
175
|
|
176
|
class ValidationFacturationView(UpdateView):
|
177
|
|
178
|
context_object_name = "invoicing"
|
179
|
model = Invoicing
|
180
|
template_name = 'facturation/validation.html'
|
181
|
|
182
|
def post(self, request, *args, **kwargs):
|
183
|
pk = self.kwargs.get('pk', None)
|
184
|
invoicing = None
|
185
|
if pk is not None:
|
186
|
invoicing = Invoicing.objects.get(pk=pk, service=self.service)
|
187
|
if not invoicing or \
|
188
|
invoicing.status != Invoicing.STATUS.closed:
|
189
|
return HttpResponseRedirect('..')
|
190
|
invoicing.get_stats_or_validate(commit=True)
|
191
|
return HttpResponseRedirect('..')
|
192
|
|
193
|
def get_context_data(self, **kwargs):
|
194
|
context = super(ValidationFacturationView, self).get_context_data(**kwargs)
|
195
|
return context
|
196
|
|
197
|
class FacturationDownloadView(cbv.DetailView):
|
198
|
context_object_name = "invoicing"
|
199
|
model = Invoicing
|
200
|
|
201
|
def get(self, *args, **kwargs):
|
202
|
invoicing = self.get_object()
|
203
|
path = render_invoicing(invoicing, delete=False)
|
204
|
content = File(file(path))
|
205
|
response = HttpResponse(content,'application/pdf')
|
206
|
response['Content-Length'] = content.size
|
207
|
response['Content-Disposition'] = 'attachment; filename="facturation-%s.pdf"' % invoicing.seq_id
|
208
|
return response
|
209
|
|
210
|
class FacturationExportView(cbv.DetailView):
|
211
|
context_object_name = "invoicing"
|
212
|
model = Invoicing
|
213
|
|
214
|
def get(self, *args, **kwargs):
|
215
|
invoicing = self.get_object()
|
216
|
path = invoicing.export_for_accounting()
|
217
|
content = File(file(path))
|
218
|
response = HttpResponse(content,'text/plain')
|
219
|
response['Content-Length'] = content.size
|
220
|
response['Content-Disposition'] = 'attachment; filename="export-%s.txt"' % invoicing.seq_id
|
221
|
return response
|
222
|
|
223
|
|
224
|
class FacturationTransmissionView(UpdateView):
|
225
|
context_object_name = "invoicing"
|
226
|
model = Invoicing
|
227
|
template_name = 'facturation/transmission.html'
|
228
|
|
229
|
def get_context_data(self, **kwargs):
|
230
|
context = super(FacturationTransmissionView, self).get_context_data(**kwargs)
|
231
|
context['b2_is_configured'] = b2_is_configured()
|
232
|
try:
|
233
|
context['b2'] = get_all_infos(context['invoicing'].seq_id)
|
234
|
except IOError, e:
|
235
|
context['error_message'] = e
|
236
|
return context
|
237
|
|
238
|
class FacturationTransmissionBuildView(cbv.DetailView):
|
239
|
context_object_name = "invoicing"
|
240
|
model = Invoicing
|
241
|
|
242
|
def get(self, *args, **kwargs):
|
243
|
invoicing = self.get_object()
|
244
|
if b2_is_configured() and invoicing.status == "validated":
|
245
|
buildall(invoicing.seq_id)
|
246
|
return HttpResponseRedirect('..')
|
247
|
|
248
|
class FacturationTransmissionMailView(UpdateView):
|
249
|
context_object_name = "invoicing"
|
250
|
model = Invoicing
|
251
|
|
252
|
def get(self, *args, **kwargs):
|
253
|
invoicing = self.get_object()
|
254
|
if b2_is_configured() and invoicing.status == "validated":
|
255
|
sendmail(invoicing.seq_id, oneb2=kwargs.get('b2'))
|
256
|
return HttpResponseRedirect('..')
|
257
|
|
258
|
|
259
|
class FacturationNoemieView(TemplateView):
|
260
|
template_name = 'facturation/noemie.html'
|
261
|
|
262
|
def get_context_data(self, **kwargs):
|
263
|
context = super(FacturationNoemieView, self).get_context_data(**kwargs)
|
264
|
context['b2_is_configured'] = b2_is_configured()
|
265
|
if b2_is_configured():
|
266
|
name = kwargs.get('name')
|
267
|
try:
|
268
|
if name:
|
269
|
context['noemie'] = noemie_from_mail(name)
|
270
|
else:
|
271
|
context['noemies'] = noemie_mails()
|
272
|
except IOError, e:
|
273
|
context['error_message'] = e
|
274
|
return context
|