if transaction status is success update item & if not, update everything
This commit is contained in:
@@ -30,6 +30,7 @@ from apps.warehouse.exceptions import (
|
||||
)
|
||||
from rest_framework import serializers
|
||||
from apps.herd.models import Rancher
|
||||
from django.db.models import Q
|
||||
from django.db import models
|
||||
|
||||
|
||||
@@ -121,29 +122,69 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
|
||||
|
||||
def create(self, validated_data):
|
||||
items_data = self.context['request'].data['items']
|
||||
rancher_code = validated_data.pop('rancher_national_code')
|
||||
with atomic():
|
||||
# get rancher with national code
|
||||
if 'rancher_national_code' in validated_data.keys():
|
||||
rancher = Rancher.objects.get(national_code=validated_data.pop('rancher_national_code'))
|
||||
validated_data.update({'rancher': rancher})
|
||||
rancher = None
|
||||
if rancher_code:
|
||||
rancher = Rancher.objects.get(national_code=rancher_code)
|
||||
validated_data['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.transaction_status_code = validated_data.get('transaction_status_code')
|
||||
obj.result_text = validated_data.get('result_text')
|
||||
obj.ref_num = validated_data.get('ref_num')
|
||||
obj.terminal = validated_data.get('terminal')
|
||||
obj.payer_cart = validated_data.get('payer_cart')
|
||||
obj.save(update_fields=['transaction_status'])
|
||||
transaction = transaction.first()
|
||||
|
||||
return obj
|
||||
# --- Case 1: success => only update items
|
||||
if transaction.transaction_status == 'success':
|
||||
for item_data in items_data:
|
||||
warehouse_models.InventoryQuotaSaleItem.objects.filter(
|
||||
Q(transaction=transaction) & (
|
||||
Q(free_product_id=item_data['free_product']) |
|
||||
Q(gov_product_id=item_data['gov_product'])
|
||||
)
|
||||
).update(**item_data)
|
||||
return transaction
|
||||
|
||||
# create transaction record
|
||||
# --- Case 2: not success => update transaction fields + items
|
||||
for field in [
|
||||
'transaction_status',
|
||||
'transaction_status_code',
|
||||
'result_text',
|
||||
'ref_num',
|
||||
'terminal',
|
||||
'payer_cart',
|
||||
'pos_date',
|
||||
'transaction_date',
|
||||
]:
|
||||
if field in validated_data:
|
||||
setattr(transaction, field, validated_data)
|
||||
|
||||
transaction.save(update_fields=[
|
||||
'transaction_status',
|
||||
'transaction_status_code',
|
||||
'result_text',
|
||||
'ref_num',
|
||||
'terminal',
|
||||
'payer_cart',
|
||||
'pos_date',
|
||||
'transaction_date',
|
||||
])
|
||||
|
||||
# items can change
|
||||
for item_data in items_data:
|
||||
warehouse_models.InventoryQuotaSaleItem.objects.filter(
|
||||
Q(transaction=transaction) & (
|
||||
Q(free_product_id=item_data['free_product']) |
|
||||
Q(gov_product_id=item_data['gov_product'])
|
||||
)
|
||||
).update(**item_data)
|
||||
|
||||
return transaction
|
||||
|
||||
# --- Case 3: create new transaction
|
||||
transaction = warehouse_models.InventoryQuotaSaleTransaction.objects.create(
|
||||
seller_organization=self.context['organization'],
|
||||
pos_device=self.context['pos_device'],
|
||||
@@ -154,12 +195,12 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
|
||||
total_price = 0
|
||||
for item_data in items_data:
|
||||
# get product by type
|
||||
gov_product = item_data.pop('gov_product') if 'gov_product' in item_data.keys() else None
|
||||
free_product = item_data.pop('free_product') if 'free_product' in item_data.keys() else None
|
||||
gov_product = item_data.pop('gov_product', None)
|
||||
free_product = item_data.pop('free_product', None)
|
||||
|
||||
distribution = QuotaDistribution.objects.get(
|
||||
distribution = QuotaDistribution.objects.filter(
|
||||
id=item_data.pop('quota_distribution')
|
||||
) if 'quota_distribution' in item_data.keys() else None
|
||||
).first() if 'quota_distribution' in item_data.keys() else None
|
||||
|
||||
# create item for transaction
|
||||
item = warehouse_models.InventoryQuotaSaleItem.objects.create(
|
||||
|
||||
Reference in New Issue
Block a user