import - quotas dashbopard by product / quotas list by product
This commit is contained in:
@@ -5,7 +5,7 @@ from django.db.models.functions import Coalesce
|
|||||||
from apps.authentication.models import Organization
|
from apps.authentication.models import Organization
|
||||||
from apps.authentication.services.service import get_all_org_child
|
from apps.authentication.services.service import get_all_org_child
|
||||||
from apps.core.services.filter.search import DynamicSearchService
|
from apps.core.services.filter.search import DynamicSearchService
|
||||||
from apps.product.models import OrganizationQuotaStats
|
from apps.product.models import OrganizationQuotaStats, QuotaDistribution
|
||||||
|
|
||||||
|
|
||||||
class QuotaDashboardService:
|
class QuotaDashboardService:
|
||||||
@@ -51,23 +51,67 @@ class QuotaDashboardService:
|
|||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_dashboard_by_product(self, organization: Organization, products: list[int]):
|
def get_dashboard_by_product(self, organization: Organization, products: dict):
|
||||||
|
|
||||||
stat_by_prod = []
|
stat_by_prod = []
|
||||||
for prod in products:
|
for prod_name, prod_id in products.items():
|
||||||
org_quota_stat = OrganizationQuotaStats.objects.filter(
|
org_quota_stat = OrganizationQuotaStats.objects.filter(
|
||||||
organization=organization,
|
organization=organization,
|
||||||
quota__product_id=prod
|
quota__product_id=prod_id
|
||||||
)
|
)
|
||||||
product_stat_data = org_quota_stat.aggregate(
|
product_stat_data = org_quota_stat.aggregate(
|
||||||
quotas_count=Count('id'),
|
quotas_count=Count('id'),
|
||||||
total_quotas_weight=models.Sum('total_amount'),
|
total_quotas_weight=Coalesce(models.Sum('total_amount'), 0),
|
||||||
active_quotas_weight=models.Sum('active_quotas_weight', filter=Q(quota__is_closed=False)),
|
active_quotas_weight=Coalesce(models.Sum(
|
||||||
closed_quotas_weight=models.Sum('closed_quotas_weight', filter=Q(quota__is_closed=True)),
|
'total_amount', filter=Q(quota__is_closed=False)
|
||||||
total_remaining_quotas_weight=models.Sum('remaining_amount'),
|
), 0),
|
||||||
total_distributed=models.Sum('quota_distributed'),
|
closed_quotas_weight=Coalesce(models.Sum(
|
||||||
total_warehouse_entry=models.Sum('inventory_received'),
|
'total_amount', filter=Q(quota__is_closed=True)
|
||||||
total_sold=models.Sum('total_sold'),
|
), 0),
|
||||||
|
total_remaining_quotas_weight=Coalesce(models.Sum('remaining_amount'), 0),
|
||||||
|
total_distributed=Coalesce(models.Sum('total_distributed'), 0),
|
||||||
|
total_warehouse_entry=Coalesce(models.Sum('inventory_received'), 0),
|
||||||
|
total_sold=Coalesce(models.Sum('sold_amount'), 0),
|
||||||
|
)
|
||||||
|
received_distribution_weight = QuotaDistribution.objects.select_related(
|
||||||
|
'quota', 'assigned_organization'
|
||||||
|
).filter(
|
||||||
|
quota__product_id=prod_id,
|
||||||
|
quota__is_closed=False,
|
||||||
|
assigned_organization=organization,
|
||||||
|
) if not organization.type.key == 'ADM' else QuotaDistribution.objects.select_related(
|
||||||
|
'quota', 'assigned_organization'
|
||||||
|
).filter(
|
||||||
|
quota__product_id=prod_id,
|
||||||
|
quota__is_closed=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# product total distributed weight from quota
|
||||||
|
given_distribution_weight = QuotaDistribution.objects.select_related(
|
||||||
|
'quota', 'assigned_organization'
|
||||||
|
).filter(
|
||||||
|
quota__product_id=prod_id,
|
||||||
|
quota__is_closed=False,
|
||||||
|
assigner_organization=organization,
|
||||||
|
) if not organization.type.key == 'ADM' else QuotaDistribution.objects.select_related(
|
||||||
|
'quota', 'assigned_organization'
|
||||||
|
).filter(
|
||||||
|
quota__product_id=prod_id,
|
||||||
|
quota__is_closed=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
product_stat_data.update(
|
||||||
|
product_name=prod_name,
|
||||||
|
product_id=prod_id,
|
||||||
|
received_distribution_number=received_distribution_weight.count(),
|
||||||
|
received_distribution_weight=received_distribution_weight.aggregate(
|
||||||
|
total_weight=models.Sum('weight')
|
||||||
|
)['total_weight'] or 0,
|
||||||
|
|
||||||
|
given_distribution_number=given_distribution_weight.count(),
|
||||||
|
given_distribution_weight=given_distribution_weight.aggregate(
|
||||||
|
total_weight=models.Sum('weight')
|
||||||
|
)['total_weight'] or 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
stat_by_prod.append(product_stat_data)
|
stat_by_prod.append(product_stat_data)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
|||||||
from apps.core.pagination import CustomPageNumberPagination
|
from apps.core.pagination import CustomPageNumberPagination
|
||||||
from apps.product import models as product_models
|
from apps.product import models as product_models
|
||||||
from apps.product.exceptions import QuotaExpiredTimeException
|
from apps.product.exceptions import QuotaExpiredTimeException
|
||||||
|
from apps.product.models import OrganizationQuotaStats
|
||||||
from apps.product.services.quota_dashboard_service import QuotaDashboardService
|
from apps.product.services.quota_dashboard_service import QuotaDashboardService
|
||||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers
|
from apps.product.web.api.v1.serializers import quota_distribution_serializers
|
||||||
from apps.product.web.api.v1.serializers import quota_serializers
|
from apps.product.web.api.v1.serializers import quota_serializers
|
||||||
@@ -381,6 +382,66 @@ class QuotaViewSet(BaseViewSet, SoftDeleteMixin, QuotaDashboardService, viewsets
|
|||||||
|
|
||||||
return Response(quota_dashboard)
|
return Response(quota_dashboard)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=False,
|
||||||
|
url_path='quotas_dashboard_by_product',
|
||||||
|
url_name='quotas_dashboard_by_product',
|
||||||
|
name='quotas_dashboard_by_product'
|
||||||
|
)
|
||||||
|
def quotas_dashboard_by_product(self, request):
|
||||||
|
"""
|
||||||
|
dashboard of all quotas & their information
|
||||||
|
"""
|
||||||
|
|
||||||
|
org = get_organization_by_user(request.user)
|
||||||
|
query_param = self.request.query_params # noqa
|
||||||
|
|
||||||
|
# filter by date
|
||||||
|
start_date = query_param.get('start') if 'start' in query_param.keys() else None
|
||||||
|
end_date = query_param.get('end') if 'end' in query_param.keys() else None
|
||||||
|
|
||||||
|
# get organization quota stats and get product ids
|
||||||
|
org_quota_stat = OrganizationQuotaStats.objects.select_related('organization', 'quota').filter(
|
||||||
|
organization=org
|
||||||
|
) if not org.type.key == 'ADM' else OrganizationQuotaStats.objects.select_related('organization', 'quota').all()
|
||||||
|
|
||||||
|
products = {f'{stat.quota.product.name}': stat.quota.product.id for stat in org_quota_stat}
|
||||||
|
|
||||||
|
dashboard_data_by_product = self.get_dashboard_by_product(self, org, products)
|
||||||
|
|
||||||
|
return Response(dashboard_data_by_product)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=True,
|
||||||
|
url_path='org_quota_stats_by_product',
|
||||||
|
url_name='org_quota_stats_by_product',
|
||||||
|
name='org_quota_stats_by_product'
|
||||||
|
)
|
||||||
|
def org_quota_stats_by_product(self, request, pk=None):
|
||||||
|
"""
|
||||||
|
list of quotas by product
|
||||||
|
"""
|
||||||
|
org = get_organization_by_user(request.user)
|
||||||
|
query_param = self.request.query_params # noqa
|
||||||
|
|
||||||
|
# filter by date
|
||||||
|
start_date = query_param.get('start') if 'start' in query_param.keys() else None
|
||||||
|
end_date = query_param.get('end') if 'end' in query_param.keys() else None
|
||||||
|
|
||||||
|
queryset = self.filter_query(
|
||||||
|
self.get_queryset(visibility_by_org_scope=True).filter(
|
||||||
|
is_closed=False)) # return by search param or all objects
|
||||||
|
|
||||||
|
# paginate queryset
|
||||||
|
page = self.paginate_queryset(
|
||||||
|
queryset.filter(product_id=pk).order_by('-modify_date')
|
||||||
|
)
|
||||||
|
if page is not None: # noqa
|
||||||
|
serializer = self.get_serializer(page, many=True, context={'org': org})
|
||||||
|
return self.get_paginated_response(serializer.data)
|
||||||
|
|
||||||
@action(
|
@action(
|
||||||
methods=['get'],
|
methods=['get'],
|
||||||
detail=False,
|
detail=False,
|
||||||
|
|||||||
Reference in New Issue
Block a user