From 450a02dda19a5355ea4ac968bfae5a06c6dc9f0d Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Sun, 16 Nov 2025 15:51:15 +0330 Subject: [PATCH] change retrieve function in quota --- apps/product/models.py | 4 ++-- apps/product/signals.py | 13 +++++++++++-- apps/product/web/api/v1/viewsets/quota_api.py | 7 +++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/apps/product/models.py b/apps/product/models.py index 84ec11a..2c68ea0 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -822,12 +822,12 @@ class OrganizationQuotaStats(BaseModel): """ calculate total/sold/remaining """ from apps.warehouse.models import InventoryQuotaSaleItem - if not main_quota: + if main_quota: # calculate total amount of distribution self.total_amount = self.distributions.filter().aggregate( total=Sum('weight') )['total'] or 0 - + print(self.total_amount) self.total_distributed = self.distributions.filter().exclude( assigned_organization=self.organization ).aggregate(total=Sum('weight'))['total'] or 0 diff --git a/apps/product/signals.py b/apps/product/signals.py index 4b32734..98904fd 100644 --- a/apps/product/signals.py +++ b/apps/product/signals.py @@ -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: diff --git a/apps/product/web/api/v1/viewsets/quota_api.py b/apps/product/web/api/v1/viewsets/quota_api.py index 0e11d8f..ee1ec43 100644 --- a/apps/product/web/api/v1/viewsets/quota_api.py +++ b/apps/product/web/api/v1/viewsets/quota_api.py @@ -287,6 +287,13 @@ class QuotaViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet, DynamicS return Response(data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN) + def retrieve(self, request, *args, **kwargs): + instance = self.get_object() + serializer = self.get_serializer(instance, context={ + "org": get_organization_by_user(request.user) + }) + return Response(serializer.data) + @action( methods=['patch'], detail=True,