aboutsummaryrefslogtreecommitdiffstats
path: root/main.ha
blob: 5afcef1f194c3e62e14f39185aaff010cdea340a (plain) (blame)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (C) 2023-2024 Adam House
// 
// This file is part of leddy.
// 
// leddy is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
// 
// leddy is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along with
// leddy. If not, see <https://www.gnu.org/licenses/>.

use bufio;
use encoding::utf8;
use fmt;
use fs;
use getopt;
use io;
use os;
use strings;

use errs;
use ledger;
use reports;

const version: str = "0.1.1";

// TODO: Implement find (locate entries by name) and inspect (specific acct
//       history) reports.

export fn main() void = {
	const cmd = getopt::parse(os::args,
		"plain text accounting tool",
		('b', "date", "beginning date filter"),
		('e', "date", "ending date filter"),
		('f', "file", "ledger file path"),
		('q', "include equity in bs"),
		('v', "output version and exit"),
		('z', "include zero bals"),
		"command",
	);
	defer getopt::finish(&cmd);

	let b = 0i64;
	let e = 0i64;
	let f = "";
	let q = false;
	let v = false;
	let z = false;

	for (let i = 0z; i < len(cmd.opts); i += 1) {
		const opt = cmd.opts[i];
		switch (opt.0) {
			case 'b' => {
				match (ledger::isodate_to_unix(opt.1)) {
					case let n: i64 => {
						b = n;
					};
					case => fmt::fatal("FAIL invalid -b");
				};
			};
			case 'e' => {
				match (ledger::isodate_to_unix(opt.1)) {
					case let n: i64 => e = n;
					case => fmt::fatal("FAIL invalid -e");
				};
			};
			case 'f' => f = opt.1;
			case 'q' => q = true;
			case 'v' => v = true;
			case 'z' => z = true;
			case => abort();
		};
	};

	if (v) {
		fmt::println(version)!;
		return;
	};

	if (len(f) == 0) {
		fmt::fatal("FAIL no ledger file");
	};

	const file = match (os::open(f, fs::flag::RDONLY)) {
		case let f: io::file => yield f;
		case let err: fs::error => {
			fmt::fatalf(
				"FAIL bad read at {}: {}", f, fs::strerror(err)
			);
		};
	};

	let command = reports::report::BALANCE_SHEET;
	if (len(cmd.args) >= 1) {
		command = switch (cmd.args[0]) {
			case "tb" => yield reports::report::TRIAL_BALANCE;
			case "is" => yield reports::report::INCOME_STATEMENT;
			case "bs" => yield reports::report::BALANCE_SHEET;
			case "fx" => yield reports::report::EXCHANGE_RATES;
			case => fmt::fatal("FAIL invalid command");
		};
	};

	let led = ledger::new(b, e);
	//defer ledger::finish(&led);
	
	let cur_entry = ledger::new_entry();
	let cur_line = 0;
	for (true) {
		cur_line += 1;
		const line = match (ledger::next_line(file)) {
			case let l: str => yield l;
			case void => break;
			case let e: errs::error => {
				fmt::fatalf(
					"FAIL (line {}): {}",
					cur_line,
					errs::strerror(e),
				);
			};
		};

		if (
			len(strings::trim(line)) == 0 ||
			strings::hasprefix(line, '#')
		) {
			continue;
		};

		match (process_line(&led, line, &cur_entry, cur_line)) {
			case void => free(line);
			case let e: errs::error => {
				fmt::fatalf(
					"FAIL parse (line {} or preceding entry): {}",
					cur_line,
					errs::strerror(e)
				);
			};
		};
	};

	io::close(file)!;

	// finalize the last entry if it is not followed by anything
	if (len(cur_entry.name) > 0) {
		const res = finalize_entry(&led, &cur_entry);
		if (res is errs::error) {
			fmt::fatalf(
				"FAIL parse (line {} or preceding entry): {}",
				cur_line + 1,
				errs::strerror(res: errs::error)
			);
		};
	};

	// display whatever report
	switch (command) {
		case reports::report::TRIAL_BALANCE =>
			reports::print_tb(&led);
		case reports::report::INCOME_STATEMENT => 
			reports::print_is(&led, z);
		case reports::report::BALANCE_SHEET =>
			reports::print_bs(&led, z, q);
		case reports::report::EXCHANGE_RATES =>
			reports::print_exchange_rates(&led);
	};
};

fn process_line(
	led: *ledger::ledger, l: str, e: *ledger::entry, line_num: int,
) (void | errs::error) = {
	match (ledger::parse(l)) {
		case let h: ledger::entry => {
			if (len(e.name) == 0) {
				*e = h;
				return;
			};

			match (finalize_entry(led, e)) {
				case void => *e = h;
				case let e: errs::error => return e;
			};
		};
		case let d: ledger::detail => {
			if (len(e.name) == 0) {
				fmt::fatalf(
					"FAIL orphaned detail (line {})",
					line_num
				);
			};
			append(e.details, d);
		};
		case let e: errs::invalid => {
			fmt::fatalf(
				"FAIL (line {}): {}",
				line_num,
				errs::strerror(e),
			);
		};
	};
};

fn finalize_entry(
	led: *ledger::ledger, old: *ledger::entry
) (void | errs::error) = {
	const res = ledger::validate(led, old, &led.r);
	
	if (res is errs::error) {
		return res;
	};

	match (ledger::add_entry(led, old)) {
		case void => ledger::finish_entry(old);
		case let err: errs::error => {
			return err;
		};
	};
};