From d270e7367c6968f16e39392f01b96a219b0ab047 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 14 Feb 2016 17:58:31 -0800 Subject: Record and display tips (closes #7) --- public/css/coinos.css | 2 +- public/css/coinos.less | 2 +- public/js/calculator.coffee | 1 + public/js/calculator.js | 3 ++- public/js/report.coffee | 50 +++++++++++++++++++++++++++++-------------- public/js/report.js | 44 +++++++++++++++++++++++-------------- views/transactions/index.html | 18 ++++++++++++---- 7 files changed, 81 insertions(+), 39 deletions(-) diff --git a/public/css/coinos.css b/public/css/coinos.css index 9ddd44bd..59765af1 100644 --- a/public/css/coinos.css +++ b/public/css/coinos.css @@ -167,7 +167,7 @@ div#calculator #qr { padding-bottom: 10px; margin-left: 20px; } -td#total { +td.total { font-weight: bold; text-align: right; } diff --git a/public/css/coinos.less b/public/css/coinos.less index 42cdc3bc..6bc2cb6a 100644 --- a/public/css/coinos.less +++ b/public/css/coinos.less @@ -71,7 +71,7 @@ div#calculator { #qr { padding-bottom: 10px; margin-left: 20px; } } -td#total { font-weight: bold; text-align: right; } +td.total { font-weight: bold; text-align: right; } #main-logo { background-image: url('../img/coinos_logo.png'); diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee index e6cc813e..1b5f5034 100644 --- a/public/js/calculator.coffee +++ b/public/js/calculator.coffee @@ -217,6 +217,7 @@ logTransaction = (txid, amount) -> date: moment().format("YYYY-MM-DD HH:mm:ss"), received: amount, exchange: g.exchange + tip: g.tip ) getAddress() diff --git a/public/js/calculator.js b/public/js/calculator.js index fe3ce42c..f54e70bd 100644 --- a/public/js/calculator.js +++ b/public/js/calculator.js @@ -224,7 +224,8 @@ address: g.user.address, date: moment().format("YYYY-MM-DD HH:mm:ss"), received: amount, - exchange: g.exchange + exchange: g.exchange, + tip: g.tip }); } return getAddress(); diff --git a/public/js/report.coffee b/public/js/report.coffee index 57da46c4..64284cbc 100644 --- a/public/js/report.coffee +++ b/public/js/report.coffee @@ -52,21 +52,44 @@ display = (transactions) -> $('.alert').fadeIn() else $('.alert').hide() + btc_total = btc_tips_total = 0 + fiat_total = fiat_tips_total = 0 + $.each(transactions, -> + btc_tip_str = "" + fiat_tip_str = "" + exchange = parseFloat(@exchange) - received = parseFloat(@received) - amount = received * exchange + tip = @tip + btc = parseFloat(@received) + fiat = btc * exchange + + btc_total += parseFloat(btc.toFixed(8)) + fiat_total += parseFloat(fiat.toFixed(2)) + + if tip > 1 + btc = btc / tip + btc_tip = (btc * tip - btc).toFixed(8) + fiat_tip = (btc_tip * exchange).toFixed(2) + + btc_tips_total += parseFloat(btc_tip) + fiat_tips_total += parseFloat(fiat_tip) + + btc_tip_str = "
+ #{btc_tip}" + fiat_tip_str = "
+ #{fiat_tip}" + + fiat = btc * exchange + notes = @notes txid = @txid - address = @address date = moment(@date, 'YYYY-MM-DD h:mm:ss').format('MMM D h:mma') row = $(""" #{date}   #{exchange.toFixed(2)} - #{received.toFixed(8)} - #{amount.toFixed(2)} + #{btc.toFixed(8)}#{btc_tip_str} + #{fiat.toFixed(2)}#{fiat_tip_str} """) @@ -117,16 +140,11 @@ display = (transactions) -> ) ) - btc = 0 - $('table.report tbody td:nth-child(3)').each(-> - btc += parseFloat($(this).html()) - ) - $('#btc').html(btc.toFixed(8)) - - cad = 0 - $('table.report tbody td:nth-child(4)').each(-> - cad += parseFloat($(this).html()) - ) - $('#cad').html(cad.toFixed(2)) + $('#btc_total').html("#{parseFloat(btc_total).toFixed(8)}") + $('#fiat_total').html("#{parseFloat(fiat_total).toFixed(2)}") + $('#btc_tips').html("#{parseFloat(btc_tips_total).toFixed(8)}") + $('#fiat_tips').html("#{parseFloat(fiat_tips_total).toFixed(2)}") + $('#btc').html("#{parseFloat(btc_total - btc_tips_total).toFixed(8)}") + $('#fiat').html("#{parseFloat(fiat_total - fiat_tips_total).toFixed(2)}") $('.report').fadeIn() diff --git a/public/js/report.js b/public/js/report.js index 51d091cf..e73a691d 100644 --- a/public/js/report.js +++ b/public/js/report.js @@ -52,23 +52,39 @@ }; display = function(transactions) { - var btc, cad; + var btc_tips_total, btc_total, fiat_tips_total, fiat_total; $('.report tbody tr').remove(); if (transactions.length === 0) { $('.report').hide(); return $('.alert').fadeIn(); } else { $('.alert').hide(); + btc_total = btc_tips_total = 0; + fiat_total = fiat_tips_total = 0; $.each(transactions, function() { - var address, amount, date, exchange, notes, received, row, txid; + var btc, btc_tip, btc_tip_str, date, exchange, fiat, fiat_tip, fiat_tip_str, notes, row, tip, txid; + btc_tip_str = ""; + fiat_tip_str = ""; exchange = parseFloat(this.exchange); - received = parseFloat(this.received); - amount = received * exchange; + tip = this.tip; + btc = parseFloat(this.received); + fiat = btc * exchange; + btc_total += parseFloat(btc.toFixed(8)); + fiat_total += parseFloat(fiat.toFixed(2)); + if (tip > 1) { + btc = btc / tip; + btc_tip = (btc * tip - btc).toFixed(8); + fiat_tip = (btc_tip * exchange).toFixed(2); + btc_tips_total += parseFloat(btc_tip); + fiat_tips_total += parseFloat(fiat_tip); + btc_tip_str = "
+ " + btc_tip + ""; + fiat_tip_str = "
+ " + fiat_tip + ""; + } + fiat = btc * exchange; notes = this.notes; txid = this.txid; - address = this.address; date = moment(this.date, 'YYYY-MM-DD h:mm:ss').format('MMM D h:mma'); - row = $("\n " + date + "  \n " + (exchange.toFixed(2)) + "\n " + (received.toFixed(8)) + "\n " + (amount.toFixed(2)) + "\n"); + row = $("\n " + date + "  \n " + (exchange.toFixed(2)) + "\n " + (btc.toFixed(8)) + btc_tip_str + "\n " + (fiat.toFixed(2)) + fiat_tip_str + "\n"); if (notes) { row.attr('data-notes', notes); row.find('span').removeClass('hidden'); @@ -115,16 +131,12 @@ } }); }); - btc = 0; - $('table.report tbody td:nth-child(3)').each(function() { - return btc += parseFloat($(this).html()); - }); - $('#btc').html(btc.toFixed(8)); - cad = 0; - $('table.report tbody td:nth-child(4)').each(function() { - return cad += parseFloat($(this).html()); - }); - $('#cad').html(cad.toFixed(2)); + $('#btc_total').html("" + (parseFloat(btc_total).toFixed(8))); + $('#fiat_total').html("" + (parseFloat(fiat_total).toFixed(2))); + $('#btc_tips').html("" + (parseFloat(btc_tips_total).toFixed(8))); + $('#fiat_tips').html("" + (parseFloat(fiat_tips_total).toFixed(2))); + $('#btc').html("" + (parseFloat(btc_total - btc_tips_total).toFixed(8))); + $('#fiat').html("" + (parseFloat(fiat_total - fiat_tips_total).toFixed(2))); return $('.report').fadeIn(); } }; diff --git a/views/transactions/index.html b/views/transactions/index.html index 07c0c7db..a8a07f82 100644 --- a/views/transactions/index.html +++ b/views/transactions/index.html @@ -20,17 +20,27 @@ Time Rate - BTC - CAD +    BTC +    CAD - Total: + Subtotal: - + + + + Tips: + + + + + Total: + + -- cgit v1.2.3