From 36df84da989bf947e9bc26ca38ba58b0c343f5c4 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Tue, 27 Jan 2026 15:26:25 +0330 Subject: [PATCH] fix --> bug of calculated distribution of tag batch --- apps/tag/services/tag_batch_service.py | 2 +- apps/tag/signals/tag_distribution_signals.py | 4 +++- apps/tag/web/api/v1/serializers.py | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/tag/services/tag_batch_service.py b/apps/tag/services/tag_batch_service.py index fc4b320..d46d3b9 100644 --- a/apps/tag/services/tag_batch_service.py +++ b/apps/tag/services/tag_batch_service.py @@ -25,7 +25,7 @@ class TagBatchService: base_data = qs.aggregate( batch_count=Count('id', distinct=True), - total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0), + total_distributed_tags=Coalesce(Sum('total_distributed_tags', distinct=True), 0), total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0), tag_count_created_by_batch=Count('tag'), has_distributed_batches_number=Count( diff --git a/apps/tag/signals/tag_distribution_signals.py b/apps/tag/signals/tag_distribution_signals.py index 28c5099..e44c7cf 100644 --- a/apps/tag/signals/tag_distribution_signals.py +++ b/apps/tag/signals/tag_distribution_signals.py @@ -14,7 +14,9 @@ def calculate_tag_batch_details(sender, instance: TagDistribution, **kwargs): distributed_tags = distributions.aggregate(count=Count('tag'))['count'] - instance.batch.total_distributed_tags = distributed_tags + instance.batch.total_distributed_tags = distributed_tags \ + if not instance.batch.total_distributed_tags != 0 \ + else instance.total_tag_count instance.batch.total_remaining_tags = int(instance.batch.request_number) - distributed_tags instance.batch.save(update_fields=['total_distributed_tags', 'total_remaining_tags']) diff --git a/apps/tag/web/api/v1/serializers.py b/apps/tag/web/api/v1/serializers.py index 6e8ae11..c4cd7fe 100644 --- a/apps/tag/web/api/v1/serializers.py +++ b/apps/tag/web/api/v1/serializers.py @@ -205,6 +205,8 @@ class TagDistributionBatchSerializer(serializers.ModelSerializer): 'batch_identity': dist.batch.batch_identity if dist.batch else None, 'species_code': dist.species_code, 'distributed_number': dist.distributed_number, + 'total_tag_count': dist.total_tag_count, + 'remaining_number': dist.remaining_number, 'serial_from': dist.batch.serial_from if dist.batch else None, 'serial_to': dist.batch.serial_to if dist.batch else None, } for dist in instance.distributions.all()]