Source

flowrate / flowrate / ui / flows.html

  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
<!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 Flows</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;
}

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

#flows tr {
    padding: 0;
}

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

#flows tr.flowrow {
    padding: 0;
    cursor: pointer;
}

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

#flows td a {
    cursor: default;
}

#flows td.flowamount {
    text-align: right;
    font-family: monospace;
}

#flows tr#flowedit 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" src="/chart.js"></script>
<script type="text/javascript">

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

//                                   FLOWS                                   //

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_flows() {
    var h = http("GET", "/flows" + document.location.search, true);
    h[200] = function(h) {
        var j = JSON.parse(h.responseText);
        if (j.data) {
            for (var i = 0; i < j.data.length; i++) {
                add_flow_row(j.data[i]);
            }
        }
    }
    h.send();
}

flows = {};

function add_flow_row(flow) {
    var r = null;
    var d = null;
    var a = null;
    var i = null;

    var flowid = segment(flow.id, -1);

    flows[flowid] = flow;

    // Create flow row
    r = document.createElement("tr");
    r.className = 'flowrow';
    r.id = 'flowrow' + flowid;
    r.flow = flow;
    r.onclick = function () { edit_flow(flowid) };
    
    // ID cell
    d = document.createElement("td");
    d.className = 'flowid';
    set_text(d, flowid);
    r.appendChild(d);
    
    // start
    d = document.createElement("td");
    d.className = 'flowstart';
    set_text(d, flow.start);
    r.appendChild(d);
    
    // end
    d = document.createElement("td");
    d.className = 'flowend';
    set_text(d, flow.end);
    r.appendChild(d);
    
    // period
    d = document.createElement("td");
    d.className = 'flowperiod';
    set_text(d, flow.period);
    r.appendChild(d);
    
    // unit
    d = document.createElement("td");
    d.className = 'flowunit';
    set_text(d, flow.unit);
    r.appendChild(d);
    
    // days
    d = document.createElement("td");
    d.className = 'flowday';
    set_text(d, flow.days.join(","));
    r.appendChild(d);
    
    // credit
    d = document.createElement("td");
    d.className = 'flowcredit';
    r.appendChild(d);
    a = document.createElement("a");
    a.href = flow.credit;
    set_text(a, option_text($('edit_credit').options, flow.credit));
    d.appendChild(a);
    
    // debit
    d = document.createElement("td");
    d.className = 'flowdebit';
    r.appendChild(d);
    a = document.createElement("a");
    a.href = flow.debit;
    set_text(a, option_text($('edit_debit').options, flow.debit));
    d.appendChild(a);
    
    // description
    d = document.createElement("td");
    d.className = 'flowdesc';
    set_text(d, flow.description);
    r.appendChild(d);
    
    // amount
    d = document.createElement("td");
    d.className = 'flowamount';
    set_text(d, flow.amount);
    r.appendChild(d);
    
    // Insert the row in order by postdate descending
    var rowset = $("flowheader").parentNode;
    for (var i=0; i < rowset.childNodes.length; i++) {
        var existing = rowset.childNodes[i];
        if (existing.className == 'flowrow') {
            if (existing.flow.debit <= flow.debit) {
                rowset.insertBefore(r, existing);
                break;
            }
        }
    }
    // If no match:
    if (r.parentNode == null) rowset.appendChild(r);

    return r;
}

var flowid_being_edited = null;

function edit_flow(flowid) {
    // Unhide any previously-edited row
    if (!(flowid_being_edited == null)) {
        $('flowrow' + flowid_being_edited).style.display = 'table-row';
    }

    var flowedit = $('flowedit');

    if (flowid == null) {
        // Keep existing values to make it easy to copy an existing row
        // Remove the row from its current position
        flowedit.parentNode.removeChild(flowedit);
        // Insert before the header row
        $('flowheader').parentNode.insertBefore(flowedit, $('flowheader'));
    } else {
        var flow = flows[flowid];
        $('edit_start').value = flow.start;
        $('edit_end').value = flow.end;
        $('edit_period').value = flow.period;
        $('edit_unit').value = flow.unit;
        $('edit_days').value = flow.days.join(",");
        $('edit_credit').value = flow.credit;
        $('edit_debit').value = flow.debit;
        $('edit_description').value = flow.description;
        $('edit_amount').value = flow.amount;

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

    flowid_being_edited = flowid;
}

function save_flow() {
    var flow = {};
    flow.start = $('edit_start').value;
    flow.end = $('edit_end').value;
    flow.period = parseInt($('edit_period').value);
    flow.unit = $('edit_unit').value;
    flow.days = [];
    var d = $('edit_days').value.split(",");
    for (var i = 0; i < d.length; i++) {
        flow.days.push(parseInt(d[i]));
    }
    flow.credit = $('edit_credit').value;
    flow.debit = $('edit_debit').value;
    flow.description = $('edit_description').value;
    flow.amount = $('edit_amount').value;

    if (flowid_being_edited == null) {
        // New flow
        var h = http("POST", '/flows', false, "Saving new flow");
        // TODO: add new table row
        h[201] = function(h) {
            flow.id = h.getResponseHeader("Location");
            add_flow_row(flow);
        };
    } else {
        // Update existing flow
        var h = http("PUT", '/flows/' + flowid_being_edited, false,
                     "Updating flow " + flowid_being_edited);
        h[204] = function(h) {
            // Remove and re-add the row. This will also move it if dates change
            var flowrow = $('flowrow' + flowid_being_edited);
            flowrow.parentNode.removeChild(flowrow);
            flow.id = flowid_being_edited;
            flowrow = add_flow_row(flow);

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

//                                   OTHER                                   //

budgets = [];

function drawChart() {
    // Get the budgets data
    var h = http("GET", "/budgets" + document.location.search, true,
                 "Retrieving budget data...");
    h[200] = function(h) {
        var j = JSON.parse(h.responseText);
        if (keys(j.data).length == 0) return;

        console.log(JSON.stringify(j.data));
        for (var a in j.data) {
            var dgdata = j.data[a];
            var acctid = parseInt(a);
            for (var i = 0; i < budgets.length; i++) {
                if (budgets[i][0].indexOf(acctid) != -1) {
                    var dataset = budgets[i][1];
                    if (budgets[i][2] == ' Budget') {
                        for (var dg in dgdata) {
                            dataset[dg] = (dataset[dg] || 0) + dgdata[dg][0];
                        }
                    } else { // ' Actual'
                        for (var dg in dgdata) {
                            dataset[dg] = (dataset[dg] || 0) + dgdata[dg][1];
                        }
                    }
                }
            }
        }

        var table = make_table(budgets);
        draw_table(table, 'Budgets');

        // Show transactions 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 = budgets[item.column - 1][0];
                } else if (item.row != null) {
                    seldg = table.getValue(item.row, 0);
                } else if (item.column != null) {
                    selaccts = budgets[item.column - 1][0];
                }
            }

            var qs = [];
            if (selaccts != null) qs.push("accounts=" + selaccts.join(","));
            if (seldg == null) {
                qs.push("years=" + query['years']);
                qs.push("months=" + query['months']);
                qs.push("days=" + query['days']);
            } else {
                qs.push("years=" + seldg.substring(0, 4));
                if (seldg.length >= 7) {
                    qs.push("months=" + parseInt(seldg.substring(5, 7)));
                }
                if (seldg.length >= 10) {
                    qs.push("days=" + parseInt(seldg.substring(8, 10)));
                }
            }
            window.open("/transactions/manager?" + qs.join("&"), "_blank");
        }

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

    }
    h.send();
}

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

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;
    $("txslink").href = $("txslink").href + "?" + qs;
    $("varslink").href = $("varslink").href + "?" + qs;

    populate_accounts();
    populate_flows();

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

    edit_flow(null);
    $('edit_start').value = ISODate(new Date());
}

</script>
</head>

<body onLoad="init()">
<div class='header'>
    <h1 id="banner">Flowrate:
        <a id="accountslink" href='/accounts/manager'>Accounts</a>
        | <a id="txslink" href='/transactions/manager'>Transactions</a>
        | Flows
        | <a id="varslink" href='/variables/manager'>Variables</a>
        | <a id="importlink" href="/import">Import</a>
    </h1>
    <div id="status"></div>
    <p><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></p>
</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='flows'>
<tr id='flowedit'>
    <td><input id='edit_save' type='button' value='Save' onClick='save_flow()' /></td>
    <td><input id='edit_start' type='text' size='10' /></td>
    <td><input id='edit_end' type='text' size='10' /></td>
    <td><input id='edit_period' type='text' size='3' /></td>
    <td><select id='edit_unit'>
        <option>years</option>
        <option selected="selected">months</option>
        <option>weeks</option>
        </select></td>
    <td><input id='edit_days' type='text' size='4' /></td>
    <td><select id='edit_credit'></select></td>
    <td><select id='edit_debit'></select></td>
    <td><input id='edit_description' type='text' value="New flow" /></td>
    <td><input id='edit_amount' type='text' size='10' style="text-align: right" value="0.00" /></td>
</tr>
<tr id='flowheader'>
    <th>ID&nbsp;<span onClick="edit_flow(null)" title="New flow" style="cursor: pointer">(+)</span></th>
    <th>Start&nbsp;Date</th>
    <th>End&nbsp;Date</th>
    <th>Period</th>
    <th>Unit</th>
    <th>Days</th>
    <th>Credit</th>
    <th>Debit</th>
    <th>Description</th>
    <th>Amount</th>
</tr>
</table>
</body>
</html>