change retrieve function in quota

This commit is contained in:
2025-11-16 15:51:15 +03:30
parent 13f3c90f50
commit 450a02dda1
3 changed files with 20 additions and 4 deletions

View File

@@ -260,6 +260,8 @@ def organization_quota_stats(sender, instance: Quota, created: bool, **kwargs):
"""
set total received distributions for every organization
"""
from apps.warehouse.models import InventoryQuotaSaleItem
if getattr(instance, 'stat_from_signal', False):
return
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
@@ -267,8 +269,15 @@ def organization_quota_stats(sender, instance: Quota, created: bool, **kwargs):
organization=instance.registerer_organization,
)
org_quota_stat.total_amount = instance.quota_weight
org_quota_stat.save(update_fields=['total_amount'])
org_quota_stat.update_amount()
org_quota_stat.total_distributed = instance.quota_distributed
org_quota_stat.sold_amount = InventoryQuotaSaleItem.objects.filter(
quota_distribution__quota=instance,
transaction__transaction_status='success'
).aggregate(
total=Sum('weight')
)['total'] or 0
org_quota_stat.remaining_amount = org_quota_stat.total_amount - org_quota_stat.total_distributed
org_quota_stat.save(update_fields=['total_amount', 'total_distributed', 'sold_amount', 'remaining_amount'])
# delete quota
if instance.trash: