Files

158 lines
5.6 KiB
JavaScript

/* Skuld — the year report of docs/spec.md section 6: the accounts, the totals,
* the tax figures, the four figures for the tax return, the non-taxable
* accounts and the TOT rows. */
(function () {
"use strict";
var yearInput = document.querySelector('[name="year"]');
var exportLink = document.querySelector("[data-export]");
var accountsBody = document.querySelector("[data-accounts]");
var totalsBox = document.querySelector("[data-totals]");
var taxReturnBox = document.querySelector("[data-tax-return]");
var taxNote = document.querySelector("[data-tax-note]");
var priceText = document.querySelector("[data-price]");
var methodText = document.querySelector("[data-method]");
var nonTaxableCard = document.querySelector("[data-non-taxable]");
var nonTaxableBody = document.querySelector("[data-non-taxable-rows]");
var custodyCard = document.querySelector("[data-custody]");
var custodyBody = document.querySelector("[data-custody-rows]");
function currentYear() {
return new Date().getFullYear();
}
function figure(label, value) {
var box = document.createElement("div");
box.className = "figure";
var labelBox = document.createElement("div");
labelBox.className = "figure-label";
labelBox.textContent = label;
var valueBox = document.createElement("div");
valueBox.className = "figure-value";
valueBox.textContent = value;
box.appendChild(labelBox);
box.appendChild(valueBox);
return box;
}
function cell(row, text, className) {
var td = document.createElement("td");
td.textContent = text;
if (className) {
td.className = className;
}
row.appendChild(td);
return td;
}
function renderAccounts(report) {
accountsBody.textContent = "";
var totalSats = report.accounts.reduce(function (sum, account) {
return sum + account.sats;
}, 0);
accountsBody.appendChild(Skuld.totalsRow(report.totals, totalSats));
report.accounts.forEach(function (account) {
accountsBody.appendChild(Skuld.sheetRow(account));
});
}
function renderTotals(report, settings) {
var totals = report.totals;
totalsBox.textContent = "";
var figures = [
["Formue", Skuld.formatKroner(totals.formue_ore)],
["Gevinst", Skuld.formatKroner(totals.gevinst_ore)],
["Tap", Skuld.formatKroner(totals.tap_ore)],
["Brukt inngangsverdi", Skuld.formatKroner(totals.brukt_ore)],
["Netto", Skuld.formatKroner(totals.netto_ore)],
["Inntekt", Skuld.formatKroner(totals.inntekt_ore)],
["Skatt (" + settings.tax_rate_percent + " %)", Skuld.formatKroner(totals.skatt_ore)],
["Formuesskatt", Skuld.formatKroner(totals.formuesskatt_ore)],
["Forvaltningskostnad", Skuld.formatKroner(totals.admin_cost_ore)]
];
figures.forEach(function (pair) {
totalsBox.appendChild(figure(pair[0], pair[1]));
});
var price = report.year_end_price_ore
? Skuld.formatKroner(report.year_end_price_ore) + " per BTC"
: "Ingen kurs for dette året";
priceText.textContent = "Kurs " + Skuld.formatDate(
String(report.year + 1) + "-01-01"
) + ": " + price
+ (report.year_end_price_source ? " (" + report.year_end_price_source + ")" : "");
methodText.textContent = "Metoden som gav tala: " + totals.lot_method + ".";
}
function renderTaxReturn(report) {
var block = report.skattemelding;
taxReturnBox.textContent = "";
[
["Formue", block.formue],
["Gevinst", block.gevinst],
["Tap", block.tap],
["Inntekt", block.inntekt]
].forEach(function (pair) {
taxReturnBox.appendChild(figure(pair[0], Skuld.formatKroner(pair[1])));
});
taxNote.textContent = block.note
+ (block.accounts.length ? " Med i grunnlaget: " + block.accounts.join(", ") + "." : "");
}
function renderNonTaxable(report) {
nonTaxableBody.textContent = "";
nonTaxableCard.hidden = !report.non_taxable.length;
report.non_taxable.forEach(function (account) {
var row = document.createElement("tr");
cell(row, account.name, "");
cell(row, Skuld.formatSats(account.sats), "numeric");
cell(row, account.gevinst_ore ? Skuld.formatKroner(account.gevinst_ore) : "-", "numeric");
cell(row, account.tap_ore ? Skuld.formatKroner(account.tap_ore) : "-", "numeric");
nonTaxableBody.appendChild(row);
});
}
function renderCustody(report) {
custodyBody.textContent = "";
custodyCard.hidden = !report.custody_totals.length;
report.custody_totals.forEach(function (reconciliation) {
var row = document.createElement("tr");
cell(row, reconciliation.name, "");
cell(row, Skuld.formatSats(reconciliation.sats), "numeric");
custodyBody.appendChild(row);
});
}
function load() {
var year = Number(yearInput.value) || currentYear();
yearInput.value = year;
exportLink.href = "/api/report/export.csv?year=" + year;
Skuld.clearErrors();
return Promise.all([
Skuld.api("GET", "/api/report?year=" + year),
Skuld.api("GET", "/api/settings")
]).then(function (results) {
var report = results[0];
var settings = results[1];
renderAccounts(report);
renderTotals(report, settings);
renderTaxReturn(report);
renderNonTaxable(report);
renderCustody(report);
return null;
});
}
Skuld.on("report", function () {
yearInput.value = currentYear();
yearInput.addEventListener("change", function () {
load().catch(Skuld.showError);
});
document.querySelector("[data-print]").addEventListener("click", function () {
window.print();
});
return load();
});
})();