2020-03-20 23:30:49 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-03-21 08:22:38 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
from tempfile import mkdtemp
|
|
|
|
|
2020-03-21 15:49:18 +00:00
|
|
|
from crispy_forms.helper import FormHelper
|
2020-03-20 23:30:49 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-03-21 15:49:18 +00:00
|
|
|
from django.db.models import Q
|
2020-03-21 08:22:38 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template.loader import render_to_string
|
2020-03-21 15:49:18 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-03-20 23:52:26 +00:00
|
|
|
from django.views.generic import CreateView, UpdateView
|
2020-03-21 06:36:07 +00:00
|
|
|
from django.views.generic.base import View
|
2020-03-20 23:30:49 +00:00
|
|
|
from django_tables2 import SingleTableView
|
2020-03-21 08:22:38 +00:00
|
|
|
from note_kfet.settings.base import BASE_DIR
|
2020-03-20 23:30:49 +00:00
|
|
|
|
2020-03-22 00:22:27 +00:00
|
|
|
from .forms import InvoiceForm, ProductFormSet, ProductFormSetHelper
|
|
|
|
from .models import Invoice, Product
|
|
|
|
from .tables import InvoiceTable
|
2020-03-20 23:30:49 +00:00
|
|
|
|
|
|
|
|
2020-03-22 00:22:27 +00:00
|
|
|
class InvoiceCreateView(LoginRequiredMixin, CreateView):
|
2020-03-20 23:52:26 +00:00
|
|
|
"""
|
2020-03-22 00:22:27 +00:00
|
|
|
Create Invoice
|
2020-03-20 23:52:26 +00:00
|
|
|
"""
|
2020-03-22 00:22:27 +00:00
|
|
|
model = Invoice
|
|
|
|
form_class = InvoiceForm
|
2020-03-21 15:49:18 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
form = context['form']
|
|
|
|
form.helper = FormHelper()
|
|
|
|
form.helper.form_tag = False
|
|
|
|
form_set = ProductFormSet(instance=form.instance)
|
|
|
|
context['formset'] = form_set
|
|
|
|
context['helper'] = ProductFormSetHelper()
|
|
|
|
context['no_cache'] = True
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
ret = super().form_valid(form)
|
|
|
|
|
2020-03-21 17:59:13 +00:00
|
|
|
kwargs = {}
|
|
|
|
for key in self.request.POST:
|
|
|
|
value = self.request.POST[key]
|
2020-03-22 14:24:54 +00:00
|
|
|
if key.endswith("amount") and value:
|
2020-03-21 17:59:13 +00:00
|
|
|
kwargs[key] = str(int(100 * float(value)))
|
2020-03-22 14:24:54 +00:00
|
|
|
elif value:
|
2020-03-21 17:59:13 +00:00
|
|
|
kwargs[key] = value
|
|
|
|
|
|
|
|
formset = ProductFormSet(kwargs, instance=form.instance)
|
2020-03-21 15:49:18 +00:00
|
|
|
if formset.is_valid():
|
|
|
|
for f in formset:
|
|
|
|
if f.is_valid() and f.instance.designation:
|
|
|
|
f.save()
|
|
|
|
f.instance.save()
|
|
|
|
else:
|
|
|
|
f.instance = None
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2020-03-22 00:22:27 +00:00
|
|
|
return reverse_lazy('treasury:invoice')
|
2020-03-20 23:52:26 +00:00
|
|
|
|
|
|
|
|
2020-03-22 00:22:27 +00:00
|
|
|
class InvoiceListView(LoginRequiredMixin, SingleTableView):
|
2020-03-20 23:30:49 +00:00
|
|
|
"""
|
2020-03-22 00:22:27 +00:00
|
|
|
List existing Invoices
|
2020-03-20 23:30:49 +00:00
|
|
|
"""
|
2020-03-22 00:22:27 +00:00
|
|
|
model = Invoice
|
|
|
|
table_class = InvoiceTable
|
2020-03-20 23:52:26 +00:00
|
|
|
|
|
|
|
|
2020-03-22 00:22:27 +00:00
|
|
|
class InvoiceUpdateView(LoginRequiredMixin, UpdateView):
|
2020-03-20 23:52:26 +00:00
|
|
|
"""
|
2020-03-22 00:22:27 +00:00
|
|
|
Create Invoice
|
2020-03-20 23:52:26 +00:00
|
|
|
"""
|
2020-03-22 00:22:27 +00:00
|
|
|
model = Invoice
|
|
|
|
form_class = InvoiceForm
|
2020-03-21 15:49:18 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
form = context['form']
|
|
|
|
form.helper = FormHelper()
|
|
|
|
form.helper.form_tag = False
|
2020-03-22 14:24:54 +00:00
|
|
|
form.fields['date'].initial = form.instance.date
|
2020-03-21 15:49:18 +00:00
|
|
|
form_set = ProductFormSet(instance=form.instance)
|
|
|
|
context['formset'] = form_set
|
|
|
|
context['helper'] = ProductFormSetHelper()
|
|
|
|
context['no_cache'] = True
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
ret = super().form_valid(form)
|
|
|
|
|
2020-03-21 17:59:13 +00:00
|
|
|
kwargs = {}
|
|
|
|
for key in self.request.POST:
|
|
|
|
value = self.request.POST[key]
|
2020-03-22 14:24:54 +00:00
|
|
|
if key.endswith("amount") and value:
|
2020-03-21 17:59:13 +00:00
|
|
|
kwargs[key] = str(int(100 * float(value)))
|
2020-03-22 14:24:54 +00:00
|
|
|
elif value:
|
2020-03-21 17:59:13 +00:00
|
|
|
kwargs[key] = value
|
|
|
|
|
|
|
|
formset = ProductFormSet(kwargs, instance=form.instance)
|
2020-03-21 15:49:18 +00:00
|
|
|
saved = []
|
|
|
|
if formset.is_valid():
|
|
|
|
for f in formset:
|
|
|
|
if f.is_valid() and f.instance.designation:
|
|
|
|
f.save()
|
|
|
|
f.instance.save()
|
|
|
|
saved.append(f.instance.pk)
|
|
|
|
else:
|
|
|
|
f.instance = None
|
2020-03-22 14:24:54 +00:00
|
|
|
Product.objects.filter(~Q(pk__in=saved), invoice=form.instance).delete()
|
2020-03-21 15:49:18 +00:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2020-03-22 00:22:27 +00:00
|
|
|
return reverse_lazy('treasury:invoice')
|
2020-03-21 06:36:07 +00:00
|
|
|
|
|
|
|
|
2020-03-22 00:22:27 +00:00
|
|
|
class InvoiceRenderView(LoginRequiredMixin, View):
|
2020-03-21 06:36:07 +00:00
|
|
|
"""
|
2020-03-22 00:22:27 +00:00
|
|
|
Render Invoice as generated PDF
|
2020-03-21 06:36:07 +00:00
|
|
|
"""
|
2020-03-21 08:22:38 +00:00
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
pk = kwargs["pk"]
|
2020-03-22 00:22:27 +00:00
|
|
|
invoice = Invoice.objects.get(pk=pk)
|
|
|
|
products = Product.objects.filter(invoice=invoice).all()
|
2020-03-21 08:22:38 +00:00
|
|
|
|
2020-03-22 14:24:54 +00:00
|
|
|
invoice.place = "Cachan"
|
|
|
|
invoice.my_name = "BDE ENS Cachan"
|
|
|
|
invoice.my_address_street = "61 avenue du Président Wilson"
|
|
|
|
invoice.my_city = "94230 Cachan"
|
|
|
|
invoice.bank_code = 30003
|
|
|
|
invoice.desk_code = 3894
|
|
|
|
invoice.account_number = 37280662
|
|
|
|
invoice.rib_key = 14
|
|
|
|
invoice.bic = "SOGEFRPP"
|
|
|
|
|
|
|
|
invoice.description = invoice.description.replace("\r", "").replace("\n", "\\\\ ")
|
|
|
|
invoice.address = invoice.address.replace("\r", "").replace("\n", "\\\\ ")
|
2020-03-22 00:22:27 +00:00
|
|
|
tex = render_to_string("treasury/invoice_sample.tex", dict(obj=invoice, products=products))
|
2020-03-21 08:22:38 +00:00
|
|
|
try:
|
|
|
|
os.mkdir(BASE_DIR + "/tmp")
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
tmp_dir = mkdtemp(prefix=BASE_DIR + "/tmp/")
|
|
|
|
|
2020-03-21 16:29:39 +00:00
|
|
|
try:
|
2020-03-22 00:22:27 +00:00
|
|
|
with open("{}/invoice-{:d}.tex".format(tmp_dir, pk), "wb") as f:
|
2020-03-21 16:29:39 +00:00
|
|
|
f.write(tex.encode("UTF-8"))
|
|
|
|
del tex
|
|
|
|
|
2020-03-21 16:54:08 +00:00
|
|
|
for _ in range(2):
|
|
|
|
error = subprocess.Popen(
|
2020-03-22 00:22:27 +00:00
|
|
|
["pdflatex", "invoice-{}.tex".format(pk)],
|
2020-03-21 16:54:08 +00:00
|
|
|
cwd=tmp_dir,
|
|
|
|
).wait()
|
|
|
|
|
|
|
|
if error:
|
2020-03-22 00:22:27 +00:00
|
|
|
raise IOError("An error attempted while generating a invoice (code=" + str(error) + ")")
|
2020-03-21 16:29:39 +00:00
|
|
|
|
2020-03-22 00:22:27 +00:00
|
|
|
pdf = open("{}/invoice-{}.pdf".format(tmp_dir, pk), 'rb').read()
|
2020-03-21 16:29:39 +00:00
|
|
|
response = HttpResponse(pdf, content_type="application/pdf")
|
2020-03-22 00:22:27 +00:00
|
|
|
response['Content-Disposition'] = "inline;filename=invoice-{:d}.pdf".format(pk)
|
2020-03-21 16:29:39 +00:00
|
|
|
except IOError as e:
|
|
|
|
raise e
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(tmp_dir)
|
2020-03-21 08:22:38 +00:00
|
|
|
|
|
|
|
return response
|