account balance

This commit is contained in:
forest 2020-05-12 00:45:37 -05:00
parent 54948a386b
commit d06e07cfd3
5 changed files with 108 additions and 7 deletions

View File

@ -152,6 +152,7 @@ def create():
return render_template( return render_template(
"create-capsul.html", "create-capsul.html",
created_os=created_os, created_os=created_os,
account_balance=format(account_balance, '.2f'),
ssh_public_keys=ssh_public_keys, ssh_public_keys=ssh_public_keys,
ssh_public_key_count=len(ssh_public_keys), ssh_public_key_count=len(ssh_public_keys),
no_ssh_public_keys=len(ssh_public_keys) == 0, no_ssh_public_keys=len(ssh_public_keys) == 0,
@ -214,12 +215,14 @@ def get_payments():
g.user_payments = get_model().list_payments_for_account(session["account"]) g.user_payments = get_model().list_payments_for_account(session["account"])
return g.user_payments return g.user_payments
average_number_of_days_in_a_month = 30.44
def get_account_balance(): def get_account_balance():
average_number_of_days_in_a_month = 30.44
vm_cost_dollars = 0.0 vm_cost_dollars = 0.0
for vm in get_vms(): for vm in get_vms():
end_datetime = vm["deleted"] if vm["deleted"] else datetime.now() end_datetime = vm["deleted"] if vm["deleted"] else datetime.utcnow()
vm_months = ( end_datetime - vm["created"] ).days / average_number_of_days_in_a_month vm_months = ( end_datetime - vm["created"] ).days / average_number_of_days_in_a_month
vm_cost_dollars += vm_months * float(vm["dollars_per_month"]) vm_cost_dollars += vm_months * float(vm["dollars_per_month"])
@ -230,11 +233,30 @@ def get_account_balance():
@bp.route("/account-balance") @bp.route("/account-balance")
@account_required @account_required
def account_balance(): def account_balance():
errors = list()
payments = get_payments() payments = get_payments()
account_balance = get_account_balance() account_balance = get_account_balance()
vms_billed = list()
for vm in get_vms():
end_datetime = vm["deleted"] if vm["deleted"] else datetime.utcnow()
print(end_datetime)
print(vm["created"])
vm_months = (end_datetime - vm["created"]).days / average_number_of_days_in_a_month
vms_billed.append(dict(
id=vm["id"],
dollars_per_month=vm["dollars_per_month"],
created=vm["created"].strftime("%b %d %Y"),
deleted=vm["deleted"].strftime("%b %d %Y") if vm["deleted"] else "N/A",
months=format(vm_months, '.3f'),
dollars=format(vm_months * float(vm["dollars_per_month"]), '.2f')
))
return render_template( return render_template(
"account-balance.html", "account-balance.html",
payments=payments, has_payments=len(payments)>0, account_balance=account_balance has_vms=len(vms_billed)>0,
vms_billed=vms_billed,
payments=list(map(lambda x: dict(dollars=x["dollars"], created=x["created"].strftime("%b %d %Y")), payments)),
has_payments=len(payments)>0,
account_balance=format(account_balance, '.2f')
) )

View File

@ -83,6 +83,6 @@ INSERT INTO accounts (email)
VALUES ('forest.n.johnson@gmail.com'); VALUES ('forest.n.johnson@gmail.com');
INSERT INTO payments (email, dollars, created) INSERT INTO payments (email, dollars, created)
VALUES ('forest.n.johnson@gmail.com', 100.00, TO_TIMESTAMP('2020-05-05','YYYY-MM-DD')); VALUES ('forest.n.johnson@gmail.com', 20.00, TO_TIMESTAMP('2020-05-05','YYYY-MM-DD'));
UPDATE schemaversion SET version = 2; UPDATE schemaversion SET version = 2;

View File

@ -170,7 +170,12 @@ thead {
background: #bdc7b812; background: #bdc7b812;
} }
td, th { td, th {
padding: 0.2em 1em; font: calc(0.40rem + 1vmin) monospace;
padding: 0.1em 1em;
}
table.small td, table.small th {
font: calc(0.35rem + 0.83vmin) monospace;
padding: 0.1em 1em;
} }
th { th {
border-right: 4px solid #241e1e; border-right: 4px solid #241e1e;

View File

@ -4,10 +4,82 @@
{% block content %} {% block content %}
<div class="third-margin"> <div class="third-margin">
<h1>Account Balance</h1> <h1>Account Balance: ${{ account_balance }}</h1>
</div> </div>
<div class="half-margin"> <div class="half-margin">
<div class="row">
{% if has_payments %}
<div>
<div class="third-margin">
<h1>Payments</h1>
</div>
<table>
<thead>
<tr>
<th>amount</th>
<th>date</th>
</tr>
</thead>
<tbody>
{% for payment in payments %}
<tr>
<td>${{ payment["dollars"] }}</td>
<td>{{ payment["created"] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
<ul>
<li>
<h1>PAYMENT OPTIONS</h1>
<ul>
<li>
<a href="/console/stripe">Add funds with Credit/Debit (stripe)</a>
<ul><li>notice: stripe will load nonfree javascript </li></ul>
</li>
<li><a href="/console/btcpay">Add funds with Bitcoin/Litecoin/Monero (btcpay)</a></li>
<li>Cash: email treasurer@cyberia.club</li>
</ul>
</li>
</ul>
</div>
{% if has_vms %}
<div class="third-margin">
<h1>Capsuls Billed</h1>
</div>
<table class="small">
<thead>
<tr>
<th>id</th>
<th>created</th>
<th>deleted</th>
<th>$/month</th>
<th>months</th>
<th>$ billed</th>
</tr>
</thead>
<tbody>
{% for vm in vms_billed %}
<tr>
<td>{{ vm["id"] }}</td>
<td>{{ vm["created"] }}</td>
<td>{{ vm["deleted"] }}</td>
<td>${{ vm["dollars_per_month"] }}</td>
<td>{{ vm["months"] }}</td>
<td>${{ vm["dollars"] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}

View File

@ -45,6 +45,8 @@
* net is calculated as a per-month average * net is calculated as a per-month average
* all VMs come standard with one public IPv4 addr</pre> * all VMs come standard with one public IPv4 addr</pre>
<pre>
Your <a href="/console/account-balance">account balance</a>: ${{ account_balance }}</div>
<form method="post"> <form method="post">
<div class="row justify-start"> <div class="row justify-start">