aboutsummaryrefslogtreecommitdiff
path: root/src/cmd_reports.c
blob: a7be9953a4e77b1e09dfe26e1b5b786fd76c9567 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "commands.h"
#include "cmd_util.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "db.h"
#include "reports.h"
#include "util.h"

/* ------------------------------------------------------------------ */
/* reports and SIE                                                     */
/* ------------------------------------------------------------------ */

static yyjson_mut_val *h_report_trial_balance(struct req *r)
{
    int64_t fy = 0;
    if (req_fy(r, &fy) != 0)
        return NULL;
    int include_zero = 0;
    arg_bool(r->args, "include_zero", &include_zero);
    char *err = NULL;
    yyjson_mut_val *res = report_trial_balance(
        r->rdoc, r->db, r->org_id, fy, arg_str(r->args, "from"),
        arg_str(r->args, "to"), include_zero, &err);
    if (!res) {
        yyjson_mut_val *e = fail(r, "INTERNAL", err ? err : "report failed");
        free(err);
        return e;
    }
    return res;
}

static yyjson_mut_val *h_report_income_statement(struct req *r)
{
    int64_t fy = 0;
    if (req_fy(r, &fy) != 0)
        return NULL;
    char *err = NULL;
    yyjson_mut_val *res = report_income_statement(
        r->rdoc, r->db, r->org_id, fy, arg_str(r->args, "from"),
        arg_str(r->args, "to"), &err);
    if (!res) {
        yyjson_mut_val *e = fail(r, "INTERNAL", err ? err : "report failed");
        free(err);
        return e;
    }
    return res;
}

static yyjson_mut_val *h_report_balance_sheet(struct req *r)
{
    int64_t fy = 0;
    if (req_fy(r, &fy) != 0)
        return NULL;
    char *err = NULL;
    yyjson_mut_val *res = report_balance_sheet(
        r->rdoc, r->db, r->org_id, fy, arg_str(r->args, "to"), &err);
    if (!res) {
        yyjson_mut_val *e = fail(r, "INTERNAL", err ? err : "report failed");
        free(err);
        return e;
    }
    return res;
}

static yyjson_mut_val *h_report_vat(struct req *r)
{
    const char *from = arg_str(r->args, "from");
    const char *to = arg_str(r->args, "to");
    if (!from || !to)
        return fail(r, "INVALID_ARGS", "from and to are required");
    char *err = NULL;
    yyjson_mut_val *res =
        report_vat(r->rdoc, r->db, r->org_id, from, to, &err);
    if (!res) {
        yyjson_mut_val *e = fail(r, "INTERNAL", err ? err : "report failed");
        free(err);
        return e;
    }
    return res;
}

static yyjson_mut_val *h_report_general_ledger(struct req *r)
{
    int64_t fy = 0;
    if (req_fy(r, &fy) != 0)
        return NULL;
    yyjson_val *accounts = yyjson_obj_get(r->args, "accounts");
    if (accounts && !yyjson_is_arr(accounts))
        return fail(r, "INVALID_ARGS", "accounts must be an array of strings");
    char *err = NULL;
    yyjson_mut_val *res = report_general_ledger(
        r->rdoc, r->db, r->org_id, fy, arg_str(r->args, "from"),
        arg_str(r->args, "to"), accounts, &err);
    if (!res) {
        yyjson_mut_val *e = fail(r, "INTERNAL", err ? err : "report failed");
        free(err);
        return e;
    }
    return res;
}

static yyjson_mut_val *h_report_voucher_list(struct req *r)
{
    int64_t fy = 0;
    if (req_fy(r, &fy) != 0)
        return NULL;
    char *err = NULL;
    yyjson_mut_val *res = report_voucher_list(
        r->rdoc, r->db, r->org_id, fy, arg_str(r->args, "series"), &err);
    if (!res) {
        yyjson_mut_val *e = fail(r, "INTERNAL", err ? err : "report failed");
        free(err);
        return e;
    }
    return res;
}

static yyjson_mut_val *h_report_vat_eskd(struct req *r)
{
    const char *from = arg_str(r->args, "from");
    const char *to = arg_str(r->args, "to");
    if (!from || !to)
        return fail(r, "INVALID_ARGS", "from and to are required");
    char *err = NULL;
    yyjson_mut_val *res =
        report_vat_eskd(r->rdoc, r->db, r->org_id, from, to,
                        arg_str(r->args, "upplysning"), &err);
    if (!res) {
        yyjson_mut_val *e = fail(r, "INVALID_ARGS",
                                 err ? err : "could not build the eSKD file");
        free(err);
        return e;
    }
    return res;
}


static const struct cmd_arg args_report_trial_balance[] = {
    { "fiscal_year", ARG_INT, 0, NULL, NULL,
      "Fiscal year id; defaults to the latest" },
    { "from", ARG_DATE, 0, NULL, NULL, "Narrow the period" },
    { "to", ARG_DATE, 0, NULL, NULL, "Narrow the period" },
    { "include_zero", ARG_BOOL, 0, "false", NULL,
      "Include accounts with no activity" },
};

static const struct cmd_arg args_report_income_statement[] = {
    { "fiscal_year", ARG_INT, 0, NULL, NULL,
      "Fiscal year id; defaults to the latest" },
    { "from", ARG_DATE, 0, NULL, NULL, "Narrow the period" },
    { "to", ARG_DATE, 0, NULL, NULL, "Narrow the period" },
};

static const struct cmd_arg args_report_balance_sheet[] = {
    { "fiscal_year", ARG_INT, 0, NULL, NULL,
      "Fiscal year id; defaults to the latest" },
    { "to", ARG_DATE, 0, NULL, NULL, "Report through this date" },
};

static const struct cmd_arg args_report_vat[] = {
    { "from", ARG_DATE, 1, NULL, NULL, "Period start" },
    { "to", ARG_DATE, 1, NULL, NULL, "Period end" },
};

static const struct cmd_arg args_report_general_ledger[] = {
    { "fiscal_year", ARG_INT, 0, NULL, NULL,
      "Fiscal year id; defaults to the latest" },
    { "accounts", ARG_JSON, 0, NULL, NULL,
      "Array of account numbers to include" },
    { "from", ARG_DATE, 0, NULL, NULL, "Narrow the period" },
    { "to", ARG_DATE, 0, NULL, NULL, "Narrow the period" },
};

static const struct cmd_arg args_report_voucher_list[] = {
    { "fiscal_year", ARG_INT, 0, NULL, NULL,
      "Fiscal year id; defaults to the latest" },
    { "series", ARG_STR, 0, NULL, NULL, "Number series filter" },
};

static const struct cmd_arg args_report_vat_eskd[] = {
    { "from", ARG_DATE, 1, NULL, NULL, "Period start" },
    { "to", ARG_DATE, 1, NULL, NULL, "Period end" },
    { "upplysning", ARG_STR, 0, NULL, NULL,
      "Free-text message to Skatteverket" },
};

const struct command g_cmd_reports[] = {
    { "report.trial_balance", "Saldobalans (IB, period, UB)", PERM_READ, 1, 0,
      0, h_report_trial_balance, CMD_ARGS(args_report_trial_balance) },
    { "report.income_statement", "Resultaträkning", PERM_READ, 1, 0, 0,
      h_report_income_statement, CMD_ARGS(args_report_income_statement) },
    { "report.balance_sheet", "Balansräkning", PERM_READ, 1, 0, 0,
      h_report_balance_sheet, CMD_ARGS(args_report_balance_sheet) },
    { "report.vat", "Momsdeklaration ruta för ruta", PERM_READ, 1, 0, 0,
      h_report_vat, CMD_ARGS(args_report_vat) },
    { "report.general_ledger", "Huvudbok", PERM_READ, 1, 0, 0,
      h_report_general_ledger, CMD_ARGS(args_report_general_ledger) },
    { "report.voucher_list", "Verifikationslista", PERM_READ, 1, 0, 0,
      h_report_voucher_list, CMD_ARGS(args_report_voucher_list) },
    { "report.vat_eskd", "eSKD XML for the momsdeklaration (ISO-8859-1)",
      PERM_READ, 1, 0, 0, h_report_vat_eskd, CMD_ARGS(args_report_vat_eskd) },
};

const struct cmd_table g_cmd_table_reports = {
    g_cmd_reports, sizeof g_cmd_reports / sizeof g_cmd_reports[0]
};