@@ -103,6 +103,7 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
103103 total_transactions = len (txns )
104104 for i , txn in enumerate (txns , start = 1 ):
105105 cp_iban = txn .get ("meta" , {}).get ("counter_party_iban" )
106+ cp_name = txn .get ("meta" , {}).get ("counter_party_preferred_name" )
106107 transaction_type = (
107108 "debit" if txn ["transaction_type" ].lower () == "debit" else "credit"
108109 )
@@ -121,14 +122,29 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
121122 ):
122123 continue
123124
125+ # Check if the IBAN matches
124126 if cp_iban == firefly_account ["attributes" ].get ("iban" ):
125127 yield f"Matching account found via IBAN: { txn ['description' ]} - { cp_iban } "
126128 linked_account = firefly_account
127129 matching += 1
128130 break
129131
132+ # Check if the name matches, as a final fallback
133+ # This is not prefered, but can be used if the IBAN is not available or when the account uses multiple IBANs
134+ # Firefly doesn't allow to create multiple accounts with the same name, so this should be safe
135+ if cp_name is not None and cp_name == firefly_account [
136+ "attributes"
137+ ].get ("name" ):
138+ yield f"Matching account found via name: { txn ['description' ]} - { cp_name } "
139+ linked_account = firefly_account
140+ matching += 1
141+ break
142+
130143 if linked_account is None :
131- yield f"No match, still a valid IBAN. Creating a new account: { txn } - { cp_iban } "
144+ account_type = (
145+ "revenue" if transaction_type == "credit" else "Expense"
146+ )
147+ yield f"No match, still a valid IBAN. Creating a new account: { txn } - { cp_iban } - { account_type } "
132148 response = await self ._firefly_client .create_account (
133149 {
134150 "name" : txn .get ("meta" , {}).get (
@@ -151,7 +167,6 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
151167 newly_created += 1
152168 else :
153169 unmatching += 1
154- # TODO: find the unknown destination account
155170 yield f"Transaction has no IBAN: { txn ['description' ]} "
156171
157172 # Ensure the amount is always positive
0 commit comments