aboutsummaryrefslogtreecommitdiff
path: root/src/ledger.h
blob: dee8be06026825e4f416fb1cb91ee11effaa60cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef BOKF_LEDGER_H
#define BOKF_LEDGER_H

#include <sqlite3.h>
#include <stddef.h>
#include <stdint.h>

struct ledger_row {
    const char *account;
    int64_t debit_ore;
    int64_t credit_ore;
    const char *description;
};

struct ledger_error {
    const char *code;
    char msg[512];
    int has_details;
    int64_t difference_ore;
};

struct ledger_post_opts {
    int64_t org_id;
    int64_t user_id;
    int64_t token_id;
    const char *date;
    const char *description;
    const char *series; /* NULL -> "A" */
    const struct ledger_row *rows;
    size_t nrows;
    int64_t corrects_voucher_id; /* 0 = none */
    const int64_t *attachment_ids;
    size_t n_attachments;
    const char *client_ref; /* NULL = none */
    int dry_run;
    const char *source; /* NULL -> "manual" or "agent" */
    int already_in_tx;  /* caller owns the transaction (no BEGIN/COMMIT) */
};

int ledger_post(sqlite3 *db, const struct ledger_post_opts *o,
                struct ledger_error *e, char **out_result_json);

/* Appends the canonical bytes for a voucher hash (SCHEMA.md 7.1) and
   returns the 32-byte hash. */
void ledger_voucher_hash(const unsigned char prev[32], int64_t org_id,
                         const char *fy_label, const char *series,
                         int64_t number, const char *date,
                         const char *description,
                         const struct ledger_row *rows, size_t nrows,
                         unsigned char out[32]);

struct ledger_verify_result {
    int64_t vouchers_checked;
    int64_t first_bad_voucher_id;        /* 0 = none */
    int64_t unbalanced_vouchers;
    int64_t first_unbalanced_voucher_id; /* 0 = none */
};

/* Recomputes every org's voucher hash chain (SCHEMA.md 7.1) and scans for
   unbalanced vouchers. 0 = intact, 1 = broken, -1 = database error. */
int ledger_verify(sqlite3 *db, struct ledger_verify_result *out, char **err);

#endif