Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter for duplicate user/amount/date transactions #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bin/reconcile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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])
Expand Down