Skip to content

Commit

Permalink
Merge pull request #48 from scanta2/FailedAssertOFX
Browse files Browse the repository at this point in the history
Fix ofx assert for Fidelity. Fixes #47
  • Loading branch information
jbms committed Jul 25, 2020
2 parents e1e7a60 + a01b782 commit 9eb4361
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
43 changes: 22 additions & 21 deletions beancount_import/source/ofx.py
Expand Up @@ -842,6 +842,21 @@ def get_subaccount_cash(inv401ksource: Optional[str] = None) -> str:
if stripped_checknum:
posting_meta[CHECK_KEY] = D(stripped_checknum)

fee_total = ZERO
for fee_key in ['fees', 'commission']:
amount = getattr(raw, fee_key, None)
if amount is not None and amount != ZERO:
fee_total += amount
entry.postings.append(
Posting(
account=get_account_by_key(account,
fee_key + '_account'),
units=Amount(number=amount, currency=self.currency),
cost=None,
price=None,
flag=None,
meta=None))

cash_transfer_transaction_amount = None
if raw.trantype == 'INCOME' or raw.trantype == 'INVBANKTRAN' or raw.trantype == 'STMTTRN':
# Cash-only transaction
Expand Down Expand Up @@ -929,9 +944,15 @@ def get_subaccount_cash(inv401ksource: Optional[str] = None) -> str:
date=None,
label='FIXME',
merge=False)
elif raw.trantype == 'TRANSFER' and units == ZERO:
# Internal transfer, i.e. from after-tax to roth
continue
else:
number_per_fix = unitprice
if abs(total + fee_total + (units * unitprice)) >= TOLERANCE:
number_per_fix = normalize_fraction((abs(total)-abs(fee_total))/units)
cost_spec = CostSpec(
number_per=unitprice,
number_per=number_per_fix,
number_total=None,
currency=self.currency,
date=None,
Expand Down Expand Up @@ -1022,26 +1043,6 @@ def get_subaccount_cash(inv401ksource: Optional[str] = None) -> str:
flag=None,
meta=external_meta))

fee_total = ZERO
for fee_key in ['fees', 'commission']:
amount = getattr(raw, fee_key, None)
if amount is not None and amount != ZERO:
fee_total += amount
entry.postings.append(
Posting(
account=get_account_by_key(account,
fee_key + '_account'),
units=Amount(number=amount, currency=self.currency),
cost=None,
price=None,
flag=None,
meta=None))

if raw.trantype in STOCK_BUY_SELL_TYPES:
assert abs(total + fee_total +
(units * unitprice)) < TOLERANCE, abs(
total + fee_total + (units * unitprice))

if not security_transaction_exists and not cash_transaction_exists:
results.add_pending_entry(
ImportResult(
Expand Down
Expand Up @@ -125,14 +125,14 @@

; features: []
2018-08-01 * "BUYSTOCK"
Expenses:Investment:MyBank:Fees 63.4869 USD
Expenses:Investment:MyBank:Commission 23.0233 USD
Assets:Investment:MyBank:SDVMV 60.01318 SDVMV {67.141053527 USD}
date: 2018-08-01
ofx_fitid: "aedf1852aa39a54-623ee.4d104.5"
ofx_type: "BUYSTOCK"
Assets:Investment:MyBank:Cash -4115.86 USD
ofx_fitid: "aedf1852aa39a54-623ee.4d104.5"
Expenses:Investment:MyBank:Fees 63.4869 USD
Expenses:Investment:MyBank:Commission 23.0233 USD

;; date: 2018-08-01
;; info: {"filename": "<testdata>/investment_buy_sell_income.ofx", "type": "application/x-ofx"}
Expand All @@ -150,15 +150,15 @@

; features: []
2018-08-01 * "SELLSTOCK"
Expenses:Investment:MyBank:Fees 31.9944 USD
Expenses:Investment:MyBank:Commission 57.7239 USD
Assets:Investment:MyBank:EEBHF -41.50416 EEBHF {} @ 83.661853593 USD
date: 2018-08-01
ofx_fitid: "4a5141ead2c672e8a559.65-80e.b"
ofx_type: "SELLSTOCK"
Income:MyBank:Capital-Gains:EEBHF
Assets:Investment:MyBank:Cash 3382.60 USD
ofx_fitid: "4a5141ead2c672e8a559.65-80e.b"
Expenses:Investment:MyBank:Fees 31.9944 USD
Expenses:Investment:MyBank:Commission 57.7239 USD

;; date: 2018-08-04
;; info: {"filename": "<testdata>/investment_buy_sell_income.ofx", "type": "application/x-ofx"}
Expand Down

0 comments on commit 9eb4361

Please sign in to comment.