transaction system deploying
This commit is contained in:
@@ -84,7 +84,7 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDevice
|
||||
|
||||
|
||||
class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDeviceMixin):
|
||||
queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all()
|
||||
queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all().prefetch_related('items')
|
||||
serializer_class = warehouse_serializers.InventoryQuotaSaleTransactionSerializer
|
||||
permission_classes = [AllowAny]
|
||||
search_fields = [
|
||||
|
||||
@@ -8,11 +8,12 @@ from apps.product.services.services import (
|
||||
from apps.pos_device.services.services import pos_organizations_sharing_information
|
||||
from apps.pos_device.pos.api.v1.serializers.device import DeviceSerializer
|
||||
from apps.herd.pos.api.v1.serializers import RancherSerializer
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from apps.warehouse.exceptions import (
|
||||
TotalInventorySaleException
|
||||
)
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from rest_framework import serializers
|
||||
from apps.herd.models import Rancher
|
||||
from django.db import models
|
||||
|
||||
|
||||
@@ -95,25 +96,62 @@ class InventoryEntrySerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
|
||||
rancher_national_code = serializers.CharField(max_length=50, required=False)
|
||||
class Meta: # noqa
|
||||
model = warehouse_models.InventoryQuotaSaleTransaction
|
||||
fields = '__all__'
|
||||
depth = 0
|
||||
|
||||
def create(self, validated_data):
|
||||
items_data = self.context['request'].data['items']
|
||||
|
||||
# get rancher with national code
|
||||
rancher = Rancher.objects.get(national_code=validated_data.pop('rancher_national_code'))
|
||||
validated_data.update({'rancher': rancher})
|
||||
|
||||
# if transaction exists, update transaction status
|
||||
transaction = self.Meta.model.objects.filter(
|
||||
transaction_id=validated_data.get('transaction_id')
|
||||
)
|
||||
if transaction.exists():
|
||||
obj = transaction.first()
|
||||
obj.transaction_status = validated_data.get('transaction_status')
|
||||
obj.save(update_fields=['transaction_status'])
|
||||
|
||||
return obj
|
||||
|
||||
# create transaction record
|
||||
transaction = warehouse_models.InventoryQuotaSaleTransaction.objects.create(
|
||||
**validated_data
|
||||
)
|
||||
|
||||
# calculate total price of product items in shopping cart
|
||||
total_price = 0
|
||||
for item_data in items_data:
|
||||
item = warehouse_models.InventoryQuotaSaleItem.objects.create(
|
||||
transaction=transaction,
|
||||
**item_data
|
||||
)
|
||||
total_price += item.total_price
|
||||
transaction.transaction_price = total_price
|
||||
transaction.save()
|
||||
|
||||
return transaction
|
||||
|
||||
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']
|
||||
if 'quota_distribution' in attrs.keys():
|
||||
distribution = attrs['quota_distribution']
|
||||
|
||||
total_sale_weight = inventory_entry.inventory_sales.aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
total_sale_weight = distribution.inventory_sales.aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
|
||||
if total_sale_weight + attrs['weight'] > distribution.warehouse_balance:
|
||||
raise TotalInventorySaleException()
|
||||
if total_sale_weight + attrs['weight'] > distribution.warehouse_balance:
|
||||
raise TotalInventorySaleException()
|
||||
|
||||
return attrs
|
||||
|
||||
@@ -122,10 +160,29 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
representation['rancher'] = RancherSerializer(instance.rancher).data
|
||||
if instance.rancher:
|
||||
representation['rancher'] = RancherSerializer(instance.rancher).data
|
||||
representation['pos_device'] = DeviceSerializer(instance.pos_device).data
|
||||
representation['seller_organization'] = instance.seller_organization.name
|
||||
representation['inventory_entry'] = InventoryEntrySerializer(instance.inventory_entry).data
|
||||
if instance.seller_organization:
|
||||
representation['seller_organization'] = instance.seller_organization.name
|
||||
if instance.inventory_entry:
|
||||
representation['inventory_entry'] = InventoryEntrySerializer(instance.inventory_entry).data
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class InventoryQuotaSaleItemSerializer(serializers.ModelSerializer):
|
||||
product_name = serializers.CharField(source='product.name', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = warehouse_models.InventoryQuotaSaleItem
|
||||
fields = [
|
||||
'id',
|
||||
"transaction",
|
||||
"product",
|
||||
"product_name",
|
||||
"weight",
|
||||
"unit_price",
|
||||
"total_price",
|
||||
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user