free sale of inventory entry system deployment - add dhi state to rancher
This commit is contained in:
@@ -7,10 +7,13 @@ 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.product.exceptions import DistributionWeightException
|
||||
from apps.warehouse.services.services import create_extra_sale
|
||||
from apps.herd.pos.api.v1.serializers import RancherSerializer
|
||||
from apps.product.models import QuotaDistribution, Product
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from apps.core.models import SystemConfig
|
||||
from django.db.transaction import atomic
|
||||
from apps.warehouse.exceptions import (
|
||||
TotalInventorySaleException
|
||||
)
|
||||
@@ -99,6 +102,7 @@ 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__'
|
||||
@@ -106,60 +110,66 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
|
||||
|
||||
def create(self, validated_data):
|
||||
items_data = self.context['request'].data['items']
|
||||
with atomic():
|
||||
# get rancher with national code
|
||||
rancher = Rancher.objects.get(national_code=validated_data.pop('rancher_national_code'))
|
||||
validated_data.update({'rancher': rancher})
|
||||
|
||||
# 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,
|
||||
quota_distribution=QuotaDistribution.objects.get(
|
||||
id=item_data.pop('quota_distribution')
|
||||
),
|
||||
product=Product.objects.get(
|
||||
id=item_data.pop('product')
|
||||
),
|
||||
**item_data
|
||||
# if transaction exists, update transaction status
|
||||
transaction = self.Meta.model.objects.filter(
|
||||
transaction_id=validated_data.get('transaction_id')
|
||||
)
|
||||
total_price += item.total_price
|
||||
transaction.transaction_price = total_price
|
||||
transaction.save()
|
||||
if transaction.exists():
|
||||
obj = transaction.first()
|
||||
obj.transaction_status = validated_data.get('transaction_status')
|
||||
obj.save(update_fields=['transaction_status'])
|
||||
|
||||
return transaction
|
||||
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,
|
||||
quota_distribution=QuotaDistribution.objects.get(
|
||||
id=item_data.pop('quota_distribution')
|
||||
),
|
||||
product=Product.objects.get(
|
||||
id=item_data.pop('product')
|
||||
),
|
||||
**item_data
|
||||
)
|
||||
total_price += item.total_price
|
||||
|
||||
# create extra sale
|
||||
create_extra_sale(transaction=transaction, sale_item=item)
|
||||
|
||||
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
|
||||
distribution weight
|
||||
"""
|
||||
if 'quota_distribution' in attrs.keys():
|
||||
distribution = attrs['quota_distribution']
|
||||
|
||||
total_sale_weight = distribution.inventory_sales.aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
items = self.context['request'].data['items']
|
||||
for item in items:
|
||||
if 'quota_distribution' in item.keys():
|
||||
distribution = QuotaDistribution.objects.get(id=item.get('quota_distribution'))
|
||||
total_sale_weight = distribution.sale_items.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.weight:
|
||||
raise DistributionWeightException()
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
Reference in New Issue
Block a user