import - first impression of quota dashboard by product

This commit is contained in:
2025-12-08 08:38:02 +03:30
parent 6491c53306
commit 6816c92661
3 changed files with 171 additions and 151 deletions

View File

@@ -1,4 +1,5 @@
from django.db.models import Sum, Count
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
@@ -48,3 +49,27 @@ class QuotaDashboardService:
return {
"quotas_summary": org_quota_stats,
}
@staticmethod
def get_dashboard_by_product(self, organization: Organization, products: list[int]):
stat_by_prod = []
for prod in products:
org_quota_stat = OrganizationQuotaStats.objects.filter(
organization=organization,
quota__product_id=prod
)
product_stat_data = org_quota_stat.aggregate(
quotas_count=Count('id'),
total_quotas_weight=models.Sum('total_amount'),
active_quotas_weight=models.Sum('active_quotas_weight', filter=Q(quota__is_closed=False)),
closed_quotas_weight=models.Sum('closed_quotas_weight', filter=Q(quota__is_closed=True)),
total_remaining_quotas_weight=models.Sum('remaining_amount'),
total_distributed=models.Sum('quota_distributed'),
total_warehouse_entry=models.Sum('inventory_received'),
total_sold=models.Sum('total_sold'),
)
stat_by_prod.append(product_stat_data)
return stat_by_prod