some new changes in excel & ranching adashboard fromdevelopent

This commit is contained in:
2025-12-14 14:24:10 +03:30
3 changed files with 92 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ from django.db.models import Sum, Count, Q
from django.db.models.functions import Coalesce
from apps.herd.models import Rancher, Herd
from apps.warehouse.models import InventoryQuotaSaleItem
class RancherDashboardService:
@@ -28,9 +29,9 @@ class RancherDashboardService:
total_without_herd=Coalesce(Count("id", filter=Q(without_herd=True)), 0),
total_natural_ranchers=Coalesce(Count("id", filter=Q(rancher_type='N')), 0),
total_legal_ranchers=Coalesce(Count("id", filter=Q(rancher_type='L')), 0),
total_industrial_ranchers=Coalesce(Count("id", filter=Q(rancher_type='I')), 0),
total_village_ranchers=Coalesce(Count("id", filter=Q(rancher_type='V')), 0),
total_nomadic_ranchers=Coalesce(Count("id", filter=Q(rancher_type='N')), 0),
total_industrial_ranchers=Coalesce(Count("id", filter=Q(activity='I')), 0),
total_village_ranchers=Coalesce(Count("id", filter=Q(activity='V')), 0),
total_nomadic_ranchers=Coalesce(Count("id", filter=Q(activity='N')), 0),
)
herds_data = herds.aggregate(
@@ -43,3 +44,45 @@ class RancherDashboardService:
)
return {"rancher_dashboard": ranchers_data, "herd_dashboard": herds_data}
@staticmethod
def get_rancher_dashboard(self, rancher: Rancher):
"""
get rancher dashboard
"""
rancher_data = rancher.herd.aggregate(
total_herds_count=Coalesce(Count("id"), 0),
total_heavy_livestock_count=Coalesce(Sum("heavy_livestock_number"), 0),
total_light_livestock_count=Coalesce(Sum("light_livestock_number"), 0),
)
transaction_sale_items = InventoryQuotaSaleItem.objects.select_related(
'transaction', 'quota_stat'
).filter(
transaction__rancher=rancher,
# transaction__transaction_status='success',
).aggregate(
total_purchased_weight=Coalesce(Sum('weight'), 0),
)
rancher_data.update(transaction_sale_items)
return rancher_data
@staticmethod
def get_rancher_dashboard_by_quota_usage(self, rancher: Rancher):
"""
get rancher dashboard by quota usage
"""
transaction_sale_items = InventoryQuotaSaleItem.objects.select_related(
'transaction', 'quota_stat'
).filter(
transaction__rancher=rancher,
transaction__transaction_status='success',
).values('quota_stat')
print(list(set(transaction_sale_items)))
return {}

View File

@@ -195,3 +195,47 @@ class RancherViewSet(BaseViewSet, RancherDashboardService, SoftDeleteMixin, view
query_string=query_string,
)
return Response(rancher_dashboard_data)
@action(
methods=['get'],
detail=True,
url_path='rancher_dashboard',
url_name='rancher_dashboard',
name='rancher_dashboard'
)
def rancher_dashboard(self, request, pk=None):
""" rancher dashboard """
rancher = self.get_object()
query_param = self.request.query_params # noqa
query_string = query_param.get('search', None)
rancher_dashboard_data = self.get_rancher_dashboard(
self,
rancher=rancher
)
return Response(rancher_dashboard_data)
@action(
methods=['get'],
detail=True,
url_path='rancher_dashboard_by_quota_usage',
url_name='rancher_dashboard_by_quota_usage',
name='rancher_dashboard_by_quota_usage'
)
def rancher_dashboard_by_quota_usage(self, request, pk=None):
""" rancher dashboard """
rancher = self.get_object()
query_param = self.request.query_params # noqa
query_string = query_param.get('search', None)
rancher_dashboard_data = self.get_rancher_dashboard_by_quota_usage(
self,
rancher=rancher
)
return Response(rancher_dashboard_data)

View File

@@ -6,10 +6,10 @@ from openpyxl.styles import Font
from rest_framework import viewsets
from rest_framework.decorators import action
from apps.authentication.services.visibility_services import apply_visibility_filter
from apps.core.mixins.search_mixin import ExcelDynamicSearchMixin
from apps.warehouse import models as warehouse_models
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
from apps.authentication.services.visibility_services import apply_visibility_filter
from common.helper_excel import create_header, excel_description, create_header_freez, create_value, shamsi_date, \
convert_str_to_date
from common.helpers import get_organization_by_user
@@ -309,7 +309,7 @@ class WareHouseExcelViewSet(viewsets.ModelViewSet, ExcelDynamicSearchMixin):
rancher_data = data.get('rancher')
national_code = rancher_data.get('national_code', '-') if rancher_data else '-'
rancher_name = rancher_data.get('fullname', '-') if rancher_data else '-'
rancher_name = data.get('rancher_fullname', '-') if rancher_data else '-'
seller_org = data.get('seller_organization')
org_name = seller_org.get('name', '-') if seller_org else '-'