finance: fucking income expence
This commit is contained in:
@@ -381,126 +381,154 @@ class PartyExpenceCreateSerializer(serializers.ModelSerializer):
|
||||
"party",
|
||||
]
|
||||
|
||||
def validate(self, data):
|
||||
"""Validasiya qilish"""
|
||||
price = data.get('price')
|
||||
exchange_rate = data.get('exchange_rate')
|
||||
party = data.get('party')
|
||||
currency = data.get('currency', 'uzs').lower()
|
||||
|
||||
if price and price < 0:
|
||||
raise serializers.ValidationError("Narxi manfiy bo'lishi mumkin emas")
|
||||
|
||||
if exchange_rate and exchange_rate < 0:
|
||||
raise serializers.ValidationError("Kurs manfiy bo'lishi mumkin emas")
|
||||
|
||||
if not party:
|
||||
raise serializers.ValidationError("Party talab qilinadi")
|
||||
|
||||
# USD kursi tekshiruvi
|
||||
try:
|
||||
usd_course = UsdCourse.objects.first()
|
||||
if not usd_course or not usd_course.value:
|
||||
raise serializers.ValidationError("USD kursi topilmadi")
|
||||
except UsdCourse.DoesNotExist:
|
||||
raise serializers.ValidationError("USD kursi topilmadi")
|
||||
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
usd_value = UsdCourse.objects.first().value
|
||||
# USD kursi olish
|
||||
usd_course = UsdCourse.objects.first()
|
||||
if not usd_course:
|
||||
raise serializers.ValidationError("USD kursi topilmadi")
|
||||
|
||||
usd_value = usd_course.value
|
||||
|
||||
# Exchange rate va final price hisoblash
|
||||
exchange_rate = validated_data.get("exchange_rate") or 1
|
||||
final_price = validated_data.get("price") * exchange_rate
|
||||
currency = validated_data.get("currency", "uzs").lower()
|
||||
|
||||
expence = Expence.objects.create(
|
||||
cash_transaction=validated_data.get("cash_transaction"),
|
||||
payment_type=validated_data.get("payment_type"),
|
||||
project_folder=validated_data.get("project_folder"),
|
||||
project=validated_data.get("project"),
|
||||
counterparty=validated_data.get("counterparty"),
|
||||
price=(
|
||||
validated_data.get("price") * validated_data.get("exchange_rate")
|
||||
if validated_data.get("exchange_rate")
|
||||
else validated_data.get("price")
|
||||
),
|
||||
exchange_rate=validated_data.get("exchange_rate"),
|
||||
currency=validated_data.get("currency"),
|
||||
price=final_price,
|
||||
exchange_rate=exchange_rate,
|
||||
currency=currency,
|
||||
date=validated_data.get("date"),
|
||||
comment=validated_data.get("comment"),
|
||||
party=validated_data.get("party"),
|
||||
user=self.context.get('user'),
|
||||
)
|
||||
|
||||
cash_transaction = expence.cash_transaction
|
||||
payment_type = expence.payment_type
|
||||
|
||||
if validated_data.get("currency") == "uzs":
|
||||
party = expence.party
|
||||
|
||||
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 yangilash
|
||||
if payment_type.total_uzs > expence.price:
|
||||
payment_type.total_uzs -= expence.price
|
||||
|
||||
|
||||
# Kontrapartiya hisobini yangilash
|
||||
if expence.counterparty:
|
||||
if expence.counterparty.kredit_uzs != 0:
|
||||
if expence.counterparty.kredit_uzs > 0:
|
||||
expence.counterparty.kredit_uzs -= expence.price
|
||||
expence.counterparty.total_kredit -= expence.price
|
||||
|
||||
expence.counterparty.debit_uzs += (
|
||||
expence.counterparty.kredit_uzs - expence.price
|
||||
)
|
||||
|
||||
# ✅ TUZATILDI: Debit to'g'ridan-to'g'ri oshadi
|
||||
expence.counterparty.debit_uzs += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
else:
|
||||
expence.counterparty.debit_uzs += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
|
||||
expence.counterparty.save()
|
||||
if expence.party.currency == "uzs":
|
||||
expence.party.party_amount.payment_amount -= expence.price
|
||||
expence.party.party_amount.paid_amount += expence.price
|
||||
expence.party.party_amount.save()
|
||||
expence.party.save()
|
||||
elif expence.party.currency == 'usd':
|
||||
expence.party.party_amount.payment_amount -= round(expence.price / usd_value)
|
||||
expence.party.party_amount.paid_amount += round(expence.price / usd_value)
|
||||
expence.party.save()
|
||||
elif validated_data.get("currency") == "usd":
|
||||
|
||||
# Party hisobini yangilash
|
||||
if party:
|
||||
if party.currency == "uzs":
|
||||
# ✅ Birjani to'g'ri hisoblash
|
||||
party.party_amount.payment_amount -= expence.price
|
||||
party.party_amount.paid_amount += expence.price
|
||||
party.party_amount.save()
|
||||
elif party.currency == 'usd':
|
||||
# ✅ USD kursi bilan to'g'ri konvertatsiya
|
||||
converted_price = round(expence.price / usd_value)
|
||||
party.party_amount.payment_amount -= converted_price
|
||||
party.party_amount.paid_amount += converted_price
|
||||
party.party_amount.save()
|
||||
|
||||
party.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 yangilash
|
||||
if payment_type.total_usd > expence.price:
|
||||
payment_type.total_usd -= expence.price
|
||||
|
||||
|
||||
# Kontrapartiya hisobini yangilash
|
||||
if expence.counterparty:
|
||||
if expence.counterparty.kredit_usd != 0:
|
||||
expence.counterparty.kredit_usd -= validated_data.get("price")
|
||||
if expence.counterparty.kredit_usd > 0:
|
||||
# ✅ TUZATILDI: expence.price ishlatish
|
||||
expence.counterparty.kredit_usd -= expence.price
|
||||
expence.counterparty.total_kredit -= expence.price
|
||||
|
||||
expence.counterparty.debit_usd += (
|
||||
expence.counterparty.kredit_usd
|
||||
- validated_data.get("price")
|
||||
)
|
||||
|
||||
# ✅ TUZATILDI: Debit to'g'ridan-to'g'ri oshadi
|
||||
expence.counterparty.debit_usd += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
else:
|
||||
expence.counterparty.debit_usd += validated_data.get("price")
|
||||
expence.counterparty.debit_usd += expence.price
|
||||
expence.counterparty.total_debit += expence.price
|
||||
|
||||
expence.counterparty.save()
|
||||
|
||||
if expence.party.currency == "usd":
|
||||
expence.party.party_amount.payment_amount -= validated_data.get(
|
||||
"price"
|
||||
)
|
||||
expence.party.party_amount.paid_amount += validated_data.get(
|
||||
"price"
|
||||
)
|
||||
expence.party.save()
|
||||
elif expence.party.currency == "uzs":
|
||||
expence.party.party_amount.payment_amount -= validated_data.get(
|
||||
"price"
|
||||
) * usd_value
|
||||
expence.party.party_amount.paid_amount += validated_data.get(
|
||||
"price"
|
||||
) * usd_value
|
||||
expence.party.save()
|
||||
expence.party.party_amount.save()
|
||||
|
||||
# Party hisobini yangilash
|
||||
if party:
|
||||
if party.currency == "usd":
|
||||
# ✅ TUZATILDI: expence.price ishlatish
|
||||
party.party_amount.payment_amount -= expence.price
|
||||
party.party_amount.paid_amount += expence.price
|
||||
party.party_amount.save()
|
||||
elif party.currency == "uzs":
|
||||
# ✅ TUZATILDI: expence.price bilan konvertatsiya
|
||||
converted_price = expence.price * usd_value
|
||||
party.party_amount.payment_amount -= converted_price
|
||||
party.party_amount.paid_amount += converted_price
|
||||
party.party_amount.save()
|
||||
|
||||
party.save()
|
||||
|
||||
# Barcha o'zgarishlari saqlash
|
||||
cash_transaction.save()
|
||||
payment_type.save()
|
||||
|
||||
return expence
|
||||
|
||||
|
||||
class PartyPaymentHistorySerializer(serializers.ModelSerializer):
|
||||
counterparty = serializers.SerializerMethodField(method_name='get_counterparty')
|
||||
user = serializers.SerializerMethodField(method_name='get_user')
|
||||
|
||||
class Meta:
|
||||
model = Expence
|
||||
fields = [
|
||||
'id', 'counterparty', 'price', 'created_at', 'date', 'user'
|
||||
]
|
||||
|
||||
def get_counterparty(self, obj):
|
||||
return {
|
||||
'id': obj.counterparty.id,
|
||||
'name': obj.counterparty.name,
|
||||
} if obj.counterparty else None
|
||||
|
||||
def get_user(self, obj):
|
||||
return {
|
||||
'id': obj.user.id,
|
||||
'full_name': obj.user.full_name,
|
||||
'profile_image': obj.user.profile_image.url if obj.user.profile_image else None
|
||||
} if obj.user else None
|
||||
Reference in New Issue
Block a user