add: income-expence update api
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user