stepwise / static /visuals /render_reval.js
Ashish-K's picture
Accountancy: Revaluation and capital accounts chapter (5 review rounds); exact rupee marking; currency sign accepted (part 2)
cbf5092 verified
Raw History Blame Contribute Delete
12.2 kB
// Drawing for the revaluation / capital accounts class: adjustments table, Revaluation A/c, partners' capital accounts, explorer.
import { ratioStr, inr, rupee } from "./core.js";
import * as RV from "./revaluation.js";
function el(tag, attrs = {}, ...kids) {
const n = document.createElement(tag);
for (const [k, v] of Object.entries(attrs || {})) { if (v == null || v === false) continue; if (k === "text") n.textContent = v; else if (k.startsWith("on")) n.addEventListener(k.slice(2), v); else n.setAttribute(k, v === true ? "" : v); }
for (const k of kids.flat()) if (k != null) n.append(k.nodeType ? k : document.createTextNode(String(k)));
return n;
}
const cap = (t) => el("div", { class: "vz-cap", text: t });
export function adjustmentsTable(inst) {
const t = el("table", { class: "vz-table" });
t.append(el("thead", null, el("tr", null, el("th", { text: "Item" }), el("th", { class: "num", text: "In the books (₹)" }), el("th", { class: "num", text: "Agreed now (₹)" }))));
const tb = el("tbody");
for (const it of inst.items) {
if (it.kind === "asset" || it.kind === "liability") tb.append(el("tr", null, el("td", { text: `${it.name} (${it.kind})` }), el("td", { class: "num", text: inr(it.book) }), el("td", { class: "num", text: inr(it.now) })));
if (it.kind === "provision") tb.append(el("tr", null, el("td", { text: `Provision for doubtful debts (debtors ${inr(it.debtors)}, new rate ${it.rate}%)` }), el("td", { class: "num", text: inr(it.book) }), el("td", { class: "num", text: `${it.rate}% of debtors` })));
if (it.kind === "unrecAsset" || it.kind === "unrecLiab") tb.append(el("tr", null, el("td", { text: `${it.name.replace(/^./, (s) => s.toUpperCase())} (${it.kind === "unrecAsset" ? "asset" : "liability"}, not recorded)` }), el("td", { class: "num", text: "—" }), el("td", { class: "num", text: inr(it.now) })));
}
if (inst.wcr) tb.append(el("tr", null, el("td", { text: "Workmen Compensation Reserve / claim" }), el("td", { class: "num", text: inr(inst.wcr.reserve) }), el("td", { class: "num", text: `claim ${inr(inst.wcr.claim)}` })));
for (const [name, v] of [["General Reserve", inst.gr], ["Profit and Loss A/c", inst.pl], ["Advertisement Suspense A/c", inst.adv]]) if (v) tb.append(el("tr", null, el("td", { text: name + (name.startsWith("Profit") ? (v > 0 ? " (credit)" : " (debit)") : "") }), el("td", { class: "num", text: inr(Math.abs(v)) }), el("td", { class: "num", text: "—" })));
t.append(tb); return el("div", { class: "vz-block" }, cap("Adjustments agreed"), t);
}
export function revalAccount(inst, sol) {
const rows = Math.max(sol.dr.length + (sol.result > 0 ? 1 : 0), sol.cr.length + (sol.result < 0 ? 1 : 0));
const drL = [...sol.dr.map((l) => [`To ${l.name}`, l.amt]), ...(sol.result > 0 ? [[`To Profit: ${sol.oldIdx.map((i) => `${inst.partners[i]} ${inr(sol.oldShare(i).mul(sol.result).val())}`).join(", ")}`, sol.result]] : [])];
const crL = [...sol.cr.map((l) => [`By ${l.name}`, l.amt]), ...(sol.result < 0 ? [[`By Loss: ${sol.oldIdx.map((i) => `${inst.partners[i]} ${inr(sol.oldShare(i).mul(-sol.result).val())}`).join(", ")}`, -sol.result]] : [])];
const t = el("table", { class: "vz-table vz-t" });
t.append(el("thead", null, el("tr", null, el("th", { text: "Dr. Particulars" }), el("th", { class: "num", text: "₹" }), el("th", { text: "Cr. Particulars" }), el("th", { class: "num", text: "₹" }))));
const tb = el("tbody");
for (let k = 0; k < rows; k++) { const a = drL[k] || ["", ""], b = crL[k] || ["", ""]; tb.append(el("tr", null, el("td", { text: a[0] }), el("td", { class: "num", text: a[1] === "" ? "" : inr(a[1]) }), el("td", { text: b[0] }), el("td", { class: "num", text: b[1] === "" ? "" : inr(b[1]) }))); }
const tot = drL.reduce((x, l) => x + l[1], 0);
tb.append(el("tr", { class: "tot" }, el("td"), el("td", { class: "num", text: inr(tot) }), el("td"), el("td", { class: "num", text: inr(tot) })));
t.append(tb); return el("div", { class: "vz-block vz-scroll" }, cap("Revaluation A/c"), t);
}
export function capitalAccounts(inst, sol, accOverride = null, only = null) {
const P = inst.partners; const idx = only != null ? [only] : P.map((_, i) => i);
const acc = (i) => (accOverride && i === only ? accOverride : sol.acc[i]);
const names = (side) => { const s = []; for (const i of idx) for (const l of acc(i)[side]) if (!s.includes(l.name)) s.push(l.name); return s; };
const t = el("table", { class: "vz-table vz-cap-acc" });
t.append(el("thead", null, el("tr", null, el("th", { text: "Particulars" }), ...idx.map((i) => el("th", { class: "num", text: P[i] })))));
const tb = el("tbody");
const section = (side, label) => {
tb.append(el("tr", { class: "sec" }, el("td", { colspan: idx.length + 1, text: label })));
for (const nm of names(side)) tb.append(el("tr", null, el("td", { text: `${side === "dr" ? "To" : "By"} ${nm}` }), ...idx.map((i) => { const l = acc(i)[side].find((x) => x.name === nm); return el("td", { class: "num", text: l ? inr(l.amt) : "—" }); })));
tb.append(el("tr", { class: "tot" }, el("td", { text: "Total" }), ...idx.map((i) => el("td", { class: "num", text: inr(acc(i)[side].reduce((x, l) => x + l.amt, 0)) }))));
};
section("dr", "Dr. side"); section("cr", "Cr. side");
t.append(tb);
return el("div", { class: "vz-block vz-scroll" }, cap(only != null ? `${P[only]}'s Capital A/c (as prepared)` : "Partners' Capital A/cs"), t);
}
export function revalFigure(fig, { reveal = false } = {}) {
const inst = fig.inst; const sol = RV.solve(inst); const show = fig.show || [];
const box = el("div", { class: "vz" }, cap(`${inst.firm} · ${RV.SCENARIOS[inst.scenario]} · old ratio ${ratioStr(sol.oldIdx.map((i) => inst.old[i]))}`), adjustmentsTable(inst));
if (show.includes("account") && fig.account) box.append(capitalAccounts(inst, sol, fig.account, fig.partner));
if (reveal) box.append(revalAccount(inst, sol), capitalAccounts(inst, sol));
if (reveal && fig.after) { const s2 = RV.solve(fig.after); box.append(cap("After the change"), revalAccount(fig.after, s2), capitalAccounts(fig.after, s2)); }
return box;
}
export function revalExplorer(inst0, rngNew) {
let inst = JSON.parse(JSON.stringify(inst0)); let step = 0;
const STEPS = ["The adjustments", "Revaluation A/c", "Share the result in the OLD ratio", "Reserves and accumulated items", "Partners' Capital A/cs"];
const root = el("div", { class: "vz vz-explorer" }, cap(`${inst.firm} · ${RV.SCENARIOS[inst.scenario]}`));
const say = el("p", { class: "vz-say", "aria-live": "polite" }); const stage = el("div", { class: "vz-stage" }); const controls = el("div", { class: "vz-controls" });
const back = el("button", { class: "btn ghost", type: "button", text: "← Back", onclick: () => { if (step > 0) { step--; draw(); } } });
const next = el("button", { class: "btn", type: "button", text: "Next →", onclick: () => { if (step < STEPS.length - 1) { step++; draw(); } } });
const dots = el("ol", { class: "vz-dots" });
root.append(el("div", { class: "vz-steps" }, back, dots, next), say, stage, controls);
const words = [
() => "Each line is something the partners agreed to change. Increases in assets and decreases in liabilities are gains; the opposite are losses.",
() => "Losses on the debit side (To), gains on the credit side (By). Only the CHANGE in each value is entered; for the provision, only the difference between the new and old provision.",
() => `The balance belongs to the OLD partners in the OLD ratio ${ratioStr(RV.solve(inst).oldIdx.map((i) => inst.old[i]))}, whatever the new ratio is. Change the new ratio below and watch: these shares do not move.`,
() => "Reserves and accumulated profits are credited, accumulated losses and Advertisement Suspense debited, all in the OLD ratio. A workmen compensation claim covered by the reserve is kept back as a liability.",
() => "Everything lands in the Capital A/cs: opening balance, revaluation, reserves, goodwill, cash brought in. The balance is the closing capital.",
];
function shareTable(sol) {
const t = el("table", { class: "vz-table" }); t.append(el("thead", null, el("tr", null, el("th", { text: "Old partner" }), el("th", { class: "num", text: "Old share" }), el("th", { class: "num", text: sol.result >= 0 ? "Profit (₹)" : "Loss (₹)" }))));
const tb = el("tbody"); for (const i of sol.oldIdx) tb.append(el("tr", null, el("td", { text: inst.partners[i] }), el("td", { class: "num", text: String(sol.oldShare(i)) }), el("td", { class: "num", text: inr(sol.oldShare(i).mul(Math.abs(sol.result)).val()) })));
t.append(tb); return t;
}
function draw() {
const sol = RV.solve(inst);
dots.replaceChildren(...STEPS.map((s, i) => el("li", { class: i === step ? "on" : i < step ? "done" : "" }, el("button", { type: "button", "aria-label": s, title: s, onclick: () => { step = i; draw(); } }))));
back.disabled = step === 0; next.disabled = step === STEPS.length - 1;
say.replaceChildren(el("strong", { text: `${step + 1}. ${STEPS[step]}. ` }), words[step]());
stage.replaceChildren(adjustmentsTable(inst));
if (step >= 1) stage.append(revalAccount(inst, sol));
if (step === 2) stage.append(shareTable(sol));
if (step >= 3) stage.append(capitalAccounts(inst, sol));
}
function buildControls() {
controls.replaceChildren();
const asset = inst.items.find((i) => i.kind === "asset");
if (asset) {
const base = inst0.items.find((i) => i.name === asset.name).book;
const inp = el("input", { type: "range", min: Math.round(base * 0.7 / 1000) * 1000, max: Math.round(base * 1.3 / 1000) * 1000, step: 1000, value: asset.now, "aria-label": asset.name });
const out = el("span", { class: "vz-num", text: rupee(asset.now) });
inp.addEventListener("input", () => { asset.now = Number(inp.value); out.textContent = rupee(asset.now); draw(); });
controls.append(el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: `${asset.name} agreed at` }), inp, out));
}
if (inst.wcr) {
const inp = el("input", { type: "range", min: 0, max: inst.wcr.reserve * 2, step: 1000, value: inst.wcr.claim, "aria-label": "Workmen compensation claim" });
const out = el("span", { class: "vz-num", text: rupee(inst.wcr.claim) });
inp.addEventListener("input", () => { inst.wcr.claim = Number(inp.value); out.textContent = rupee(inst.wcr.claim); draw(); });
controls.append(el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: `Compensation claim (reserve ${rupee(inst.wcr.reserve)})` }), inp, out));
}
if (inst.scenario !== "retirement") {
const row = el("div", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: "New ratio" }));
inst.partners.forEach((p, i) => {
const val = el("span", { class: "vz-num", text: inst.new[i] });
row.append(el("span", { class: "vz-step" }, p, el("button", { type: "button", class: "react", "aria-label": `less for ${p}`, text: "−", onclick: () => { if (inst.new[i] > 1) { inst.new[i]--; val.textContent = inst.new[i]; draw(); } } }), val,
el("button", { type: "button", class: "react", "aria-label": `more for ${p}`, text: "+", onclick: () => { if (inst.new[i] < 9) { inst.new[i]++; val.textContent = inst.new[i]; draw(); } } })));
});
controls.append(row);
}
controls.append(el("button", { type: "button", class: "react", text: "Reset", onclick: () => { inst = JSON.parse(JSON.stringify(inst0)); buildControls(); draw(); } }));
}
buildControls(); draw();
return root;
}
export const CSS = `
.vz-scroll,.vz-block{overflow-x:auto;max-width:100%}
.vz>*,.vz-stage>*{min-width:0}
.vz,.vz-stage{min-width:0;max-width:100%}
.vz-t td:nth-child(3),.vz-t th:nth-child(3){border-left:2px solid var(--rule)}
.vz-table tr.tot td{font-weight:600;border-top:2px solid var(--rule)}
.vz-table tr.sec td{font-family:var(--font-display);color:var(--ink-soft);background:var(--paper);font-size:.85rem}
.vz-cap-acc td:first-child{white-space:normal;min-width:11em}
`;