diff --git a/apps/herd/pos/api/v1/serializers.py b/apps/herd/pos/api/v1/serializers.py index 1da222b..8608010 100644 --- a/apps/herd/pos/api/v1/serializers.py +++ b/apps/herd/pos/api/v1/serializers.py @@ -10,6 +10,7 @@ from rest_framework import serializers from apps.herd.models import Herd, Rancher from django.db.transaction import atomic + class HerdSerializer(serializers.ModelSerializer): """ Herd Serializer """ @@ -46,7 +47,6 @@ class RancherSerializer(serializers.ModelSerializer): rancher = Rancher.objects.create(**validated_data) # create rancher if 'livestock' in data.keys(): live_stocks = data['livestock'] - print(live_stocks) livestock_types = {stock.id: stock for stock in livestock_models.LiveStockType.objects.all()} for stock in live_stocks: diff --git a/apps/herd/services/services.py b/apps/herd/services/services.py index 7513280..cfe633b 100644 --- a/apps/herd/services/services.py +++ b/apps/herd/services/services.py @@ -1,6 +1,6 @@ +from apps.livestock.models import LiveStock, TemporaryLiveStock from decimal import Decimal from apps.herd.models import Rancher -from apps.livestock.models import LiveStock from apps.warehouse.models import ( InventoryEntry, InventoryQuotaSaleItem @@ -16,21 +16,39 @@ def get_rancher_statistics(rancher: Rancher = None) -> typing.Any: """ get statistics of a rancher """ # noqa livestocks = LiveStock.objects.filter(herd__rancher=rancher) # noqa + temporary_livestock = TemporaryLiveStock.objects.filter(rancher=rancher) + + if livestocks: + stats = livestocks.aggregate( + herd_count=Count("herd", distinct=True), + light_count=Count('id', filter=Q(weight_type='L')), + heavy_count=Count('id', filter=Q(weight_type='H')), + sheep=Count('id', filter=Q(type__name='گوسفند')), # noqa + goat=Count('id', filter=Q(type__name='بز')), + cow=Count('id', filter=Q(type__name='گاو')), + camel=Count('id', filter=Q(type__name='شتر')), + horse=Count('id', filter=Q(type__name='بز')), + ) + 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='بز')), + ) - stats = livestocks.aggregate( - herd_count=Count("herd", distinct=True), - light_count=Count('id', filter=Q(weight_type='L')), - heavy_count=Count('id', filter=Q(weight_type='H')), - sheep=Count('id', filter=Q(type__name='گوسفند')), # noqa - goat=Count('id', filter=Q(type__name='بز')), - cow=Count('id', filter=Q(type__name='گاو')), - camel=Count('id', filter=Q(type__name='شتر')), - horse=Count('id', filter=Q(type__name='بز')), - ) return [{'name': key, 'value': value} for key, value in stats.items()], stats -def rancher_quota_weight(rancher, inventory_entry: InventoryEntry = None, distribution: QuotaDistribution = None): +def rancher_quota_weight( + rancher: Rancher, + inventory_entry: InventoryEntry = None, + distribution: QuotaDistribution = None +): """ :param rancher: Rancher instance :param inventory_entry: InventoryEntry instance diff --git a/apps/pos_device/pos/api/v1/viewsets/device.py b/apps/pos_device/pos/api/v1/viewsets/device.py index c930b31..fc7c481 100644 --- a/apps/pos_device/pos/api/v1/viewsets/device.py +++ b/apps/pos_device/pos/api/v1/viewsets/device.py @@ -98,6 +98,7 @@ class POSDeviceViewSet(viewsets.ModelViewSet, POSDeviceMixin): 'name': device_owner_org.name, 'en_name': device_owner_org.en_name, 'phone': device_owner_org.phone, + 'type': device_owner_org.type.name, } return Response({