add: income-expence update api
This commit is contained in:
@@ -134,4 +134,54 @@ class IncomeCreateSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class IncomeDeleteSerializer(serializers.Serializer):
|
||||
comment = serializers.CharField()
|
||||
comment = serializers.CharField()
|
||||
|
||||
|
||||
class IncomeUpdateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Income
|
||||
fields = [
|
||||
'project_folder', 'project', 'price', 'type_income', 'counterparty',
|
||||
'date', 'comment', 'audit', 'file',
|
||||
]
|
||||
extra_kwargs = {
|
||||
'price': {'required': False},
|
||||
}
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
old_price = instance.price
|
||||
instance.project_folder = validated_data.get('project_folder', instance.project_folder)
|
||||
instance.project = validated_data.get('project', instance.project)
|
||||
instance.price = validated_data.get('price', instance.price)
|
||||
instance.type_income = validated_data.get('type_income', instance.type_income)
|
||||
instance.counterparty = validated_data.get('counterparty', instance.counterparty)
|
||||
instance.date = validated_data.get('date', instance.date)
|
||||
instance.comment = validated_data.get('comment', instance.comment)
|
||||
instance.audit = validated_data.get('audit', instance.audit)
|
||||
instance.file = validated_data.get('file', instance.file)
|
||||
if validated_data.get('price'):
|
||||
cash_transaction = instance.cash_transaction
|
||||
payment_type = instance.payment_type
|
||||
if old_price > validated_data.get('price'):
|
||||
if instance.currency == 'uzs':
|
||||
cash_transaction.income_balance_uzs -= (old_price - validated_data.get('price'))
|
||||
cash_transaction.total_balance_uzs -= (old_price - validated_data.get('price'))
|
||||
payment_type.total_uzs -= (old_price - validated_data.get('price'))
|
||||
else:
|
||||
cash_transaction.income_balance_usd -= (old_price - validated_data.get('price'))
|
||||
cash_transaction.total_balance_usd -= (old_price - validated_data.get('price'))
|
||||
payment_type.total_usd -= (old_price - validated_data.get('price'))
|
||||
|
||||
elif old_price < validated_data.get('price'):
|
||||
if instance.currency == 'uzs':
|
||||
cash_transaction.income_balance_uzs += (old_price - validated_data.get('price'))
|
||||
cash_transaction.total_balance_uzs += (old_price - validated_data.get('price'))
|
||||
payment_type.total_uzs += (old_price - validated_data.get('price'))
|
||||
else:
|
||||
cash_transaction.income_balance_usd += (old_price - validated_data.get('price'))
|
||||
cash_transaction.total_balance_usd += (old_price - validated_data.get('price'))
|
||||
payment_type.total_usd += (old_price - validated_data.get('price'))
|
||||
cash_transaction.save()
|
||||
payment_type.save()
|
||||
instance.save()
|
||||
return instance
|
||||
Reference in New Issue
Block a user