fix --> import serial to distributions in dist batch

This commit is contained in:
2026-01-21 11:10:46 +03:30
parent 4cabee3502
commit 40e0daeb60
2 changed files with 60 additions and 3 deletions

View File

@@ -79,8 +79,63 @@ class TagDistributionService:
return {'tag_distributions': distributions, 'distributions_batch': distributions_batch} return {'tag_distributions': distributions, 'distributions_batch': distributions_batch}
def edit_distribution(self, batch: TagDistributionBatch, data: dict = None): def edit_distribution(self, batch: TagDistributionBatch = None, data: dict = None, org: Organization = None):
""" """
edit record of distributed tags edit record of distributed tags
""" """
# clear and hard delete of distributions
distributions = batch.distributions.all()
for dist in distributions:
pass pass
batch.distributions.clear()
distributions.delete()
# create new distributions and update batch
total_counted_tags = 0
distributions = []
assigned_org = Organization.objects.get(id=data.get('assigned_org'))
for distribution in data.get('dists'):
batch_identity = distribution.get('batch_identity', None)
# if batch identity exists distribute tags of batch
if batch_identity:
batch = TagBatch.objects.get(batch_identity=batch_identity)
tags = Tag.objects.filter(
batches__batch_identity=batch_identity,
species_code=distribution.get('species_code'),
status='F'
)
distribution_type = 'batch'
else:
batch = None
# get tags without batch and only with species code
tags = Tag.objects.filter(
species_code=distribution.get('species_code'),
status='F'
)
distribution_type = 'random'
if tags.count() < distribution.get('count'):
raise TagException(
"تعداد وارد شده از تعداد موجودی این گونه بیشتر میباشد.", # noqa
403
)
dist = TagDistribution.objects.create(
batch=batch,
assigner_org=org,
assigned_org=assigned_org,
species_code=distribution.get('species_code'),
distributed_number=distribution.get('count'),
dist_identity=generate_unique_code(f"{random.randint(1000, 9999)}"),
)
# get counted tag ids and filter by them to update status To Reserve
counted_tags_obj = tags.order_by('create_date')[:int(distribution.get('count'))]
counted_tag_ids = [tag.id for tag in counted_tags_obj]
tags.filter(id__in=counted_tag_ids).update(status='R')
dist.tag.add(*counted_tags_obj)
distributions.append(dist)
total_counted_tags += distribution.get('count')

View File

@@ -203,7 +203,9 @@ class TagDistributionBatchSerializer(serializers.ModelSerializer):
'id': dist.id, 'id': dist.id,
'dist_identity': dist.dist_identity, 'dist_identity': dist.dist_identity,
'species_code': dist.species_code, 'species_code': dist.species_code,
'distributed_number': dist.distributed_number 'distributed_number': dist.distributed_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()] } for dist in instance.distributions.all()]
return representation return representation