From 799d6973bf66f2958daa6fa10405a7f7dfeb922f Mon Sep 17 00:00:00 2001 From: Joshua Greben Date: Wed, 7 Feb 2024 10:22:22 -0800 Subject: [PATCH] Check for matching day before or after payment in folio --- .../download_payment_batch_detail_report.rb | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/cyber_source/download_payment_batch_detail_report.rb b/cyber_source/download_payment_batch_detail_report.rb index 15123de..aef95bd 100644 --- a/cyber_source/download_payment_batch_detail_report.rb +++ b/cyber_source/download_payment_batch_detail_report.rb @@ -53,7 +53,8 @@ def main if index > 1 user_id = batch_detail.split(',')[4] paid = batch_detail.split(',')[8] - paydate = batch_detail.split(',')[12]&.chomp + batch_date = batch_detail.split(',')[2] + transaction_date = batch_detail.split(',')[12]&.chomp begin accounts = folio_client.get('/accounts', { query: "userId==#{user_id}" }) @@ -67,13 +68,13 @@ def main payments = [] accounts['accounts'].each do |a| - next unless is_a_payment?(a, paydate) + next unless is_a_payment?(a, batch_date, transaction_date) payments << a['amount'].to_f end accounts['accounts'].each do |account| - next unless is_a_payment?(account, paydate) + next unless is_a_payment?(account, batch_date, transaction_date) puts user_id puts "TOTAL PAYMENTS: #{payments.sum}" @@ -82,7 +83,7 @@ def main if (payments.sum == paid.to_f) payload = { - paydate: Date.parse(paydate), + transaction_date: Date.parse(transaction_date), user_id: user_id, folio_payment_id: account['id'], library: account['feeFineOwner'], @@ -99,7 +100,7 @@ def main puts credits sleep(ENV. fetch('SLEEP', 2)&.to_i) end - credits.any? && CSV.open('files/credits.csv', 'w+') do |csv| + credits.any? && CSV.open('files/credits.csv', 'a') do |csv| csv << credits.first.keys credits.each do |credit| csv << credit.values @@ -110,11 +111,13 @@ def main puts e.backtrace end - def is_a_payment?(account, paydate) - payment_date = Date.parse(paydate) + def is_a_payment?(account, batch_date, transaction_date) + tx_date = Date.parse(transaction_date) + bx_date = Date.parse(batch_date) + dates = [tx_date - 1, tx_date, tx_date + 1, bx_date - 1, bx_date, bx_date + 1] - (created_date(account) == payment_date || updated_date(account) == payment_date) && - (account['paymentStatus']['name'] == 'Paid fully') + dates.include?(created_date(account)) || dates.include?(updated_date(account)) && + (account['status']['name'] == 'Closed' || account['paymentStatus']['name'] == 'Paid fully') end def created_date(account)