diff --git a/apps/tag/services/tag_batch_service.py b/apps/tag/services/tag_batch_service.py new file mode 100644 index 0000000..964b686 --- /dev/null +++ b/apps/tag/services/tag_batch_service.py @@ -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]) diff --git a/apps/tag/services/tag_distribution_services.py b/apps/tag/services/tag_distribution_services.py index 5a99304..308a254 100644 --- a/apps/tag/services/tag_distribution_services.py +++ b/apps/tag/services/tag_distribution_services.py @@ -191,12 +191,14 @@ class TagDistributionService: species = LiveStockSpecies.objects.values('value') for spec in species: - items_list.append({ - spec.get('value'): 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'))) - ), - }) + dist_data = distributions.aggregate( + dist_count=Count('id', 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})