diff --git a/apps/product/models.py b/apps/product/models.py index 08348b5..13aca3f 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -402,7 +402,7 @@ class Quota(BaseModel): now = datetime.now() persian_date = jdatetime.datetime.fromgregorian(datetime=now) - return persian_date.month in self.month_choices + return persian_date.month in self.sale_license def save(self, calculate_final_price=None, *args, **kwargs): if not self.quota_id: diff --git a/apps/product/web/api/v1/serializers/quota_distribution_serializers.py b/apps/product/web/api/v1/serializers/quota_distribution_serializers.py index a35c75f..d45746d 100644 --- a/apps/product/web/api/v1/serializers/quota_distribution_serializers.py +++ b/apps/product/web/api/v1/serializers/quota_distribution_serializers.py @@ -1,5 +1,6 @@ from rest_framework import serializers from apps.product import models as product_models +from rest_framework.exceptions import APIException from apps.product.web.api.v1.serializers.quota_serializers import QuotaSerializer from django.db import models from apps.product.exceptions import ( @@ -55,6 +56,19 @@ class QuotaDistributionSerializer(serializers.ModelSerializer): if total + amount > quota.quota_weight: raise QuotaWeightException() + # total warehouse inventory entry + total_entry = self.instance.inventory_entry.aggregate( + total=models.Sum('weight') + )['total'] or 0 + + # if inventory entry weight is bigger than distribute weight in edit distribution + if total_entry > self.instance.weight: + raise APIException("وزن وارد شده کمتر از وزن ورودی به انبار است", code=403) # noqa + + # if weight is more than distribution remaining weight + if self.instance.weight > self.instance.remaining_weight: + raise APIException("وزن وارد شده بیشتر از وزن باقیمانده است", code=403) # noqa + return data def to_representation(self, instance): diff --git a/apps/warehouse/web/api/v1/api.py b/apps/warehouse/web/api/v1/api.py index aef3ae5..78d9677 100644 --- a/apps/warehouse/web/api/v1/api.py +++ b/apps/warehouse/web/api/v1/api.py @@ -70,7 +70,6 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin): @transaction.atomic def update(self, request, pk=None, *args, **kwargs): """ edit inventory """ - inventory = self.get_object() serializer = self.serializer_class(data=request.data, instance=inventory, partial=True) if serializer.is_valid(): diff --git a/apps/warehouse/web/api/v1/serializers.py b/apps/warehouse/web/api/v1/serializers.py index df800be..cdc1504 100644 --- a/apps/warehouse/web/api/v1/serializers.py +++ b/apps/warehouse/web/api/v1/serializers.py @@ -42,8 +42,12 @@ class InventoryEntrySerializer(serializers.ModelSerializer): total=models.Sum('weight') )['total'] or 0 - if total_entered + attrs['weight'] > distribution.weight: - raise InventoryEntryWeightException() + if self.instance.weight is 0: + if total_entered + attrs['weight'] > distribution.weight: + raise InventoryEntryWeightException() + elif self.instance.weight is not 0: + if total_entered - self.instance.weight + attrs['weight'] > distribution.weight: + raise InventoryEntryWeightException() return attrs