Compare commits
10 Commits
0b08107c14
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
| 3209dd2d31 | |||
| 0a84ca6fe4 | |||
| cc81bc8a3d | |||
| 3258991014 | |||
| fb58e6c3aa | |||
| c5b87e8591 | |||
| 0dd145131f | |||
| 74a870380e | |||
| f798b72dbc | |||
| 93180edc0b |
@@ -0,0 +1,25 @@
|
|||||||
|
# Generated by Django 5.0 on 2026-02-07 07:29
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('authentication', '0060_organization_ownership_code'),
|
||||||
|
('tag', '0042_tagdistribution_parent_and_more'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='tagdistributionbatch',
|
||||||
|
name='owner_org',
|
||||||
|
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tag_distribution_batch', to='authentication.organization'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='tagdistributionbatch',
|
||||||
|
name='top_root_distribution',
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -138,6 +138,12 @@ class TagDistributionBatch(BaseModel):
|
|||||||
null=True
|
null=True
|
||||||
)
|
)
|
||||||
dist_batch_identity = models.CharField(max_length=20, default="0", unique=True, null=True)
|
dist_batch_identity = models.CharField(max_length=20, default="0", unique=True, null=True)
|
||||||
|
owner_org = models.ForeignKey(
|
||||||
|
Organization,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name='tag_distribution_batch',
|
||||||
|
null=True
|
||||||
|
)
|
||||||
assigner_org = models.ForeignKey(
|
assigner_org = models.ForeignKey(
|
||||||
Organization,
|
Organization,
|
||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
@@ -163,6 +169,7 @@ class TagDistributionBatch(BaseModel):
|
|||||||
total_tag_count = models.IntegerField(default=0)
|
total_tag_count = models.IntegerField(default=0)
|
||||||
total_distributed_tag_count = models.PositiveBigIntegerField(default=0)
|
total_distributed_tag_count = models.PositiveBigIntegerField(default=0)
|
||||||
remaining_tag_count = models.PositiveBigIntegerField(default=0)
|
remaining_tag_count = models.PositiveBigIntegerField(default=0)
|
||||||
|
top_root_distribution = models.BooleanField(default=False)
|
||||||
is_closed = models.BooleanField(default=False)
|
is_closed = models.BooleanField(default=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|||||||
@@ -76,11 +76,13 @@ class TagDistributionService:
|
|||||||
# create distribution batch
|
# create distribution batch
|
||||||
distributions_batch = TagDistributionBatch.objects.create(
|
distributions_batch = TagDistributionBatch.objects.create(
|
||||||
parent=TagDistributionBatch.objects.get(id=data.get('parent')) if data.get('parent') else None,
|
parent=TagDistributionBatch.objects.get(id=data.get('parent')) if data.get('parent') else None,
|
||||||
|
owner_org=assigned_org,
|
||||||
assigner_org=org,
|
assigner_org=org,
|
||||||
assigned_org=assigned_org,
|
assigned_org=assigned_org,
|
||||||
total_tag_count=total_counted_tags,
|
total_tag_count=total_counted_tags,
|
||||||
dist_batch_identity=generate_unique_code(f"{random.randint(1000, 9999)}"),
|
dist_batch_identity=generate_unique_code(f"{random.randint(1000, 9999)}"),
|
||||||
distribution_type=distribution_type,
|
distribution_type=distribution_type,
|
||||||
|
top_root_distribution=True
|
||||||
)
|
)
|
||||||
distributions_batch.distributions.add(*distributions)
|
distributions_batch.distributions.add(*distributions)
|
||||||
|
|
||||||
@@ -186,7 +188,9 @@ class TagDistributionService:
|
|||||||
parent_tag_distribution = TagDistribution.objects.get(
|
parent_tag_distribution = TagDistribution.objects.get(
|
||||||
id=dist_data['parent_tag_distribution']
|
id=dist_data['parent_tag_distribution']
|
||||||
)
|
)
|
||||||
batch = TagBatch.objects.get(batch_identity=dist_data.get('batch_identity'))
|
batch = TagBatch.objects.get(
|
||||||
|
batch_identity=dist_data.get('batch_identity')
|
||||||
|
) if dist_data.get('batch_identity') else None
|
||||||
|
|
||||||
tags = Tag.objects.filter(
|
tags = Tag.objects.filter(
|
||||||
distributions__tag_distribution_batch=parent_batch,
|
distributions__tag_distribution_batch=parent_batch,
|
||||||
@@ -218,10 +222,11 @@ class TagDistributionService:
|
|||||||
|
|
||||||
dist_batch = TagDistributionBatch.objects.create(
|
dist_batch = TagDistributionBatch.objects.create(
|
||||||
parent=parent_batch,
|
parent=parent_batch,
|
||||||
|
owner_org=assigned_org,
|
||||||
assigner_org=org,
|
assigner_org=org,
|
||||||
assigned_org=assigned_org,
|
assigned_org=assigned_org,
|
||||||
total_tag_count=total_counted_tags,
|
total_tag_count=total_counted_tags,
|
||||||
distribution_type='batch',
|
distribution_type=parent_batch.distribution_type,
|
||||||
dist_batch_identity=generate_unique_code(
|
dist_batch_identity=generate_unique_code(
|
||||||
f"{random.randint(1000, 9999)}"
|
f"{random.randint(1000, 9999)}"
|
||||||
)
|
)
|
||||||
@@ -243,7 +248,7 @@ class TagDistributionService:
|
|||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
|
|
||||||
if tag_batch.assigner_org != org:
|
if tag_batch.assigner_org != org:
|
||||||
raise PermissionDenied("اجازه ویرایش این توزیع را ندارید")
|
raise PermissionDenied("اجازه ویرایش این توزیع را ندارید") # noqa
|
||||||
|
|
||||||
for dist in tag_batch.distributions.all():
|
for dist in tag_batch.distributions.all():
|
||||||
dist.tag.all().update(
|
dist.tag.all().update(
|
||||||
@@ -266,7 +271,9 @@ class TagDistributionService:
|
|||||||
parent_tag_distribution = TagDistribution.objects.get(
|
parent_tag_distribution = TagDistribution.objects.get(
|
||||||
id=dist_data['parent_tag_distribution']
|
id=dist_data['parent_tag_distribution']
|
||||||
)
|
)
|
||||||
batch = TagBatch.objects.get(batch_identity=dist_data.get('batch_identity'))
|
batch = TagBatch.objects.get(
|
||||||
|
batch_identity=dist_data.get('batch_identity')
|
||||||
|
) if dist_data.get('batch_identity') else None
|
||||||
count = dist_data['count']
|
count = dist_data['count']
|
||||||
|
|
||||||
tags = Tag.objects.filter(
|
tags = Tag.objects.filter(
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from django.db.models import Sum
|
||||||
|
from django.db.models.functions import Coalesce
|
||||||
from django.db.models.signals import m2m_changed
|
from django.db.models.signals import m2m_changed
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
@@ -15,14 +17,19 @@ def update_batch_on_distribution_change(
|
|||||||
if not instance.batch:
|
if not instance.batch:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if instance.parent:
|
||||||
|
return
|
||||||
|
|
||||||
batch = instance.batch
|
batch = instance.batch
|
||||||
|
|
||||||
distributions = TagDistribution.objects.filter(batch=batch)
|
distributions = TagDistribution.objects.filter(batch=batch)
|
||||||
|
|
||||||
distributed_tags = Tag.objects.filter(
|
distributed_tags = Tag.objects.filter(
|
||||||
distributions__batch=batch
|
distributions__batch=batch,
|
||||||
|
status__in=['R', 'A'],
|
||||||
).distinct().count()
|
).distinct().count()
|
||||||
|
|
||||||
|
print("distributed_tags", distributed_tags)
|
||||||
batch.total_distributed_tags = distributed_tags
|
batch.total_distributed_tags = distributed_tags
|
||||||
batch.total_remaining_tags = (
|
batch.total_remaining_tags = (
|
||||||
int(batch.request_number) - distributed_tags
|
int(batch.request_number) - distributed_tags
|
||||||
@@ -53,121 +60,16 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
|||||||
tag_dist_batch = instance
|
tag_dist_batch = instance
|
||||||
parent = tag_dist_batch.parent
|
parent = tag_dist_batch.parent
|
||||||
if parent:
|
if parent:
|
||||||
# parent.total_distributed_tag_count += tag_dist_batch.total_tag_count
|
parent.total_distributed_tag_count = parent.children.aggregate(
|
||||||
parent.remaining_tag_count = 20
|
total=Coalesce(Sum('total_tag_count'), 0)
|
||||||
print(parent.remaining_tag_count)
|
)['total']
|
||||||
parent.save(update_fields=['remaining_tag_count'])
|
parent.remaining_tag_count = (
|
||||||
|
parent.total_tag_count - parent.total_distributed_tag_count
|
||||||
|
)
|
||||||
|
parent.parent_flag = True
|
||||||
|
parent.save(update_fields=['remaining_tag_count', 'total_distributed_tag_count'])
|
||||||
|
|
||||||
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_tag_count
|
if not getattr(instance, 'parent_flag', False):
|
||||||
instance.flag = True
|
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_tag_count
|
||||||
tag_dist_batch.save(update_fields=['remaining_tag_count'])
|
instance.flag = True
|
||||||
|
tag_dist_batch.save(update_fields=['remaining_tag_count'])
|
||||||
# @receiver(m2m_changed, sender=TagDistribution.tag.through)
|
|
||||||
# def on_tags_added_to_distribution(sender, instance, action, pk_set, **kwargs):
|
|
||||||
# if action != 'post_add':
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# if not pk_set:
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# with transaction.atomic():
|
|
||||||
#
|
|
||||||
# Tag.objects.filter(
|
|
||||||
# id__in=pk_set
|
|
||||||
# ).update(
|
|
||||||
# status='R',
|
|
||||||
# organization=instance.assigned_org
|
|
||||||
# )
|
|
||||||
#
|
|
||||||
# total = instance.tag.count()
|
|
||||||
#
|
|
||||||
# instance.total_tag_count = total
|
|
||||||
# instance.distributed_number = total - instance.remaining_number
|
|
||||||
# instance.remaining_number = total - instance.distributed_number
|
|
||||||
# instance.save(update_fields=[
|
|
||||||
# 'total_tag_count',
|
|
||||||
# 'distributed_number',
|
|
||||||
# 'remaining_number'
|
|
||||||
# ])
|
|
||||||
#
|
|
||||||
# if instance.batch:
|
|
||||||
# batch = instance.batch
|
|
||||||
#
|
|
||||||
# distributed_tags = Tag.objects.filter(
|
|
||||||
# batches=batch,
|
|
||||||
# status__in=['R', 'A']
|
|
||||||
# ).count()
|
|
||||||
#
|
|
||||||
# total_tags = batch.tag.count()
|
|
||||||
#
|
|
||||||
# batch.total_distributed_tags = distributed_tags
|
|
||||||
# batch.total_remaining_tags = total_tags - distributed_tags
|
|
||||||
# batch.status = (
|
|
||||||
# 'distributed'
|
|
||||||
# if batch.total_remaining_tags == 0
|
|
||||||
# else 'created'
|
|
||||||
# )
|
|
||||||
#
|
|
||||||
# batch.save(update_fields=[
|
|
||||||
# 'total_distributed_tags',
|
|
||||||
# 'total_remaining_tags',
|
|
||||||
# 'status'
|
|
||||||
# ])
|
|
||||||
#
|
|
||||||
#
|
|
||||||
# @receiver(m2m_changed, sender=TagDistribution.tag.through)
|
|
||||||
# def on_tags_removed_from_distribution(sender, instance, action, pk_set, **kwargs):
|
|
||||||
# if action not in ['post_remove', 'post_clear']:
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# if action == 'post_clear':
|
|
||||||
# pk_set = list(instance.tag.values_list('id', flat=True))
|
|
||||||
#
|
|
||||||
# if not pk_set:
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# with transaction.atomic():
|
|
||||||
#
|
|
||||||
# Tag.objects.filter(id__in=pk_set).update(
|
|
||||||
# status='R',
|
|
||||||
# organization=instance.assigner_org
|
|
||||||
# )
|
|
||||||
#
|
|
||||||
# total = instance.tag.count()
|
|
||||||
# instance.total_tag_count = total
|
|
||||||
# instance.distributed_number = total
|
|
||||||
# instance.remaining_number = 0
|
|
||||||
# instance.save(update_fields=[
|
|
||||||
# 'total_tag_count',
|
|
||||||
# 'distributed_number',
|
|
||||||
# 'remaining_number'
|
|
||||||
# ])
|
|
||||||
#
|
|
||||||
# if instance.batch:
|
|
||||||
# batch = instance.batch
|
|
||||||
# distributed_tags = Tag.objects.filter(
|
|
||||||
# batches=batch,
|
|
||||||
# status__in=['R', 'A']
|
|
||||||
# ).count()
|
|
||||||
# total_tags = batch.tag.count()
|
|
||||||
# batch.total_distributed_tags = distributed_tags
|
|
||||||
# batch.total_remaining_tags = total_tags - distributed_tags
|
|
||||||
# batch.status = 'distributed' if batch.total_remaining_tags == 0 else 'created'
|
|
||||||
# batch.save(update_fields=[
|
|
||||||
# 'total_distributed_tags',
|
|
||||||
# 'total_remaining_tags',
|
|
||||||
# 'status'
|
|
||||||
# ])
|
|
||||||
#
|
|
||||||
# for dist_batch in instance.tag_distribution_batch.all():
|
|
||||||
# total_dist = dist_batch.distributions.aggregate(
|
|
||||||
# total=Count('tag')
|
|
||||||
# ).get('total', 0)
|
|
||||||
# dist_batch.total_distributed_tag_count = total_dist
|
|
||||||
# dist_batch.remaining_tag_count = dist_batch.total_tag_count - total_dist
|
|
||||||
# dist_batch.is_closed = dist_batch.remaining_tag_count == 0
|
|
||||||
# dist_batch.save(update_fields=[
|
|
||||||
# 'total_distributed_tag_count',
|
|
||||||
# 'remaining_tag_count',
|
|
||||||
# 'is_closed'
|
|
||||||
# ])
|
|
||||||
|
|||||||
@@ -478,9 +478,9 @@ class TagDistributionViewSet(
|
|||||||
@action(
|
@action(
|
||||||
methods=['put'],
|
methods=['put'],
|
||||||
detail=True,
|
detail=True,
|
||||||
url_path='distribute_distribution',
|
url_path='edit_distribute_distribution',
|
||||||
url_name='distribute_distribution',
|
url_name='edit_distribute_distribution',
|
||||||
name='distribute_distribution',
|
name='edit_distribute_distribution',
|
||||||
)
|
)
|
||||||
def update_distribute_from_distribution(self, request, pk=None):
|
def update_distribute_from_distribution(self, request, pk=None):
|
||||||
"""
|
"""
|
||||||
@@ -558,9 +558,9 @@ class TagDistributionViewSet(
|
|||||||
|
|
||||||
class TagDistributionBatchViewSet(
|
class TagDistributionBatchViewSet(
|
||||||
BaseViewSet,
|
BaseViewSet,
|
||||||
|
viewsets.ModelViewSet,
|
||||||
SoftDeleteMixin,
|
SoftDeleteMixin,
|
||||||
DynamicSearchMixin,
|
DynamicSearchMixin,
|
||||||
viewsets.ModelViewSet,
|
|
||||||
TagDistributionService
|
TagDistributionService
|
||||||
):
|
):
|
||||||
queryset = tag_models.TagDistributionBatch.objects.all()
|
queryset = tag_models.TagDistributionBatch.objects.all()
|
||||||
@@ -578,8 +578,23 @@ class TagDistributionBatchViewSet(
|
|||||||
"""
|
"""
|
||||||
list of tag distribution batches
|
list of tag distribution batches
|
||||||
"""
|
"""
|
||||||
|
org = get_organization_by_user(request.user)
|
||||||
|
|
||||||
queryset = self.get_queryset(visibility_by_org_scope=True).filter(is_closed=False).order_by('-create_date')
|
queryset = self.get_queryset(
|
||||||
|
visibility_by_org_scope=True
|
||||||
|
).filter(
|
||||||
|
is_closed=False,
|
||||||
|
top_root_distribution=True,
|
||||||
|
).order_by('-create_date')
|
||||||
|
|
||||||
|
if not queryset:
|
||||||
|
queryset = self.get_queryset(
|
||||||
|
visibility_by_org_scope=True
|
||||||
|
).filter(
|
||||||
|
is_closed=False,
|
||||||
|
owner_org=org,
|
||||||
|
top_root_distribution=False,
|
||||||
|
).order_by('-create_date')
|
||||||
|
|
||||||
queryset = self.filter_query(self.filter_queryset(queryset))
|
queryset = self.filter_query(self.filter_queryset(queryset))
|
||||||
|
|
||||||
@@ -598,6 +613,26 @@ class TagDistributionBatchViewSet(
|
|||||||
serializer = self.serializer_class(distribution_batch)
|
serializer = self.serializer_class(distribution_batch)
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=True,
|
||||||
|
url_path='child_list',
|
||||||
|
url_name='child_list',
|
||||||
|
name='child_list'
|
||||||
|
)
|
||||||
|
def child_list(self, request, pk=None):
|
||||||
|
"""
|
||||||
|
list of all child from a tag distribution batch
|
||||||
|
"""
|
||||||
|
dist_batch = self.get_object()
|
||||||
|
queryset = dist_batch.children.all()
|
||||||
|
|
||||||
|
page = self.paginate_queryset(queryset)
|
||||||
|
if page is not None: # noqa
|
||||||
|
serializer = self.get_serializer(page, many=True)
|
||||||
|
return self.get_paginated_response(serializer.data)
|
||||||
|
return Response(self.serializer_class(queryset).data)
|
||||||
|
|
||||||
@action(
|
@action(
|
||||||
methods=['post'],
|
methods=['post'],
|
||||||
detail=True,
|
detail=True,
|
||||||
@@ -612,6 +647,8 @@ class TagDistributionBatchViewSet(
|
|||||||
dist_batch.is_closed = True
|
dist_batch.is_closed = True
|
||||||
dist_batch.save()
|
dist_batch.save()
|
||||||
dist_batch.distributions.all().update(is_closed=True) # close distributions of batch
|
dist_batch.distributions.all().update(is_closed=True) # close distributions of batch
|
||||||
|
for distribute in dist_batch.distributions.all():
|
||||||
|
distribute.tag.all().update(status='F')
|
||||||
|
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@@ -670,3 +707,19 @@ class TagDistributionBatchViewSet(
|
|||||||
dashboard_data = self.distribution_batch_main_dashboard(org=org, is_closed=params.get('is_closed'))
|
dashboard_data = self.distribution_batch_main_dashboard(org=org, is_closed=params.get('is_closed'))
|
||||||
|
|
||||||
return Response(dashboard_data, status=status.HTTP_200_OK)
|
return Response(dashboard_data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def destroy(self, request, pk=None, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
delete tag distribution batch and free their tag from distribute
|
||||||
|
"""
|
||||||
|
|
||||||
|
dist_batch = self.get_object()
|
||||||
|
|
||||||
|
for distribute in dist_batch.distributions.all():
|
||||||
|
distribute.tag.all().update(status='F')
|
||||||
|
distribute.tag.clear()
|
||||||
|
distribute.soft_delete()
|
||||||
|
|
||||||
|
dist_batch.soft_delete()
|
||||||
|
|
||||||
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|||||||
@@ -281,8 +281,14 @@ class InventoryQuotaSaleTransactionViewSet(
|
|||||||
|
|
||||||
org = get_organization_by_user(request.user)
|
org = get_organization_by_user(request.user)
|
||||||
if org.free_visibility_by_scope:
|
if org.free_visibility_by_scope:
|
||||||
|
|
||||||
tr_objects = self.get_queryset(visibility_by_org_scope=True)
|
tr_objects = self.get_queryset(visibility_by_org_scope=True)
|
||||||
tr_item_objects = InventoryQuotaSaleItemViewSet().get_queryset(visibility_by_org_scope=True)
|
|
||||||
|
tr_item_view = InventoryQuotaSaleItemViewSet()
|
||||||
|
tr_item_view.request = request
|
||||||
|
tr_item_view.kwargs = {'pk': None}
|
||||||
|
tr_item_objects = tr_item_view.get_queryset(visibility_by_org_scope=True)
|
||||||
|
|
||||||
transaction_dashboard_data = self.get_dashboard(
|
transaction_dashboard_data = self.get_dashboard(
|
||||||
org,
|
org,
|
||||||
free_visibility_tr_objects=tr_objects,
|
free_visibility_tr_objects=tr_objects,
|
||||||
|
|||||||
Reference in New Issue
Block a user