From 22831a12d685ef5d2defe98b219ba1441f7345a4 Mon Sep 17 00:00:00 2001 From: Red S Date: Mon, 27 Feb 2023 11:06:34 -0800 Subject: [PATCH] feat: get_available_cash now takes as input settlement_fund_balance different institutions seem to compute this in different ways. overridable method now includes settlement_fund_balance for use if needed in importers --- beancount_reds_importers/importers/fidelity/__init__.py | 3 +++ .../libreader/csv_multitable_reader.py | 2 +- beancount_reds_importers/libreader/csvreader.py | 2 +- beancount_reds_importers/libreader/ofxreader.py | 9 +++++++-- beancount_reds_importers/libreader/reader.py | 2 +- .../libtransactionbuilder/investments.py | 5 ++--- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/beancount_reds_importers/importers/fidelity/__init__.py b/beancount_reds_importers/importers/fidelity/__init__.py index 29213c8..d0438b7 100644 --- a/beancount_reds_importers/importers/fidelity/__init__.py +++ b/beancount_reds_importers/importers/fidelity/__init__.py @@ -27,3 +27,6 @@ def get_target_acct_custom(self, transaction, ticker=None): if transaction.memo.startswith("FEES"): return self.config['fees'] return None + + def get_available_cash(self, settlement_fund_balance=0): + return getattr(self.ofx_account.statement, 'available_cash', None) diff --git a/beancount_reds_importers/libreader/csv_multitable_reader.py b/beancount_reds_importers/libreader/csv_multitable_reader.py index a9ff406..8a8f4ef 100644 --- a/beancount_reds_importers/libreader/csv_multitable_reader.py +++ b/beancount_reds_importers/libreader/csv_multitable_reader.py @@ -94,5 +94,5 @@ def get_balance_positions(self): # TODO raise "Not supported" - def get_available_cash(self): + def get_available_cash(self, settlement_fund_balance=0): return None diff --git a/beancount_reds_importers/libreader/csvreader.py b/beancount_reds_importers/libreader/csvreader.py index a5c65db..de7975e 100644 --- a/beancount_reds_importers/libreader/csvreader.py +++ b/beancount_reds_importers/libreader/csvreader.py @@ -190,7 +190,7 @@ def get_transactions(self): continue yield ot - def get_available_cash(self): + def get_available_cash(self, settlement_fund_balance=0): return None # TOOD: custom, overridable diff --git a/beancount_reds_importers/libreader/ofxreader.py b/beancount_reds_importers/libreader/ofxreader.py index 28f8463..6e52f27 100644 --- a/beancount_reds_importers/libreader/ofxreader.py +++ b/beancount_reds_importers/libreader/ofxreader.py @@ -60,8 +60,13 @@ def get_balance_positions(self): return [] yield from self.ofx_account.statement.positions - def get_available_cash(self): - return getattr(self.ofx_account.statement, 'available_cash', None) + def get_available_cash(self, settlement_fund_balance=0): + available_cash = getattr(self.ofx_account.statement, 'available_cash', None) + if available_cash is not None: + # Some institutions compute available_cash this way. For others, override this method + # in the importer + return available_cash - settlement_fund_balance + return None def get_max_transaction_date(self): try: diff --git a/beancount_reds_importers/libreader/reader.py b/beancount_reds_importers/libreader/reader.py index 9ea4347..3598272 100644 --- a/beancount_reds_importers/libreader/reader.py +++ b/beancount_reds_importers/libreader/reader.py @@ -41,5 +41,5 @@ def get_balance_statement(self, file=None): def get_balance_positions(self): return [] - def get_available_cash(self): + def get_available_cash(self, settlement_fund_balance=0): return None diff --git a/beancount_reds_importers/libtransactionbuilder/investments.py b/beancount_reds_importers/libtransactionbuilder/investments.py index 1858b5f..f83664d 100644 --- a/beancount_reds_importers/libtransactionbuilder/investments.py +++ b/beancount_reds_importers/libtransactionbuilder/investments.py @@ -375,15 +375,14 @@ def extract_balances_and_prices(self, file, counter): new_entries.append(price_entry) # ----------------- available cash - available_cash = self.get_available_cash() + available_cash = self.get_available_cash(settlement_fund_balance) if available_cash is not None: - balance = available_cash - settlement_fund_balance metadata = data.new_metadata(file.name, next(counter)) metadata.update(self.build_metadata(file, metatype='balance_cash')) try: bal_date = date if date else self.file_date(file).date() # unavailable file_date raises AttributeError balance_entry = data.Balance(metadata, bal_date, self.config['cash_account'], - amount.Amount(balance, self.currency), + amount.Amount(available_cash, self.currency), None, None) new_entries.append(balance_entry) except AttributeError: