From 887751f439e1ee7b7ad22d078365cf2b622e76b0 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 15 Apr 2020 11:13:28 +1000 Subject: [PATCH] Implement extensions --- README.md | 1 + config.example.yml | 3 +++ demo/config.yml | 6 +++++- demo/ext_pending.py | 30 ++++++++++++++++++++++++++++++ demo/ledger.journal | 4 ++++ ledger_pyreport/__init__.py | 5 +++++ 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 demo/ext_pending.py diff --git a/README.md b/README.md index b6ca19c..6731cf4 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ ledger-pyreport is a lightweight Flask webapp for generating interactive and pri * Accounts for both profit and loss, and other comprehensive income * Simulates annual closing of books, with presentation of income/expenses on the balance sheet as retained earnings and current year earnings * Can simulate cash basis accounting, using FIFO methodology to recode transactions involving liabilities and non-cash assets +* Extensible through custom programming hooks ## Background, demo and screenshots diff --git a/config.example.yml b/config.example.yml index 2c92c9b..5076372 100644 --- a/config.example.yml +++ b/config.example.yml @@ -23,3 +23,6 @@ current_year_earnings: 'Equity:Current Year Earnings' cash_asset_accounts: ['Assets:Current:Cash on Hand', 'Assets:Current:Cash at Bank'] # Which account to charge non-cash transactions to in cash basis mode, when no other account is suitable cash_other_income: 'Income:Other Income' + +# Extensions to load +extensions: [] diff --git a/demo/config.yml b/demo/config.yml index 53ad1ca..f20804d 100644 --- a/demo/config.yml +++ b/demo/config.yml @@ -1,5 +1,5 @@ # Set up how we will call Ledger -ledger_file: demo/ledger.journal +ledger_file: ledger.journal ledger_args: [] report_commodity: '$' @@ -23,3 +23,7 @@ current_year_earnings: 'Equity:Retained Earnings:Current Year Earnings' cash_asset_accounts: ['Assets:Current:Cash on Hand', 'Assets:Current:Cash at Bank', 'Assets:Current:International Account'] # Which account to charge non-cash transactions to in cash basis mode, when no other account is suitable cash_other_income: 'Income:Other Income' + +# Extensions to load +extensions: +- ext_pending.py diff --git a/demo/ext_pending.py b/demo/ext_pending.py new file mode 100644 index 0000000..fe80b06 --- /dev/null +++ b/demo/ext_pending.py @@ -0,0 +1,30 @@ +# ledger-pyreport +# Copyright © 2020 Lee Yingtong Li (RunasSudo) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +# Extension to move pending postings to a pending transactions account + +_ledger_raw_transactions_at_date = ledger.raw_transactions_at_date +def ledger_raw_transactions_at_date(*args, **kwargs): + l = _ledger_raw_transactions_at_date(*args, **kwargs) + + for transaction in l.transactions: + for posting in transaction.postings: + if posting.state == Posting.State.PENDING: + posting.account = l.get_account('Liabilities:Current:Pending Transactions') + + return l + +ledger.raw_transactions_at_date = ledger_raw_transactions_at_date diff --git a/demo/ledger.journal b/demo/ledger.journal index 58e963b..eaf15a9 100644 --- a/demo/ledger.journal +++ b/demo/ledger.journal @@ -38,4 +38,8 @@ Expenses:Depreciation $500.00 Assets:Non-current:Plant:Less Accumulated Depreciation +2019-10-02 Pending transaction + Expenses:Stationery $10.00 + ! Assets:Current:Cash at Bank + P 2019-10-01 00:00:00 EUR $1.30 diff --git a/ledger_pyreport/__init__.py b/ledger_pyreport/__init__.py index 52e925b..9dd5f61 100644 --- a/ledger_pyreport/__init__.py +++ b/ledger_pyreport/__init__.py @@ -359,3 +359,8 @@ def debug_imbalances(): total_cr = sum((p.amount for t in transactions for p in t.postings if p.amount < 0), Balance()).exchange(report_commodity, True) return flask.render_template('transactions.html', date=date, pstart=pstart, period=describe_period(date, pstart), account=None, ledger=l, transactions=transactions, total_dr=total_dr, total_cr=total_cr, report_commodity=report_commodity, cash=cash) + +# Load extensions +for ext_name in config['extensions']: + with open(ext_name, 'r') as f: + exec(f.read())