@@ -21,18 +21,21 @@ def __init__(self):
2121 self .token = PERSONAL_TOKEN_DEFAULT
2222 self .hostname = HOSTNAME_DEFAULT
2323 self .name_format = ACCOUNTS_NAME_FORMAT_DEFAULT
24+ self .auto_detect_transfers = bool (AUTO_DETECT_TRANSFERS_DEFAULT )
25+ self .transfer_source_transaction = TRANSFER_SOURCE_TRANSACTION_NAME_DEFAULT .strip ().split ("," )
26+ self .transfer_destination_transaction = TRANSFER_DESTINATION_TRANSACTION_NAME_DEFAULT .strip ().split ("," )
2427 self .headers = None
2528
2629 def _post (self , endpoint , payload ):
2730 response = requests .post ("{}{}" .format (self .hostname , endpoint ), json = payload , headers = self .headers )
2831 if response .status_code != 200 :
29- raise ValueError ("Request to your Firefly3 instance failed. Please double check your personal token." )
32+ raise ValueError ("Request to your Firefly3 instance failed. Please double check your personal token. Error : " + str ( response . json ()) )
3033 return response .json ()
3134
3235 def _get (self , endpoint , params = None ):
3336 response = requests .get ("{}{}" .format (self .hostname , endpoint ), params = params , headers = self .headers )
3437 if response .status_code != 200 :
35- raise ValueError ("Request to your Firefly3 instance failed. Please double check your personal token." )
38+ raise ValueError ("Request to your Firefly3 instance failed. Please double check your personal token. Error : " + str ( response . json ()) )
3639 return response .json ()
3740
3841 def validate (self ):
@@ -110,14 +113,15 @@ def __init__(self, f3_cli, account_id):
110113 self .f3_cli = f3_cli
111114 self .account_id = int (account_id )
112115 self .payloads = []
113- self .transfer_out = [] # [date, montant]
114- self .transfer_in = [] # [date, montant]
115- # --> On extrait pour chaque compte, une liste unique de dates. Et pour chacunes de ces dates on regarde,
116- # quel compte a emis, quel compte a reçu. Une fois qu'on les a, on crée le transfert (add_transfert?) et pas oublier
117- # de supprimer les 2 transactions du payload (ou alors juste on ne les ajotue pas au payload dans ce genre de cas
116+ self .transfer_out = {}
117+ self .transfer_in = {}
118118
119119 def __len__ (self ):
120- return len (self .payloads )
120+ count = 0
121+ for transfer_list in [self .transfer_in , self .transfer_out ]:
122+ for date in transfer_list :
123+ count = count + len (transfer_list [date ])
124+ return len (self .payloads ) + count
121125
122126 def add_transaction (self , ca_payload ):
123127 payload = {"transactions" : [{}]}
@@ -128,22 +132,12 @@ def add_transaction(self, ca_payload):
128132 renames = get_key_from_value (self .f3_cli .a_rename_transaction , transaction_name )
129133 transaction ["description" ] = renames [0 ] if len (renames ) > 0 else transaction_name
130134
131- date = time .mktime (time .strptime (ca_payload ["dateOperation" ], '%b %d, %Y %H:%M:%S %p' ))
132- transaction ["date" ] = time .strftime ("%Y-%m-%d " , time .gmtime (date ))
135+ date = time .mktime (time .strptime (ca_payload ["dateOperation" ], '%b %d, %Y, %H:%M:%S %p' ))
136+ transaction ["date" ] = time .strftime ("%Y-%m-%dT%T " , time .gmtime (date ))
133137
134138 transaction ["amount" ] = abs (ca_payload ["montant" ])
135139 transaction ["currency_code" ] = ca_payload ["idDevise" ]
136140
137- accounts = get_key_from_value (self .f3_cli .aa_account , transaction_name )
138- if ca_payload ["montant" ] > 0 :
139- transaction ["type" ] = "deposit"
140- transaction ["source_name" ] = accounts [0 ] if len (accounts ) > 0 else "Cash account"
141- transaction ["destination_id" ] = self .account_id
142- else :
143- transaction ["type" ] = "withdrawal"
144- transaction ["source_id" ] = self .account_id
145- transaction ["destination_name" ] = accounts [0 ] if len (accounts ) > 0 else "Cash account"
146-
147141 budgets = get_key_from_value (self .f3_cli .aa_budget , transaction_name )
148142 if len (budgets ) != 0 :
149143 transaction ["budget_id" ] = self .f3_cli .get_budget_id (budgets [0 ])
@@ -157,12 +151,86 @@ def add_transaction(self, ca_payload):
157151 tags .append (tag )
158152 transaction ["tags" ] = tags
159153
160- transaction ["notes" ] = transaction_name
154+ transaction ["notes" ] = "CREDIT AGRICOLE NAME : " + transaction_name
155+
156+ if self .f3_cli .auto_detect_transfers and is_in_list (self .f3_cli .transfer_source_transaction , transaction_name ):
157+ key = transaction ["date" ]
158+ if key not in self .transfer_out :
159+ self .transfer_out [key ] = []
160+ self .transfer_out [key ].append (payload )
161161
162- if
163- self .payloads .append (payload )
162+ elif self .f3_cli .auto_detect_transfers and is_in_list (self .f3_cli .transfer_destination_transaction , transaction_name ):
163+ key = transaction ["date" ]
164+ if key not in self .transfer_in :
165+ self .transfer_in [key ] = []
166+ self .transfer_in [key ].append (payload )
167+
168+ else :
169+ accounts = get_key_from_value (self .f3_cli .aa_account , transaction_name )
170+ if ca_payload ["montant" ] > 0 :
171+ transaction ["type" ] = "deposit"
172+ transaction ["source_name" ] = accounts [0 ] if len (accounts ) > 0 else "Cash account"
173+ transaction ["destination_id" ] = self .account_id
174+ else :
175+ transaction ["type" ] = "withdrawal"
176+ transaction ["source_id" ] = self .account_id
177+ transaction ["destination_name" ] = accounts [0 ] if len (accounts ) > 0 else "Cash account"
178+
179+ self .payloads .append (payload )
164180
165181 def post (self ):
166182 for payload in self .payloads :
167183 print ("." , end = '' )
168184 self .f3_cli ._post (endpoint = _TRANSACTIONS_ENDPOINT , payload = payload )
185+
186+ @staticmethod
187+ def post_transfers (f3transactions_list , f3_cli ):
188+ payloads = []
189+ detected_transfers = 0
190+
191+ # Loop through all transaction packages
192+ for f3_from_transactions in f3transactions_list :
193+ # Count detected transfers for later test
194+ detected_transfers = detected_transfers + len (f3_from_transactions ) - len (f3_from_transactions .payloads )
195+ # Loop through dates of outgoing transfers of the above transaction package
196+ for date_out in f3_from_transactions .transfer_out .keys ():
197+ # Loop through outgoing transfers for the above date
198+ for transfer_out in f3_from_transactions .transfer_out [date_out ]:
199+ # Get the amount
200+ amount_out = transfer_out ["transactions" ][0 ]["amount" ]
201+ # Now loop through other transaction packages
202+ for f3_to_transactions in f3transactions_list :
203+ # If the above transaction package is the same than the first one, skip it
204+ if f3_to_transactions .account_id == f3_from_transactions .account_id :
205+ continue
206+ # Loop through dates of incoming transfers
207+ for date_in in f3_to_transactions .transfer_in .keys ():
208+ # If the incoming transfer date is the same than our outgoing transfer date, check amounts
209+ if date_in == date_out :
210+ # Loop through incoming transfers
211+ for transfer_in in f3_to_transactions .transfer_in [date_in ]:
212+ # Get the amount
213+ amount_in = transfer_in ["transactions" ][0 ]["amount" ]
214+ # If incoming transfer amount is the same than outgoing transfer amount,
215+ # it means that we found a corresponding incoming transfer for our outgoing transfer
216+ if amount_in == amount_out :
217+ transfer_out ["transactions" ][0 ]["type" ] = "transfer"
218+ # We clarify the transfer payload (who's the source and destination)
219+ transfer_out ["transactions" ][0 ]["source_id" ] = f3_from_transactions .account_id
220+ transfer_out ["transactions" ][0 ]["destination_id" ] = f3_to_transactions .account_id
221+ # We rename the transaction
222+ transfer_out ["transactions" ][0 ]["description" ] = "Personal transfer"
223+ # We save the payload to push it later
224+ payloads .append (transfer_out )
225+
226+ # Check amount of transfers
227+ if detected_transfers % 2 != 0 or len (payloads ) * 2 != detected_transfers :
228+ print ("\n WARN: Wrong quantity of transfers detected (" + str (detected_transfers ) + ") for " + str (len (payloads )) + " payload(s). You must double check your \" transfer-source-transaction-name\" and \" transfer-destination-transaction-name\" because some transfers hadn't been recognized." )
229+
230+ # Now push each payload
231+ for payload in payloads :
232+ print ("." , end = '' )
233+ f3_cli ._post (endpoint = _TRANSACTIONS_ENDPOINT , payload = payload )
234+
235+
236+
0 commit comments