From 7c3529ec52911f2ff42a524a2fe8ff3943295aa9 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Thu, 4 Apr 2024 13:54:20 +0200 Subject: [PATCH] add none choices for subsidy payment attachments --- scipost_django/finances/forms.py | 31 +++++++++++++++++++---------- scipost_django/finances/managers.py | 6 ++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/scipost_django/finances/forms.py b/scipost_django/finances/forms.py index 163678983..07f9bb967 100644 --- a/scipost_django/finances/forms.py +++ b/scipost_django/finances/forms.py @@ -240,15 +240,26 @@ class SubsidyPaymentForm(forms.ModelForm): super().__init__(*args, **kwargs) self.fields["subsidy"].initial = subsidy self.fields["subsidy"].widget = forms.HiddenInput() - print(subsidy.attachments.invoices()) - self.fields["invoice"].choices = [ - (att.id, f"{att.attachment.name.split('/')[-1]}") - for att in subsidy.attachments.invoices() + + invoice_qs = subsidy.attachments.unattached().invoices() + if self.instance.invoice: + invoice_qs |= SubsidyAttachment.objects.filter(id=self.instance.invoice.id) + self.fields["invoice"].initial = self.instance.invoice.id + + proofs_qs = subsidy.attachments.unattached().proofs_of_payment() + if self.instance.proof_of_payment: + proofs_qs |= SubsidyAttachment.objects.filter( + id=self.instance.proof_of_payment.id + ) + self.fields["proof_of_payment"].initial = self.instance.proof_of_payment.id + + self.fields["invoice"].choices = [(None, "---")] + [ + (att.id, f"{att.attachment.name.split('/')[-1]}") for att in invoice_qs ] - self.fields["proof_of_payment"].choices = [ - (att.id, f"{att.attachment.name.split('/')[-1]}") - for att in subsidy.attachments.proofs_of_payment() + self.fields["proof_of_payment"].choices = [(None, "---")] + [ + (att.id, f"{att.attachment.name.split('/')[-1]}") for att in proofs_qs ] + self.helper = FormHelper() self.helper.layout = Layout( Field("subsidy"), @@ -278,10 +289,8 @@ class SubsidyPaymentForm(forms.ModelForm): def save(self, commit=True): instance = super().save(commit=False) - if invoice := self.cleaned_data["invoice"]: - instance.invoice = invoice - if proof_of_payment := self.cleaned_data["proof_of_payment"]: - instance.proof_of_payment = proof_of_payment + instance.invoice = self.cleaned_data["invoice"] or None + instance.proof_of_payment = self.cleaned_data["proof_of_payment"] or None if commit: instance.save() return instance diff --git a/scipost_django/finances/managers.py b/scipost_django/finances/managers.py index d2adcc17a..b6b19719a 100644 --- a/scipost_django/finances/managers.py +++ b/scipost_django/finances/managers.py @@ -33,6 +33,12 @@ class SubsidyAttachmentQuerySet(models.QuerySet): def orphaned(self): return self.filter(subsidy__isnull=True) + def unattached(self): + return self.filter( + models.Q(proof_of_payment_for__isnull=True) + & models.Q(invoice_for__isnull=True) + ) + class PubFracQuerySet(models.QuerySet): def uncompensated(self): -- GitLab