nx_stmt_lib.nx source
↩ module page · 88 lines · 4297 B
1// nx_stmt_lib.nx -- F985: FINANCIAL STATEMENTS, DERIVED FROM THE LEDGER. Never typed, never stored.
2//
3// Every figure below is computed from ledger transfers at read time via the chart of accounts. There
4// is no place to enter a balance-sheet number, so a statement CANNOT disagree with the books -- the
5// classic failure where the reported balance sheet and the underlying ledger drift apart is not a bug
6// to guard against here, it is structurally impossible.
7//
8// WHAT IT DERIVES:
9// trial balance -- total debits vs total credits across a set of accounts
10// balance sheet -- assets = liabilities + equity (the accounting equation, CHECKED not assumed)
11// income statement-- revenue - expenses = net income
12//
13// ★FAIL-CLOSED ON A PARTIAL PICTURE (the property that matters): if ANY account in the requested set
14// has no registered kind or no registered side, the statement is REFUSED outright (returns 0) rather
15// than silently omitting that account. A balance sheet that quietly drops an account it did not
16// understand is worse than no balance sheet -- it looks authoritative and is wrong. Omission is the
17// most dangerous error in financial reporting, so it is the one thing this refuses to do.
18//
19// SCALE ENVELOPE (declared): each figure walks the caller's account list and calls coa_natural per
20// account, which walks the transfer index -- O(accounts x transfers). Same rung as nx_ledger_lib.
21// license_tier: ORIGINAL No hw writes (Rule 26). LIB (no main).
22
23import "nx_coa_lib.nx"
24
25// TRIAL BALANCE across an account set. out[0]=total debits, out[1]=total credits.
26// Returns 1 when debits == credits (the books are internally consistent), else 0.
27func stmt_trial_balance(ledger_prefix: *u8, accts: *i64, n: i64, out: *i64) -> i64 {
28 let s: *i64 = sys_mmap(8 * 4) as *i64
29 out[0] = 0
30 out[1] = 0
31 var i: i64 = 0
32 while i < n {
33 led_sums(ledger_prefix, accts[i] as *u8, s)
34 out[0] = out[0] + s[0]
35 out[1] = out[1] + s[1]
36 i = i + 1
37 }
38 if out[0] == out[1] { return 1 }
39 return 0
40}
41
42// Sum the NATURAL balances of every account in the set whose registered kind == `kind`.
43// Writes the sum to out[0]. Returns 1 on success.
44// Returns 0 = FAIL-CLOSED if ANY account in the set is unresolvable (no kind, or kind has no side).
45func stmt_by_kind(ledger_prefix: *u8, coa_prefix: *u8, accts: *i64, n: i64, kind: *u8, out: *i64) -> i64 {
46 var total: i64 = 0
47 let ty: *u8 = sys_mmap(64)
48 let one: *i64 = sys_mmap(16) as *i64
49 var i: i64 = 0
50 while i < n {
51 let a: *u8 = accts[i] as *u8
52 if coa_type(coa_prefix, a, ty) != 1 { return 0 }
53 if coa_natural(ledger_prefix, coa_prefix, a, one) != 1 { return 0 }
54 if mt_streq(ty, kind) == 1 { total = total + one[0] }
55 i = i + 1
56 }
57 out[0] = total
58 return 1
59}
60
61// BALANCE SHEET. out[0]=assets out[1]=liabilities out[2]=equity out[3]=variance.
62// variance = assets - (liabilities + equity); it MUST be 0 for a sound book.
63// Returns 1 if derived AND balanced, 0 if refused (fail-closed) or out of balance -- the caller
64// distinguishes them by reading out[3] (untouched on refusal).
65func stmt_balance_sheet(ledger_prefix: *u8, coa_prefix: *u8, accts: *i64, n: i64, out: *i64) -> i64 {
66 let one: *i64 = sys_mmap(16) as *i64
67 if stmt_by_kind(ledger_prefix, coa_prefix, accts, n, "asset" as *u8, one) != 1 { return 0 }
68 out[0] = one[0]
69 if stmt_by_kind(ledger_prefix, coa_prefix, accts, n, "liability" as *u8, one) != 1 { return 0 }
70 out[1] = one[0]
71 if stmt_by_kind(ledger_prefix, coa_prefix, accts, n, "equity" as *u8, one) != 1 { return 0 }
72 out[2] = one[0]
73 out[3] = out[0] - (out[1] + out[2])
74 if out[3] == 0 { return 1 }
75 return 0
76}
77
78// INCOME STATEMENT. out[0]=revenue out[1]=expenses out[2]=net income (revenue - expenses).
79// Returns 1 on success, 0 = fail-closed.
80func stmt_income(ledger_prefix: *u8, coa_prefix: *u8, accts: *i64, n: i64, out: *i64) -> i64 {
81 let one: *i64 = sys_mmap(16) as *i64
82 if stmt_by_kind(ledger_prefix, coa_prefix, accts, n, "revenue" as *u8, one) != 1 { return 0 }
83 out[0] = one[0]
84 if stmt_by_kind(ledger_prefix, coa_prefix, accts, n, "expense" as *u8, one) != 1 { return 0 }
85 out[1] = one[0]
86 out[2] = out[0] - out[1]
87 return 1
88}