summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnders Betts <anders.betts@gmail.com>2026-09-22 19:52:19 +0200
committerAnders Betts <anders.betts@gmail.com>2026-09-22 19:52:19 +0200
commite1a24f6250f5e498e524ce14a5acbed5fc5c2a91 (patch)
tree6583ad2bf0e22bf83e37dc135ce663cb97a104af /src
parent4dee87e83257c81413b29ccb33989e4df109a61c (diff)
downloadbokf-e1a24f6250f5e498e524ce14a5acbed5fc5c2a91.tar.gz
bokf-e1a24f6250f5e498e524ce14a5acbed5fc5c2a91.zip
reports: income statement skips imported Stäng closings
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src')
-rw-r--r--src/reports.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/src/reports.c b/src/reports.c
index 2ca7e45..0316abf 100644
--- a/src/reports.c
+++ b/src/reports.c
@@ -60,11 +60,15 @@ static yyjson_mut_val *fy_json(yyjson_mut_doc *doc, const struct fy_info *fy)
/* 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`.
- Amounts signed: debit positive. */
+ skip_closings drops the source system's "Stäng ..." closing vouchers from
+ the period movements (imported years close the P&L accounts straight to
+ 2099, so the year otherwise nets to zero); the K2 report and the SRU
+ export use the same convention. Amounts signed: debit positive. */
static yyjson_mut_val *balance_query(yyjson_mut_doc *doc, sqlite3 *db,
int64_t org_id, int64_t fy_id,
const char *from, const char *to,
- const char *types_filter, char **err)
+ const char *types_filter,
+ int skip_closings, char **err)
{
char ib_series[16];
db_setting_copy(db, org_id, "series_ib", "IB", ib_series,
@@ -73,8 +77,10 @@ static yyjson_mut_val *balance_query(yyjson_mut_doc *doc, sqlite3 *db,
snprintf(sql, sizeof sql,
"SELECT a.number,a.name,a.type,"
" COALESCE(SUM(CASE WHEN v.series <> 'IB' AND v.series <> ?5"
+ " AND (COALESCE(v.description,'') NOT LIKE 'Stäng%%' OR ?6 = 0)"
" 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 (COALESCE(v.description,'') NOT LIKE 'Stäng%%' OR ?6 = 0)"
" 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'"
@@ -99,6 +105,7 @@ static yyjson_mut_val *balance_query(yyjson_mut_doc *doc, sqlite3 *db,
sqlite3_bind_text(st, 3, to, -1, SQLITE_TRANSIENT);
sqlite3_bind_int64(st, 4, fy_id);
sqlite3_bind_text(st, 5, ib_series, -1, SQLITE_TRANSIENT);
+ sqlite3_bind_int(st, 6, skip_closings);
yyjson_mut_val *arr = yyjson_mut_arr(doc);
while (sqlite3_step(st) == SQLITE_ROW) {
const char *number = (const char *)sqlite3_column_text(st, 0);
@@ -133,7 +140,8 @@ yyjson_mut_val *report_trial_balance(yyjson_mut_doc *doc, sqlite3 *db,
if (!to)
to = fy.end;
- yyjson_mut_val *rows = balance_query(doc, db, org_id, fy.id, from, to, NULL, err);
+ yyjson_mut_val *rows = balance_query(doc, db, org_id, fy.id, from, to, NULL,
+ 0, err);
if (!rows)
return NULL;
@@ -192,7 +200,7 @@ yyjson_mut_val *report_income_statement(yyjson_mut_doc *doc, sqlite3 *db,
yyjson_mut_val *rows = balance_query(
doc, db, org_id, fy.id, from, to,
- " AND a.type IN ('revenue','expense')", err);
+ " AND a.type IN ('revenue','expense')", 1, err);
if (!rows)
return NULL;
@@ -249,7 +257,7 @@ yyjson_mut_val *report_balance_sheet(yyjson_mut_doc *doc, sqlite3 *db,
yyjson_mut_val *rows = balance_query(
doc, db, org_id, fy.id, fy.start, to,
- " AND a.type IN ('asset','liability','equity')", err);
+ " AND a.type IN ('asset','liability','equity')", 0, err);
if (!rows)
return NULL;
@@ -285,10 +293,11 @@ yyjson_mut_val *report_balance_sheet(yyjson_mut_doc *doc, sqlite3 *db,
yyjson_mut_arr_add_val(*target, copy);
}
- /* current year result belongs to equity */
+ /* current year result belongs to equity; the closings stay included so a
+ transferred result (2099) does not get counted twice */
yyjson_mut_val *inc = balance_query(
doc, db, org_id, fy.id, fy.start, to,
- " AND a.type IN ('revenue','expense')", err);
+ " AND a.type IN ('revenue','expense')", 0, err);
int64_t result = 0;
if (inc) {
size_t m = yyjson_mut_arr_size(inc);
@@ -543,7 +552,7 @@ yyjson_mut_val *report_general_ledger(yyjson_mut_doc *doc, sqlite3 *db,
if (!to)
to = fy.end;
yyjson_mut_val *bal =
- balance_query(doc, db, org_id, fy.id, from, to, NULL, err);
+ balance_query(doc, db, org_id, fy.id, from, to, NULL, 0, err);
if (!bal)
return NULL;
sqlite3_stmt *rs = NULL;