Skip to content

Commit

Permalink
healthequity: skip balance assertion when multiple transactions in a day
Browse files Browse the repository at this point in the history
beancount only supports one balance assertion per day, so if we create
intra-day balance assertions on days with multiple transactions, all but the
last of them will be wrong. Skip the balance assertions if there are multiple
transactions in a day instead.
  • Loading branch information
jktomer committed Jan 8, 2024
1 parent ef9ee77 commit df6692b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
40 changes: 25 additions & 15 deletions beancount_import/source/healthequity.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,26 +519,36 @@ def convert_account(entry: RawEntry):
results=results)

balance_entries = collections.OrderedDict(
) # type: Dict[Tuple[datetime.date, str, str], ImportResult]
) # type: Dict[Tuple[datetime.date, str, str], Optional[ImportResult]]

for entry in transactions:
date = entry.date + datetime.timedelta(days=1)
balance_entries[(date, entry.account,
entry.balance.currency)] = ImportResult(
date=date,
entries=[
Balance(
date=date,
meta=None,
account=entry.account,
amount=entry.balance,
tolerance=None,
diff_amount=None)
],
info=get_info(entry))
key = (date, entry.account, entry.balance.currency)

# When multiple transactions happen on the same day, we can't trust
# the reported balance because we don't know which order to apply
# them in. Just skip it, and put a tombstone in place of the one
# that was already there.
if key in balance_entries:
balance_entries[key] = None
continue

balance_entries[key] = ImportResult(
date=date,
entries=[
Balance(
date=date,
meta=None,
account=entry.account,
amount=entry.balance,
tolerance=None,
diff_amount=None)
],
info=get_info(entry))

for entry in balance_entries.values():
results.add_pending_entry(entry)
if entry != None:
results.add_pending_entry(entry)

for balance in balances:
# Skip outputting recent balances --- just output prices.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,6 @@
source_desc: "Buy"
Assets:HSA:HealthEquity:Cash -1936.76 USD

;; date: 2016-03-12
;; info: {"filename": "<testdata>/data/1234567/cash-transactions-other.csv", "line": 5, "type": "text/csv"}

2016-03-12 balance Assets:HSA:HealthEquity:Cash 500.00 USD

;; date: 2016-03-12
;; info: {"filename": "<testdata>/data/1234567/investment-transactions.csv", "line": 3, "type": "text/csv"}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@

2016-03-01 balance Assets:HSA:HealthEquity:Cash 836.76 USD

;; date: 2016-03-12
;; info: {"filename": "<testdata>/data/1234567/cash-transactions-other.csv", "line": 5, "type": "text/csv"}

2016-03-12 balance Assets:HSA:HealthEquity:Cash 500.00 USD

;; date: 2016-03-12
;; info: {"filename": "<testdata>/data/1234567/investment-transactions.csv", "line": 3, "type": "text/csv"}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@

2016-03-01 balance Assets:HSA:HealthEquity:Cash 836.76 USD

;; date: 2016-03-12
;; info: {"filename": "<testdata>/data/1234567/cash-transactions-other.csv", "line": 5, "type": "text/csv"}

2016-03-12 balance Assets:HSA:HealthEquity:Cash 500.00 USD

;; date: 2016-03-12
;; info: {"filename": "<testdata>/data/1234567/investment-transactions.csv", "line": 3, "type": "text/csv"}

Expand Down

0 comments on commit df6692b

Please sign in to comment.