some apis of pos
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# Generated by Django 5.0 on 2025-08-20 11:14
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('herd', '0016_rancher_activity_rancher_heavy_livestock_number_and_more'),
|
||||
('pos_device', '0061_posfreeproducts'),
|
||||
('warehouse', '0012_inventoryentry_balance'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='inventoryquotasaletransaction',
|
||||
name='pos_device',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='transactions', to='pos_device.device'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='inventoryquotasaletransaction',
|
||||
name='rancher',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='transactions', to='herd.rancher'),
|
||||
),
|
||||
]
|
||||
@@ -1,5 +1,7 @@
|
||||
from apps.product import models as product_models
|
||||
from apps.authentication.models import User
|
||||
from apps.pos_device.models import Device
|
||||
from apps.herd.models import Rancher
|
||||
from apps.core.models import BaseModel
|
||||
from django.db import models
|
||||
|
||||
@@ -41,6 +43,18 @@ class InventoryEntry(BaseModel):
|
||||
|
||||
|
||||
class InventoryQuotaSaleTransaction(BaseModel):
|
||||
rancher = models.ForeignKey(
|
||||
Rancher,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='transactions',
|
||||
null=True
|
||||
)
|
||||
pos_device = models.ForeignKey(
|
||||
Device,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='transactions',
|
||||
null=True
|
||||
)
|
||||
transaction_id = models.CharField(max_length=50, null=True)
|
||||
seller_organization = models.ForeignKey(
|
||||
product_models.Organization,
|
||||
|
||||
55
apps/warehouse/pos/api/v1/api.py
Normal file
55
apps/warehouse/pos/api/v1/api.py
Normal 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 = ['']
|
||||
@@ -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
|
||||
)
|
||||
|
||||
|
||||
@@ -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))
|
||||
]
|
||||
|
||||
@@ -4,6 +4,7 @@ from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
path('web/api/', include('apps.warehouse.web.api.v1.urls')),
|
||||
path('pos/api/', include('apps.warehouse.pos.api.v1.urls')),
|
||||
path('excel/', include('apps.warehouse.services.excel.urls')),
|
||||
|
||||
]
|
||||
|
||||
@@ -53,8 +53,11 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
""" custom create of inventory entry """
|
||||
|
||||
# create inventory entry
|
||||
inventory_balance = request.data['weight']
|
||||
|
||||
request.data.update({
|
||||
'organization': (get_organization_by_user(request.user)).id
|
||||
'organization': (get_organization_by_user(request.user)).id,
|
||||
'balance': inventory_balance
|
||||
})
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
|
||||
@@ -19,6 +19,7 @@ class InventoryEntrySerializer(serializers.ModelSerializer):
|
||||
"organization",
|
||||
"distribution",
|
||||
"weight",
|
||||
"balance",
|
||||
"lading_number",
|
||||
"delivery_address",
|
||||
"is_confirmed",
|
||||
|
||||
Reference in New Issue
Block a user