add: income-expence update api

This commit is contained in:
behruz-dev
2025-09-25 17:16:45 +05:00
parent e93f91fdb1
commit dc079480e6
5 changed files with 154 additions and 4 deletions

View File

@@ -136,4 +136,54 @@ class ExpenceListSerializer(serializers.ModelSerializer):
class ExpenceDeleteSerializer(serializers.Serializer):
comment = serializers.CharField()
comment = serializers.CharField()
class ExpenceUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = Expence
fields = [
'project_folder', 'project', 'price', 'expence_type', '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.expence_type = validated_data.get('expence_type', instance.expence_type)
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.expence_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.expence_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.expence_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.expence_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

View File

@@ -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