diff --git a/apps/tag/services/tag_batch_service.py b/apps/tag/services/tag_batch_service.py index 964b686..35b6e10 100644 --- a/apps/tag/services/tag_batch_service.py +++ b/apps/tag/services/tag_batch_service.py @@ -1,5 +1,10 @@ +from django.db.models import Sum, Q +from django.db.models.aggregates import Count +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.livestock.models import LiveStockSpecies from apps.tag.models import TagBatch @@ -8,7 +13,7 @@ class TagBatchService: services of tag batch """ - def main_dashboard(self, org: Organization = None): + def tag_batch_main_dashboard(self, org: Organization = None): """ dashboard data of batch main page """ @@ -20,3 +25,38 @@ class TagBatchService: child_org = get_all_org_child(org) child_org.append(org) tag_batches = tag_batches.filter(organization_id__in=[child.id for child in child_org]) + + tag_batch_data = tag_batches.aggregate( + batch_count=Count('id'), + total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0), + total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0), + tag_count_created_by_batch=Count('tag'), + has_distributed_batches_number=Count('id', filter=Q(status='distributed')) + ) + + species = LiveStockSpecies.objects.values('value') + + data_by_species_list = [] + for spec in species: + tag_batch_data_by_species = tag_batches.aggregate( + batch_count=Count('id', species_code=spec.get('value')), + total_distributed_tags=Coalesce( + Sum('total_distributed_tags', filter=Q(species_code=spec.get('value'))), 0 + ), + total_remaining_tags=Coalesce( + Sum('total_remaining_tags', filter=Q(species_code=spec.get('value'))), 0 + ), + tag_count_created_by_batch=Coalesce( + Count('tag', filter=Q(species_code=spec.get('value'))), 0 + ), + has_distributed_batches_number=Count( + 'id', filter=Q(status='distributed', species_code=spec.get('value')) + ) + ) + tag_batch_data_by_species.update({'species_code': spec.get('value')}) + + data_by_species_list.append(tag_batch_data_by_species) + + tag_batch_data.update({'batch_data_by_species': data_by_species_list}) + + return tag_batch_data diff --git a/apps/tag/web/api/v1/api.py b/apps/tag/web/api/v1/api.py index 3fcd00e..430546a 100644 --- a/apps/tag/web/api/v1/api.py +++ b/apps/tag/web/api/v1/api.py @@ -16,6 +16,7 @@ from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin from apps.tag import exceptions as tag_exceptions from apps.tag import models as tag_models from apps.tag.models import TagBatch +from apps.tag.services.tag_batch_service import TagBatchService from apps.tag.services.tag_distribution_services import TagDistributionService from apps.tag.services.tag_services import TagService from common.helpers import get_organization_by_user @@ -305,7 +306,7 @@ class AllocatedTagsViewSet(SoftDeleteMixin, viewsets.ModelViewSet): serializer_class = AllocatedTagsSerializer -class TagBatchViewSet(BaseViewSet, SoftDeleteMixin, DynamicSearchMixin, viewsets.ModelViewSet): +class TagBatchViewSet(BaseViewSet, SoftDeleteMixin, DynamicSearchMixin, TagBatchService, viewsets.ModelViewSet): queryset = TagBatch.objects.all() serializer_class = TagBatchSerializer filter_backends = [SearchFilter] @@ -332,6 +333,23 @@ class TagBatchViewSet(BaseViewSet, SoftDeleteMixin, DynamicSearchMixin, viewsets return self.get_paginated_response(serializer.data) return Response(self.serializer_class(queryset).data) + @action( + methods=['get'], + detail=False, + url_name='main_dashboard', + url_path='main_dashboard', + name='main_dashboard', + ) + def main_dashboard(self, request): + """ + dashboard of tag batches main page + """ + org = get_organization_by_user(request.user) + + dashboard_data = self.tag_batch_main_dashboard(org=org) + + return Response(dashboard_data, status=status.HTTP_200_OK) + def destroy(self, request, pk=None, *args, **kwargs): """ soft delete batch with tag items