Source

flowrate / flowrate / ui / transactions.html

Full commit
  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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Flowrate Transactions</title>

<style type='text/css'>

body {
    margin: 0;
    padding: 0;
    font: 10pt Verdana, sans-serif;
}

.header {
    padding: 0.25em 1em;
    background-color: #8495C0;
    color: white;
    border-bottom: 1px solid #425367;
}

h1 {
    font: 700 14pt Verdana, sans-serif;
}

h1 a {
    color: #DDDDEE;
    text-decoration: none;
}

#status {
    position: fixed;
    top: 0;
    padding: 0.25em;
    right: 0;
    width: 50%;
    height: 3em;
    overflow: auto;
}

#status p {
    margin: 0;
    padding: 0;
    width: 100%;
    text-align: right;
}

#fatal_error {
    visibility: hidden;
    position: fixed;
    top: 20px;
    left: 20px;
    right: 20px;
    bottom: 20px;
    background-color: #FFFFFF;
    border: 2px solid DarkRed;
    padding: 2em;
    overflow: auto;
    z-index: -1;
}

#fatal_error_title {
    font: 700 18pt Verdana, sans-serif;
    border-bottom: 1px solid DarkRed;
    color: DarkRed;
}

#transactions {
    padding: 0 0.25em;
    border-collapse: collapse;
    width: 100%;
}

#transactions tr {
    padding: 0;
}

#transactions th {
    background-color: DarkGrey;
    color: white;
    padding: 0.25em;
    margin: 0;
}

#transactions tr.txrow {
    padding: 0;
    cursor: pointer;
}

#transactions tr.flowrow {
    padding: 0;
    background-color: #EEFFEE;
}

#transactions td {
    vertical-align: bottom;
    padding: 0 0.25em;
    border: 1px solid LightGrey;
}

#transactions td a {
    cursor: default;
}

#transactions td.txamount {
    text-align: right;
    font-family: monospace;
}

#transactions tr#txedit input[type=text] {
    width: 100%;
    background-color: #FFFFF0;
    color: black;
}

</style>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="/json2.js"></script>
<script type="text/javascript" src="/common.js"></script>
<script type="text/javascript">

google.load('visualization', '1.0', {'packages':['corechart']});


//                                TRANSACTIONS                                //


accounts = {};

function populate_accounts() {
    var h = http("GET", "/accounts", false);
    h[200] = function(h) {
        var j = JSON.parse(h.responseText);
        for (var i = 0; i < j.data.length; i++) {
            var a = j.data[i];

            var acctid = segment(a.id, -1);
            accounts[acctid] = a;

            var c = document.createElement("option");
            c.text = truncate(a.name, 20);
            c.value = a.id;
            $('edit_credit').options.add(c);

            var d = document.createElement("option");
            d.text = truncate(a.name, 20);
            d.value = a.id;
            $('edit_debit').options.add(d);
        }
    }
    h.send();
}

function populate_transactions() {
    var h = http("GET", "/transactions" + location.search, true,
                 "Loading transactions...");
    h[200] = function(h) {
        var j = JSON.parse(h.responseText);
        if (j.data) {
            for (var i = 0; i < j.data.length; i++) {
                add_tx_row(j.data[i], true);
                add_tx_activity(j.data[i]);
            }
            drawChart();
        }
    }
    h.send();
}

function tx_dategroup(tx) {
    if (query['dategroup'] == 'year') return tx.postdate.substring(0, 4);
    if (query['dategroup'] == 'day') return tx.postdate;
    // Note this handles both 'month' and unknown values
    return tx.postdate.substring(0, 7);
}

function date_from_iso(d) {
    var y = parseInt(d.substring(0, 4), 10);
    var m = parseInt(d.substring(5, 7), 10) - 1;
    var d = parseInt(d.substring(8, 10), 10);
    return new Date(y, m, d)
}

txs = {};

function add_tx_row(tx, already_sorted) {
    if (already_sorted == undefined) already_sorted = false;

    var r = null;
    var d = null;
    var a = null;
    var i = null;

    if (tx.id == null) {
        // Dummy transaction from a flow
        var txid = null;
    } else {
        // Real transaction
        var txid = segment(tx.id, -1);
        txs[txid] = tx;
    }

    // Create tx row
    var r = document.createElement("tr");
    r.tx = tx;
    r.tx.dategroup = tx_dategroup(tx);
    if (txid) {
        r.className = 'txrow';
        r.id = 'txrow' + txid;
        r.onclick = function () { edit_transaction(txid) };
    } else {
        r.className = 'flowrow';
        if (date_from_iso(tx.postdate) < new Date()) {
            r.style.backgroundColor = '#FFEEEE';
        }
    }

    add_tx_cells_fast(r, txid, tx);

    // Insert the row in order by postdate descending
    var rowset = $("txheader").parentNode;
    if (!already_sorted) {
        for (var i=0; i < rowset.childNodes.length; i++) {
            var existing = rowset.childNodes[i];
            if (existing.className == 'txrow' || existing.className == 'flowrow') {
                if (existing.tx.postdate <= tx.postdate) {
                    rowset.insertBefore(r, existing);
                    break;
                }
            }
        }
    }
    // If no match:
    if (r.parentNode == null) rowset.appendChild(r);

    return r;
}

function add_tx_cells(r, txid, tx) {
    // ID cell + link
    var d = document.createElement("td");
    d.className = 'txid';
    set_text(d, txid || '');
    r.appendChild(d);

    // postdate
    d = document.createElement("td");
    d.className = 'txdate';
    set_text(d, tx.postdate);
    r.appendChild(d);

    // credit
    d = document.createElement("td");
    d.className = 'txcredit';
    r.appendChild(d);
    a = document.createElement("a");
    a.href = tx.credit;
    set_text(a, option_text($('edit_credit').options, tx.credit));
    d.appendChild(a);

    // debit
    d = document.createElement("td");
    d.className = 'txdebit';
    r.appendChild(d);
    a = document.createElement("a");
    a.href = tx.debit;
    set_text(a, option_text($('edit_debit').options, tx.debit));
    d.appendChild(a);

    // description
    d = document.createElement("td");
    d.className = 'txdesc';
    set_text(d, tx.description);
    r.appendChild(d);

    // amount
    d = document.createElement("td");
    d.className = 'txamount';
    set_text(d, '$' + commafy(tx.amount.toFixed(2)));
    r.appendChild(d);
}

function add_tx_cells_fast(r, txid, tx) {
    // Like add_tx_cells, but around 10x faster
    // using innerHTML instead of createElement and appendChild

    // ID cell + link
    var d = "<td class='txid'>" + (txid || '') + "</td>";
    // postdate
    d = d + "<td class='txdate'>" + tx.postdate + "</td>";
    // credit
    var o = option_text($('edit_credit').options, tx.credit);
    d = d + "<td class='txcredit'><a href='" + tx.credit + "'>" + o + "</a></td>";
    // debit
    o = option_text($('edit_debit').options, tx.debit);
    d = d + "<td class='txdebit'><a href='" + tx.debit + "'>" + o + "</a></td>";
    // description
    d = d + "<td class='txdesc'>" + tx.description + "</td>";
    // amount
    d = d + "<td class='txamount'>$" + commafy(tx.amount.toFixed(2)) + "</td>";

    r.innerHTML = d;
}

// A list of (acct ids, dategroup columns) tuples.
activity = [];
activity_dategroups = [];

function add_tx_activity(tx) {
    var dg = tx_dategroup(tx);

    if (activity_dategroups.indexOf(dg) == -1) {
        activity_dategroups.push(dg);
        // Add the dategroup to all existing account activity
        for (var a=0; a < activity.length; a++) {
            activity[a][1][dg] = 0;
        }
    }

    // Credit account
    var ca = parseInt(segment(tx.credit, -1));
    var c_acct = accounts[ca];
    var da = parseInt(segment(tx.debit, -1));
    var d_acct = accounts[da];
    for (var a = 0; a < activity.length; a++) {
        if (activity[a][0].indexOf(ca) != -1) {
            if (c_acct.type == 'asset' || c_acct.type == 'expense') {
                activity[a][1][dg] -= tx.amount;
            } else {
                activity[a][1][dg] += tx.amount;
            }
        }
        if (activity[a][0].indexOf(da) != -1) {
            if (d_acct.type == 'asset' || d_acct.type == 'expense') {
                activity[a][1][dg] += tx.amount;
            } else {
                activity[a][1][dg] -= tx.amount;
            }
        }
    }
}


var txid_being_edited = null;

function edit_transaction(txid) {
    // Unhide any previously-edited row
    if (!(txid_being_edited == null)) {
        $('txrow' + txid_being_edited).style.display = 'table-row';
    }

    var txedit = $('txedit');

    if (txid == null) {
        // Keep existing values to make it easy to copy an existing row
        // Remove the row from its current position
        txedit.parentNode.removeChild(txedit);
        // Insert before the header row
        $('txheader').parentNode.insertBefore(txedit, $('txheader'));
    } else {
        var tx = txs[txid];
        $('edit_postdate').value = tx.postdate;
        $('edit_credit').value = tx.credit;
        $('edit_debit').value = tx.debit;
        $('edit_description').value = tx.description;
        $('edit_amount').value = tx.amount;

        // Remove the row from its current position
        txedit.parentNode.removeChild(txedit);
        // Insert the edit row before the existing row, then hide the existing
        var txrow = $('txrow' + txid);
        txrow.parentNode.insertBefore(txedit, txrow);
        txrow.style.display = 'none';
    }

    txid_being_edited = txid;
}

function save_transaction() {
    var tx = {};
    tx.postdate = $('edit_postdate').value;
    tx.credit = $('edit_credit').value;
    tx.debit = $('edit_debit').value;
    tx.description = $('edit_description').value;
    tx.amount = parseFloat($('edit_amount').value.replace(/[^0-9.]/g, ''));

    if (txid_being_edited == null) {
        // New transaction
        var h = http("POST", '/transactions', false, "Saving new transaction");
        // TODO: add new table row
        h[201] = function(h) {
            tx.id = h.getResponseHeader("Location");
            add_tx_row(tx);
        };
    } else {
        // Update existing transaction
        var h = http("PUT", '/transactions/' + txid_being_edited, false,
                     "Updating transaction " + txid_being_edited);
        h[204] = function(h) {
            // Remove and re-add the row; this moves it if it changed dates
            var txrow = $('txrow' + txid_being_edited);
            txrow.parentNode.removeChild(txrow);
            tx.id = txid_being_edited;
            txrow = add_tx_row(tx);

            // Also move the txedit row before it
            var txedit = $('txedit');
            txedit.parentNode.removeChild(txedit);
            txrow.parentNode.insertBefore(txedit, txrow);
            txrow.style.display = 'none';
        };
    }
    h.setRequestHeader("Content-Type", "application/json");
    h.send(JSON.stringify({"body": tx}));
}

function calc_edit_amount(e) {
    try { if (e.value[0] == "=") e.value = eval(e.value.substring(1)); } catch(e) {}
}

//                                   OTHER                                   //


function drawChart() {
    if (activity.length == 0) return;

    var table = make_table(activity);
    draw_table(table, 'Activity');

    // Highlight rows matching a clicked point or column (account set)
    function handle_select() {
        var selection = chart.getSelection();
        var seldg = null;
        var selaccts = null;
        for (var i = 0; i < selection.length; i++) {
            // We are assuming one selection for now
            var item = selection[i];
            if (item.row != null && item.column != null) {
                seldg = table.getValue(item.row, 0);
                selaccts = activity[item.column - 1][0];
            } else if (item.row != null) {
                seldg = table.getValue(item.row, 0);
            } else if (item.column != null) {
                selaccts = activity[item.column - 1][0];
            }
        }

        var rowset = $("txheader").parentNode;
        for (var i=0; i < rowset.childNodes.length; i++) {
            var r = rowset.childNodes[i];
            if (r.className == 'txrow' || r.className == 'flowrow') {
                if ((seldg == null || seldg == r.tx.dategroup) &&
                    (selaccts == null ||
                     selaccts.indexOf(parseInt(segment(r.tx.credit, -1))) != -1 ||
                     selaccts.indexOf(parseInt(segment(r.tx.debit, -1))) != -1
                    )) {
                    r.style.fontWeight = 'bold';
                } else {
                    r.style.fontWeight = 'normal';
                }
            }
        }
    }

    google.visualization.events.addListener(chart, 'select', handle_select);

}

query = {
    'accounts': '',
    'credits': '',
    'debits': '',
    'years': '',
    'months': '',
    'days': '',
    'dategroup': '',
    };

function init() {
    var pairs = document.location.search.substring('?'.length).split('&');
    var keyval = null;
    for (var i = 0; i < pairs.length; i++) {
        keyval = pairs[i].split('=');
        query[unescape(keyval[0])] = unescape(keyval[1]);
    }

    $('accounts').value = query['accounts'];
    $('credits').value = query['credits'];
    $('debits').value = query['debits'];
    $('years').value = query['years'];
    $('months').value = query['months'];
    $('days').value = query['days'];
    $('dategroup').value = query['dategroup'];

    qs = 'accounts=' + encodeURIComponent(query['accounts']) +
         '&years=' + encodeURIComponent(query['years']) +
         '&months=' + encodeURIComponent(query['months']) +
         '&dategroup=' + query['dategroup'];
    $("accountslink").href = $("accountslink").href + "?" + qs;
    $("flowslink").href = $("flowslink").href + "?" + qs;
    $("varslink").href = $("varslink").href + "?" + qs;

    populate_accounts();

    var req_accounts = (query['accounts'] + ',' + query['credits'] + ','
                        + query['debits']).split(",");
    var acctsets = account_sets(req_accounts);
    for (var i=0; i < acctsets.length; i++) {
        activity.push([acctsets[i], {}])
    }
    populate_transactions();

    edit_transaction(null);
    $('edit_postdate').value = ISODate(new Date());
}

</script>
</head>

<body onLoad="init()">
<div class='header'>
    <h1 id="banner">Flowrate:
        <a id="accountslink" href='/accounts/manager'>Accounts</a>
        | Transactions
        | <a id="flowslink" href='/flows/manager'>Flows</a>
        | <a id="varslink" href='/variables/manager'>Variables</a>
        | <a id="importlink" href="/import">Import</a>
    </h1>
    <div id="status"></div>
    <div>
        <form action='' method='GET'>
            Accounts: <input type='text' id="accounts" name="accounts" value="" size="9" />
            Credits: <input type='text' id="credits" name="credits" value="" size="9" />
            Debits: <input type='text' id="debits" name="debits" value="" size="9" />
            Years: <input type='text' id="years" name="years" value="" size="9" />
            Months: <input type='text' id="months" name="months" value="" size="6" />
            Days: <input type='text' id="days" name="days" value="" size="6" />
            Group: <select id="dategroup" name="dategroup">
                   <option>year</option>
                   <option selected="selected">month</option>
                   <option>day</option>
                   </select>
            <input type='submit' value="GO" />
        </form>
    </div>
</div>

<div id='fatal_error'>
    <h2 id='fatal_error_title'></h2>
    <p style='float: right'><input type="button" value="Close" onClick="remove_fatal()" /></p>
    <p id='fatal_error_msg'></p>
</div>

<div id="chart_div"></div>

<table id='transactions'>
<tr id='txedit'>
    <td><input id='edit_save' type='button' value='Save' onClick='save_transaction()' /></td>
    <td><input id='edit_postdate' type='text' size='10' /></td>
    <td><select id='edit_credit'></select></td>
    <td><select id='edit_debit'></select></td>
    <td><input id='edit_description' type='text' value="New transaction" /></td>
    <td><input id='edit_amount' type='text' size='10' style="text-align: right" value="0.00" 
               onBlur='calc_edit_amount(this)' /></td>
</tr>
<tr id='txheader'>
    <th>ID&nbsp;<span onClick="edit_transaction(null)" title="New transaction" style="cursor: pointer">(+)</span></th>
    <th>Post&nbsp;Date</th>
    <th>Credit</th>
    <th>Debit</th>
    <th>Description</th>
    <th>Amount</th>
</tr>
</table>
</body>
</html>