finance: fucking income expence
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.db import transaction
|
||||
|
||||
from rest_framework import generics, views, parsers, filters
|
||||
from rest_framework.response import Response
|
||||
@@ -84,33 +85,93 @@ class IncomeDeleteApiView(generics.GenericAPIView):
|
||||
permission_classes = [HasRolePermission]
|
||||
|
||||
def post(self, request, id):
|
||||
income = get_object_or_404(Income, id=id)
|
||||
income = get_object_or_404(Income, id=id, is_deleted=False)
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
comment = serializer.validated_data.get('comment')
|
||||
DeletedIncome.objects.create(
|
||||
income=income,
|
||||
comment=comment
|
||||
)
|
||||
income.is_deleted = True
|
||||
if income.currency == 'uzs':
|
||||
income.cash_transaction.expence_balance_uzs -= income.price
|
||||
income.cash_transaction.total_balance_uzs -= income.price
|
||||
income.payment_type.total_uzs -= income.price if income.payment_type.total_uzs > income.price else 0
|
||||
else:
|
||||
income.cash_transaction.expence_balance_usd -= income.price
|
||||
income.cash_transaction.total_balance_usd -= income.price
|
||||
income.payment_type.total_usd -= income.price if income.payment_type.total_usd > income.price else 0
|
||||
with transaction.atomic():
|
||||
comment = serializer.validated_data.get('comment')
|
||||
currency = income.currency.lower()
|
||||
|
||||
# Deleted record yaratish
|
||||
DeletedIncome.objects.create(
|
||||
income=income,
|
||||
comment=comment
|
||||
)
|
||||
|
||||
cash_transaction = income.cash_transaction
|
||||
payment_type = income.payment_type
|
||||
counterparty = income.counterparty
|
||||
|
||||
if currency == 'uzs':
|
||||
# Balanslarni qaytarish (o'chirilgani teskari qilish)
|
||||
cash_transaction.income_balance_uzs -= income.price # ✅ INCOME BALANCE KAMAYISHI KERAK
|
||||
cash_transaction.total_balance_uzs = (
|
||||
cash_transaction.income_balance_uzs - cash_transaction.expence_balance_uzs
|
||||
)
|
||||
payment_type.total_uzs -= income.price # ✅ QAYTARISH
|
||||
|
||||
# Kontrapartiya hisobini qaytarish
|
||||
if counterparty:
|
||||
if counterparty.kredit_uzs > 0:
|
||||
# Agar kredit bo'lsa, uni kamayitish (teskari)
|
||||
counterparty.kredit_uzs -= income.price
|
||||
counterparty.total_kredit -= income.price
|
||||
|
||||
counterparty.debit_uzs += income.price
|
||||
counterparty.total_debit += income.price
|
||||
else:
|
||||
# Agar debit bo'lsa, uni oshirish (teskari)
|
||||
counterparty.debit_uzs += income.price
|
||||
counterparty.total_debit += income.price
|
||||
|
||||
counterparty.save()
|
||||
|
||||
elif currency == 'usd':
|
||||
# Balanslarni qaytarish (o'chirilgani teskari qilish)
|
||||
cash_transaction.income_balance_usd -= income.price # ✅ INCOME BALANCE KAMAYISHI KERAK
|
||||
cash_transaction.total_balance_usd = (
|
||||
cash_transaction.income_balance_usd - cash_transaction.expence_balance_usd
|
||||
)
|
||||
payment_type.total_usd -= income.price # ✅ QAYTARISH
|
||||
|
||||
# Kontrapartiya hisobini qaytarish
|
||||
if counterparty:
|
||||
if counterparty.kredit_usd > 0:
|
||||
# Agar kredit bo'lsa, uni kamayitish (teskari)
|
||||
counterparty.kredit_usd -= income.price
|
||||
counterparty.total_kredit -= income.price
|
||||
|
||||
counterparty.debit_usd += income.price
|
||||
counterparty.total_debit += income.price
|
||||
else:
|
||||
# Agar debit bo'lsa, uni oshirish (teskari)
|
||||
counterparty.debit_usd += income.price
|
||||
counterparty.total_debit += income.price
|
||||
|
||||
counterparty.save()
|
||||
|
||||
# is_deleted = True qilish (hard delete emas, soft delete)
|
||||
income.is_deleted = True
|
||||
|
||||
# Barcha o'zgarishlari saqlash
|
||||
cash_transaction.save()
|
||||
payment_type.save()
|
||||
income.save()
|
||||
|
||||
return Response(
|
||||
{
|
||||
'success': True,
|
||||
'message': 'Income o\'chirildi',
|
||||
'data': {
|
||||
'income_id': income.id,
|
||||
'price': income.price,
|
||||
'currency': income.currency
|
||||
}
|
||||
},
|
||||
status=200
|
||||
)
|
||||
|
||||
income.cash_transaction.save()
|
||||
income.payment_type.save()
|
||||
income.save()
|
||||
return Response(
|
||||
{
|
||||
'success': True,
|
||||
'message': 'Income deleted',
|
||||
}, status=200
|
||||
)
|
||||
|
||||
|
||||
class IncomeUpdateApiView(generics.GenericAPIView):
|
||||
|
||||
Reference in New Issue
Block a user