Coverage for accounting/signals.py: 94%
23 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
1from django.dispatch import receiver
2from django.db.models import signals, Sum
3from invoices.models import Invoice
4from .models import Credit, Debit, ProductCharge
7@receiver(signals.post_save, sender=Debit)
8@receiver(signals.post_save, sender=Credit)
9def update_balance_on_movement_save(sender, instance, **kwargs):
10 if not getattr(instance, "skip_signal", None):
11 next_movements = instance.next_movements
13 for movement in next_movements:
14 movement.skip_signal = True
15 movement.save()
18@receiver(signals.pre_save, sender=ProductCharge)
19def update_product_charge_paid_amount(sender, instance, *args, **kwargs):
20 paid_amount = instance.payment_allocations.aggregate(paid_amount=Sum("amount")).get("paid_amount") or 0
21 instance.paid_amount = paid_amount
23 invoiced_amount = (
24 instance.invoice_configurations.filter(invoice__status=Invoice.Status.VALID)
25 .aggregate(invoiced_amount=Sum("amount"))
26 .get("invoiced_amount")
27 or 0
28 )
29 instance.invoiced_amount = invoiced_amount
31 if instance.paid_amount == instance.amount:
32 instance.payment_status = ProductCharge.PaymentStatus.PAID
33 elif instance.paid_amount > 0 and instance.paid_amount < instance.amount: 33 ↛ 34line 33 didn't jump to line 34, because the condition on line 33 was never true
34 instance.payment_status = ProductCharge.PaymentStatus.PARTIALLY
35 else:
36 instance.payment_status = ProductCharge.PaymentStatus.PENDING