aboutsummaryrefslogtreecommitdiffstats
path: root/firefly_cli/commands/account.py
diff options
context:
space:
mode:
Diffstat (limited to 'firefly_cli/commands/account.py')
-rw-r--r--firefly_cli/commands/account.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/firefly_cli/commands/account.py b/firefly_cli/commands/account.py
index 9dbfab6..81c2e1d 100644
--- a/firefly_cli/commands/account.py
+++ b/firefly_cli/commands/account.py
@@ -23,6 +23,8 @@ def _create_args(p):
p.add_argument("--opening-balance", dest="opening_balance", default=None,
help="initial balance (asset accounts); dated today")
p.add_argument("--currency", default=None, help="currency code, e.g. EUR")
+ p.add_argument("--if-not-exists", dest="if_not_exists", action="store_true",
+ help="if an account with this name exists, return it (existed:true) instead of erroring")
@registry.command("account create", help="create an asset, expense, or revenue account", args=_create_args)
def cmd_create(args, ctx):
@@ -30,6 +32,17 @@ def cmd_create(args, ctx):
raise FireflyError(
f'Unsupported account type "{args.type}". '
f'Use one of: {", ".join(_CREATE_TYPES)}.')
+ if getattr(args, "if_not_exists", False):
+ # Resolve by name to detect existence; avoids parsing Firefly's 422
+ # "name already in use" error string. Missing = ResolutionError -> create.
+ from firefly_cli.errors import ResolutionError
+ try:
+ existing = ctx.resolver.account(args.name)
+ except ResolutionError:
+ existing = None
+ if existing is not None:
+ output.emit({**existing, "existed": True}, human=ctx.human)
+ return 0
body = {"name": args.name, "type": args.type}
if args.type == "asset":
body["account_role"] = "defaultAsset"
@@ -52,9 +65,22 @@ def cmd_get(args, ctx):
output.emit(acc, human=ctx.human)
return 0
-@registry.command("account balance", help="show current balance for one account (name or id)", args=_name_arg)
+def _balance_args(p):
+ p.add_argument("account", help="account name or id")
+ p.add_argument("--at", default=None,
+ help="balance as of this date YYYY-MM-DD (default: current)")
+
+@registry.command("account balance", help="show balance for one account (name or id); --at for a historical date", args=_balance_args)
def cmd_balance(args, ctx):
acc = ctx.resolver.account(args.account)
+ if args.at:
+ # Firefly recomputes current_balance as of ?date= on the account show endpoint.
+ dated = output.unwrap(ctx.client.request(
+ "GET", f"/api/v1/accounts/{acc['id']}", params={"date": args.at}))
+ output.emit({"id": acc["id"], "name": dated.get("name") or acc.get("name"),
+ "date": args.at,
+ "current_balance": dated.get("current_balance")}, human=ctx.human)
+ return 0
output.emit({"id": acc["id"], "name": acc.get("name"),
"current_balance": acc.get("current_balance")}, human=ctx.human)
return 0