From 9ed3fd55f7919b570dcb8ce3294018c5f0652836 Mon Sep 17 00:00:00 2001 From: Tom Hodder Date: Tue, 26 Oct 2021 19:42:51 +0100 Subject: [PATCH] add filter for duplicate user/amount/date transactions --- bin/reconcile.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bin/reconcile.rb b/bin/reconcile.rb index 1b9eb3d..f4e9f24 100755 --- a/bin/reconcile.rb +++ b/bin/reconcile.rb @@ -45,6 +45,8 @@ def send_subscribe_email(email, full_name) db = PG.connect(dbname: 'hackspace', user: 'hackspace', password: pgpass) +db.prepare 'dupesql', "SELECT count(*) FROM lhspayments_payment WHERE user_id = $1 AND payment_type=1 AND amount = $2 AND CAST(timestamp AS date) = $3" + ofx.bank_account.statement.transactions.each do |transaction| if transaction.fit_id.to_i < 200000000000000 # Barclays now returns non_unique fit_ids in a low range for uncleared transactions. @@ -85,6 +87,17 @@ def send_subscribe_email(email, full_name) next end + # ignore transactions that are duplicates for the same user, amount and + # transaction date. + # get the date part of the OFX transaction DTPOSTED field as a string + trn_date = transaction.date.strftime("%Y-%m-%d") + # search for transactions that match the user, amount and date-part + dupes = db.exec_prepared('dupesql', [user['id'], transaction.amount, trn_date])[0]['count'] + if dupes.to_i > 0 + puts "User #{user['full_name']}(#{user['id']}) has matching payment #{transaction.amount} on date #{transaction.date.iso8601()} (though FITID was not matched)" + next + end + db.transaction do |db| db.exec_params("INSERT INTO lhspayments_payment (id, timestamp, user_id, amount, payment_type, payment_state) VALUES ($1, $2, $3, $4, 1, 2)", [transaction.fit_id, transaction.date.iso8601(), user['id'], transaction.amount])