change retrieve function in quota
This commit is contained in:
@@ -822,12 +822,12 @@ class OrganizationQuotaStats(BaseModel):
|
|||||||
""" calculate total/sold/remaining """
|
""" calculate total/sold/remaining """
|
||||||
from apps.warehouse.models import InventoryQuotaSaleItem
|
from apps.warehouse.models import InventoryQuotaSaleItem
|
||||||
|
|
||||||
if not main_quota:
|
if main_quota:
|
||||||
# calculate total amount of distribution
|
# calculate total amount of distribution
|
||||||
self.total_amount = self.distributions.filter().aggregate(
|
self.total_amount = self.distributions.filter().aggregate(
|
||||||
total=Sum('weight')
|
total=Sum('weight')
|
||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
|
print(self.total_amount)
|
||||||
self.total_distributed = self.distributions.filter().exclude(
|
self.total_distributed = self.distributions.filter().exclude(
|
||||||
assigned_organization=self.organization
|
assigned_organization=self.organization
|
||||||
).aggregate(total=Sum('weight'))['total'] or 0
|
).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
|
set total received distributions for every organization
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from apps.warehouse.models import InventoryQuotaSaleItem
|
||||||
if getattr(instance, 'stat_from_signal', False):
|
if getattr(instance, 'stat_from_signal', False):
|
||||||
return
|
return
|
||||||
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
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,
|
organization=instance.registerer_organization,
|
||||||
)
|
)
|
||||||
org_quota_stat.total_amount = instance.quota_weight
|
org_quota_stat.total_amount = instance.quota_weight
|
||||||
org_quota_stat.save(update_fields=['total_amount'])
|
org_quota_stat.total_distributed = instance.quota_distributed
|
||||||
org_quota_stat.update_amount()
|
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
|
# delete quota
|
||||||
if instance.trash:
|
if instance.trash:
|
||||||
|
|||||||
@@ -287,6 +287,13 @@ class QuotaViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet, DynamicS
|
|||||||
return Response(data, status=status.HTTP_201_CREATED)
|
return Response(data, status=status.HTTP_201_CREATED)
|
||||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
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(
|
@action(
|
||||||
methods=['patch'],
|
methods=['patch'],
|
||||||
detail=True,
|
detail=True,
|
||||||
|
|||||||
Reference in New Issue
Block a user