some apis of pos

This commit is contained in:
2025-08-20 14:44:43 +03:30
parent 14cd349a7d
commit 20e4bfad75
12 changed files with 316 additions and 29 deletions

View File

@@ -0,0 +1,55 @@
from apps.warehouse.pos.api.v1 import serializers as warehouse_serializers
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
from apps.core.mixins.search_mixin import DynamicSearchMixin
from apps.warehouse import models as warehouse_models
from common.helpers import get_organization_by_user
from rest_framework.permissions import AllowAny
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import viewsets, filters
from django.db import transaction
from rest_framework import status
import typing
class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDeviceMixin):
queryset = warehouse_models.InventoryEntry.objects.all()
serializer_class = warehouse_serializers.InventoryEntrySerializer
permission_classes = [AllowAny]
search_fields = [
"distribution__distribution_id",
"organization__name",
"weight",
"balance",
"lading_number",
"is_confirmed",
]
date_field = "create_date"
@action(
methods=['get'],
detail=False,
url_path='my_entries',
url_name='my_entries',
name='my_entries'
)
def inventory_entries(self, request):
""" list of pos inventory entries """
organization = self.get_device_organization()
entries = self.queryset.filter(organization=organization)
queryset = self.filter_query(entries) # return by search param or all objects
# paginate & response
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet):
queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all()
serializer_class = warehouse_serializers.InventoryQuotaSaleTransactionSerializer
filter_backends = [filters.SearchFilter]
search_fields = ['']

View File

@@ -0,0 +1,93 @@
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 = [
"id",
"create_date",
"modify_date",
"organization",
"distribution",
"weight",
"balance",
"lading_number",
"delivery_address",
"is_confirmed",
"notes",
]
def to_representation(self, instance):
""" custom output of inventory entry serializer """
representation = super().to_representation(instance)
if instance.document:
representation['document'] = instance.document
if instance.distribution:
# distribution data
representation['distribution'] = {
'distribution_identity': instance.distribution.distribution_id,
'sale_unit': instance.distribution.quota.sale_unit.unit,
'id': instance.distribution.id
}
representation['quota'] = {
'quota_identity': instance.distribution.quota.quota_id,
'quota_weight': instance.distribution.quota.quota_weight,
}
representation['product'] = {
'name': instance.distribution.quota.product.name,
'id': instance.distribution.quota.product.id,
}
return representation
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
)

View File

@@ -0,0 +1,11 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import api
router = DefaultRouter()
router.register(r'inventory_entry', api.InventoryEntryViewSet, basename='inventory_entry')
urlpatterns = [
path('v1/', include(router.urls))
]