Skip to content

Commit

Permalink
cli: abacus is main candidate for CLI entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
epogrebnyak committed Oct 22, 2023
1 parent 9e5f7d3 commit e242259
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 106 deletions.
27 changes: 0 additions & 27 deletions abacus.sh

This file was deleted.

21 changes: 15 additions & 6 deletions abacus/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@ FIXME:
- bx ledger init must fail when file is present
- `--title` fails when written first with post entry (see fail.sh)

CODES:

# TODO: add more codes from https://www.audit-it.ru/plan_schetov/
# --code 50


OPERATIONS:
# abacus chart alias --operation invoice --debit ar --credit sales --requires cost
# abacus chart alias --operation cost --debit cogs --credit goods --requires invoice

TODO:
- [ ] change names to prefix+account name Asset:equipment (Equipment).........................
- [ ] interacting with retained earnings may be a problem
- [ ] change names to prefix+account name Asset:equipment (Equipment)
- [ ] add totals to trial balance
- [ ] memo + build-you-own-x
- [ ] RAS codes
- [ ] --requires flag in alias
- [ ] more textbook examples
- [ ] adjustment example (with trial balance)

WONTFIX:
- [ ] injest .as file with bx ... commands
- [ ] --requires flag in alias
- [ ] "run" command - injest .as file with bx ... commands
- [ ] adjust and postclose entries
- [ ] --title for csv file
- [ ] --title for post command
- [ ] help messages
- [ ] types of entries (S,B,A,C1,C2,C3,C4,P)
- [ ] post entry with chart check
- [ ] adjustment and post-close entries
- [ ] `--reference` or `--refer` or meta
- [ ] `--reference` or `--refer` or --meta
- [ ] post-close example
- [ ] add operation title to entry title
- [ ] add small hash for entry
Expand Down
5 changes: 4 additions & 1 deletion abacus/cli/chart_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def _add(self, attribute: str, account_name: str) -> "ChartCommand":
else:
raise AbacusError(f"Account name <{account_name}> already taken.")
setattr(self.chart, attribute, account_names + [account_name])
self.log(f"Added account <{account_name}> of type <{attribute}> to chart.")
# FIXME: may refine the message
self.log(
f"Added account to chart: <{account_name}>, account types is <{attribute}>."
)
return self

def offset_many(self, account_name, contra_account_names) -> "ChartCommand":
Expand Down
37 changes: 37 additions & 0 deletions abacus/cli/inspect_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys

from abacus import Amount
from abacus.engine.accounts import CreditAccount, DebitAccount


def side(ledger, account_name):
match ledger[account_name]:
case DebitAccount(_, _):
return "debit"
case CreditAccount(_, _):
return "credit"
case _:
raise TypeError(ledger[account_name].__class__.__name__)


def print_account_info(ledger, chart, account_name: str):
account = ledger[account_name]
print(" Account:", chart.namer[account_name])
print(" Short name:", account_name)
print(" Long name:", chart.namer.compose_name_long(account_name))
print(" Code:", chart.codes.get(account_name, "not assigned"))
print(" Type:", account.split_on_caps())
print(" Debits:", str(account.debits) + ",", "total", sum(account.debits))
print(" Credits:", str(account.credits) + ",", "total", sum(account.credits))
print(" Balance:", account.balance())
print("Balance side:", side(ledger, account_name).capitalize())


def assert_account_balance(ledger, account_name: str, assert_amount: Amount):
amount = ledger[account_name].balance()
expected_amount = Amount(assert_amount)
msg_ok = f"Check passed for account {account_name}, account balance is {expected_amount} as expected."
msg_fail = f"Check failed for {account_name}, expected balance is {expected_amount}, got {amount}."
if amount != expected_amount:
sys.exit(msg_fail)
print(msg_ok)
27 changes: 16 additions & 11 deletions abacus/cli/ledger_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing import Dict

from abacus import Amount, Chart, Entry, Ledger
from abacus.engine.entries import LineJSON
from abacus.cli.logger import Logger
from abacus.engine.entries import LineJSON


@dataclass
Expand All @@ -19,10 +19,11 @@ def echo(self):
def log(self, message):
self.logger.log(message)
return self

@classmethod
def init(cls, path) -> "LedgerCommand":
store = LineJSON(path).file.touch()
store = LineJSON(path)
store.file.touch()
return LedgerCommand(store).log(f"Created ledger file {path}.")

@classmethod
Expand All @@ -39,22 +40,26 @@ def post_starting_balances(self, chart: Chart, starting_balances_dict: Dict):
) # check all accounts are present in chart
me = chart.ledger().make_multiple_entry(starting_balances_dict)
entries = me.entries(chart.null_account)
self.store.append_many(entries)
return self
return self.post_many(entries)

def post_entry(self, debit: str, credit: str, amount: str):
entry = Entry(debit=debit, credit=credit, amount=Amount(amount))
self.store.append(entry)
self.log(f"Posted entry to ledger: {entry}")
return self.post_many([entry])

def post_many(self, entries: list[Entry]):
self.store.append_many(entries)
for entry in entries:
self.log(f"Posted entry to ledger: {entry}")
return self

def post_operations(self, chart, operations: list[str], amounts: list[str]):
entries = chart.make_entries_for_operations(operations, amounts)
return self.post_many(entries)

def post_closing_entries(self, chart):
closing_entries = (
Ledger.new(chart)
.post_many(entries=self.store.yield_entries())
.closing_entries(chart)
)
self.store.append_many(closing_entries)
for entry in closing_entries:
self.log(f"Posted entry to ledger: {entry}")
return self
return self.post_many(closing_entries)
Loading

0 comments on commit e242259

Please sign in to comment.