reconcile.helper: Add entry_point to avoid traceback
This commit is contained in:
parent
026f54bca1
commit
71bf8137a5
1 changed files with 115 additions and 105 deletions
|
@ -114,7 +114,7 @@ def reconciliation_report_path(account, end_date):
|
||||||
return f'Financial/Controls/Reports-for-Treasurer/{end_date}_{account_name}_bank-reconciliation.csv'
|
return f'Financial/Controls/Reports-for-Treasurer/{end_date}_{account_name}_bank-reconciliation.csv'
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args(argv):
|
||||||
parser = argparse.ArgumentParser(description='Reconciliation helper')
|
parser = argparse.ArgumentParser(description='Reconciliation helper')
|
||||||
parser.add_argument('--beancount-file', required=True)
|
parser.add_argument('--beancount-file', required=True)
|
||||||
parser.add_argument('--account', help='Full account name, e.g. "Liabilities:CreditCard:AMEX"', required=True)
|
parser.add_argument('--account', help='Full account name, e.g. "Liabilities:CreditCard:AMEX"', required=True)
|
||||||
|
@ -126,7 +126,7 @@ def parse_args():
|
||||||
parser.add_argument('--cost-function', default='COST')
|
parser.add_argument('--cost-function', default='COST')
|
||||||
parser.add_argument('--grep-output-filename')
|
parser.add_argument('--grep-output-filename')
|
||||||
# parser.add_argument('--report-group-regex')
|
# parser.add_argument('--report-group-regex')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args(args=argv[1:])
|
||||||
if args.month or args.period:
|
if args.month or args.period:
|
||||||
if not (args.month and args.period):
|
if not (args.month and args.period):
|
||||||
parser.error('--month and --period must be used together')
|
parser.error('--month and --period must be used together')
|
||||||
|
@ -140,29 +140,29 @@ def beancount_file_exists(path):
|
||||||
return os.path.isfile(path)
|
return os.path.isfile(path)
|
||||||
|
|
||||||
|
|
||||||
args = parse_args()
|
def main(args):
|
||||||
if not beancount_file_exists(args.beancount_file):
|
if not beancount_file_exists(args.beancount_file):
|
||||||
sys.exit(f'Beancount file does not exist: {args.beancount_file}')
|
sys.exit(f'Beancount file does not exist: {args.beancount_file}')
|
||||||
if args.month or args.period:
|
if args.month or args.period:
|
||||||
parsed_date = datetime.datetime.strptime(args.month, '%Y-%m').date()
|
parsed_date = datetime.datetime.strptime(args.month, '%Y-%m').date()
|
||||||
preDate = end_of_month(parsed_date - relativedelta(months=args.period)).isoformat()
|
preDate = end_of_month(parsed_date - relativedelta(months=args.period)).isoformat()
|
||||||
lastDateInPeriod = end_of_month(parsed_date).isoformat()
|
lastDateInPeriod = end_of_month(parsed_date).isoformat()
|
||||||
month = args.month
|
month = args.month
|
||||||
else:
|
else:
|
||||||
preDate = args.prev_end_date
|
preDate = args.prev_end_date
|
||||||
lastDateInPeriod = args.cur_end_date.isoformat()
|
lastDateInPeriod = args.cur_end_date.isoformat()
|
||||||
month = args.cur_end_date.strftime('%Y-%m')
|
month = args.cur_end_date.strftime('%Y-%m')
|
||||||
grep_output_file: typing.IO
|
grep_output_file: typing.IO
|
||||||
if args.grep_output_filename:
|
if args.grep_output_filename:
|
||||||
grep_output_file = open(args.grep_output_filename, 'w')
|
grep_output_file = open(args.grep_output_filename, 'w')
|
||||||
else:
|
else:
|
||||||
grep_output_file = tempfile.NamedTemporaryFile(prefix='bc-reconcile-grep-output_', mode='w', delete=False)
|
grep_output_file = tempfile.NamedTemporaryFile(prefix='bc-reconcile-grep-output_', mode='w', delete=False)
|
||||||
beancount_file = args.beancount_file
|
beancount_file = args.beancount_file
|
||||||
account = args.account
|
account = args.account
|
||||||
cost_function = args.cost_function
|
cost_function = args.cost_function
|
||||||
statement_match = args.statement_match if args.statement_match else month
|
statement_match = args.statement_match if args.statement_match else month
|
||||||
|
|
||||||
QUERIES = {
|
QUERIES = {
|
||||||
f"00: CLEARED BAL ENDING DAY BEFORE {preDate}":
|
f"00: CLEARED BAL ENDING DAY BEFORE {preDate}":
|
||||||
# $CONLEDGER -V -C -e "$preDate" bal "/$acct/"
|
# $CONLEDGER -V -C -e "$preDate" bal "/$acct/"
|
||||||
f"""SELECT sum({cost_function}(position)) AS aa WHERE account = "{account}"
|
f"""SELECT sum({cost_function}(position)) AS aa WHERE account = "{account}"
|
||||||
|
@ -208,15 +208,15 @@ QUERIES = {
|
||||||
f"""SELECT sum(number({cost_function}(position))) AS aa
|
f"""SELECT sum(number({cost_function}(position))) AS aa
|
||||||
WHERE account = "{account}"
|
WHERE account = "{account}"
|
||||||
and META("bank-statement") ~ "{statement_match}" and number({cost_function}(position)) > 0""",
|
and META("bank-statement") ~ "{statement_match}" and number({cost_function}(position)) > 0""",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Run Beancount queries.
|
# Run Beancount queries.
|
||||||
print(f"START RECONCILIATION FOR {account} ENDING {lastDateInPeriod} (previous end date {preDate})")
|
print(f"START RECONCILIATION FOR {account} ENDING {lastDateInPeriod} (previous end date {preDate})")
|
||||||
entries, _, options = loader.load_file(beancount_file)
|
entries, _, options = loader.load_file(beancount_file)
|
||||||
uncleared_rows = [] # Hack to capture results of query 03.
|
uncleared_rows = [] # Hack to capture results of query 03.
|
||||||
cleared_balance = decimal.Decimal('0')
|
cleared_balance = decimal.Decimal('0')
|
||||||
all_trans_balance = decimal.Decimal('0')
|
all_trans_balance = decimal.Decimal('0')
|
||||||
for desc, query in QUERIES.items():
|
for desc, query in QUERIES.items():
|
||||||
rtypes, rrows = run_query(entries, options, query, numberify=True)
|
rtypes, rrows = run_query(entries, options, query, numberify=True)
|
||||||
if not rrows:
|
if not rrows:
|
||||||
print(f'{desc:<55} {"N/A":>11}')
|
print(f'{desc:<55} {"N/A":>11}')
|
||||||
|
@ -239,9 +239,19 @@ for desc, query in QUERIES.items():
|
||||||
print(desc)
|
print(desc)
|
||||||
print(textwrap.indent(tabulate(rrows, headers=headers), ' '))
|
print(textwrap.indent(tabulate(rrows, headers=headers), ' '))
|
||||||
|
|
||||||
uncleared = [(r[0], r[2], r[4] or r[3], r[1]) for r in uncleared_rows]
|
uncleared = [(r[0], r[2], r[4] or r[3], r[1]) for r in uncleared_rows]
|
||||||
report_path = os.path.join(os.getenv('CONSERVANCY_REPOSITORY', ''), reconciliation_report_path(account, lastDateInPeriod))
|
report_path = os.path.join(os.getenv('CONSERVANCY_REPOSITORY', ''), reconciliation_report_path(account, lastDateInPeriod))
|
||||||
# TODO: Make the directory if it doesn't exist.
|
# TODO: Make the directory if it doesn't exist.
|
||||||
with open(report_path, 'w') as f:
|
with open(report_path, 'w') as f:
|
||||||
f.write(reconciliation_report(account, lastDateInPeriod, cleared_balance, uncleared, '1900-01-01', all_trans_balance, []))
|
f.write(reconciliation_report(account, lastDateInPeriod, cleared_balance, uncleared, '1900-01-01', all_trans_balance, []))
|
||||||
print(f'Wrote reconciliation report: {report_path}.')
|
print(f'Wrote reconciliation report: {report_path}.')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
args = parse_args(sys.argv)
|
||||||
|
main(args)
|
||||||
|
|
||||||
|
|
||||||
|
def entry_point():
|
||||||
|
args = parse_args(sys.argv)
|
||||||
|
main(args)
|
||||||
|
|
Loading…
Reference in a new issue