fix - set exception in org_quota_stat signal
This commit is contained in:
@@ -4,6 +4,7 @@ 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 (
|
||||||
@@ -11,6 +12,7 @@ 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,
|
||||||
@@ -326,6 +328,11 @@ 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
|
||||||
|
if org_quota_stat.remaining_amount < 0:
|
||||||
|
raise QuotaException(
|
||||||
|
"وزن سهمیه نباید از مقدار توزیع شده کمتر باشد", # noqa
|
||||||
|
status.HTTP_403_FORBIDDEN
|
||||||
|
)
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user