2024-02-07 01:26:49 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
2020-03-20 23:30:49 +00:00
|
|
|
# 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-08-13 13:20:15 +00:00
|
|
|
from django.core.exceptions import ValidationError, PermissionDenied
|
2020-09-11 20:52:16 +00:00
|
|
|
from django.db import transaction
|
2020-03-21 15:49:18 +00:00
|
|
|
from django.db.models import Q
|
2020-04-22 01:26:45 +00:00
|
|
|
from django.forms import Form
|
2020-03-21 08:22:38 +00:00
|
|
|
from django.http import HttpResponse
|
2020-03-23 22:42:37 +00:00
|
|
|
from django.shortcuts import redirect
|
2020-03-21 15:49:18 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-07-30 15:30:21 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-08-13 13:20:15 +00:00
|
|
|
from django.views.generic import UpdateView, DetailView
|
2020-03-23 21:43:16 +00:00
|
|
|
from django.views.generic.base import View, TemplateView
|
2020-08-07 09:04:54 +00:00
|
|
|
from django.views.generic.edit import BaseFormView, DeleteView
|
2024-07-30 19:42:45 +00:00
|
|
|
from django_tables2 import MultiTableMixin, SingleTableMixin, SingleTableView
|
2020-04-22 01:26:45 +00:00
|
|
|
from note.models import SpecialTransaction, NoteSpecial, Alias
|
2020-03-21 08:22:38 +00:00
|
|
|
from note_kfet.settings.base import BASE_DIR
|
2020-03-31 02:16:30 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-08-13 13:20:15 +00:00
|
|
|
from permission.views import ProtectQuerysetMixin, ProtectedCreateView
|
2020-03-20 23:30:49 +00:00
|
|
|
|
2021-09-05 22:47:11 +00:00
|
|
|
from .forms import InvoiceForm, ProductFormSet, ProductFormSetHelper, RemittanceForm, \
|
|
|
|
LinkTransactionToRemittanceForm, SogeCreditForm
|
2020-04-22 01:26:45 +00:00
|
|
|
from .models import Invoice, Product, Remittance, SpecialTransactionProxy, SogeCredit
|
|
|
|
from .tables import InvoiceTable, RemittanceTable, SpecialTransactionTable, SogeCreditTable
|
2020-03-20 23:30:49 +00:00
|
|
|
|
|
|
|
|
2020-08-15 21:27:58 +00:00
|
|
|
class InvoiceCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
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-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Create new invoice")}
|
2020-03-21 15:49:18 +00:00
|
|
|
|
2020-08-13 13:20:15 +00:00
|
|
|
def get_sample_object(self):
|
|
|
|
return Invoice(
|
|
|
|
id=0,
|
|
|
|
object="",
|
|
|
|
description="",
|
|
|
|
name="",
|
|
|
|
address="",
|
|
|
|
)
|
|
|
|
|
2020-03-21 15:49:18 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-24 19:22:15 +00:00
|
|
|
|
2020-03-21 15:49:18 +00:00
|
|
|
form = context['form']
|
|
|
|
form.helper = FormHelper()
|
2020-03-24 19:22:15 +00:00
|
|
|
# Remove form tag on the generation of the form in the template (already present on the template)
|
2020-03-21 15:49:18 +00:00
|
|
|
form.helper.form_tag = False
|
2020-03-24 19:22:15 +00:00
|
|
|
# The formset handles the set of the products
|
2020-03-21 15:49:18 +00:00
|
|
|
form_set = ProductFormSet(instance=form.instance)
|
|
|
|
context['formset'] = form_set
|
|
|
|
context['helper'] = ProductFormSetHelper()
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
2020-09-04 13:53:00 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
del form.fields["locked"]
|
|
|
|
return form
|
|
|
|
|
2020-09-11 20:52:16 +00:00
|
|
|
@transaction.atomic
|
2020-03-21 15:49:18 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
ret = super().form_valid(form)
|
|
|
|
|
2020-03-24 19:22:15 +00:00
|
|
|
# For each product, we save it
|
2020-03-27 12:50:02 +00:00
|
|
|
formset = ProductFormSet(self.request.POST, instance=form.instance)
|
2020-03-21 15:49:18 +00:00
|
|
|
if formset.is_valid():
|
|
|
|
for f in formset:
|
2020-03-24 19:22:15 +00:00
|
|
|
# We don't save the product if the designation is not entered, ie. if the line is empty
|
2020-03-21 15:49:18 +00:00
|
|
|
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 17:27:22 +00:00
|
|
|
return reverse_lazy('treasury:invoice_list')
|
2020-03-20 23:52:26 +00:00
|
|
|
|
|
|
|
|
2020-08-13 13:20:15 +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-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Invoices list")}
|
2020-03-20 23:52:26 +00:00
|
|
|
|
2020-08-13 13:20:15 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2020-08-15 21:27:58 +00:00
|
|
|
# Check that the user is authenticated
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
return self.handle_no_permission()
|
|
|
|
|
2023-09-28 16:48:57 +00:00
|
|
|
if not PermissionBackend.has_model_perm(self.request, Invoice(), "view"):
|
2020-08-13 13:20:15 +00:00
|
|
|
raise PermissionDenied(_("You are not able to see the treasury interface."))
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
2020-03-20 23:52:26 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class InvoiceUpdateView(ProtectQuerysetMixin, 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-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Update an invoice")}
|
2020-03-21 15:49:18 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-24 19:22:15 +00:00
|
|
|
|
2020-03-21 15:49:18 +00:00
|
|
|
form = context['form']
|
|
|
|
form.helper = FormHelper()
|
2020-03-24 19:22:15 +00:00
|
|
|
# Remove form tag on the generation of the form in the template (already present on the template)
|
2020-03-21 15:49:18 +00:00
|
|
|
form.helper.form_tag = False
|
2020-03-24 19:22:15 +00:00
|
|
|
# The formset handles the set of the products
|
2020-08-07 09:04:54 +00:00
|
|
|
form_set = ProductFormSet(instance=self.object)
|
2020-03-21 15:49:18 +00:00
|
|
|
context['formset'] = form_set
|
|
|
|
context['helper'] = ProductFormSetHelper()
|
|
|
|
|
2020-08-07 09:04:54 +00:00
|
|
|
if self.object.locked:
|
|
|
|
for field_name in form.fields:
|
|
|
|
form.fields[field_name].disabled = True
|
|
|
|
for f in form_set.forms:
|
|
|
|
for field_name in f.fields:
|
|
|
|
f.fields[field_name].disabled = True
|
|
|
|
|
2020-03-21 15:49:18 +00:00
|
|
|
return context
|
|
|
|
|
2020-09-04 13:53:00 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
del form.fields["id"]
|
|
|
|
return form
|
|
|
|
|
2020-09-11 20:52:16 +00:00
|
|
|
@transaction.atomic
|
2020-03-21 15:49:18 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
ret = super().form_valid(form)
|
|
|
|
|
2020-03-27 12:50:02 +00:00
|
|
|
formset = ProductFormSet(self.request.POST, instance=form.instance)
|
2020-03-21 15:49:18 +00:00
|
|
|
saved = []
|
2020-03-24 19:22:15 +00:00
|
|
|
# For each product, we save it
|
2020-03-21 15:49:18 +00:00
|
|
|
if formset.is_valid():
|
|
|
|
for f in formset:
|
2020-03-24 19:22:15 +00:00
|
|
|
# We don't save the product if the designation is not entered, ie. if the line is empty
|
2020-03-21 15:49:18 +00:00
|
|
|
if f.is_valid() and f.instance.designation:
|
|
|
|
f.save()
|
|
|
|
f.instance.save()
|
|
|
|
saved.append(f.instance.pk)
|
|
|
|
else:
|
|
|
|
f.instance = None
|
2020-03-24 19:22:15 +00:00
|
|
|
# Remove old products that weren't given in the form
|
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 17:27:22 +00:00
|
|
|
return reverse_lazy('treasury:invoice_list')
|
2020-03-21 06:36:07 +00:00
|
|
|
|
|
|
|
|
2020-08-07 09:04:54 +00:00
|
|
|
class InvoiceDeleteView(ProtectQuerysetMixin, LoginRequiredMixin, DeleteView):
|
|
|
|
"""
|
|
|
|
Delete a non-validated WEI registration
|
|
|
|
"""
|
|
|
|
model = Invoice
|
|
|
|
extra_context = {"title": _("Delete invoice")}
|
|
|
|
|
2020-09-04 13:53:00 +00:00
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
if self.get_object().locked:
|
|
|
|
raise PermissionDenied(_("This invoice is locked and can't be deleted."))
|
|
|
|
return super().delete(request, *args, **kwargs)
|
|
|
|
|
2020-08-07 09:04:54 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('treasury:invoice_list')
|
|
|
|
|
|
|
|
|
2020-03-22 00:22:27 +00:00
|
|
|
class InvoiceRenderView(LoginRequiredMixin, View):
|
2020-03-21 06:36:07 +00:00
|
|
|
"""
|
2020-03-24 19:22:15 +00:00
|
|
|
Render Invoice as a generated PDF with the given information and a LaTeX template
|
2020-03-21 06:36:07 +00:00
|
|
|
"""
|
2020-03-21 08:22:38 +00:00
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
pk = kwargs["pk"]
|
2021-06-15 12:40:32 +00:00
|
|
|
invoice = Invoice.objects.filter(PermissionBackend.filter_queryset(request, Invoice, "view")).get(pk=pk)
|
2020-08-06 20:30:14 +00:00
|
|
|
tex = invoice.tex
|
2020-03-24 19:22:15 +00:00
|
|
|
|
2020-03-21 08:22:38 +00:00
|
|
|
try:
|
|
|
|
os.mkdir(BASE_DIR + "/tmp")
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
2020-03-24 19:22:15 +00:00
|
|
|
# We render the file in a temporary directory
|
2020-03-21 08:22:38 +00:00
|
|
|
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-24 19:22:15 +00:00
|
|
|
# The file has to be rendered twice
|
2021-05-05 17:13:16 +00:00
|
|
|
for _ignored in range(2):
|
2020-03-21 16:54:08 +00:00
|
|
|
error = subprocess.Popen(
|
2020-09-06 19:19:17 +00:00
|
|
|
["/usr/bin/xelatex", "-interaction=nonstopmode", "invoice-{}.tex".format(pk)],
|
2020-03-21 16:54:08 +00:00
|
|
|
cwd=tmp_dir,
|
2020-03-24 19:22:15 +00:00
|
|
|
stdin=open(os.devnull, "r"),
|
|
|
|
stderr=open(os.devnull, "wb"),
|
|
|
|
stdout=open(os.devnull, "wb"),
|
2020-03-21 16:54:08 +00:00
|
|
|
).wait()
|
|
|
|
|
|
|
|
if error:
|
2020-09-05 07:00:16 +00:00
|
|
|
with open("{}/invoice-{:d}.log".format(tmp_dir, pk), "r") as f:
|
|
|
|
log = f.read()
|
|
|
|
raise IOError("An error attempted while generating a invoice (code=" + str(error) + ")\n\n" + log)
|
2020-03-21 16:29:39 +00:00
|
|
|
|
2020-03-24 19:22:15 +00:00
|
|
|
# Display the generated pdf as a HTTP Response
|
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-04-23 16:28:16 +00:00
|
|
|
response['Content-Disposition'] = "inline;filename=Facture%20n°{:d}.pdf".format(pk)
|
2020-03-21 16:29:39 +00:00
|
|
|
except IOError as e:
|
|
|
|
raise e
|
|
|
|
finally:
|
2020-04-19 18:45:59 +00:00
|
|
|
# Delete all temporary files
|
2020-03-21 16:29:39 +00:00
|
|
|
shutil.rmtree(tmp_dir)
|
2020-03-21 08:22:38 +00:00
|
|
|
|
|
|
|
return response
|
2020-03-22 17:27:22 +00:00
|
|
|
|
|
|
|
|
2020-08-15 21:27:58 +00:00
|
|
|
class RemittanceCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
2020-03-22 17:27:22 +00:00
|
|
|
"""
|
|
|
|
Create Remittance
|
|
|
|
"""
|
|
|
|
model = Remittance
|
|
|
|
form_class = RemittanceForm
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Create a new remittance")}
|
2020-03-22 17:27:22 +00:00
|
|
|
|
2020-08-13 13:20:15 +00:00
|
|
|
def get_sample_object(self):
|
|
|
|
return Remittance(
|
|
|
|
remittance_type_id=1,
|
|
|
|
comment="",
|
|
|
|
)
|
|
|
|
|
2020-03-22 17:27:22 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('treasury:remittance_list')
|
|
|
|
|
2020-03-23 21:43:16 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2020-04-06 10:13:12 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2020-04-06 10:13:12 +00:00
|
|
|
context["special_transactions"] = SpecialTransactionTable(data=SpecialTransaction.objects.none())
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2020-04-06 10:13:12 +00:00
|
|
|
return context
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2020-03-22 17:27:22 +00:00
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
class RemittanceListView(LoginRequiredMixin, MultiTableMixin, TemplateView):
|
2020-03-22 17:27:22 +00:00
|
|
|
"""
|
|
|
|
List existing Remittances
|
|
|
|
"""
|
2020-03-23 21:43:16 +00:00
|
|
|
template_name = "treasury/remittance_list.html"
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Remittances list")}
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
tables = [
|
|
|
|
lambda data: RemittanceTable(data, prefix="opened-remittances-"),
|
|
|
|
lambda data: RemittanceTable(data, prefix="closed-remittances-"),
|
|
|
|
lambda data: SpecialTransactionTable(data, prefix="no-remittance-", exclude=('remittance_remove', )),
|
|
|
|
lambda data: SpecialTransactionTable(data, prefix="with-remittance-", exclude=('remittance_add', )),
|
|
|
|
]
|
|
|
|
paginate_by = 10 # number of rows in tables
|
|
|
|
|
2020-08-13 13:20:15 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2020-08-15 21:27:58 +00:00
|
|
|
# Check that the user is authenticated
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
return self.handle_no_permission()
|
|
|
|
|
2023-09-28 16:48:57 +00:00
|
|
|
if not PermissionBackend.has_model_perm(self.request, Remittance(), "view"):
|
2020-08-13 13:20:15 +00:00
|
|
|
raise PermissionDenied(_("You are not able to see the treasury interface."))
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
def get_tables_data(self):
|
|
|
|
return [
|
|
|
|
Remittance.objects.filter(closed=False).filter(
|
|
|
|
PermissionBackend.filter_queryset(self.request, Remittance, "view")),
|
|
|
|
Remittance.objects.filter(closed=True).filter(
|
|
|
|
PermissionBackend.filter_queryset(self.request, Remittance, "view")),
|
|
|
|
SpecialTransaction.objects.filter(source__in=NoteSpecial.objects.filter(~Q(remittancetype=None)),
|
|
|
|
specialtransactionproxy__remittance=None).filter(
|
|
|
|
PermissionBackend.filter_queryset(self.request, Remittance, "view")),
|
|
|
|
SpecialTransaction.objects.filter(source__in=NoteSpecial.objects.filter(~Q(remittancetype=None)),
|
|
|
|
specialtransactionproxy__remittance__closed=False).filter(
|
|
|
|
PermissionBackend.filter_queryset(self.request, Remittance, "view")),
|
|
|
|
]
|
|
|
|
|
2020-03-23 21:43:16 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2020-04-06 10:13:12 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
tables = context["tables"]
|
|
|
|
names = [
|
|
|
|
"opened_remittances",
|
|
|
|
"closed_remittances",
|
|
|
|
"special_transactions_no_remittance",
|
|
|
|
"special_transactions_with_remittance",
|
|
|
|
]
|
|
|
|
for name, table in zip(names, tables):
|
|
|
|
context[name] = table
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2020-04-06 10:13:12 +00:00
|
|
|
return context
|
2020-03-22 17:27:22 +00:00
|
|
|
|
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
class RemittanceUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableMixin, UpdateView):
|
2020-03-22 17:27:22 +00:00
|
|
|
"""
|
|
|
|
Update Remittance
|
|
|
|
"""
|
|
|
|
model = Remittance
|
|
|
|
form_class = RemittanceForm
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Update a remittance")}
|
2020-03-22 17:27:22 +00:00
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
table_class = SpecialTransactionTable
|
|
|
|
context_table_name = "special_transactions"
|
|
|
|
|
2020-03-22 17:27:22 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('treasury:remittance_list')
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
def get_table_data(self):
|
|
|
|
return SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self.object).filter(
|
|
|
|
PermissionBackend.filter_queryset(self.request, Remittance, "view"))
|
2020-03-23 21:43:16 +00:00
|
|
|
|
2024-07-30 19:42:45 +00:00
|
|
|
def get_table_kwargs(self):
|
|
|
|
return {"exclude": ('remittance_add', 'remittance_remove', ) if self.object.closed else ('remittance_add', )}
|
2020-03-23 22:42:37 +00:00
|
|
|
|
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class LinkTransactionToRemittanceView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
2020-03-24 19:22:15 +00:00
|
|
|
"""
|
|
|
|
Attach a special transaction to a remittance
|
|
|
|
"""
|
2020-03-23 22:42:37 +00:00
|
|
|
model = SpecialTransactionProxy
|
|
|
|
form_class = LinkTransactionToRemittanceForm
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Attach a transaction to a remittance")}
|
2020-03-23 22:42:37 +00:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('treasury:remittance_list')
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2020-04-06 10:13:12 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-23 22:42:37 +00:00
|
|
|
|
2020-04-06 10:13:12 +00:00
|
|
|
form = context["form"]
|
2020-03-23 22:42:37 +00:00
|
|
|
form.fields["last_name"].initial = self.object.transaction.last_name
|
|
|
|
form.fields["first_name"].initial = self.object.transaction.first_name
|
|
|
|
form.fields["bank"].initial = self.object.transaction.bank
|
|
|
|
form.fields["amount"].initial = self.object.transaction.amount
|
|
|
|
form.fields["remittance"].queryset = form.fields["remittance"] \
|
2020-03-24 16:06:50 +00:00
|
|
|
.queryset.filter(remittance_type__note=self.object.transaction.source)
|
2020-03-23 22:42:37 +00:00
|
|
|
|
2020-04-06 10:13:12 +00:00
|
|
|
return context
|
2020-03-23 22:42:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UnlinkTransactionToRemittanceView(LoginRequiredMixin, View):
|
2020-03-24 19:22:15 +00:00
|
|
|
"""
|
|
|
|
Unlink a special transaction and its remittance
|
|
|
|
"""
|
|
|
|
|
2020-03-23 22:42:37 +00:00
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
pk = kwargs["pk"]
|
|
|
|
transaction = SpecialTransactionProxy.objects.get(pk=pk)
|
|
|
|
|
2020-03-24 19:22:15 +00:00
|
|
|
# The remittance must be open (or inexistant)
|
2020-03-23 22:42:37 +00:00
|
|
|
if transaction.remittance and transaction.remittance.closed:
|
|
|
|
raise ValidationError("Remittance is already closed.")
|
|
|
|
|
|
|
|
transaction.remittance = None
|
|
|
|
transaction.save()
|
|
|
|
|
|
|
|
return redirect('treasury:remittance_list')
|
2020-04-22 01:26:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SogeCreditListView(LoginRequiredMixin, ProtectQuerysetMixin, SingleTableView):
|
|
|
|
"""
|
|
|
|
List all Société Générale credits
|
|
|
|
"""
|
|
|
|
model = SogeCredit
|
|
|
|
table_class = SogeCreditTable
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("List of credits from the Société générale")}
|
2020-04-22 01:26:45 +00:00
|
|
|
|
2020-08-13 13:20:15 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2020-08-15 21:27:58 +00:00
|
|
|
# Check that the user is authenticated
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
return self.handle_no_permission()
|
|
|
|
|
2023-09-28 16:48:57 +00:00
|
|
|
if not PermissionBackend.has_model_perm(self.request, SogeCredit(), "view"):
|
2020-08-13 13:20:15 +00:00
|
|
|
raise PermissionDenied(_("You are not able to see the treasury interface."))
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
def get_queryset(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Filter the table with the given parameter.
|
|
|
|
:param kwargs:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
qs = super().get_queryset()
|
|
|
|
if "search" in self.request.GET:
|
|
|
|
pattern = self.request.GET["search"]
|
2020-08-04 18:04:41 +00:00
|
|
|
if pattern:
|
|
|
|
qs = qs.filter(
|
|
|
|
Q(user__first_name__iregex=pattern)
|
|
|
|
| Q(user__last_name__iregex=pattern)
|
|
|
|
| Q(user__note__alias__name__iregex="^" + pattern)
|
|
|
|
| Q(user__note__alias__normalized_name__iregex="^" + Alias.normalize(pattern))
|
|
|
|
)
|
2020-04-22 01:26:45 +00:00
|
|
|
|
2020-09-07 19:33:23 +00:00
|
|
|
if "valid" not in self.request.GET or not self.request.GET["valid"]:
|
|
|
|
qs = qs.filter(credit_transaction__valid=False)
|
2020-04-22 01:26:45 +00:00
|
|
|
|
2020-10-07 08:42:52 +00:00
|
|
|
return qs
|
2020-04-22 01:26:45 +00:00
|
|
|
|
2021-09-05 22:47:11 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['form'] = SogeCreditForm(self.request.POST or None)
|
|
|
|
return context
|
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
|
|
|
|
class SogeCreditManageView(LoginRequiredMixin, ProtectQuerysetMixin, BaseFormView, DetailView):
|
|
|
|
"""
|
|
|
|
Manage credits from the Société générale.
|
|
|
|
"""
|
|
|
|
model = SogeCredit
|
|
|
|
form_class = Form
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Manage credits from the Société générale")}
|
2020-04-22 01:26:45 +00:00
|
|
|
|
2020-09-11 20:52:16 +00:00
|
|
|
@transaction.atomic
|
2020-04-22 01:26:45 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
if "validate" in form.data:
|
|
|
|
self.get_object().validate(True)
|
|
|
|
elif "delete" in form.data:
|
|
|
|
self.get_object().delete()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if "validate" in self.request.POST:
|
|
|
|
return reverse_lazy('treasury:manage_soge_credit', args=(self.get_object().pk,))
|
|
|
|
return reverse_lazy('treasury:soge_credits')
|