fix - list of assigned_distribution & assigner_distributions of me

This commit is contained in:
2025-11-18 10:33:52 +03:30
parent 5f6ff8344d
commit 5e50438f55
3 changed files with 12 additions and 6 deletions

View File

@@ -4,7 +4,6 @@ from django.db import models
from django.db.models import Sum, Q from django.db.models import Sum, Q
from django.db.models.signals import post_save, post_delete, post_init from django.db.models.signals import post_save, post_delete, post_init
from django.dispatch import receiver from django.dispatch import receiver
from rest_framework import status
from apps.product.validators.quota_stats_validator import QuotaStatsValidator from apps.product.validators.quota_stats_validator import QuotaStatsValidator
from apps.warehouse.models import ( from apps.warehouse.models import (
@@ -12,7 +11,6 @@ from apps.warehouse.models import (
InventoryEntry InventoryEntry
) )
from common.helpers import get_organization_by_user from common.helpers import get_organization_by_user
from .exceptions import QuotaException
from .models import ( from .models import (
QuotaDistribution, QuotaDistribution,
Quota, Quota,
@@ -328,7 +326,10 @@ def organization_quota_stats(sender, instance: Quota, created: bool, **kwargs):
total=Sum('weight') total=Sum('weight')
)['total'] or 0 )['total'] or 0
org_quota_stat.remaining_amount = org_quota_stat.total_amount - org_quota_stat.total_distributed org_quota_stat.remaining_amount = org_quota_stat.total_amount - org_quota_stat.total_distributed
# validate total_amount & distributed with remaining in update
QuotaStatsValidator.validate_quota_weight_update(org_quota_stat.remaining_amount) QuotaStatsValidator.validate_quota_weight_update(org_quota_stat.remaining_amount)
org_quota_stat.save(update_fields=['total_amount', 'total_distributed', 'sold_amount', 'remaining_amount']) org_quota_stat.save(update_fields=['total_amount', 'total_distributed', 'sold_amount', 'remaining_amount'])
# delete quota # delete quota

View File

@@ -35,10 +35,12 @@ class QuotaSerializer(serializers.ModelSerializer):
representation['been_sold'] = quota_weight_by_org['been_sold'] representation['been_sold'] = quota_weight_by_org['been_sold']
representation['distributions'] = [{ representation['distributions'] = [{
"id": dist.id, "id": dist.id,
"organization": org.name, "create_date": dist.create_date,
"organization_id": org.id, "modify_date": dist.modify_date,
"assigner_organization": dist.assigner_organization.name,
"assigner_organization_id": dist.assigner_organization.id,
"weight": dist.weight, "weight": dist.weight,
} for dist in instance.distributions_assigned.all()] } for dist in instance.distributions_assigned.filter(assigned_organization=org)]
if isinstance(instance, product_models.Quota): if isinstance(instance, product_models.Quota):
if instance.sale_unit: if instance.sale_unit:

View File

@@ -398,10 +398,13 @@ class QuotaViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet, DynamicS
def get_distributions_by_quota(self, request, pk=None): def get_distributions_by_quota(self, request, pk=None):
""" list of distributions by quota """ """ list of distributions by quota """
my_org = get_organization_by_user(request.user)
try: try:
quota = self.get_object() quota = self.get_object()
queryset = self.filter_query( queryset = self.filter_query(
quota.distributions_assigned.all().order_by('-modify_date') quota.distributions_assigned.filter(
assigner_organization=my_org
).order_by('-modify_date')
) # return by search param or all objects ) # return by search param or all objects
# paginate queryset # paginate queryset