From 3a33fc04eefc07d90a2d8b7ccda7e1f79e77b35f Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Mon, 14 Mar 2022 00:28:17 +0100
Subject: [PATCH] Fix constraints for manual invoicing

---
 aleksis/apps/tezor/models/invoice.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/aleksis/apps/tezor/models/invoice.py b/aleksis/apps/tezor/models/invoice.py
index 5380242..5d84ec5 100644
--- a/aleksis/apps/tezor/models/invoice.py
+++ b/aleksis/apps/tezor/models/invoice.py
@@ -67,8 +67,10 @@ class Invoice(BasePayment, PureDjangoModel):
     number = models.CharField(verbose_name=_("Invoice number"), max_length=255)
     due_date = models.DateField(verbose_name=_("Payment due date"), null=True)
 
-    for_content_type = models.ForeignKey(ContentType, on_delete=models.SET_NULL, null=True)
-    for_object_id = models.PositiveIntegerField()
+    for_content_type = models.ForeignKey(
+        ContentType, on_delete=models.SET_NULL, null=True, blank=True
+    )
+    for_object_id = models.PositiveIntegerField(null=True, blank=True)
     for_object = GenericForeignKey("for_content_type", "for_object_id")
 
     # For manual invoicing
@@ -119,7 +121,10 @@ class Invoice(BasePayment, PureDjangoModel):
         constraints = [
             models.UniqueConstraint(fields=["number", "group"], name="number_uniq_per_group"),
             models.CheckConstraint(
-                check=(Q(for_object_id__isnull=True) | Q(person__isnull=True)),
+                check=(
+                    (Q(for_object_id__isnull=True) & Q(person__isnull=False))
+                    | (Q(for_object_id__isnull=False) & Q(person__isnull=True))
+                ),
                 name="object_or_person",
             ),
         ]
-- 
GitLab