finance: import income and expence commands added
This commit is contained in:
@@ -15,7 +15,6 @@ class ExpenceCreateSerializer(serializers.ModelSerializer):
|
||||
]
|
||||
|
||||
def validate(self, data):
|
||||
"""Balanslari tekshirish"""
|
||||
cash_transaction = data.get('cash_transaction')
|
||||
payment_type = data.get('payment_type')
|
||||
price = data.get('price')
|
||||
@@ -24,7 +23,6 @@ class ExpenceCreateSerializer(serializers.ModelSerializer):
|
||||
|
||||
final_price = price
|
||||
|
||||
# Payment type balansi tekshiruvi
|
||||
if currency == 'uzs' and payment_type.total_uzs < final_price:
|
||||
raise serializers.ValidationError(f"Yetarli UZS balansi yo'q. Mavjud: {payment_type.total_uzs}")
|
||||
elif currency == 'usd' and payment_type.total_usd < final_price:
|
||||
@@ -60,56 +58,43 @@ class ExpenceCreateSerializer(serializers.ModelSerializer):
|
||||
user = self.context.get('user')
|
||||
|
||||
if currency == 'uzs':
|
||||
# Cash transaction balansini yangilash
|
||||
cash_transaction.expence_balance_uzs += expence.price
|
||||
cash_transaction.total_balance_uzs = cash_transaction.income_balance_uzs - cash_transaction.expence_balance_uzs
|
||||
|
||||
# Payment type balansini kamayitish
|
||||
payment_type.total_uzs -= expence.price
|
||||
|
||||
# Kontrapartiya hisobini yangilash
|
||||
if expence.counterparty:
|
||||
if expence.counterparty.kredit_uzs > 0:
|
||||
# Kredit bo'lsa, uni qisqartirish
|
||||
expence.counterparty.kredit_uzs -= expence.price
|
||||
expence.counterparty.total_kredit -= expence.price
|
||||
|
||||
# Debit oshirish
|
||||
expence.counterparty.debit_uzs += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
else:
|
||||
# Kredit yo'q bo'lsa, to'g'ridan-to'g'ri debit oshirish
|
||||
expence.counterparty.debit_uzs += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
|
||||
expence.counterparty.save()
|
||||
|
||||
elif currency == 'usd':
|
||||
# Cash transaction balansini yangilash
|
||||
cash_transaction.expence_balance_usd += expence.price
|
||||
cash_transaction.total_balance_usd = cash_transaction.income_balance_usd - cash_transaction.expence_balance_usd
|
||||
|
||||
# Payment type balansini kamayitish
|
||||
payment_type.total_usd -= expence.price
|
||||
|
||||
# Kontrapartiya hisobini yangilash
|
||||
if expence.counterparty:
|
||||
if expence.counterparty.kredit_usd > 0:
|
||||
# Kredit bo'lsa, uni qisqartirish
|
||||
expence.counterparty.kredit_usd -= expence.price # ✅ TUZATILDI
|
||||
expence.counterparty.kredit_usd -= expence.price
|
||||
expence.counterparty.total_kredit -= expence.price
|
||||
|
||||
# Debit oshirish
|
||||
expence.counterparty.debit_usd += expence.price # ✅ TUZATILDI
|
||||
expence.counterparty.debit_usd += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
else:
|
||||
# Kredit yo'q bo'lsa, to'g'ridan-to'g'ri debit oshirish
|
||||
expence.counterparty.debit_usd += expence.price # ✅ TUZATILDI
|
||||
expence.counterparty.debit_usd += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
|
||||
expence.counterparty.save()
|
||||
|
||||
# Bildirishnoma yuborish
|
||||
body = f"""{user.full_name} {expence.price} {expence.currency.upper()}... \n screen: /monitoring"""
|
||||
data = {
|
||||
"screen": "/monitoring",
|
||||
@@ -200,7 +185,6 @@ class ExpenceUpdateSerializer(serializers.ModelSerializer):
|
||||
}
|
||||
|
||||
def validate_price(self, value):
|
||||
"""Narxi manfiy bo'lmasligi tekshiruvi"""
|
||||
if value and value < 0:
|
||||
raise serializers.ValidationError("Narxi manfiy bo'lishi mumkin emas")
|
||||
return value
|
||||
@@ -213,7 +197,6 @@ class ExpenceUpdateSerializer(serializers.ModelSerializer):
|
||||
new_counterparty = validated_data.get('counterparty', instance.counterparty)
|
||||
currency = instance.currency.lower()
|
||||
|
||||
# Asosiy ma'lumotlarni yangilash
|
||||
instance.project_folder = validated_data.get('project_folder', instance.project_folder)
|
||||
instance.project = validated_data.get('project', instance.project)
|
||||
instance.price = new_price
|
||||
@@ -224,38 +207,31 @@ class ExpenceUpdateSerializer(serializers.ModelSerializer):
|
||||
instance.audit = validated_data.get('audit', instance.audit)
|
||||
instance.file = validated_data.get('file', instance.file)
|
||||
|
||||
# Agar narxi o'zgarsa, balanslarni yangilash
|
||||
if validated_data.get('price') and old_price != new_price:
|
||||
price_difference = new_price - old_price # ✅ To'g'ri farq
|
||||
price_difference = new_price - old_price
|
||||
cash_transaction = instance.cash_transaction
|
||||
payment_type = instance.payment_type
|
||||
|
||||
if currency == 'uzs':
|
||||
# Cash transaction balansini yangilash
|
||||
cash_transaction.expence_balance_uzs += price_difference
|
||||
cash_transaction.total_balance_uzs = (
|
||||
cash_transaction.income_balance_uzs - cash_transaction.expence_balance_uzs
|
||||
)
|
||||
|
||||
# Payment type balansini yangilash
|
||||
payment_type.total_uzs -= price_difference # ✅ Narx oshsa, balans kamayadi
|
||||
payment_type.total_uzs -= price_difference
|
||||
|
||||
elif currency == 'usd':
|
||||
# Cash transaction balansini yangilash
|
||||
cash_transaction.expence_balance_usd += price_difference
|
||||
cash_transaction.total_balance_usd = (
|
||||
cash_transaction.income_balance_usd - cash_transaction.expence_balance_usd
|
||||
)
|
||||
|
||||
# Payment type balansini yangilash
|
||||
payment_type.total_usd -= price_difference # ✅ Narx oshsa, balans kamayadi
|
||||
payment_type.total_usd -= price_difference
|
||||
|
||||
cash_transaction.save()
|
||||
payment_type.save()
|
||||
|
||||
# Agar kontrapartiya o'zgarsa, uning hisobini yangilash
|
||||
if new_counterparty != old_counterparty:
|
||||
# Eski kontrapartyani qaytarish
|
||||
if old_counterparty:
|
||||
if currency == 'uzs':
|
||||
if old_counterparty.debit_uzs > 0:
|
||||
@@ -274,7 +250,6 @@ class ExpenceUpdateSerializer(serializers.ModelSerializer):
|
||||
|
||||
old_counterparty.save()
|
||||
|
||||
# Yangi kontrapartyani yangilash
|
||||
if new_counterparty:
|
||||
if currency == 'uzs':
|
||||
if new_counterparty.kredit_uzs > 0:
|
||||
|
||||
Reference in New Issue
Block a user