91 lines
4.0 KiB
Python
91 lines
4.0 KiB
Python
from django.db import connection
|
|
from apps.transactions.models import Transaction
|
|
from apps.rates.models import ExchangeRate
|
|
from decimal import Decimal
|
|
import datetime
|
|
|
|
class Calc:
|
|
def ShowDiffRate(self, DayDiff, CurrentRate, Crypto, Fiat):
|
|
with connection.cursor() as cursor:
|
|
sql = """SELECT rate, datetime_valid FROM exchangerate WHERE crypto_currency = %s AND fiat_currency = %s
|
|
AND datetime_valid BETWEEN %s AND %s
|
|
ORDER BY datetime_valid DESC"""
|
|
startDate = datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=int(DayDiff))
|
|
endDate = startDate + datetime.timedelta(days=1)
|
|
cursor.execute(sql, [Crypto, Fiat, startDate, endDate])
|
|
rateMinusXDay = cursor.fetchone()
|
|
if rateMinusXDay:
|
|
return {"dateRelative": "%s days ago" % (DayDiff,), "date" : rateMinusXDay[1], "rateEUR": rateMinusXDay[0],
|
|
"diffPercentage": (CurrentRate.rate-Decimal(rateMinusXDay[0]))/Decimal(rateMinusXDay[0])*100, "rateUSD": None}
|
|
return None
|
|
|
|
def GetCurrentValue(self, userid, crypto, total_investment, current_value, total_tax_free):
|
|
amount_kept = 0
|
|
out = {}
|
|
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"""
|
|
cursor.execute(sql, [crypto, userid])
|
|
bought = cursor.fetchone()
|
|
if bought[0]:
|
|
out["bought"] = bought[1]
|
|
total_investment += Decimal(bought[1])
|
|
amount_kept += Decimal(bought[0])
|
|
else:
|
|
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"""
|
|
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])
|
|
else:
|
|
out["sold"] = None
|
|
|
|
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()
|
|
|
|
if rateEUR:
|
|
cv = Decimal(rateEUR.rate) * amount_kept
|
|
current_value += cv
|
|
out["current_value"] = cv
|
|
out["amount_kept"] = amount_kept
|
|
out["rateEUR"] = rateEUR.rate
|
|
else:
|
|
out["current_value"] = None
|
|
out["amount_kept"] = None
|
|
out["rateEUR"] = None
|
|
|
|
out["rates"] = []
|
|
if rateUSD and rateEUR:
|
|
out["rates"].append({"dateRelative": "Now", "date" : rateEUR.datetime_valid, "rateEUR": rateEUR.rate, "diffPercentage": None, "rateUSD": rateUSD.rate})
|
|
|
|
if rateEUR:
|
|
for daydiff in ["1", "3", "7", "14", "30"]:
|
|
diffrate = self.ShowDiffRate(daydiff, rateEUR, crypto, 'EUR')
|
|
if diffrate:
|
|
out["rates"].append(diffrate)
|
|
|
|
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"""
|
|
cursor.execute(sql, [crypto, userid, datetime.datetime.now() - datetime.timedelta(days=365)])
|
|
bought = cursor.fetchone()
|
|
out["bought_recently"] = None
|
|
if bought[0]:
|
|
out["bought_recently"] = bought[0]
|
|
amount_within_past_year = Decimal(bought[0])
|
|
amount_available_to_sell = amount_kept - amount_within_past_year
|
|
out["bought_tax_free"] = None
|
|
out["value_tax_free"] = None
|
|
if rateEUR:
|
|
out["bought_tax_free"] = amount_available_to_sell
|
|
out["value_tax_free"] = amount_available_to_sell * rateEUR.rate
|
|
total_tax_free += out["value_tax_free"]
|
|
|
|
return (total_investment, current_value, total_tax_free, rateEUR.rate, rateUSD.rate, out)
|