Skip to content

Commit

Permalink
Merge pull request #62 from 501st-alpha1/match-autosync-payee
Browse files Browse the repository at this point in the history
Match autosync payee
  • Loading branch information
egh authored Mar 23, 2019
2 parents 81a4a2d + 90f0166 commit c2d5c2f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
66 changes: 66 additions & 0 deletions fixtures/checking-payee-match.ofx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE

<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<DTSERVER>20130525225731.258
<LANGUAGE>ENG
<DTPROFUP>20050531060000.000
<FI>
<ORG>FAKE
<FID>1101
</FI>
<INTU.BID>51123
<INTU.USERID>9774652
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID>0
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<STMTRS>
<CURDEF>USD
<BANKACCTFROM>
<BANKID>5472369148
<ACCTID>1452687~7
<ACCTTYPE>CHECKING
</BANKACCTFROM>
<BANKTRANLIST>
<DTSTART>20000101070000.000
<DTEND>20130525060000.000
<STMTTRN>
<TRNTYPE>DEBIT
<DTPOSTED>20110331120000.000
<TRNAMT>-0.01
<FITID>0000489
<NAME>Payment to MATCH PAYEE and so on and so forth
<MEMO>
</STMTTRN>
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>100.99
<DTASOF>20130525225731.258
</LEDGERBAL>
<AVAILBAL>
<BALAMT>75.99
<DTASOF>20130525225731.258
</AVAILBAL>
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
</OFX>
5 changes: 5 additions & 0 deletions fixtures/checking.lgr
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@
2011/03/31 PAYEE TEST"QUOTE
Assets:Foo $0.01
Income:Bar -$0.01

2011/03/31 Match Payee
; AutosyncPayee: Payment to MATCH PAYEE and so on and so forth
Assets:Foo -$0.01
Expenses:Bar $0.01
3 changes: 3 additions & 0 deletions ledgerautosync/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ def format_payee(self, txn):
if (hasattr(txn, 'tferaction') and txn.tferaction is not None):
tferaction = txn.tferaction.lower()

if payee != "" and self.lgr is not None:
payee = self.lgr.get_autosync_payee(payee, self.name)

payee_format = self.payee_format

if payee_format is None:
Expand Down
17 changes: 17 additions & 0 deletions ledgerautosync/ledgerwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def load_payees(self):
for line in r:
self.add_payee(line[2], line[3])

def get_autosync_payee(self, payee, account):
q = [account, "--last", "1", "--format", "%(quoted(payee))\n",
"--limit", 'tag("AutosyncPayee") == "%s"' % (payee)]
r = self.run(q)
try:
return next(r)[0]
except StopIteration:
return payee


class LedgerPython(MetaLedger):
@staticmethod
Expand Down Expand Up @@ -216,6 +225,10 @@ def check_transaction_by_id(self, key, value):
(key, Converter.clean_id(value)))
return len(q) > 0

def get_autosync_payee(self, payee, account):
logging.error("payee lookup not implemented for LedgerPython, using raw payee")
return payee


class HLedger(MetaLedger):
@staticmethod
Expand Down Expand Up @@ -257,3 +270,7 @@ def load_payees(self):
next(r) # skip headers
for line in r:
self.add_payee(line['description'], line['account'])

def get_autosync_payee(self, payee, account):
logging.error("payee lookup not implemented for HLedger, using raw payee")
return payee
14 changes: 14 additions & 0 deletions tests/test_ofx_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,17 @@ def test_investments_custom_payee(self):
self.assertEqual(
converter.format_payee(ofx.account.statement.transactions[1]),
'in')

def test_payee_match(self):
ofx = OfxParser.parse(
open(os.path.join('fixtures', 'checking-payee-match.ofx')))
ledger = Ledger(os.path.join('fixtures', 'checking.lgr'))
converter = OfxConverter(account=ofx.account, name="Foo", ledger=ledger)
self.assertEqualLedgerPosting(
converter.convert(
ofx.account.statement.transactions[0]).format(),
"""2011/03/31 Match Payee
Foo -$0.01
; ofxid: 1101.1452687~7.0000489
Expenses:Bar $0.01
""")

0 comments on commit c2d5c2f

Please sign in to comment.