aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/report.coffee
blob: 06c99050e2b53bb91d408aeddea2c7f468f734a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#= require jquery-1.8.2.min.js
#= require jquery-ui-1.9.0.custom.min.js
#= require moment.min.js

g = exports ? this

$(->
  $('#from').val(moment().subtract('days', 7).format("MM/DD/YYYY"))
  $('#to').val(moment().format("MM/DD/YYYY"))
  $('.date').datepicker(onClose: filterDates)
  $.getJSON('transactions', (data) ->
    g.transactions = data.transactions
    display(g.transactions)
    filterDates()
  )
)

filterDates = ->
  transactions = $.grep(g.transactions, (e, i) ->
    from = moment($('#from').val(), "MM/DD/YYYY")
    to = moment($('#to').val(), "MM/DD/YYYY")
    d = moment(e.date)

    return (!from? || d.diff(from) >= 0) && (!to? || d.diff(to) <= 0)
  )

  display(transactions)   

display = (transactions) ->
  $('tbody tr').remove()
  $.each(transactions, ->
    $('tbody').append("""
      <tr>
        <td>#{this.date}</td>
        <td>#{this.address}</td>
        <td>#{parseFloat(this.exchange).toFixed(2)}</td>
        <td>#{parseFloat(this.received).toFixed(2)}</td>
        <td>#{(this.received * this.exchange).toFixed(2)}</td>
      </tr>
    """)
  )

  btc = 0
  $('td:nth-child(4)').each(->
    btc += parseFloat($(this).html())
  )
  $('#btc').html(btc.toFixed(2))

  cad = 0
  $('td:nth-child(5)').each(->
    cad += parseFloat($(this).html())
  )
  $('#cad').html(cad.toFixed(2))