Files
skuld/static/index.js
T

70 lines
1.9 KiB
JavaScript

/* 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();
});
})();