change price type int -> decimal field
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db import models
|
||||
|
||||
from core.apps.shared.models import BaseModel, Region, District
|
||||
@@ -41,24 +43,71 @@ class Counterparty(BaseModel):
|
||||
district = models.ForeignKey(
|
||||
District, on_delete=models.SET_NULL, null=True, blank=True, related_name='counterparties'
|
||||
)
|
||||
balance = models.BigIntegerField(null=True, blank=True)
|
||||
balance_currency = models.CharField(
|
||||
max_length=3, choices=[('usd', 'usd'), ('uzs', 'uzs')], null=True, blank=True
|
||||
)
|
||||
balance_date = models.DateField(null=True, blank=True)
|
||||
comment = models.TextField(null=True, blank=True)
|
||||
is_archived = models.BooleanField(default=False)
|
||||
|
||||
debit_usd = models.BigIntegerField(default=0, null=True, blank=True)
|
||||
debit_uzs = models.BigIntegerField(default=0, null=True, blank=True)
|
||||
total_debit = models.BigIntegerField(default=0, null=True, blank=True)
|
||||
kredit_usd = models.BigIntegerField(default=0, null=True, blank=True)
|
||||
kredit_uzs = models.BigIntegerField(default=0, null=True, blank=True)
|
||||
total_kredit = models.BigIntegerField(default=0, null=True, blank=True)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'Kontragent'
|
||||
verbose_name_plural = 'Kontragentlar'
|
||||
|
||||
|
||||
class CounterpartyBalance(BaseModel):
|
||||
counterparty = models.OneToOneField(Counterparty, on_delete=models.CASCADE, related_name='balance')
|
||||
# balance
|
||||
balance_uzs = models.DecimalField(max_digits=15, decimal_places=2, default=0.00)
|
||||
balance_usd = models.DecimalField(max_digits=15, decimal_places=2, default=0.00)
|
||||
# balance date
|
||||
balance_date = models.DateField(null=True, blank=True)
|
||||
# kreditor -> qazrdorlik
|
||||
kredit_usd = models.DecimalField(max_digits=15, decimal_places=2, default=0.00)
|
||||
kredit_uzs = models.DecimalField(max_digits=15, decimal_places=2, default=0.00)
|
||||
# debitor -> xaqdorlik
|
||||
debit_usd = models.DecimalField(max_digits=15, decimal_places=2, default=0.00)
|
||||
debit_uzs = models.DecimalField(max_digits=15, decimal_places=2, default=0.00)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.balance_usd > 0:
|
||||
self.debit_usd = self.balance_usd
|
||||
self.kredit_usd = Decimal('0.00')
|
||||
elif self.balance_usd < 0:
|
||||
self.kredit_usd = abs(self.balance_usd)
|
||||
self.debit_usd = Decimal('0.00')
|
||||
|
||||
if self.balance_uzs > 0:
|
||||
self.debit_uzs = self.balance_uzs
|
||||
self.kredit_uzs = Decimal('0.00')
|
||||
elif self.balance_uzs < 0:
|
||||
self.kredit_uzs = abs(self.balance_uzs)
|
||||
self.debit_uzs = Decimal('0.00')
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
total_usd = self.total_balance_usd
|
||||
total_uzs = self.total_balance_uzs
|
||||
|
||||
if total_usd < 0 or total_uzs < 0:
|
||||
self.counterparty.status = 'CREDITOR'
|
||||
elif total_usd > 0 or total_uzs > 0:
|
||||
self.counterparty.status = 'DEBITOR'
|
||||
else:
|
||||
self.counterparty.status = 'CREDITOR'
|
||||
|
||||
self.counterparty.save(update_fields=['status'])
|
||||
|
||||
@property
|
||||
def total_balance_usd(self):
|
||||
return (self.debit_usd or Decimal(0)) - (self.kredit_usd or Decimal(0))
|
||||
|
||||
@property
|
||||
def total_balance_uzs(self):
|
||||
return (self.debit_uzs or Decimal(0)) - (self.kredit_uzs or Decimal(0))
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.counterparty.name} | USD: {self.total_balance_usd} | UZS: {self.total_balance_uzs}"
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Kontragent Balansi"
|
||||
verbose_name_plural = "Kontragent Balanslari"
|
||||
Reference in New Issue
Block a user