Browse Source

Be able to get bill type and child object easily

Alexandre Aubin 6 years ago
parent
commit
82a11d844b
1 changed files with 23 additions and 0 deletions
  1. 23 0
      coin/billing/models.py

+ 23 - 0
coin/billing/models.py

@@ -42,6 +42,12 @@ def invoice_pdf_filename(instance, filename):
 
 class Bill(models.Model):
 
+    CHILD_CLASS_NAMES = (
+        'Invoice',
+        'MembershipFee',
+        'Donation',
+    )
+
     BILL_STATUS_CHOICES = (
         ('open', 'À payer'),
         ('closed', 'Réglée'),
@@ -62,6 +68,23 @@ class Bill(models.Model):
                            upload_to=bill_pdf_filename,
                            null=True, blank=True,
                            verbose_name='PDF')
+
+
+    def as_child(self):
+        for child_class_name in self.CHILD_CLASS_NAMES:
+          try:
+            return self.__getattribute__(child_class_name.lower())
+          except eval(child_class_name).DoesNotExist:
+            pass
+        return self
+
+    @property
+    def type(self):
+        for child_class_name in self.CHILD_CLASS_NAMES:
+            if hasattr(self, child_class_name.lower()):
+                return child_class_name
+        return self.__class__.__name__
+
     @property
     def amount(self):
         """ Return bill amount """