Skip to content

Commit 6c54ed9

Browse files
authored
Some minor improvements to the healthequity source (#225)
* healthequity: ignore_before option * healthequity: skip balance assertion when multiple transactions in a day 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.
1 parent 30dc718 commit 6c54ed9

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

beancount_import/source/healthequity.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,12 @@ def make_import_result(csv_entry: RawTransaction, accounts: Dict[str, Open],
457457

458458

459459
class Source(description_based_source.DescriptionBasedSource):
460-
def __init__(self, directory: str, **kwargs) -> None:
460+
def __init__(self, directory: str,
461+
ignore_before: Optional[datetime.date] = None,
462+
**kwargs) -> None:
461463
super().__init__(**kwargs)
462464
self.directory = directory
465+
self.ignore_before = ignore_before
463466
self.raw_transactions = [
464467
] # type: List[Union[CashTransaction, FundTransaction]]
465468
self.raw_balances = [] # type: List[ImportedBalance]
@@ -493,9 +496,15 @@ def convert_account(entry: RawEntry):
493496
account_to_id[full_account] = account_id
494497
return entry._replace(account=full_account)
495498

496-
balances = [convert_account(entry) for entry in self.raw_balances]
499+
balances = [
500+
convert_account(entry)
501+
for entry in self.raw_balances
502+
if self.ignore_before is None or self.ignore_before <= entry.date
503+
]
497504
transactions = [
498-
convert_account(entry) for entry in self.raw_transactions
505+
convert_account(entry)
506+
for entry in self.raw_transactions
507+
if self.ignore_before is None or self.ignore_before <= entry.date
499508
]
500509

501510
description_based_source.get_pending_and_invalid_entries(
@@ -510,26 +519,36 @@ def convert_account(entry: RawEntry):
510519
results=results)
511520

512521
balance_entries = collections.OrderedDict(
513-
) # type: Dict[Tuple[datetime.date, str, str], ImportResult]
522+
) # type: Dict[Tuple[datetime.date, str, str], Optional[ImportResult]]
514523

515524
for entry in transactions:
516525
date = entry.date + datetime.timedelta(days=1)
517-
balance_entries[(date, entry.account,
518-
entry.balance.currency)] = ImportResult(
519-
date=date,
520-
entries=[
521-
Balance(
522-
date=date,
523-
meta=None,
524-
account=entry.account,
525-
amount=entry.balance,
526-
tolerance=None,
527-
diff_amount=None)
528-
],
529-
info=get_info(entry))
526+
key = (date, entry.account, entry.balance.currency)
527+
528+
# When multiple transactions happen on the same day, we can't trust
529+
# the reported balance because we don't know which order to apply
530+
# them in. Just skip it, and put a tombstone in place of the one
531+
# that was already there.
532+
if key in balance_entries:
533+
balance_entries[key] = None
534+
continue
535+
536+
balance_entries[key] = ImportResult(
537+
date=date,
538+
entries=[
539+
Balance(
540+
date=date,
541+
meta=None,
542+
account=entry.account,
543+
amount=entry.balance,
544+
tolerance=None,
545+
diff_amount=None)
546+
],
547+
info=get_info(entry))
530548

531549
for entry in balance_entries.values():
532-
results.add_pending_entry(entry)
550+
if entry != None:
551+
results.add_pending_entry(entry)
533552

534553
for balance in balances:
535554
# Skip outputting recent balances --- just output prices.

testdata/source/healthequity/test_basic/import_results.beancount

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,6 @@
220220
source_desc: "Buy"
221221
Assets:HSA:HealthEquity:Cash -1936.76 USD
222222

223-
;; date: 2016-03-12
224-
;; info: {"filename": "<testdata>/data/1234567/cash-transactions-other.csv", "line": 5, "type": "text/csv"}
225-
226-
2016-03-12 balance Assets:HSA:HealthEquity:Cash 500.00 USD
227-
228223
;; date: 2016-03-12
229224
;; info: {"filename": "<testdata>/data/1234567/investment-transactions.csv", "line": 3, "type": "text/csv"}
230225

testdata/source/healthequity/test_invalid/import_results.beancount

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333

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

36-
;; date: 2016-03-12
37-
;; info: {"filename": "<testdata>/data/1234567/cash-transactions-other.csv", "line": 5, "type": "text/csv"}
38-
39-
2016-03-12 balance Assets:HSA:HealthEquity:Cash 500.00 USD
40-
4136
;; date: 2016-03-12
4237
;; info: {"filename": "<testdata>/data/1234567/investment-transactions.csv", "line": 3, "type": "text/csv"}
4338

testdata/source/healthequity/test_matching/import_results.beancount

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333

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

36-
;; date: 2016-03-12
37-
;; info: {"filename": "<testdata>/data/1234567/cash-transactions-other.csv", "line": 5, "type": "text/csv"}
38-
39-
2016-03-12 balance Assets:HSA:HealthEquity:Cash 500.00 USD
40-
4136
;; date: 2016-03-12
4237
;; info: {"filename": "<testdata>/data/1234567/investment-transactions.csv", "line": 3, "type": "text/csv"}
4338

0 commit comments

Comments
 (0)