fix update organization - set signal for calculate remaining quota weight after distribution

This commit is contained in:
2025-06-16 10:44:12 +03:30
parent 2164c4415b
commit 1471e31aa4
6 changed files with 55 additions and 9 deletions

19
apps/product/signals.py Normal file
View File

@@ -0,0 +1,19 @@
from django.db.models import Sum
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import QuotaDistribution, Quota
def recalculate_remaining_amount(quota):
total_distributed = quota.distributions.aggregate(
total=Sum('amount_kg')
)['total'] or 0
quota.remaining_amount_kg = quota.total_amount_kg - total_distributed
quota.save(update_fields=["remaining_amount_kg"])
@receiver(post_save, sender=QuotaDistribution)
@receiver(post_delete, sender=QuotaDistribution)
def update_quota_remaining(sender, instance, **kwargs):
recalculate_remaining_amount(instance.quota)