quota, distribution, inventory entry, quota sale transaction, product informations, signals ,....
This commit is contained in:
17
apps/warehouse/web/api/v1/api.py
Normal file
17
apps/warehouse/web/api/v1/api.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import viewsets
|
||||
from django.db import transaction
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
class InventoryEntryViewSet(viewsets.ModelViewSet):
|
||||
queryset = warehouse_models.InventoryEntry.objects.all()
|
||||
serializer_class = warehouse_serializers.InventoryEntrySerializer
|
||||
|
||||
|
||||
class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet):
|
||||
queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all()
|
||||
serializer_class = warehouse_serializers.InventoryQuotaSaleTransactionSerializer
|
||||
@@ -0,0 +1,90 @@
|
||||
from apps.warehouse.exceptions import (
|
||||
InventoryEntryWeightException,
|
||||
TotalInventorySaleException
|
||||
)
|
||||
from apps.product.exceptions import QuotaExpiredTimeException
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from apps.authorization.models import UserRelations
|
||||
from rest_framework import serializers
|
||||
from django.db import models
|
||||
|
||||
|
||||
class InventoryEntrySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = warehouse_models.InventoryEntry
|
||||
fields = '__all__'
|
||||
|
||||
def create(self, validated_data):
|
||||
""" Custom create & set organization """
|
||||
|
||||
distribution = validated_data['distribution']
|
||||
organization = distribution.assigned_organization
|
||||
|
||||
return warehouse_models.InventoryEntry.objects.create(
|
||||
organization=organization,
|
||||
**validated_data
|
||||
)
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
check if inventory entries weight is not more than
|
||||
distribution weight & check quota expired time
|
||||
"""
|
||||
|
||||
distribution = attrs['distribution']
|
||||
|
||||
# check for quota expired time
|
||||
if not distribution.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
|
||||
|
||||
if total_entered + attrs['weight'] > distribution.weight:
|
||||
raise InventoryEntryWeightException()
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = warehouse_models.InventoryQuotaSaleTransaction
|
||||
fields = '__all__'
|
||||
depth = 0
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
validate total inventory sale should be fewer than
|
||||
inventory entry from distribution
|
||||
"""
|
||||
inventory_entry = attrs['inventory_entry']
|
||||
distribution = attrs['quota_distribution']
|
||||
|
||||
total_sale_weight = inventory_entry.inventory_sales.aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
|
||||
if total_sale_weight + attrs['weight'] > distribution.warehouse_balance:
|
||||
raise TotalInventorySaleException()
|
||||
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
""" Custom create & set some parameters like seller & buyer """
|
||||
|
||||
distribution = validated_data['quota_distribution']
|
||||
seller_organization = distribution.assigned_organization
|
||||
|
||||
user = self.context['request'].user
|
||||
buyer_user = user
|
||||
seller_user = validated_data['inventory_entry'].created_by
|
||||
|
||||
return warehouse_models.InventoryQuotaSaleTransaction.objects.create(
|
||||
seller_organization=seller_organization,
|
||||
seller_user=seller_user,
|
||||
buyer_user=buyer_user,
|
||||
**validated_data
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from apps.warehouse.web.api.v1 import api
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'inventory_entry', api.InventoryEntryViewSet, basename='inventory_entry')
|
||||
router.register(
|
||||
r'inventory_sale_transaction',
|
||||
api.InventoryQuotaSaleTransactionViewSet,
|
||||
basename='inventory_sale_transaction'
|
||||
)
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('v1/', include(router.urls)),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user