fix --> tag distribution dashboard by species
This commit is contained in:
@@ -6,6 +6,7 @@ from django.db.models.aggregates import Count
|
|||||||
from django.db.models.functions import Coalesce
|
from django.db.models.functions import Coalesce
|
||||||
|
|
||||||
from apps.authentication.models import Organization
|
from apps.authentication.models import Organization
|
||||||
|
from apps.livestock.models import LiveStockSpecies
|
||||||
from apps.tag.exceptions import TagException
|
from apps.tag.exceptions import TagException
|
||||||
from apps.tag.models import Tag, TagBatch, TagDistribution, TagDistributionBatch
|
from apps.tag.models import Tag, TagBatch, TagDistribution, TagDistributionBatch
|
||||||
from common.generics import generate_unique_code
|
from common.generics import generate_unique_code
|
||||||
@@ -159,16 +160,18 @@ class TagDistributionService:
|
|||||||
is_closed = False if is_closed == 'false' else True
|
is_closed = False if is_closed == 'false' else True
|
||||||
|
|
||||||
if org.type.key == 'ADM':
|
if org.type.key == 'ADM':
|
||||||
distributions_batch = TagDistributionBatch.objects.prefetch_related(
|
distribution_query = (Q(is_closed=is_closed))
|
||||||
'distributions'
|
|
||||||
).filter(is_closed=is_closed)
|
|
||||||
else:
|
else:
|
||||||
distributions_batch = TagDistributionBatch.objects.filter(
|
distribution_query = (
|
||||||
Q(assigner_org=org) |
|
Q(assigner_org=org) |
|
||||||
Q(assigned_org=org),
|
Q(assigned_org=org),
|
||||||
is_closed=is_closed,
|
Q(is_closed=is_closed)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
distributions_batch = TagDistributionBatch.objects.prefetch_related(
|
||||||
|
'distributions'
|
||||||
|
).filter(distribution_query)
|
||||||
|
|
||||||
data = distributions_batch.aggregate(
|
data = distributions_batch.aggregate(
|
||||||
count=Count('id'),
|
count=Count('id'),
|
||||||
total_sent_tag_count=Coalesce(Sum('total_tag_count', filter=Q(assigner_org=org)), 0),
|
total_sent_tag_count=Coalesce(Sum('total_tag_count', filter=Q(assigner_org=org)), 0),
|
||||||
@@ -179,11 +182,22 @@ class TagDistributionService:
|
|||||||
remaining_tag_count=Sum('remaining_tag_count'),
|
remaining_tag_count=Sum('remaining_tag_count'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# distributions item list detail
|
||||||
|
items_list = []
|
||||||
distributions = TagDistribution.objects.filter(
|
distributions = TagDistribution.objects.filter(
|
||||||
Q(assigner_org=org) |
|
distribution_query
|
||||||
Q(assigned_org=org),
|
|
||||||
is_closed=is_closed,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
print(distributions)
|
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')))
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
|
data.update({'items': items_list})
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|||||||
Reference in New Issue
Block a user