diff --git a/apps/monitor/calc.py b/apps/monitor/calc.py index b96952a..5526fd4 100644 --- a/apps/monitor/calc.py +++ b/apps/monitor/calc.py @@ -64,7 +64,7 @@ class Calc: out["crypto"] = crypto with connection.cursor() as cursor: - sql = """SELECT SUM(amount_after_fee) as after_fee, SUM(amount) as amount FROM `transaction` WHERE amount_after_fee > 0 and crypto_currency=%s and owner_id=%s""" + sql = """SELECT SUM(crypto_amount) as crypto_amount, SUM(fiat_amount) as fiat_amount FROM `transaction` WHERE transaction_type = 'B' and crypto_currency=%s and owner_id=%s""" cursor.execute(sql, [crypto, userid]) bought = cursor.fetchone() if bought[0]: @@ -75,24 +75,24 @@ class Calc: out["bought"] = None with connection.cursor() as cursor: - sql = """SELECT SUM(amount_before_fee) as before_fee, SUM(amount) as amount FROM `transaction` WHERE amount_before_fee < 0 and crypto_currency=%s and owner_id=%s""" + sql = """SELECT SUM(crypto_amount) as crypto_amount, SUM(fiat_amount) as fiat_amount FROM `transaction` WHERE transaction_type = 'S' and crypto_currency=%s and owner_id=%s""" cursor.execute(sql, [crypto, userid]) sold = cursor.fetchone() if sold[0]: out["sold"] = sold[1] - total_investment += Decimal(sold[1]) - amount_kept += Decimal(sold[0]) + total_investment -= Decimal(sold[1]) + amount_kept -= Decimal(sold[0]) else: out["sold"] = None - # moving coins to other wallet with fees. + # calculate all fees. with connection.cursor() as cursor: - sql = """SELECT SUM(amount_after_fee) as after_fee FROM `transaction` WHERE amount_before_fee = 0 and crypto_currency=%s and owner_id=%s""" + sql = """SELECT SUM(crypto_fee) as crypto_fee, SUM(fiat_fee) as fiat_fee FROM `transaction` WHERE crypto_currency=%s and owner_id=%s""" cursor.execute(sql, [crypto, userid]) - sold = cursor.fetchone() + fees = cursor.fetchone() if sold[0]: - # total_investment -= Decimal(sold[1]) - amount_kept += Decimal(sold[0]) + total_investment -= Decimal(fees[1]) + amount_kept -= Decimal(fees[0]) rateEUR = ExchangeRate.objects.filter(crypto_currency=crypto, fiat_currency='EUR').order_by('-datetime_valid').first() rateUSD = ExchangeRate.objects.filter(crypto_currency=crypto, fiat_currency='USD').order_by('-datetime_valid').first() @@ -130,7 +130,7 @@ class Calc: amount_within_past_year = 0 with connection.cursor() as cursor: - sql = """SELECT SUM(amount_after_fee) as amount FROM `transaction` WHERE amount > 0 and crypto_currency=%s and owner_id=%s and date_valid>%s""" + sql = """SELECT SUM(crypto_amount) as amount FROM `transaction` WHERE transaction_type='B' and crypto_currency=%s and owner_id=%s and date_valid>%s""" cursor.execute(sql, [crypto, userid, datetime.datetime.now() - datetime.timedelta(days=365)]) bought = cursor.fetchone() out["bought_recently"] = None diff --git a/apps/transactions/migrations/0005_auto_20210511_1638.py b/apps/transactions/migrations/0005_auto_20210511_1638.py new file mode 100644 index 0000000..9fe2dae --- /dev/null +++ b/apps/transactions/migrations/0005_auto_20210511_1638.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.2 on 2021-05-11 16:38 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('transactions', '0004_auto_20210511_0703'), + ] + + operations = [ + migrations.RemoveField( + model_name='transaction', + name='amount', + ), + migrations.RemoveField( + model_name='transaction', + name='amount_after_fee', + ), + migrations.RemoveField( + model_name='transaction', + name='amount_before_fee', + ), + ] diff --git a/apps/transactions/models.py b/apps/transactions/models.py index 872bb33..11cd5ae 100644 --- a/apps/transactions/models.py +++ b/apps/transactions/models.py @@ -1,5 +1,25 @@ from django.db import models from django.contrib.auth.models import User +from decimal import Decimal, Context + +class NonscientificDecimalField(models.DecimalField): + """ Prevents values from being displayed with E notation, with trailing 0's + after the decimal place truncated. (This causes precision to be lost in + many cases, but is more user friendly and consistent for non-scientist + users) + """ + def value_from_object(self, obj): + def remove_exponent(val): + """Remove exponent and trailing zeros. + >>> remove_exponent(Decimal('5E+3')) + Decimal('5000') + """ + context = Context(prec=self.max_digits) + return val.quantize(Decimal(1), context=context) if val == val.to_integral() else val.normalize(context) + + val = super(NonscientificDecimalField, self).value_from_object(obj) + if isinstance(val, Decimal): + return remove_exponent(val) # Create your models here. class Transaction(models.Model): @@ -12,15 +32,12 @@ class Transaction(models.Model): trade_id = models.CharField(max_length=20, default='MANUAL') owner = models.ForeignKey(User, on_delete=models.CASCADE) crypto_currency = models.CharField(max_length=10) - crypto_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) - crypto_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) - amount_before_fee = models.DecimalField(max_digits=24, decimal_places=10) - amount_after_fee = models.DecimalField(max_digits=24, decimal_places=10) - exchange_rate = models.DecimalField(max_digits=24, decimal_places=10) + crypto_amount = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True) + crypto_fee = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True) + exchange_rate = NonscientificDecimalField(max_digits=24, decimal_places=10) fiat_currency = models.CharField(max_length=10) - fiat_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) - fiat_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) - amount = models.DecimalField(max_digits=24, decimal_places=10) + fiat_amount = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True) + fiat_fee = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True) date_valid = models.DateTimeField('date transfered') transaction_type = models.CharField(max_length=1, choices=TRANSACTION_TYPES, default='B') diff --git a/apps/transactions/templates/add.html b/apps/transactions/templates/add.html index faeb569..224ab67 100644 --- a/apps/transactions/templates/add.html +++ b/apps/transactions/templates/add.html @@ -28,6 +28,13 @@ +