diff --git a/apps/herd/services/services.py b/apps/herd/services/services.py index cfe633b..2d1e41d 100644 --- a/apps/herd/services/services.py +++ b/apps/herd/services/services.py @@ -8,7 +8,8 @@ from apps.warehouse.models import ( from django.db.models import Sum from apps.product.models import Quota, QuotaDistribution from apps.herd.services.rancher_service import RancherService -from django.db.models import Count, Q +from django.db.models import Count, Q, Value +from django.db.models.functions import Coalesce import typing @@ -31,14 +32,13 @@ def get_rancher_statistics(rancher: Rancher = None) -> typing.Any: ) else: stats = temporary_livestock.aggregate( - herd_count=0, - light_count=Count('id', filter=Q(livestock_type__weight_type='L')), - heavy_count=Count('id', filter=Q(livestock_type__weight_type='H')), - sheep=Count('id', filter=Q(livestock_type__name='گوسفند')), # noqa - goat=Count('id', filter=Q(livestock_type__name='بز')), - cow=Count('id', filter=Q(livestock_type__name='گاو')), - camel=Count('id', filter=Q(livestock_type__name='شتر')), - horse=Count('id', filter=Q(livestock_type__name='بز')), + light_count=Coalesce(Sum('count', filter=Q(livestock_type__weight_type='L')), Value(0)), + heavy_count=Coalesce(Sum('count', filter=Q(livestock_type__weight_type='H')), Value(0)), + sheep=Coalesce(Sum('count', filter=Q(livestock_type__name='گوسفند')), Value(0)), # noqa + goat=Coalesce(Sum('count', filter=Q(livestock_type__name='بز')), Value(0)), + cow=Coalesce(Sum('count', filter=Q(livestock_type__name='گاو')), Value(0)), + camel=Coalesce(Sum('count', filter=Q(livestock_type__name='شتر')), Value(0)), + horse=Coalesce(Sum('count', filter=Q(livestock_type__name='بز')), Value(0)), ) return [{'name': key, 'value': value} for key, value in stats.items()], stats @@ -119,6 +119,7 @@ def rancher_quota_weight( return { "total_weight": total_weight, "remaining_weight": total_weight - rancher_remaining_usage, + "rancher_temporary_livestock": rancher.without_herd, "by_type": [{ "name": key, "name_fa": value['name_fa'],