drop old fields, make use of new fields.
display decimal with 0 properly, non scientific
This commit is contained in:
+10
-10
@@ -64,7 +64,7 @@ class Calc:
|
|||||||
out["crypto"] = crypto
|
out["crypto"] = crypto
|
||||||
|
|
||||||
with connection.cursor() as cursor:
|
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])
|
cursor.execute(sql, [crypto, userid])
|
||||||
bought = cursor.fetchone()
|
bought = cursor.fetchone()
|
||||||
if bought[0]:
|
if bought[0]:
|
||||||
@@ -75,24 +75,24 @@ class Calc:
|
|||||||
out["bought"] = None
|
out["bought"] = None
|
||||||
|
|
||||||
with connection.cursor() as cursor:
|
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])
|
cursor.execute(sql, [crypto, userid])
|
||||||
sold = cursor.fetchone()
|
sold = cursor.fetchone()
|
||||||
if sold[0]:
|
if sold[0]:
|
||||||
out["sold"] = sold[1]
|
out["sold"] = sold[1]
|
||||||
total_investment += Decimal(sold[1])
|
total_investment -= Decimal(sold[1])
|
||||||
amount_kept += Decimal(sold[0])
|
amount_kept -= Decimal(sold[0])
|
||||||
else:
|
else:
|
||||||
out["sold"] = None
|
out["sold"] = None
|
||||||
|
|
||||||
# moving coins to other wallet with fees.
|
# calculate all fees.
|
||||||
with connection.cursor() as cursor:
|
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])
|
cursor.execute(sql, [crypto, userid])
|
||||||
sold = cursor.fetchone()
|
fees = cursor.fetchone()
|
||||||
if sold[0]:
|
if sold[0]:
|
||||||
# total_investment -= Decimal(sold[1])
|
total_investment -= Decimal(fees[1])
|
||||||
amount_kept += Decimal(sold[0])
|
amount_kept -= Decimal(fees[0])
|
||||||
|
|
||||||
rateEUR = ExchangeRate.objects.filter(crypto_currency=crypto, fiat_currency='EUR').order_by('-datetime_valid').first()
|
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()
|
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
|
amount_within_past_year = 0
|
||||||
with connection.cursor() as cursor:
|
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)])
|
cursor.execute(sql, [crypto, userid, datetime.datetime.now() - datetime.timedelta(days=365)])
|
||||||
bought = cursor.fetchone()
|
bought = cursor.fetchone()
|
||||||
out["bought_recently"] = None
|
out["bought_recently"] = None
|
||||||
|
|||||||
@@ -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',
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -1,5 +1,25 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import User
|
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.
|
# Create your models here.
|
||||||
class Transaction(models.Model):
|
class Transaction(models.Model):
|
||||||
@@ -12,15 +32,12 @@ class Transaction(models.Model):
|
|||||||
trade_id = models.CharField(max_length=20, default='MANUAL')
|
trade_id = models.CharField(max_length=20, default='MANUAL')
|
||||||
owner = models.ForeignKey(User, on_delete=models.CASCADE)
|
owner = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
crypto_currency = models.CharField(max_length=10)
|
crypto_currency = models.CharField(max_length=10)
|
||||||
crypto_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
crypto_amount = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
||||||
crypto_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
crypto_fee = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
||||||
amount_before_fee = models.DecimalField(max_digits=24, decimal_places=10)
|
exchange_rate = NonscientificDecimalField(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)
|
|
||||||
fiat_currency = models.CharField(max_length=10)
|
fiat_currency = models.CharField(max_length=10)
|
||||||
fiat_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
fiat_amount = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
||||||
fiat_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
fiat_fee = NonscientificDecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
|
||||||
amount = models.DecimalField(max_digits=24, decimal_places=10)
|
|
||||||
date_valid = models.DateTimeField('date transfered')
|
date_valid = models.DateTimeField('date transfered')
|
||||||
transaction_type = models.CharField(max_length=1, choices=TRANSACTION_TYPES, default='B')
|
transaction_type = models.CharField(max_length=1, choices=TRANSACTION_TYPES, default='B')
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,13 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group row">
|
||||||
|
<label class="col-sm-2 col-form-label">Transaction Type:</label>
|
||||||
|
<div class="col-sm-4">
|
||||||
|
{{ form.transaction_type }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Trade ID:</label>
|
<label class="col-sm-2 col-form-label">Trade ID:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
@@ -43,16 +50,16 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Amount before fee:</label>
|
<label class="col-sm-2 col-form-label">Crypto Amount:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.amount_before_fee }}
|
{{ form.crypto_amount }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Amount after fee:</label>
|
<label class="col-sm-2 col-form-label">Crypto fee:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.amount_after_fee }}
|
{{ form.crypto_fee }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -73,7 +80,14 @@
|
|||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Fiat Amount:</label>
|
<label class="col-sm-2 col-form-label">Fiat Amount:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.amount }}
|
{{ form.fiat_amount }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group row">
|
||||||
|
<label class="col-sm-2 col-form-label">Fiat Fee:</label>
|
||||||
|
<div class="col-sm-4">
|
||||||
|
{{ form.fiat_fee }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -29,24 +29,31 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group row">
|
||||||
|
<label class="col-sm-2 col-form-label">Transaction Type:</label>
|
||||||
|
<div class="col-sm-4">
|
||||||
|
{{ form.transaction_type }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Crypto Currency:</label>
|
<label class="col-sm-2 col-form-label">Crypto Currency:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.crypto_currency }}
|
{{ form.crypto_currency }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Amount before fee:</label>
|
<label class="col-sm-2 col-form-label">Crypto Amount:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.amount_before_fee }}
|
{{ form.crypto_amount }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Amount after fee:</label>
|
<label class="col-sm-2 col-form-label">Crypto fee:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.amount_after_fee }}
|
{{ form.crypto_fee }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -67,14 +74,21 @@
|
|||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">Fiat Amount:</label>
|
<label class="col-sm-2 col-form-label">Fiat Amount:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.amount }}
|
{{ form.fiat_amount }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group row">
|
||||||
|
<label class="col-sm-2 col-form-label">Fiat Fee:</label>
|
||||||
|
<div class="col-sm-4">
|
||||||
|
{{ form.fiat_fee }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-2 col-form-label">{{ form.date_valid.label }}:</label>
|
<label class="col-sm-2 col-form-label">{{ form.date_valid.label }}:</label>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-4">
|
||||||
{{ form.date_valid }}
|
{{ form.date_valid }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -22,13 +22,19 @@
|
|||||||
{% for transaction in transactions %}
|
{% for transaction in transactions %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ transaction.date_valid }}</td>
|
<td>{{ transaction.date_valid }}</td>
|
||||||
<td>{{ transaction.amount_before_fee|floatformat:3 }} {{ transaction.crypto_currency }}</td>
|
<td>{{ transaction.crypto_amount|floatformat:3 }}
|
||||||
<td>{{ transaction.amount|formatcurrency }} {{ transaction.fiat_currency }}</td>
|
{% if transaction.crypto_fee > 0 %}Fee: {{ transaction.crypto_fee|floatformat:3 }}{% endif %}
|
||||||
<td>{% if transaction.amount < 0 %}
|
{{ transaction.crypto_currency }}
|
||||||
|
</td>
|
||||||
|
<td>{{ transaction.fiat_amount|formatcurrency }}
|
||||||
|
{% if transaction.fiat_fee > 0 %}Fee: {{ transaction.fiat_fee|floatformat:2 }}{% endif %}
|
||||||
|
{{ transaction.fiat_currency }}
|
||||||
|
</td>
|
||||||
|
<td>{% if transaction.transaction_type == 'S' %}
|
||||||
{{ transaction.exchange_rate|formatcurrency }} {{ transaction.crypto_currency }}/{{ transaction.fiat_currency }}
|
{{ transaction.exchange_rate|formatcurrency }} {{ transaction.crypto_currency }}/{{ transaction.fiat_currency }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>{% if transaction.amount > 0 %}
|
<td>{% if transaction.transaction_type == 'B' %}
|
||||||
{{ transaction.exchange_rate|formatcurrency }} {{ transaction.crypto_currency }}/{{ transaction.fiat_currency }}
|
{{ transaction.exchange_rate|formatcurrency }} {{ transaction.crypto_currency }}/{{ transaction.fiat_currency }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user