120 lines
5.0 KiB
Python
120 lines
5.0 KiB
Python
from django.db import models
|
|
from django.db.models import Sum, Count, Q
|
|
from django.db.models.functions import Coalesce
|
|
|
|
from apps.authentication.models import Organization
|
|
from apps.core.services.filter.search import DynamicSearchService
|
|
from apps.product.models import OrganizationQuotaStats, QuotaDistribution
|
|
|
|
|
|
class QuotaDashboardService:
|
|
"""
|
|
dashboard of quota information
|
|
"""
|
|
|
|
@staticmethod
|
|
def get_dashboard(self, org: Organization, start_date: str = None, end_date: str = None,
|
|
search_fields: list[str] = None, quota_is_closed: bool = False, query_string: str = None):
|
|
|
|
if org.type.key == 'ADM':
|
|
org_quota_stats = OrganizationQuotaStats.objects.filter(stat_type='quota', quota__is_closed=quota_is_closed)
|
|
print(len(org_quota_stats), quota_is_closed)
|
|
else:
|
|
org_quota_stats = OrganizationQuotaStats.objects.filter(
|
|
organization=org,
|
|
quota__is_closed=quota_is_closed,
|
|
)
|
|
|
|
# filter queryset (transactions & items) by date
|
|
if (start_date and end_date) or query_string:
|
|
org_quota_stats = DynamicSearchService(
|
|
queryset=org_quota_stats,
|
|
start=start_date,
|
|
end=end_date,
|
|
date_field="create_date",
|
|
search_fields=search_fields,
|
|
query_string=query_string
|
|
).apply()
|
|
|
|
org_quota_stats = org_quota_stats.aggregate(
|
|
total_quotas=Count("quota", distinct=True),
|
|
total_distributed=Coalesce(Sum("total_distributed", ), 0),
|
|
remaining_amount=Coalesce(Sum("remaining_amount", ), 0),
|
|
inventory_received=Coalesce(Sum("inventory_received", ), 0),
|
|
sold_amount=Coalesce(Sum("sold_amount", ), 0),
|
|
total_amount=Coalesce(Sum("total_amount", ), 0),
|
|
inventory_entry_balance=Coalesce(Sum("inventory_entry_balance", ), 0),
|
|
)
|
|
|
|
return {
|
|
"quotas_summary": org_quota_stats,
|
|
}
|
|
|
|
@staticmethod
|
|
def get_dashboard_by_product(self, organization: Organization, products: dict):
|
|
|
|
stat_by_prod = []
|
|
for prod_name, prod_id in products.items():
|
|
org_quota_stat = OrganizationQuotaStats.objects.filter(
|
|
organization=organization,
|
|
quota__product_id=prod_id
|
|
)
|
|
product_stat_data = org_quota_stat.aggregate(
|
|
quotas_count=Count('id'),
|
|
total_quotas_weight=Coalesce(models.Sum('total_amount'), 0),
|
|
active_quotas_weight=Coalesce(models.Sum(
|
|
'total_amount', filter=Q(quota__is_closed=False)
|
|
), 0),
|
|
closed_quotas_weight=Coalesce(models.Sum(
|
|
'total_amount', filter=Q(quota__is_closed=True)
|
|
), 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)
|
|
|
|
return stat_by_prod
|