diff --git a/apps/treasury/forms.py b/apps/treasury/forms.py index a06601b1..65fae3a4 100644 --- a/apps/treasury/forms.py +++ b/apps/treasury/forms.py @@ -45,8 +45,35 @@ class ProductFormSetHelper(FormHelper): class RemittanceForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.helper = FormHelper() - self.helper.add_input(Submit('submit', _("Submit"), attr={'class': 'btn btn-block btn-primary'})) + + if self.instance.pk: + self.fields["type"].disabled = True + self.fields["type"].required = False + + if not self.instance.closed: + self.helper.add_input(Submit('submit', _("Submit"), attr={'class': 'btn btn-block btn-primary'})) + if self.instance.transactions: + self.helper.add_input(Submit("close", _("Close"), css_class='btn btn-success')) + else: + self.fields["comment"].disabled = True + self.fields["comment"].required = False + + def clean(self): + if self.instance.closed: + self.add_error("comment", _("Remittance is already closed.")) + + cleaned_data = super().clean() + + if "type" in self.changed_data: + self.add_error("type", _("You can't change the type of the remittance.")) + + if "close" in self.data: + self.instance.closed = True + self.cleaned_data["closed"] = True + + return cleaned_data class Meta: model = Remittance diff --git a/apps/treasury/tables.py b/apps/treasury/tables.py index 3c302813..d5f3f105 100644 --- a/apps/treasury/tables.py +++ b/apps/treasury/tables.py @@ -35,10 +35,10 @@ class InvoiceTable(tables.Table): class RemittanceTable(tables.Table): - edit = tables.LinkColumn("treasury:remittance_update", - verbose_name=_("Edit"), + view = tables.LinkColumn("treasury:remittance_update", + verbose_name=_("View"), args=[A("pk")], - text=_("Edit"), + text=_("View"), attrs={ 'a': {'class': 'btn btn-primary'} }, ) @@ -52,7 +52,7 @@ class RemittanceTable(tables.Table): } model = Remittance template_name = 'django_tables2/bootstrap4.html' - fields = ('id', 'date', 'type', 'comment', 'count', 'amount', 'edit',) + fields = ('id', 'date', 'type', 'comment', 'count', 'amount', 'view',) class SpecialTransactionTable(tables.Table): diff --git a/apps/treasury/views.py b/apps/treasury/views.py index 89a009cf..71b07b0b 100644 --- a/apps/treasury/views.py +++ b/apps/treasury/views.py @@ -236,10 +236,12 @@ class RemittanceUpdateView(LoginRequiredMixin, UpdateView): def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) + form = ctx["form"] ctx["table"] = RemittanceTable(data=Remittance.objects.all()) + data = SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self.object).all() ctx["special_transactions"] = SpecialTransactionTable( - data=SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self.object).all(), - exclude=('remittance_add', )) + data=data, + exclude=('remittance_add', 'remittance_remove', ) if self.object.closed else ('remittance_add', )) return ctx diff --git a/templates/treasury/remittance_form.html b/templates/treasury/remittance_form.html index 99830601..af4170f4 100644 --- a/templates/treasury/remittance_form.html +++ b/templates/treasury/remittance_form.html @@ -4,7 +4,34 @@ {% load crispy_forms_tags pretty_money %} {% load render_table from django_tables2 %} {% block content %} +
{% trans "Remittances list" %}
+ + {% if object.pk %} +