jbms / beancount-import

Web UI for semi-automatically importing external data into beancount
GNU General Public License v2.0
396 stars 103 forks source link

ofx parser doesn't handle common stock transfers #152

Open criccomini opened 2 years ago

criccomini commented 2 years ago

A raw transaction entry like so:

RawTransactionEntry(filename='some.qfx', date=datetime.date(2000, 1, 1), fitid='1234567', trantype='TRANSFER', total=None, incometype=None, inv401ksource=None, memo='RECEIVE COMMON STOCKRECEIVE COMMON STOCK', name=None, trntype=None, uniqueid='12345M123', units=Decimal('123456.0'), unitprice=None, tferaction='IN', fees=None, commission=None, checknum=None)

Results in:

Traceback (most recent call last):
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/webserver.py", line 514, in _handle_reconciler_loaded
    loaded_reconciler = loaded_future.result()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/concurrent/futures/_base.py", line 437, in result
    return self.__get_result()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/thread_helpers.py", line 13, in wrapper
    f.set_result(fn(*args, **kwargs))
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/reconcile.py", line 396, in __init__
    all_source_results = self._prepare_sources()
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/reconcile.py", line 520, in _prepare_sources
    source.prepare(self.editor, source_results)
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/source/ofx.py", line 1445, in prepare
    state.get_accounts_and_entries()
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/source/ofx.py", line 1292, in get_accounts_and_entries
    statement.get_entries(self)
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/source/ofx.py", line 953, in get_entries
    print("!!");print(raw);unitprice = normalize_fraction(raw.unitprice)
  File "/Users/chrisriccomini/Code/beancount-vg-import/.venv/lib/python3.8/site-packages/beancount_import/source/ofx.py", line 577, in normalize_fraction
    normalized = d.normalize()
AttributeError: 'NoneType' object has no attribute 'normalize'

This line denotes stock that was transferred from one brokerage to another.

criccomini commented 2 years ago

Not sure this is the right fix, but this line appears to make the parsing work:

unitprice = normalize_fraction(raw.unitprice) if raw.unitprice else ZERO
jbms commented 2 years ago

Thanks --- would you be able to submit a PR with this change and a test case?