add - whole system of inventory entry in organization quotas stat

This commit is contained in:
2025-11-19 15:56:15 +03:30
parent 42c01f3eb5
commit 1bf6950ccb
9 changed files with 152 additions and 34 deletions

View File

@@ -1,11 +1,9 @@
from django.db import models
from rest_framework import serializers
from rest_framework import serializers, status
from apps.product.exceptions import QuotaExpiredTimeException
from apps.product.models import OrganizationQuotaStats
from apps.warehouse import models as warehouse_models
from apps.warehouse.exceptions import (
InventoryEntryWeightException
)
from apps.warehouse.exceptions import WareHouseException
class InventoryEntrySerializer(serializers.ModelSerializer):
@@ -16,7 +14,7 @@ class InventoryEntrySerializer(serializers.ModelSerializer):
"create_date",
"modify_date",
"organization",
"distribution",
"quota",
"weight",
"balance",
"lading_number",
@@ -30,32 +28,49 @@ class InventoryEntrySerializer(serializers.ModelSerializer):
check if inventory entries weight is not more than
distribution weight & check quota expired time
"""
distribution = attrs['distribution']
quota = attrs['quota']
org = attrs['organization']
# check for quota expired time
if not distribution.quota.is_in_valid_time():
if not quota.is_in_valid_time():
raise QuotaExpiredTimeException()
# total inventory entries weight
total_entered = distribution.inventory_entry.filter(is_confirmed=True).aggregate(
total=models.Sum('weight')
)['total'] or 0
# total_entered = distribution.inventory_entry.filter(is_confirmed=True).aggregate(
# total=models.Sum('weight')
# )['total'] or 0
org_quota_stat = OrganizationQuotaStats.objects.get(
organization=org,
quota=quota
)
total_entered_weight = org_quota_stat.inventory_received
remaining_weight_to_enter = org_quota_stat.remaining_amount
# if instance exists, for update check weight with distribution weight
if self.instance:
if self.instance.weight == 0:
if total_entered + attrs['weight'] > distribution.weight:
raise InventoryEntryWeightException()
if total_entered_weight + attrs['weight'] > remaining_weight_to_enter:
raise WareHouseException(
"وزن وارد شده برای ورود به انبار نباید از باقیمانده سهمیه بیشتر باشد", # noqa
status.HTTP_403_FORBIDDEN
)
elif self.instance.weight != 0:
if total_entered - self.instance.weight + attrs['weight'] > distribution.weight:
raise InventoryEntryWeightException()
if total_entered_weight - self.instance.weight + attrs['weight'] > remaining_weight_to_enter:
raise WareHouseException(
"وزن وارد شده برای ورود به انبار نباید از باقیمانده سهمیه بیشتر باشد", # noqa
status.HTTP_403_FORBIDDEN
)
# if instance is not exists for create, check entry weight with distribution
else:
if total_entered + attrs['weight'] > distribution.weight or \
total_entered + attrs['weight'] > distribution.remaining_weight:
raise InventoryEntryWeightException()
if total_entered_weight + attrs['weight'] > remaining_weight_to_enter:
# total_entered + attrs['weight'] > distribution.remaining_weight:
raise WareHouseException(
"وزن وارد شده برای ورود به انبار نباید از باقیمانده سهمیه بیشتر باشد", # noqa
status.HTTP_403_FORBIDDEN
)
return attrs