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.services.service import get_all_org_child
|
||||
from apps.core.services.filter.search import DynamicSearchService
|
||||
from apps.product.models import OrganizationQuotaStats
|
||||
from apps.product.models import OrganizationQuotaStats, QuotaDistribution
|
||||
|
||||
|
||||
class QuotaDashboardService:
|
||||
@@ -51,23 +51,67 @@ class QuotaDashboardService:
|
||||
}
|
||||
|
||||
@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 = []
|
||||
for prod in products:
|
||||
for prod_name, prod_id in products.items():
|
||||
org_quota_stat = OrganizationQuotaStats.objects.filter(
|
||||
organization=organization,
|
||||
quota__product_id=prod
|
||||
quota__product_id=prod_id
|
||||
)
|
||||
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'),
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user