diff --git a/.env.local b/.env.local index 3402c15..13815d0 100644 --- a/.env.local +++ b/.env.local @@ -7,7 +7,7 @@ ENV_NAME=DEV # Database secrets DB_HOST=31.7.78.133 DB_PORT=14352 -DB_NAME=Production +DB_NAME=Development DB_USERNAME=postgres DB_PASSWORD=pfLIVXupbDetvFMt2gUvxLXUL9b4HIOHaPcKXsBEZ1i8zl0iLUjmhUfXlGfJKcTV diff --git a/apps/herd/services/rancher_dashboard_service.py b/apps/herd/services/rancher_dashboard_service.py new file mode 100644 index 0000000..ca6dc8d --- /dev/null +++ b/apps/herd/services/rancher_dashboard_service.py @@ -0,0 +1,47 @@ +from django.db import models +from django.db.models import Count, Sum + +from apps.herd.models import Rancher, Herd +from apps.livestock.models import LiveStock + + +class FarmerDashboardService: + """ Dashboard aggregation service for Rancher """ + + @staticmethod + def get_basic_info(rancher: Rancher): + return { + "id": rancher.id, + "name": f"{rancher.first_name} {rancher.last_name}", + "mobile": rancher.mobile, + "national_code": rancher.national_code, + "province": rancher.province.name if rancher.province else None, + "city": rancher.city.name if rancher.city else None, + } + + @staticmethod + def get_herd_counts(rancher_id): + return Herd.objects.filter(rancher_id=rancher_id).aggregate( + total_herds=Count('id'), + total_heavy=Sum('heavy_livestock_number'), + total_light=Sum('light_livestock_number'), + ) + + @staticmethod + def get_livestock_counts(rancher_id): + return LiveStock.objects.filter(herd__rancher_id=rancher_id, archive=False).aggregate( + total=Count('id'), + heavy=Count('id', filter=models.Q(weight_type='H')), + light=Count('id', filter=models.Q(weight_type='L')), + ) + + @staticmethod + def get_livestock_by_type(rancher_id): + return ( + LiveStock.objects.filter(herd__rancher_id=rancher_id, archive=False) + .values("type__name") + .annotate(count=Count("id")) + ) + +# @staticmethod +# def get_dashboard(rancher_id): diff --git a/apps/warehouse/web/api/v1/api.py b/apps/warehouse/web/api/v1/api.py index a19ab22..63dc945 100644 --- a/apps/warehouse/web/api/v1/api.py +++ b/apps/warehouse/web/api/v1/api.py @@ -176,6 +176,7 @@ class InventoryQuotaSaleTransactionViewSet( 'quota_distribution__distribution_id', 'weight', 'delivery_address', 'transaction_price', 'price_paid', 'price_type', 'product_type', 'transactions_number', 'transaction_status', 'transaction_status_code', 'ref_num', 'terminal', 'payer_cart', 'transaction_date', + 'transaction__rancher__national_code' ] def list(self, request, *args, **kwargs):