change retrieve function in quota
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user