some apis of pos
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user