|
| 1 | +open System |
| 2 | + |
| 3 | +open Argu |
| 4 | + |
| 5 | +open NetCash |
| 6 | + |
| 7 | +type BalArguments = |
| 8 | + | [<Unique; AltCommandLine("-l")>] Flat |
| 9 | + | [<Unique; AltCommandLine("-t")>] Tree |
| 10 | + | [<Unique; AltCommandLine("-e")>] End of string |
| 11 | + | [<MainCommand; ExactlyOnce; Last>] Book of uri: string |
| 12 | + |
| 13 | + interface IArgParserTemplate with |
| 14 | + member s.Usage = |
| 15 | + match s with |
| 16 | + | Flat -> "show accounts as a flat list (default). Amounts exclude subaccount amounts." |
| 17 | + | Tree -> "show accounts as a tree. Amounts include subaccount amounts." |
| 18 | + | End _ -> "show balance before this date. A valid date string or \"today\"" |
| 19 | + | Book uri -> "URI to the book." |
| 20 | + |
| 21 | +[<CliPrefix(CliPrefix.DoubleDash)>] |
| 22 | +type CliArguments = |
| 23 | + | [<CustomCommandLine("bal", "balance")>] Bal of ParseResults<BalArguments> |
| 24 | + |
| 25 | + interface IArgParserTemplate with |
| 26 | + member s.Usage = |
| 27 | + match s with |
| 28 | + | Bal _ -> "Show accounts and their balances." |
| 29 | + |
| 30 | +module CmdHandlers = |
| 31 | + let bal uri tree endDate = |
| 32 | + use book = Book.OpenRead uri |
| 33 | + |
| 34 | + let filter: seq<Account> -> seq<Account> = Seq.filter (fun acc -> not acc.Hidden) |
| 35 | + |
| 36 | + let inline printBal (acc: Account) = |
| 37 | + let balance = |
| 38 | + match endDate with |
| 39 | + // TODO: replace with --current (consistent with ledger-cli) |
| 40 | + // --current |
| 41 | + // -c |
| 42 | + // Display only transactions on or before the current date. |
| 43 | + |
| 44 | + | Some "today" -> acc.GetBalanceBeforeDate(DateTime.Now, tree) |
| 45 | + | Some input -> |
| 46 | + match DateTime.TryParse input with |
| 47 | + | false, _ -> failwithf "Invalid end date: %s" input |
| 48 | + | _, date -> acc.GetBalanceBeforeDate(date, tree) |
| 49 | + | _ -> acc.GetFutureBalance(tree) |
| 50 | + |
| 51 | + Helpers.UI.FormatAmount(balance, acc.Currency, true) |
| 52 | + |
| 53 | + let rec collectTree level (root: Account) = |
| 54 | + seq { |
| 55 | + if level > -1 then |
| 56 | + (printBal root, (String.replicate level " ") + root.Name) |
| 57 | + |
| 58 | + yield! |
| 59 | + root.Children |
| 60 | + |> filter |
| 61 | + |> Seq.sortBy (fun x -> x.Name) |
| 62 | + |> Seq.collect (collectTree (level + 1)) |
| 63 | + } |
| 64 | + |
| 65 | + let inline collectList () = |
| 66 | + book.Accounts |
| 67 | + |> filter |
| 68 | + |> Seq.sortBy (fun x -> x.FullName) |
| 69 | + |> Seq.map (fun acc -> (printBal acc, acc.FullName)) |
| 70 | + |
| 71 | + let rows = |
| 72 | + if tree then |
| 73 | + collectTree -1 book.RootAccount |
| 74 | + else |
| 75 | + collectList () |
| 76 | + |> Seq.cache |
| 77 | + |
| 78 | + let rowFmt = |
| 79 | + let padding = 8 |
| 80 | + let maxBalWidth = rows |> ((Seq.map (fst >> String.length)) >> Seq.max) |
| 81 | + sprintf "{0,%d} {1}" (maxBalWidth + padding) |
| 82 | + |
| 83 | + for balance, fullName in rows do |
| 84 | + stdout.WriteLine(rowFmt, balance, fullName) |
| 85 | + |
| 86 | +[<EntryPoint>] |
| 87 | +let main argv = |
| 88 | + GnuCashEngine.Initialize() |
| 89 | + Logging.Config(Logging.LogLevel.WARN, [| Logging.Appender.console |]) |
| 90 | + |
| 91 | + let cliParser = ArgumentParser.Create<CliArguments>() |
| 92 | + |
| 93 | + try |
| 94 | + let results = cliParser.ParseCommandLine(inputs = argv, raiseOnUsage = true) |
| 95 | + |
| 96 | + match results.GetSubCommand() with |
| 97 | + | Bal args -> |
| 98 | + let tree = args.Contains BalArguments.Tree |
| 99 | + |
| 100 | + let endDate = |
| 101 | + args.TryGetResult BalArguments.End |> Option.map (fun x -> x.ToLowerInvariant()) |
| 102 | + |
| 103 | + let uri = args.GetResult BalArguments.Book |> GnuCashUri.Parse |
| 104 | + CmdHandlers.bal uri tree endDate |
| 105 | + with e -> |
| 106 | + stdout.WriteLine e.Message |
| 107 | + |
| 108 | + 0 |
0 commit comments