diff --git a/apps/treasury/tables.py b/apps/treasury/tables.py index ebc642c7..bcbbd8a4 100644 --- a/apps/treasury/tables.py +++ b/apps/treasury/tables.py @@ -4,6 +4,8 @@ import django_tables2 as tables from django.utils.translation import gettext_lazy as _ from django_tables2 import A +from note.models import SpecialTransaction +from note.templatetags.pretty_money import pretty_money from .models import Invoice, Remittance @@ -33,6 +35,14 @@ class InvoiceTable(tables.Table): class RemittanceTable(tables.Table): + edit = tables.LinkColumn("treasury:remittance_update", + verbose_name=_("Edit"), + args=[A("pk")], + text=_("Edit"), + attrs={ + 'a': {'class': 'btn btn-primary'} + }, ) + class Meta: attrs = { 'class': 'table table-condensed table-striped table-hover' @@ -40,3 +50,32 @@ class RemittanceTable(tables.Table): model = Remittance template_name = 'django_tables2/bootstrap4.html' fields = ('id', 'date', 'type', 'comment', 'size', 'amount', 'edit',) + + +class SpecialTransactionTable(tables.Table): + remittance_add = tables.LinkColumn("treasury:remittance_update", + verbose_name=_("Remittance"), + args=[A("pk")], + text=_("Add"), + attrs={ + 'a': {'class': 'btn btn-primary'} + }, ) + + remittance_remove = tables.LinkColumn("treasury:remittance_update", + verbose_name=_("Remittance"), + args=[A("pk")], + text=_("Remove"), + attrs={ + 'a': {'class': 'btn btn-primary btn-danger'} + }, ) + + def render_amount(self, value): + return pretty_money(value) + + class Meta: + attrs = { + 'class': 'table table-condensed table-striped table-hover' + } + model = SpecialTransaction + template_name = 'django_tables2/bootstrap4.html' + fields = ('id', 'source', 'destination', 'amount', 'last_name', 'first_name', 'bank',) diff --git a/apps/treasury/urls.py b/apps/treasury/urls.py index 029466e0..1901732b 100644 --- a/apps/treasury/urls.py +++ b/apps/treasury/urls.py @@ -4,7 +4,7 @@ from django.urls import path from .views import InvoiceCreateView, InvoiceListView, InvoiceUpdateView, InvoiceRenderView, RemittanceListView,\ - RemittanceCreateView, RemittanceUpdateView + RemittanceCreateView, RemittanceUpdateView app_name = 'treasury' urlpatterns = [ diff --git a/apps/treasury/views.py b/apps/treasury/views.py index 6f6c256c..3415a2b2 100644 --- a/apps/treasury/views.py +++ b/apps/treasury/views.py @@ -13,13 +13,14 @@ from django.http import HttpResponse from django.template.loader import render_to_string from django.urls import reverse_lazy from django.views.generic import CreateView, UpdateView -from django.views.generic.base import View +from django.views.generic.base import View, TemplateView from django_tables2 import SingleTableView +from note.models import SpecialTransaction from note_kfet.settings.base import BASE_DIR from .forms import InvoiceForm, ProductFormSet, ProductFormSetHelper, RemittanceForm from .models import Invoice, Product, Remittance -from .tables import InvoiceTable, RemittanceTable +from .tables import InvoiceTable, RemittanceTable, SpecialTransactionTable class InvoiceCreateView(LoginRequiredMixin, CreateView): @@ -188,13 +189,36 @@ class RemittanceCreateView(LoginRequiredMixin, CreateView): def get_success_url(self): return reverse_lazy('treasury:remittance_list') + def get_context_data(self, **kwargs): + ctx = super().get_context_data(**kwargs) -class RemittanceListView(LoginRequiredMixin, SingleTableView): + ctx["table"] = RemittanceTable(data=Remittance.objects.all()) + ctx["special_transactions"] = SpecialTransactionTable(data=SpecialTransaction.objects.none()) + + return ctx + + +class RemittanceListView(LoginRequiredMixin, TemplateView): """ List existing Remittances """ - model = Remittance - table_class = RemittanceTable + template_name = "treasury/remittance_list.html" + + def get_context_data(self, **kwargs): + ctx = super().get_context_data(**kwargs) + + ctx["opened_remittances"] = RemittanceTable(data=Remittance.objects.filter(closed=False).all()) + ctx["closed_remittances"] = RemittanceTable(data=Remittance.objects.filter(closed=True).reverse().all()) + ctx["special_transactions_no_remittance"] = SpecialTransactionTable( + data=SpecialTransaction.objects.filter(source__polymorphic_ctype__model="notespecial", + specialtransactionproxy__remittance=None).all(), + exclude=('remittance_remove', )) + ctx["special_transactions_with_remittance"] = SpecialTransactionTable( + data=SpecialTransaction.objects.filter(source__polymorphic_ctype__model="notespecial", + specialtransactionproxy__remittance__closed=False).all(), + exclude=('remittance_add', )) + + return ctx class RemittanceUpdateView(LoginRequiredMixin, UpdateView): @@ -206,3 +230,13 @@ class RemittanceUpdateView(LoginRequiredMixin, UpdateView): def get_success_url(self): return reverse_lazy('treasury:remittance_list') + + def get_context_data(self, **kwargs): + ctx = super().get_context_data(**kwargs) + + ctx["table"] = RemittanceTable(data=Remittance.objects.all()) + ctx["special_transactions"] = SpecialTransactionTable( + data=SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self.object).all(), + exclude=('remittance_add', )) + + return ctx diff --git a/templates/treasury/remittance_form.html b/templates/treasury/remittance_form.html index 50071136..99830601 100644 --- a/templates/treasury/remittance_form.html +++ b/templates/treasury/remittance_form.html @@ -2,7 +2,9 @@ {% load static %} {% load i18n %} {% load crispy_forms_tags pretty_money %} +{% load render_table from django_tables2 %} {% block content %}
{% trans "Remittances list" %}
{% crispy form %} + {% render_table special_transactions %} {% endblock %} diff --git a/templates/treasury/remittance_list.html b/templates/treasury/remittance_list.html index e775fbaa..dfada279 100644 --- a/templates/treasury/remittance_list.html +++ b/templates/treasury/remittance_list.html @@ -3,8 +3,23 @@ {% load i18n %} {% block content %} -{% render_table table %} +