06/05 Optimize app: implement Plaid
This commit is contained in:
@@ -175,23 +175,32 @@ def get_transactions(access_token, account_id, start_date=None, end_date=None,
|
||||
return data
|
||||
|
||||
|
||||
def parse_transaction(teller_txn, pfm_account_id, category_id_map):
|
||||
def parse_transaction(teller_txn, pfm_account_id, category_id_map, is_credit_card=False):
|
||||
"""
|
||||
Convert a Teller transaction dict to a PFM transaction dict ready for import.
|
||||
Returns dict with keys matching Transaction model fields.
|
||||
|
||||
Teller amounts:
|
||||
- Positive = money entering the account (income / credit)
|
||||
- Negative = money leaving the account (expense / debit)
|
||||
Teller sign convention differs by account type:
|
||||
- Depository (checking/savings): positive = inflow (income), negative = outflow (expense)
|
||||
- Credit (credit card): positive = charge/purchase (expense), negative = payment/credit (income)
|
||||
"""
|
||||
amount_raw = float(teller_txn.get('amount', 0))
|
||||
# Teller: positive = inflow/credit (income), negative = outflow/debit (expense)
|
||||
if amount_raw < 0:
|
||||
txn_type = 'expense'
|
||||
amount = abs(amount_raw)
|
||||
if is_credit_card:
|
||||
# Credit cards: positive amount = purchase (expense), negative = payment (income)
|
||||
if amount_raw > 0:
|
||||
txn_type = 'expense'
|
||||
amount = amount_raw
|
||||
else:
|
||||
txn_type = 'income'
|
||||
amount = abs(amount_raw)
|
||||
else:
|
||||
txn_type = 'income'
|
||||
amount = amount_raw
|
||||
# Depository: positive = deposit (income), negative = withdrawal (expense)
|
||||
if amount_raw < 0:
|
||||
txn_type = 'expense'
|
||||
amount = abs(amount_raw)
|
||||
else:
|
||||
txn_type = 'income'
|
||||
amount = amount_raw
|
||||
|
||||
description = teller_txn.get('description', '').strip() or 'Teller transaction'
|
||||
# Use enriched counterparty name if available
|
||||
@@ -264,9 +273,16 @@ def sync_preview(teller_account, days_back=90):
|
||||
raise
|
||||
|
||||
cat_map = build_category_map()
|
||||
is_cc = (
|
||||
teller_account.account_type == 'credit' or
|
||||
teller_account.account_subtype == 'credit_card' or
|
||||
(teller_account.pfm_account and
|
||||
teller_account.pfm_account.account_type == 'credit_card')
|
||||
)
|
||||
parsed = []
|
||||
for txn in raw_txns:
|
||||
p = parse_transaction(txn, teller_account.pfm_account_id, cat_map)
|
||||
p = parse_transaction(txn, teller_account.pfm_account_id, cat_map,
|
||||
is_credit_card=is_cc)
|
||||
parsed.append(p)
|
||||
|
||||
return parsed
|
||||
|
||||
Reference in New Issue
Block a user