Instantly share code, notes, and snippets.
CreatedJune 27, 2021 10:29
Save upsuper/f096ba18331ece5b10169c6a6255bba2 to your computer and use it in GitHub Desktop.
Beancount plugin to handle transactions geting delayed and not being included in the next bank statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright (C) 2021 Xidorn Quan | |
# | |
# 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 <https://www.gnu.org/licenses/>. | |
__plugins__= ['delayed_balance'] | |
fromcollectionsimportdefaultdict | |
fromtypingimportAny,Optional,Tuple | |
frombeancount.core.dataimportAccount,Balance,Directive, \ | |
Entries,Options,Transaction | |
frombeancount.ops.validationimportValidationError | |
defdelayed_balance(entries:Entries,options_map:Options) \ | |
->Tuple[Entries,list[Any]]: | |
"""Delay certain transaction until after the next balance entry. | |
This is for handling the case where certain transactions are not included | |
in the next statement due to delays. | |
Example: | |
2021-06-22 * "Amazon.co.jp" "Purchase some books" | |
Expenses:Shopping:Books 10.00 AUD | |
Liabilities:CBA:CreditCard -10.00 AUD | |
delayed_balance: TRUE | |
2021-06-24 balance Liabilities:CBA:CreditCard -100.00 AUD | |
will be transformed to: | |
2021-06-24 balance Liabilities:CBA:CreditCard -100.00 AUD | |
2021-06-24 * "Amazon.co.jp" "Purchase some books" | |
delayed_from: 2021-06-22 | |
Expenses:Shopping:Books 10.00 AUD | |
Liabilities:CBA:CreditCard -10.00 AUD | |
""" | |
result:Entries= [] | |
errors:list[Any]= [] | |
pending:dict[Account,list[Transaction]]=defaultdict(list) | |
forentryinentries: | |
ifisinstance(entry,Transaction): | |
# Find transactions need to be delayed | |
delayed=False | |
forpostinginentry.postings: | |
ifnotposting.meta: | |
continue | |
if'delayed_balance'notinposting.meta: | |
continue | |
ifentrynotinpending[posting.account]: | |
pending[posting.account].append(entry) | |
delayed=True | |
ifdelayed: | |
continue | |
result.append(entry) | |
ifisinstance(entry,Balance)andentry.accountinpending: | |
fortxninpending[entry.account]: | |
remaining_delayed=0 | |
# Iterate over postings with delayed balance | |
forpostingintxn.postings: | |
ifnotposting.meta: | |
continue | |
if'delayed_balance'notinposting.meta: | |
continue | |
ifposting.account==entry.account: | |
delposting.meta['delayed_balance'] | |
else: | |
remaining_delayed+=1 | |
ifremaining_delayed>0: | |
continue | |
# Add delayed transactions after the balance entry | |
txn.meta['delayed_from']=txn.date | |
result.append(Transaction( | |
txn.meta, | |
entry.date, | |
txn.flag, | |
txn.payee, | |
txn.narration, | |
txn.tags, | |
txn.links, | |
txn.postings, | |
)) | |
delpending[entry.account] | |
foraccount,txnsinpending.items(): | |
fortxnintxns: | |
forpostingintxn.postings: | |
ifnotposting.meta: | |
continue | |
if'delayed_balance'notinposting.meta: | |
continue | |
errors.append(ValidationError( | |
posting.meta, | |
"Unhandled delayed balance", | |
txn, | |
)) | |
returnresult,errors | |
if__name__=='__main__': | |
importsys | |
frombeancountimportloader | |
entries,errors,options=loader.load_file(sys.argv[1],log_errors=sys.stderr) | |
delayed_balance(entries, {}) |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment