Skip to content

Commit c498589

Browse files
committedFeb 28, 2019
Multi account OFX support
* Fix egh#32 * Allows specify format of account in case of multiple accounts when importing OFX
1 parent 678ece3 commit c498589

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed
 

‎ledgerautosync/cli.py

+49-28
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def find_ledger_file(ledgerrcpath=None):
5555
return None
5656

5757

58-
def print_results(converter, ofx, ledger, txns, args):
58+
def print_results(converter, ofx, account_idx, ledger, txns, args):
5959
"""
6060
This function is the final common pathway of program:
6161
@@ -69,18 +69,18 @@ def print_results(converter, ofx, ledger, txns, args):
6969
if (not(ledger.check_transaction_by_id
7070
("ofxid", converter.mk_ofxid(AUTOSYNC_INITIAL))) and
7171
not(ledger.check_transaction_by_id("ofxid", ALL_AUTOSYNC_INITIAL))):
72-
print(converter.format_initial_balance(ofx.account.statement))
72+
print(converter.format_initial_balance(ofx.accounts[account_idx].statement))
7373
for txn in txns:
7474
print(converter.convert(txn).format(args.indent))
7575
if args.assertions:
76-
print(converter.format_balance(ofx.account.statement))
76+
print(converter.format_balance(ofx.accounts[account_idx].statement))
7777

7878
# if OFX has positions use these to obtain commodity prices
7979
# and print "P" records to provide dated/timed valuations
8080
# Note that this outputs only the commodity price,
8181
# not your position (e.g. # shares), even though this is in the OFX record
82-
if hasattr(ofx.account.statement, 'positions'):
83-
for pos in ofx.account.statement.positions:
82+
if hasattr(ofx.accounts[account_idx].statement, 'positions'):
83+
for pos in ofx.accounts[account_idx].statement.positions:
8484
print(converter.format_position(pos))
8585

8686

@@ -112,31 +112,44 @@ def import_ofx(ledger, args):
112112
sync = OfxSynchronizer(ledger, hardcodeaccount=args.hardcodeaccount,
113113
shortenaccount=args.shortenaccount)
114114
ofx = OfxSynchronizer.parse_file(args.PATH)
115-
txns = sync.filter(
116-
ofx.account.statement.transactions,
117-
ofx.account.account_id)
118-
accountname = args.account
119-
if accountname is None:
120-
if ofx.account.institution is not None:
121-
accountname = "%s:%s" % (ofx.account.institution.organization,
122-
ofx.account.account_id)
123-
else:
124-
accountname = UNKNOWN_BANK_ACCOUNT
125115

126-
# build SecurityList (including indexing by CUSIP and ticker symbol)
127-
security_list = SecurityList(ofx)
116+
account_idx = 0
117+
for account in ofx.accounts:
118+
txns = sync.filter(
119+
account.statement.transactions,
120+
account.account_id)
121+
try:
122+
accountname = args.account[account_idx]
123+
except IndexError:
124+
accountname = None
125+
if accountname is None:
126+
if account.institution is not None:
127+
accountname = "%s:%s" % (account.institution.organization,
128+
account.account_id)
129+
elif args.account_format is not None:
130+
accountname = args.account_format.format(
131+
account_id=account.account_id,
132+
account_type=account.account_type,
133+
routing_number=account.routing_number,
134+
branch_id=account.branch_id)
135+
else:
136+
accountname = "%s:%s" % (UNKNOWN_BANK_ACCOUNT, account_idx)
137+
138+
# build SecurityList (including indexing by CUSIP and ticker symbol)
139+
security_list = SecurityList(ofx)
128140

129-
converter = OfxConverter(account=ofx.account,
130-
name=accountname,
131-
ledger=ledger,
132-
indent=args.indent,
133-
fid=args.fid,
134-
unknownaccount=args.unknownaccount,
135-
payee_format=args.payee_format,
136-
hardcodeaccount=args.hardcodeaccount,
137-
shortenaccount=args.shortenaccount,
138-
security_list=security_list)
139-
print_results(converter, ofx, ledger, txns, args)
141+
converter = OfxConverter(account=account,
142+
name=accountname,
143+
ledger=ledger,
144+
indent=args.indent,
145+
fid=args.fid,
146+
unknownaccount=args.unknownaccount,
147+
payee_format=args.payee_format,
148+
hardcodeaccount=args.hardcodeaccount, #TODO
149+
shortenaccount=args.shortenaccount, #TODO
150+
security_list=security_list)
151+
print_results(converter, ofx, account_idx, ledger, txns, args)
152+
account_idx += 1
140153

141154

142155
def import_csv(ledger, args):
@@ -233,6 +246,14 @@ def run(args=None, config=None):
233246
{tferaction} for OFX. If the input file is a CSV file,
234247
substitutions are written using the CSV file column names
235248
between {}.""")
249+
parser.add_argument(
250+
'--account-format',
251+
type=str,
252+
default=None,
253+
dest='account_format',
254+
help="""Format string to use for generating the account line.
255+
Substitutions can be written using {account_id}, {routing_number},
256+
{branch_id}, {account_type} for OFX.""")
236257
parser.add_argument('--python', action='store_true', default=False,
237258
help='use the ledger python interface')
238259
parser.add_argument('--slow', action='store_true', default=False,

0 commit comments

Comments
 (0)