Skip to content

Commit

Permalink
added box that tells you pool balance
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Jan 27, 2016
1 parent 67203a3 commit 98f9f29
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
98 changes: 81 additions & 17 deletions chezbetty/static/js/chezbetty-terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ function purchase_alert_error (error_str) {
* TERMINAL PAGE LOGIC
******************************************************************************/

// Returns the current balance of the user that is logged in on terminal
function get_user_balance () {
return parseFloat($("#user-balance").text());
}

function get_pool_balance (pool_id) {
return parseFloat($("#pool-balance-"+pool_id).text());
}

// Function to add up the items in a cart to display the total.
function calculate_total () {
total = 0;
Expand All @@ -82,7 +91,7 @@ function calculate_total () {
}
});

var balance = parseFloat($("#user-balance").text());
var balance = get_user_balance();
var discount = 0.0;
var fee = 0.0;
var need_subtotal = false;
Expand Down Expand Up @@ -159,6 +168,41 @@ function update_user_balance (balance) {
else $("#user-info-current-balance").removeClass("big-debt");
}

// Update the stored pool balance in the DOM if it changes.
function update_pool_balance (pool_id, balance) {
// Set the DOM element
$("#pool-balance-"+pool_id).text(balance);

display_and_update_pool_balance_box();
}

// Returns the pool id if a pool payment is selected, otherwise returns -1
// if the user account is paying.
function get_active_payment_account () {
var account_button = $(".purchase-payment.active");
var id = account_button.attr('id');
var fields = id.split('-');
var acct_type = fields[1];
if (acct_type == 'pool') {
return parseInt(fields[2]);
}
return -1;
}

function display_and_update_pool_balance_box () {
var active_account = get_active_payment_account();

if (active_account == -1) {
// Hide the pool balance box
$('#pool-info-current-balance').hide();
} else {
// Get the pool balance
var balance = get_pool_balance(active_account);
$("#pool-info-current-balance span").html(format_price(balance));
$('#pool-info-current-balance').show();
}
}

// Update the new tentative balance total for the user if the user were to
// submit the purchase and deposit.
function calculate_user_new_balance () {
Expand All @@ -178,9 +222,14 @@ function calculate_user_new_balance () {
purchase = 0.00;
}

var balance = parseFloat($("#user-balance").text());
var balance = get_user_balance();

var new_balance = balance - purchase + deposit;
// Only do this calculation if we are not using a pool
if (get_active_payment_account() == -1) {
var new_balance = balance - purchase + deposit;
} else {
var new_balance = balance;
}
$("#user-info-new-balance span").html(format_price(new_balance));

// Update the color formatting based on amount in account
Expand All @@ -206,7 +255,9 @@ function show_correct_purchase_button () {

// If the user is in debt, it should be just a purchase button
var balance = parseFloat($("#user-balance").text());
if (balance < 0) {
if (balance < 0 && get_active_payment_account() == -1) {
// If the user is paying with pool, then we can have purchase and
// logout button.
$("#purchase-button-purchaselogout").hide();
$("#purchase-button-purchase").show();
} else {
Expand All @@ -217,7 +268,7 @@ function show_correct_purchase_button () {
}

function calculate_wallofshame_fee_percent () {
var balance = parseFloat($("#user-balance").text());
var balance = get_user_balance();

// If not on wall of shame, no fee
if (balance > -5.0) return 0.0;
Expand Down Expand Up @@ -282,7 +333,7 @@ function purchase_success (data) {
} else {
purchase_alert_success('Purchase was recorded successfully.');

// Throw in table summarizing
// Throw in table summarizing
$("#purchase-complete-table").html(data.order_table);

// Flip to complete page
Expand Down Expand Up @@ -385,13 +436,16 @@ function deposit_success (data) {
$(".deposit-pool-name").text(data.pool_name);
$("#deposit-complete-user").hide();
$("#deposit-complete-pool").show();

// Update pool balance
update_pool_balance(data.pool_id, data.pool_balance);
} else {
$("#deposit-complete-pool").hide();
$("#deposit-complete-user").show();
}

// Update user balance
update_user_balance(data.user_balance);
// Update user balance
update_user_balance(data.user_balance);
}

// We then calculate the purchase total in case a discount/fee
// has changed.
Expand Down Expand Up @@ -435,6 +489,12 @@ function deposit_delete_success (data) {
update_user_balance(data.user_balance);
calculate_user_new_balance();

// Update any pool balances
for (var i=0; i<data.pools.length; i++) {
var pool = data.pools[i];
update_pool_balance(pool.id, pool.balance);
}

// Make the change back to the deposit entry form
$("#deposit-complete").hide();
$("#deposit-entry").show();
Expand Down Expand Up @@ -493,14 +553,9 @@ function submit_purchase (this_btn, success_cb, error_cb) {
purchase.umid = $("#user-umid").text();

// What account to pay with?
var account_button = $(".purchase-payment.active");
var id = account_button.attr('id');
var fields = id.split('-');
var acct_type = fields[1];
if (acct_type == 'pool') {
// Put pool id in we should use a pool, otherwise we will default
// to the user's account.
purchase['pool_id'] = parseInt(fields[2]);
var pool_id = get_active_payment_account();
if (pool_id > -1) {
purchase['pool_id'] = pool_id;
}

var item_count = 0;
Expand Down Expand Up @@ -563,6 +618,15 @@ $("#purchase-table tbody").on("click", ".btn-decrement-item", function () {
$(".purchase-payment").click(function () {
$(".purchase-payment").removeClass("active");
$(this).addClass("active");

// Update boxes on the left showing current balance and new balance
calculate_user_new_balance();

// Show or hide pool balance
display_and_update_pool_balance_box();

// Can also switch payment options
show_correct_purchase_button();
});

// Purchase mistake, revert this transaction and put the cart back
Expand Down
12 changes: 11 additions & 1 deletion chezbetty/templates/terminal/terminal.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</div>

<div id="user-info-current-balance" class="row well balance-box {% if user.balance < -5 %}big-debt{% endif %}">
<h4>{{ _('Current Balance') }}</h4>
<h4>{{ _('Your Current Balance') }}</h4>
<span class="current-formatted-balance">{{ user.balance|format_currency|safe }}</span>
</div>

Expand All @@ -40,8 +40,17 @@
<span>{{ user.balance|format_currency|safe }}</span>
</div>

<div id="pool-info-current-balance" class="row well balance-box" style="display:none;">
<h4>{{ _('Selected Pool Balance') }}</h4>
<span></span>
</div>

<div id="user-balance" class="hidden">{{ user.balance }}</div>
<div id="user-umid" class="hidden">{{ user.umid }}</div>

{% for pool in purchase_pools %}
<div id="pool-balance-{{ pool.id }}" class="hidden">{{ pool.balance }}</div>
{% endfor %}
</div>

{# Purchase panel #}
Expand Down Expand Up @@ -210,6 +219,7 @@
{% if user.deposit_limit >= 20 %}<button type="button" id="btn-default-20" class="btn btn-default btn-lg btn-default-deposit">$20</button>{% endif %}
{% if user.deposit_limit >= 10 %}<button type="button" id="btn-default-10" class="btn btn-default btn-lg btn-default-deposit">$10</button>{% endif %}
{% if user.deposit_limit >= 5 %}<button type="button" id="btn-default-5" class="btn btn-default btn-lg btn-default-deposit">$5</button>{% endif %}
<button type="button" id="btn-default-0" class="btn btn-default btn-lg btn-default-deposit">$0</button>
<button type="button" id="btn-deposit-entry-default-custom" class="btn btn-default btn-lg btn-custom-deposit btn-warning">{{ _('Custom Amount') }}</button>
</div>
<div id="deposit-entry-custom" class="keypad-sm" style="display: none; text-align: center">
Expand Down
22 changes: 17 additions & 5 deletions chezbetty/views_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def terminal_purchase_tag(request):
'nobarcode_items': Item.get_nobarcode_notag_items()}
else:
return {'error': 'Unable to parse TAG ID'}

item_array = render('templates/terminal/purchase_nobarcode_items.jinja2',
{'tag': tag})

Expand Down Expand Up @@ -178,6 +178,8 @@ def terminal_deposit(request):
pool = Pool.from_id(request.POST['pool_id'])
deposit = datalayer.deposit(user, pool, amount)
ret['pool_name'] = pool.name
ret['pool_balance'] = float(pool.balance)
ret['pool_id'] = pool.id

# Return a JSON blob of the transaction ID so the client can redirect to
# the deposit success page
Expand Down Expand Up @@ -220,7 +222,17 @@ def terminal_deposit_delete(request):
# Now undo old deposit
datalayer.undo_event(old_event, user)

return {'user_balance': float(user.balance)}
purchase_pools = []
for pool in Pool.all_by_owner(user, True):
if pool.balance > (pool.credit_limit * -1):
purchase_pools.append({'id': pool.id, 'balance': float(pool.balance)})

for pu in user.pools:
if pu.pool.enabled and pu.pool.balance > (pu.pool.credit_limit * -1):
purchase_pools.append({'id': pu.pool.id, 'balance': float(pu.pool.balance)})

return {'user_balance': float(user.balance),
'pools': purchase_pools}

except __user.InvalidUserException as e:
return {'error': 'Invalid user error. Please try again.'}
Expand Down Expand Up @@ -433,7 +445,7 @@ def terminal_purchase_delete(request):
# pools = Pool.all_accessable(user, True)

# return {'user' : user,
# 'btc' : btc_html,
# 'btc' : btc_html,
# 'pools': pools}

# except __user.InvalidUserException as e:
Expand Down Expand Up @@ -464,7 +476,7 @@ def terminal_purchase_delete(request):

# return {'user': user,
# 'old_event': event,
# 'old_deposit': event.transactions[0],
# 'old_deposit': event.transactions[0],
# 'pools': pools}

# except __user.InvalidUserException as e:
Expand Down Expand Up @@ -501,7 +513,7 @@ def terminal_purchase_delete(request):
# {'deposit': transaction,
# 'user': user,
# 'event': event,
# 'prev_balance': prev_balance,
# 'prev_balance': prev_balance,
# 'account_type': account_type,
# 'pool': pool}, request)

Expand Down

0 comments on commit 98f9f29

Please sign in to comment.