import - distributioin stat type for distribution tab
This commit is contained in:
@@ -16,14 +16,27 @@ class QuotaDashboardService:
|
||||
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):
|
||||
|
||||
distribution_number = 0
|
||||
|
||||
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)
|
||||
org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||
stat_type='quota',
|
||||
quota__is_closed=quota_is_closed
|
||||
)
|
||||
dist_org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||
stat_type='distribution',
|
||||
quota__is_closed=quota_is_closed
|
||||
)
|
||||
else:
|
||||
org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||
organization=org,
|
||||
quota__is_closed=quota_is_closed,
|
||||
)
|
||||
dist_org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||
stat_type='distribution',
|
||||
organization=org,
|
||||
quota__is_closed=quota_is_closed
|
||||
)
|
||||
|
||||
# filter queryset (transactions & items) by date
|
||||
if (start_date and end_date) or query_string:
|
||||
@@ -36,6 +49,19 @@ class QuotaDashboardService:
|
||||
query_string=query_string
|
||||
).apply()
|
||||
|
||||
for stat in org_quota_stats.distinct('quota'):
|
||||
if org.type.key == 'ADM':
|
||||
distribution_number += QuotaDistribution.objects.select_related('quota').filter(
|
||||
quota=stat.quota,
|
||||
quota__is_closed=False
|
||||
).count()
|
||||
else:
|
||||
distribution_number += QuotaDistribution.objects.filter(
|
||||
Q(assigner_organization=org),
|
||||
quota=stat.quota,
|
||||
quota__is_closed=False
|
||||
).count()
|
||||
|
||||
org_quota_stats = org_quota_stats.aggregate(
|
||||
total_quotas=Count("quota", distinct=True),
|
||||
total_distributed=Coalesce(Sum("total_distributed", ), 0),
|
||||
@@ -46,12 +72,25 @@ class QuotaDashboardService:
|
||||
inventory_entry_balance=Coalesce(Sum("inventory_entry_balance", ), 0),
|
||||
)
|
||||
|
||||
org_quota_stats.update(
|
||||
distribution_number=distribution_number,
|
||||
dist_remaining_amount=dist_org_quota_stats.aggregate(
|
||||
remaining_amount=Coalesce(Sum("remaining_amount"), 0), )['remaining_amount'] or 0,
|
||||
)
|
||||
|
||||
return {
|
||||
"quotas_summary": org_quota_stats,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def get_dashboard_by_product(self, organization: Organization, products: dict):
|
||||
def get_dashboard_by_product(
|
||||
self,
|
||||
organization: Organization,
|
||||
products: dict,
|
||||
start_date: str = None, end_date: str = None,
|
||||
search_fields: list[str] = None, quota_is_closed: bool = False,
|
||||
query_string: str = None
|
||||
):
|
||||
|
||||
stat_by_prod = []
|
||||
for prod_name, prod_id in products.items():
|
||||
@@ -59,6 +98,18 @@ class QuotaDashboardService:
|
||||
organization=organization,
|
||||
quota__product_id=prod_id
|
||||
)
|
||||
|
||||
# filter queryset (transactions & items) by date
|
||||
if (start_date and end_date) or query_string:
|
||||
org_quota_stats = DynamicSearchService(
|
||||
queryset=org_quota_stat,
|
||||
start=start_date,
|
||||
end=end_date,
|
||||
date_field="create_date",
|
||||
search_fields=search_fields,
|
||||
query_string=query_string
|
||||
).apply()
|
||||
|
||||
product_stat_data = org_quota_stat.aggregate(
|
||||
quotas_count=Count('id'),
|
||||
total_quotas_weight=Coalesce(models.Sum('total_amount'), 0),
|
||||
@@ -88,13 +139,13 @@ class QuotaDashboardService:
|
||||
|
||||
# product total distributed weight from quota
|
||||
given_distribution_weight = QuotaDistribution.objects.select_related(
|
||||
'quota', 'assigned_organization'
|
||||
'quota', 'assigner_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'
|
||||
'quota', 'assigner_organization'
|
||||
).filter(
|
||||
quota__product_id=prod_id,
|
||||
quota__is_closed=False,
|
||||
|
||||
Reference in New Issue
Block a user