fix --> structure of species data in main dist batch dashboard

This commit is contained in:
2026-01-25 09:59:37 +03:30
parent c0e62541c3
commit dd807f04be
2 changed files with 30 additions and 6 deletions

View File

@@ -0,0 +1,22 @@
from apps.authentication.models import Organization
from apps.authentication.services.service import get_all_org_child
from apps.tag.models import TagBatch
class TagBatchService:
"""
services of tag batch
"""
def main_dashboard(self, org: Organization = None):
"""
dashboard data of batch main page
"""
tag_batches = TagBatch.objects.select_related('organization').prefetch_related('tag')
if org.type.key != 'ADM':
# get batches with org & their child
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])

View File

@@ -191,12 +191,14 @@ class TagDistributionService:
species = LiveStockSpecies.objects.values('value')
for spec in species:
items_list.append({
spec.get('value'): distributions.aggregate(
dist_data = distributions.aggregate(
dist_count=Count('id', filter=Q(species_code=spec.get('value'))),
tag_count=Sum('distributed_number', filter=Q(species_code=spec.get('value')))
),
})
tag_count=Coalesce(
Sum('distributed_number', filter=Q(species_code=spec.get('value'))), 0
)
)
dist_data.update({'species_code': spec.get('value')}) # add species code to data
items_list.append(dist_data)
data.update({'items': items_list})