Add static/index.js for the main sheet
This commit is contained in:
1 parent
6a0e6a1c4d
commit
fd7db19b65
1 file changed
+69
@@ -0,0 +1,69 @@
|
|||||||
|
/* Skuld — the main sheet: one row per account, the totals, the non-taxable
|
||||||
|
* accounts and the TOT rows, in the order of docs/spec.md section 7.2. */
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var rows = document.querySelector("[data-rows]");
|
||||||
|
var sheet = document.querySelector("[data-sheet]");
|
||||||
|
var empty = document.querySelector("[data-empty]");
|
||||||
|
var yearInput = document.querySelector('[name="year"]');
|
||||||
|
|
||||||
|
function currentYear() {
|
||||||
|
return new Date().getFullYear();
|
||||||
|
}
|
||||||
|
|
||||||
|
function render(markets, report) {
|
||||||
|
rows.textContent = "";
|
||||||
|
if (!markets.length) {
|
||||||
|
empty.hidden = false;
|
||||||
|
sheet.hidden = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
empty.hidden = true;
|
||||||
|
sheet.hidden = false;
|
||||||
|
|
||||||
|
var totalSats = report.accounts.reduce(function (sum, account) {
|
||||||
|
return sum + account.sats;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
rows.appendChild(Skuld.totalsRow(report.totals, totalSats));
|
||||||
|
report.accounts.forEach(function (account) {
|
||||||
|
rows.appendChild(Skuld.sheetRow(account));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (report.non_taxable.length) {
|
||||||
|
rows.appendChild(Skuld.gapRow());
|
||||||
|
report.non_taxable.forEach(function (account) {
|
||||||
|
rows.appendChild(Skuld.satsOnlyRow(account.name, account.sats, ""));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (report.custody_totals.length) {
|
||||||
|
rows.appendChild(Skuld.gapRow());
|
||||||
|
report.custody_totals.forEach(function (reconciliation) {
|
||||||
|
rows.appendChild(Skuld.totRow(reconciliation));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function load() {
|
||||||
|
var year = Number(yearInput.value) || currentYear();
|
||||||
|
yearInput.value = year;
|
||||||
|
Skuld.clearErrors();
|
||||||
|
return Promise.all([
|
||||||
|
Skuld.api("GET", "/api/markets"),
|
||||||
|
Skuld.api("GET", "/api/report?year=" + year)
|
||||||
|
]).then(function (results) {
|
||||||
|
render(results[0], results[1]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Skuld.on("index", function () {
|
||||||
|
yearInput.value = currentYear();
|
||||||
|
yearInput.addEventListener("change", function () {
|
||||||
|
load().catch(Skuld.showError);
|
||||||
|
});
|
||||||
|
return load();
|
||||||
|
});
|
||||||
|
})();
|
||||||
Reference in new issue
Block a user