From 7d3aeb81e885bda19d327ce1daa0147d318dcf99 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Mon, 14 Mar 2022 00:48:31 +0100
Subject: [PATCH] Auto-fill billing details from person if not in invoice

---
 aleksis/apps/tezor/models/invoice.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/aleksis/apps/tezor/models/invoice.py b/aleksis/apps/tezor/models/invoice.py
index 18c33ae..03978c8 100644
--- a/aleksis/apps/tezor/models/invoice.py
+++ b/aleksis/apps/tezor/models/invoice.py
@@ -83,6 +83,28 @@ class Invoice(BasePayment, PureDjangoModel):
     )
     items = models.ManyToManyField("InvoiceItem", verbose_name=_("Invoice items"))
 
+    def save(self, *args, **kwargs):
+        if self.person:
+            person = self.person
+        elif self.for_object and getattr(self.for_object, "person", None):
+            person = self.for_object.person
+        else:
+            person = None
+
+        if person:
+            if not self.billing_last_name:
+                self.billing_last_name = person.last_name
+            if not self.billing_first_name:
+                self.billing_first_name = person.first_name
+            if not self.billing_address_1:
+                self.billing_address_1 = f"{person.street} {person.housenumber}"
+            if not self.billing_city:
+                self.billing_city = person.place
+            if not self.billing_postcode:
+                self.billing_postcode = person.postal_code
+
+        super().save(*args, **kwargs)
+
     @classmethod
     def get_variant_choices(cls):
         choices = []
-- 
GitLab