Browse Source

Reopen an invoice after deleting a payment if relevant

Alexandre Aubin 7 years ago
parent
commit
acc23fa38b
1 changed files with 11 additions and 0 deletions
  1. 11 0
      coin/billing/models.py

+ 11 - 0
coin/billing/models.py

@@ -599,6 +599,17 @@ def invoice_changed(sender, instance, created, **kwargs):
             accounting_log.info("Updating invoice %s (Member: %s, Total amount: %s, Amount paid: %s)."
                     % (instance.number, instance.member, instance.amount(), instance.amount_paid() ))
 
+@receiver(post_delete, sender=PaymentAllocation)
+def paymentallocation_deleted(sender, instance, **kwargs):
+
+    invoice = instance.invoice
+
+    # Reopen invoice if relevant
+    if (invoice.amount_remaining_to_pay() > 0) and (invoice.status == "closed"):
+        accounting_log.info("Reopening invoice %s ..." % invoice.number)
+        invoice.status = "open"
+        invoice.save()
+
 
 @receiver(post_delete, sender=Payment)
 def payment_deleted(sender, instance, **kwargs):