aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/PROTOCOL.md9
-rw-r--r--docs/STATE.md10
-rw-r--r--src/reports.c16
-rw-r--r--src/reports.h15
-rw-r--r--src/sie.c25
-rw-r--r--tests/test_core.c121
6 files changed, 174 insertions, 22 deletions
diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md
index 081eb6a..d50f5ee 100644
--- a/docs/PROTOCOL.md
+++ b/docs/PROTOCOL.md
@@ -451,7 +451,11 @@ linked to that voucher and each item carries that `voucher_id`.
| `sru.export` | `fiscal_year`, `adjustments?`, `submitter?`, `assisted?`, `audited?`, `ignore_unmapped?` | `INFO.SRU` + `BLANKETTER.SRU` (ISO-8859-1, base64) |
All reports are pure reads, respect locks, and return JSON rows. Amounts are
-öre. `report.general_ledger` (huvudbok) returns account blocks:
+öre. IB (ingående balans) for a balance account (asset, liability, equity) is
+all earlier history, including earlier years' `IB` vouchers, plus this
+year's `IB` vouchers; a P&L account (revenue, expense) restarts at zero at
+every fiscal-year start, so its IB is only this year's `IB` vouchers and,
+with a narrowed `from`, the year's movements before it. `report.general_ledger` (huvudbok) returns account blocks:
`{"fiscal_year","from","to","last_voucher":{...},"accounts":[{"account",
"name","ib_ore","debit_ore","credit_ore","ub_ore","rows":[{"series",
"number","date","description","row_description","debit_ore","credit_ore",
@@ -519,7 +523,8 @@ support `dry_run`, which validates without writing.
| `sie.import` | `content_base64` or `path`, `dry_run?` | one file per call; creates missing accounts and posts #VER as `source:"sie_import"`; only into an empty org fiscal year; `#IB` becomes an `IB` voucher when the year has no earlier history, otherwise the earlier vouchers carry the balances |
SIE 4 files are written in CP437 with PC8 format, `#SIETYP 4`, `#FNR`, `#ORGNR`,
-`#KONTO`, `#IB`, `#UB`, `#RES`, `#VER`, `#TRANS`. Import is the migration path
+`#KONTO`, `#IB`, `#UB`, `#RES`, `#VER`, `#TRANS`. `#IB`/`#UB` are written for
+balance accounts and `#RES` for P&L accounts, with IB as in §7.6. Import is the migration path
from Fortnox/Visma/BL and must be dry-run first; it reports exactly what would
be created.
diff --git a/docs/STATE.md b/docs/STATE.md
index 4d448fe..29fbada 100644
--- a/docs/STATE.md
+++ b/docs/STATE.md
@@ -72,6 +72,16 @@ unit tests and the docs consistency check.
`TUI-GUIDELINES.md`: drafts of encrypted fields (an employee's
personnummer) and `Enter` on action rows. Spec in `TUI-GUIDELINES.md`
"Interaction model"; decisions in `DECISIONS.md` #28.
+- **IB carry-forward (2026-09-22, branch `eff/ib-carry`, not deployed)**:
+ reports and `sie.export` now carry earlier years' `IB` vouchers into the
+ opening balance (Makandra's 2021 IB voucher held aktiekapital 2081/1940,
+ which vanished from every later year) and restart P&L accounts at each
+ fiscal-year start (they used to accumulate since 2021). SIE writes
+ `#IB`/`#UB` only for balance accounts and `#RES` only for P&L. With it,
+ Makandra's 2025/26 opening balance shows the real 17 857,63 kr gap from
+ 2022/23 (result transfer short of the P&L) instead of hiding it.
+ `test_core` has `ib_carry`. Touches the same `balance_query` SQL as
+ `eff/imported-closings` — expect a small textual conflict when merging.
- **Mail configuration**: Makandra AB (org 2) has **no** `smtp_*` settings
in bokf, so `invoice.send` there is `SMTP_NOT_CONFIGURED` (fine if
invoices are sent elsewhere — set them up when wanted). Mock AB (org 1)
diff --git a/src/reports.c b/src/reports.c
index 2ca7e45..d8ed0d2 100644
--- a/src/reports.c
+++ b/src/reports.c
@@ -58,8 +58,8 @@ static yyjson_mut_val *fy_json(yyjson_mut_doc *doc, const struct fy_info *fy)
return o;
}
-/* One row per account with IB and period movements. IB is the series "IB"
- voucher(s) of this fiscal year plus all non-IB history before `from`.
+/* One row per account with IB and period movements; IB as defined by
+ REPORT_IB_ROW_SQL.
Amounts signed: debit positive. */
static yyjson_mut_val *balance_query(yyjson_mut_doc *doc, sqlite3 *db,
int64_t org_id, int64_t fy_id,
@@ -69,19 +69,17 @@ static yyjson_mut_val *balance_query(yyjson_mut_doc *doc, sqlite3 *db,
char ib_series[16];
db_setting_copy(db, org_id, "series_ib", "IB", ib_series,
sizeof ib_series);
- char sql[1280];
+ char sql[2048];
snprintf(sql, sizeof sql,
"SELECT a.number,a.name,a.type,"
" COALESCE(SUM(CASE WHEN v.series <> 'IB' AND v.series <> ?5"
" AND v.date BETWEEN ?2 AND ?3 THEN r.debit_ore END),0),"
" COALESCE(SUM(CASE WHEN v.series <> 'IB' AND v.series <> ?5"
" AND v.date BETWEEN ?2 AND ?3 THEN r.credit_ore END),0),"
- " COALESCE(SUM(CASE WHEN ((v.series = 'IB' OR v.series = ?5)"
- " AND v.fiscal_year_id = ?4) OR (v.series <> 'IB'"
- " AND v.series <> ?5 AND v.date < ?2) THEN r.debit_ore END),0),"
- " COALESCE(SUM(CASE WHEN ((v.series = 'IB' OR v.series = ?5)"
- " AND v.fiscal_year_id = ?4) OR (v.series <> 'IB'"
- " AND v.series <> ?5 AND v.date < ?2) THEN r.credit_ore END),0)"
+ " COALESCE(SUM(CASE WHEN " REPORT_IB_ROW_SQL
+ " THEN r.debit_ore END),0),"
+ " COALESCE(SUM(CASE WHEN " REPORT_IB_ROW_SQL
+ " THEN r.credit_ore END),0)"
" FROM accounts a"
" LEFT JOIN (voucher_rows r JOIN vouchers v"
" ON v.org_id=r.org_id AND v.id=r.voucher_id)"
diff --git a/src/reports.h b/src/reports.h
index c4a6e4e..82c29e5 100644
--- a/src/reports.h
+++ b/src/reports.h
@@ -6,6 +6,21 @@
#include "yyjson.h"
+/* SQL condition: voucher row `r` of voucher `v` on account `a` belongs to the
+ opening balance of fiscal year ?4 (org ?1) for a period starting ?2; ?5 is
+ the configured IB series. Balance accounts carry all earlier history,
+ including earlier years' IB vouchers; P&L accounts restart at the year
+ start, so only this year's IB vouchers and movements before ?2 count. */
+#define REPORT_IB_ROW_SQL \
+ "((v.series = 'IB' OR v.series = ?5) AND (v.fiscal_year_id = ?4" \
+ " OR (v.date < (SELECT f.start_date FROM fiscal_years f" \
+ " WHERE f.org_id = ?1 AND f.id = ?4)" \
+ " AND a.type NOT IN ('revenue','expense'))))" \
+ " OR (v.series <> 'IB' AND v.series <> ?5 AND v.date < ?2" \
+ " AND (a.type NOT IN ('revenue','expense') OR v.date >=" \
+ " (SELECT f.start_date FROM fiscal_years f" \
+ " WHERE f.org_id = ?1 AND f.id = ?4)))"
+
yyjson_mut_val *report_trial_balance(yyjson_mut_doc *doc, sqlite3 *db,
int64_t org_id, int64_t fy_id,
const char *from, const char *to,
diff --git a/src/sie.c b/src/sie.c
index 6e0eab4..c2ce19a 100644
--- a/src/sie.c
+++ b/src/sie.c
@@ -11,6 +11,7 @@
#include "db.h"
#include "ledger.h"
#include "log.h"
+#include "reports.h"
#include "util.h"
#include "version.h"
@@ -136,24 +137,21 @@ int sie_export_file(sqlite3 *db, int64_t org_id, int64_t fy_id,
put_date(f, end);
put(f, "\n");
- /* accounts and balances: IB = this year's IB-series voucher(s) plus all
- non-IB history before the year; UB = everything through the end;
- RES = the year's non-IB movements. */
+ /* accounts and balances: IB per REPORT_IB_ROW_SQL, UB = IB plus the
+ year's non-IB movements, RES = those movements. #IB/#UB are written
+ for balance accounts only, #RES for P&L accounts only. */
char ib_series[16];
db_setting_copy(db, org_id, "series_ib", "IB", ib_series,
sizeof ib_series);
if (sqlite3_prepare_v2(
db,
"SELECT a.number,a.name,"
- " COALESCE(SUM(CASE WHEN ((v.series = 'IB' OR v.series = ?5)"
- " AND v.fiscal_year_id = ?4) OR (v.series <> 'IB'"
- " AND v.series <> ?5 AND v.date < ?2)"
- " THEN r.debit_ore - r.credit_ore END),0),"
- " COALESCE(SUM(CASE WHEN v.date <= ?3"
+ " COALESCE(SUM(CASE WHEN " REPORT_IB_ROW_SQL
" THEN r.debit_ore - r.credit_ore END),0),"
" COALESCE(SUM(CASE WHEN v.series <> 'IB' AND v.series <> ?5"
" AND v.date >= ?2 AND v.date <= ?3"
- " THEN r.debit_ore - r.credit_ore END),0)"
+ " THEN r.debit_ore - r.credit_ore END),0),"
+ " a.type IN ('revenue','expense')"
" FROM accounts a LEFT JOIN (voucher_rows r JOIN vouchers v"
" ON v.org_id=r.org_id AND v.id=r.voucher_id)"
" ON r.org_id=a.org_id AND r.account_id=a.id"
@@ -173,8 +171,13 @@ int sie_export_file(sqlite3 *db, int64_t org_id, int64_t fy_id,
const char *number = (const char *)sqlite3_column_text(st, 0);
const char *name = (const char *)sqlite3_column_text(st, 1);
int64_t ib = sqlite3_column_int64(st, 2);
- int64_t ub = sqlite3_column_int64(st, 3);
- int64_t res = sqlite3_column_int64(st, 4);
+ int64_t res = sqlite3_column_int64(st, 3);
+ int pl = sqlite3_column_int(st, 4);
+ int64_t ub = ib + res;
+ if (pl)
+ ib = ub = 0;
+ else
+ res = 0;
put(f, "#KONTO ");
put(f, number);
put(f, " ");
diff --git a/tests/test_core.c b/tests/test_core.c
index a000ffe..50dde15 100644
--- a/tests/test_core.c
+++ b/tests/test_core.c
@@ -2067,6 +2067,126 @@ static void test_ib(struct tctx *t)
yyjson_doc_free(d);
}
+/* Opening balances carry earlier years' IB vouchers; P&L accounts restart
+ at every year start (reports and SIE export). */
+static void test_ib_carry(struct tctx *t)
+{
+ (void)t;
+ yyjson_doc *d;
+
+ d = call(reqf("{\"v\":1,\"id\":\"ibc1\",\"cmd\":\"org.create\","
+ "\"session\":\"%s\",\"args\":{\"name\":\"Överföring AB\"}}",
+ g_session));
+ CHECK_OK(d);
+ int org = (int)jint(d, "result.id");
+ yyjson_doc_free(d);
+ d = call(reqf("{\"v\":1,\"id\":\"ibc2\",\"cmd\":\"fiscal_year.list\","
+ "\"session\":\"%s\",\"org\":%d}",
+ g_session, org));
+ CHECK_OK(d);
+ char start[11] = "", end[11] = "";
+ snprintf(start, sizeof start, "%s", jstr(d, "result.items.0.start_date"));
+ snprintf(end, sizeof end, "%s", jstr(d, "result.items.0.end_date"));
+ yyjson_doc_free(d);
+ int y = atoi(end) + 1;
+
+ const char *posts[][4] = {
+ { start, "IB", "1940", "2010" },
+ { start, "V", "1930", "3001" },
+ { end, "V", "8999", "2099" },
+ };
+ const int64_t amounts[] = { 2500000, 100000, 100000 };
+ for (size_t i = 0; i < 3; i++) {
+ d = call(reqf("{\"v\":1,\"id\":\"ibc3\",\"cmd\":\"voucher.post\","
+ "\"session\":\"%s\",\"org\":%d,\"args\":{\"date\":"
+ "\"%s\",\"description\":\"År 1\",\"series\":\"%s\","
+ "\"rows\":[{\"account\":\"%s\",\"debit_ore\":%lld},"
+ "{\"account\":\"%s\",\"credit_ore\":%lld}]}}",
+ g_session, org, posts[i][0], posts[i][1], posts[i][2],
+ (long long)amounts[i], posts[i][3],
+ (long long)amounts[i]));
+ CHECK_OK(d);
+ yyjson_doc_free(d);
+ }
+
+ d = call(reqf("{\"v\":1,\"id\":\"ibc4\",\"cmd\":\"fiscal_year.open\","
+ "\"session\":\"%s\",\"org\":%d,\"args\":{\"label\":\"%d\","
+ "\"start_date\":\"%d-01-01\",\"end_date\":\"%d-12-31\"}}",
+ g_session, org, y, y, y));
+ CHECK_OK(d);
+ int64_t fy2 = jint(d, "result.id");
+ yyjson_doc_free(d);
+ d = call(reqf("{\"v\":1,\"id\":\"ibc5\",\"cmd\":\"voucher.post\","
+ "\"session\":\"%s\",\"org\":%d,\"args\":{\"date\":"
+ "\"%d-02-01\",\"description\":\"År 2\",\"rows\":"
+ "[{\"account\":\"1930\",\"debit_ore\":5000},"
+ "{\"account\":\"3001\",\"credit_ore\":5000}]}}",
+ g_session, org, y));
+ CHECK_OK(d);
+ yyjson_doc_free(d);
+
+ d = call(reqf("{\"v\":1,\"id\":\"ibc6\",\"cmd\":\"report.trial_balance\","
+ "\"session\":\"%s\",\"org\":%d,\"args\":{\"fiscal_year\":"
+ "%lld}}",
+ g_session, org, (long long)fy2));
+ CHECK_OK(d);
+ CHECK(find_amount(d, "result.accounts", "account", "1940", "ib_ore") ==
+ 2500000);
+ CHECK(find_amount(d, "result.accounts", "account", "2010", "ib_ore") ==
+ -2500000);
+ CHECK(find_amount(d, "result.accounts", "account", "2099", "ib_ore") ==
+ -100000);
+ CHECK(find_amount(d, "result.accounts", "account", "3001", "ib_ore") == 0);
+ CHECK(find_amount(d, "result.accounts", "account", "8999", "ib_ore") == -1);
+ CHECK(jint(d, "result.totals.ib_ore") == 0);
+ yyjson_doc_free(d);
+
+ d = call(reqf("{\"v\":1,\"id\":\"ibc7\",\"cmd\":\"report.trial_balance\","
+ "\"session\":\"%s\",\"org\":%d,\"args\":{\"fiscal_year\":"
+ "%lld,\"from\":\"%d-03-01\"}}",
+ g_session, org, (long long)fy2, y));
+ CHECK_OK(d);
+ CHECK(find_amount(d, "result.accounts", "account", "3001", "ib_ore") ==
+ -5000);
+ CHECK(find_amount(d, "result.accounts", "account", "1930", "ib_ore") ==
+ 105000);
+ yyjson_doc_free(d);
+
+ d = call(reqf("{\"v\":1,\"id\":\"ibc8\",\"cmd\":\"report.balance_sheet\","
+ "\"session\":\"%s\",\"org\":%d,\"args\":{\"fiscal_year\":"
+ "%lld}}",
+ g_session, org, (long long)fy2));
+ CHECK_OK(d);
+ CHECK(find_amount(d, "result.equity.accounts", "account", "2010",
+ "amount_ore") == 2500000);
+ CHECK(jint(d, "result.assets.total_ore") == 2605000);
+ CHECK(jint(d, "result.assets.total_ore") ==
+ jint(d, "result.liabilities.total_ore") +
+ jint(d, "result.equity.total_ore"));
+ yyjson_doc_free(d);
+
+ d = call(reqf("{\"v\":1,\"id\":\"ibc9\",\"cmd\":\"sie.export\","
+ "\"session\":\"%s\",\"org\":%d,\"args\":{\"fiscal_year\":"
+ "%lld}}",
+ g_session, org, (long long)fy2));
+ CHECK_OK(d);
+ FILE *sf = fopen(jstr(d, "result.path"), "rb");
+ yyjson_doc_free(d);
+ CHECK(sf != NULL);
+ if (sf) {
+ char sbuf[65536];
+ size_t sn = fread(sbuf, 1, sizeof sbuf - 1, sf);
+ sbuf[sn] = '\0';
+ fclose(sf);
+ CHECK(strstr(sbuf, "#IB 0 2010 -25000.00") != NULL);
+ CHECK(strstr(sbuf, "#UB 0 1930 1050.00") != NULL);
+ CHECK(strstr(sbuf, "#RES 0 3001 -50.00") != NULL);
+ CHECK(strstr(sbuf, "#IB 0 3001") == NULL);
+ CHECK(strstr(sbuf, "#UB 0 3001") == NULL);
+ CHECK(strstr(sbuf, "#RES 0 1930") == NULL);
+ }
+}
+
static void test_period_lock(struct tctx *t)
{
yyjson_doc *d;
@@ -5502,6 +5622,7 @@ static const struct ttest TESTS[] = {
{ "attachments", test_attachments, "vouchers idempotency" },
{ "templates", test_templates, "org_members" },
{ "ib", test_ib, "org_members" },
+ { "ib_carry", test_ib_carry, "org_members" },
{ "period_lock", test_period_lock, "fiscal_years" },
{ "sie_export", test_sie_export, "ib" },
{ "sie_import", test_sie_import, "attachments sie_export" },