aboutsummaryrefslogblamecommitdiffstats
path: root/main.ha
blob: 5afcef1f194c3e62e14f39185aaff010cdea340a (plain) (tree)






























































































































































































































                                                                                      
// 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;
		};
	};
};