aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/report.coffee
blob: ed0cb23246e859cad05bea8ee0c0acc96286830c (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#= require ../js/jquery-1.8.2.min.js
#= require ../js/jquery-ui.min.js
#= require ../js/bootstrap.min.js
#= require ../js/moment.min.js

g = exports ? this

$(->
  from = moment().subtract('days', 7)
  to = moment()

  $('#from').html(from.format("MMMM Do, YYYY"))
  $('#to').html(to.format("MMMM Do, YYYY"))

  $('#from_date').val(from.format("MM/DD/YYYY"))
  $('#to_date').val(to.format("MM/DD/YYYY"))

  $('#from_date').datepicker(onClose: filterDates)
  $('#to_date').datepicker(onClose: filterDates)

  $('#from').click(-> $('#from_date').datepicker('show'))
  $('#to').click(-> $('#to_date').datepicker('show'))

  $.getJSON('transactions.json', (data) ->
    g.transactions = data.transactions
    display(g.transactions)
    filterDates()
  )
)

filterDates = ->
  $('#from').html(moment($('#from_date').datepicker('getDate')).format("MMMM Do, YYYY")) 
  $('#to').html(moment($('#to_date').datepicker('getDate')).format("MMMM Do, YYYY"))

  transactions = $.grep(g.transactions, (e, i) ->
    return false unless e

    from = moment($('#from_date').val(), "MM/DD/YYYY")
    to = moment($('#to_date').val(), "MM/DD/YYYY")

    d = moment(e.date)
    amount = parseFloat(e.exchange) * parseFloat(e.received)

    !isNaN(parseFloat(amount)) && isFinite(amount) &&
      (!from? || d.diff(from) >= 0) && (!to? || d.diff(to) <= 0) 
  )

  display(transactions)   

display = (transactions) ->
  $('.report tbody tr').remove()

  if transactions.length is 0
    $('.report').hide()
    $('.alert').fadeIn()
  else
    $('.alert').hide()
    $.each(transactions, ->
      exchange = parseFloat(@exchange)
      received = parseFloat(@received)
      amount = received * exchange
      notes = @notes
      txid = @txid
      address = @address
      date = moment(@date, 'YYYY-MM-DD h:mm:ss').format('MMM D h:mma')

      row = $("""
        <tr id='#{@txid}'>
          <td>#{date}&nbsp;&nbsp;<span class='glyphicon glyphicon-tag hidden'></span></td>
          <td>#{exchange.toFixed(2)}</td>
          <td>#{received.toFixed(8)}</td>
          <td>#{amount.toFixed(2)}</td>
        </tr>
      """)

      if notes
        row.attr('data-notes', notes)
        row.find('span').removeClass('hidden')

      $('.report tbody').append(row) 
      
      row.click(-> 
        $('#confirm, #yousure').hide()
        $('#buttons, #modal textarea').show()
        notes = $(this).attr('data-notes')

        $('#modal').modal()
        $('#modal textarea').val('')
        $('#modal .btn-primary').toggle(txid? and txid.length > 5) 

        $('#modal .btn-danger').off().click(->
          $('#yousure, #confirm').show()
          $('#buttons, #modal textarea').hide()
        )

        $('#confirm .btn-danger').click(->
          $.ajax(type: 'delete', url: "/#{$('#user').val()}/transactions/#{txid}")
          $('#modal').modal('hide')
          row.fadeOut('slow')
        )

        $('#modal .btn-primary').off().click(->
          window.open("https://blockchain.info/tx/#{txid}", '_blank')
          $('#modal').modal('hide')
        )

        $('#modal textarea').off().change(->
          notes = $(this).val()
          $.post("/transactions/#{txid}", notes: $(this).val()) if txid

          row.find('span').addClass('hidden')
          if notes
            row.find('span').removeClass('hidden')

          row.attr('data-notes', $(this).val())
        )

        if notes
          $('#modal textarea').val(notes) 
      )
    )

    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))

    $('.report').fadeIn()