Skip to content

Instantly share code, notes, and snippets.

@supriya
Created February 28, 2011 14:27
Show Gist options
  • Save supriya/847378 to your computer and use it in GitHub Desktop.
Save supriya/847378 to your computer and use it in GitHub Desktop.
require "fastercsv"
class FirstDirectDepositIncentives
attr_reader :mode, :start_date, :end_date, :bucket, :report, :cards, :dry_run
def initialize(options ={})
@start_date = options.fetch(:start_date){Time.zone.now.beginning_of_day}
@end_date = options.fetch(:end_date){Time.zone.now}
@dry_run = options[:dry_run]
@incentives_agent = Agent.find_by_code("INSIGHT_CARDS")
@agent_ids = [Agent.find_by_code('CS').id, Agent.find_by_code('em').id]
@bucket = TransactionFeed.between(@start_date, @end_date).extended_transaction_type(:ach_deposit).for_agents(@agent_ids)
@report = []
@cards = []
end
def real_payout?
@dry_run == false
end
def run
@users = []
@first_direct_deposit = nil
@bucket.each do |transaction|
next if transaction.card.nil? || transaction.card.created_at < Time.zone.parse("2010-04-01").beginning_of_day || transaction.person.is_employee? == true || transaction.amount < 200.00 || (transaction.person.loans.any? && transaction.person.loans.after(Time.zone.parse("2010-01-01").beginning_of_day) && transaction.person.best_card.store.agent.code == 'CS')
cardholder_card = transaction.card
direct_deposits = cardholder_card.person.transaction_feeds.extended_transaction_type(:ach_deposit).between(start_date, end_date)
previous_month_direct_deposits = cardholder_card.person.transaction_feeds.extended_transaction_type(:ach_deposit).before(start_date)
if direct_deposits.count >= 1 && previous_month_direct_deposits.count == 0
user = cardholder_card.card_creation_transaction.user
existing_user = @users.detect{|u| u[:user_id] == user.id}
if transaction == direct_deposits.first
if incentive_not_paid_yet?(user, transaction)
if existing_user
position = @users.rindex(existing_user)
existing_user[:count] = existing_user[:count] + 1
existing_user[:card_ids] << cardholder_card.id
existing_user[:transaction_ids] << transaction.id
@users[position] = existing_user
else
@users << {:count => 1, :user_id => user.id, :transaction_ids => [transaction.id], :card_ids => [cardholder_card.id]}
end
end
end
end
end
@users.each do |user|
teller = User.find(user[:user_id])
user_transactions = user[:transaction_ids]
if has_card_account_association?(teller)
card_account = teller.user_incentive_card_account_association.card_account
count = user[:count]
if should_get_incentive?(teller, count)
pay_incentive(teller, card_account, user_transactions, count)
end
else
user_transactions.each do |t|
log(:transaction => TransactionFeed.find(t), :person => TransactionFeed.find(t).person, :user => teller, :message => "Teller does not have incentives card setup.")
end
end
end
return @users
end
def pay_incentive(user, card_account, transactions, count)
if real_payout?
incentive_amount = incentive_amount(user, count)
result = CallHandlerExtract.load_funds_on_card(:version => :v1, :amount => incentive_amount,
:customer_number => card_account.send(:decrypt_customer_number), :store => incentives_store,
:user => incentives_user)
transactions.each do |t|
logging_results(t, user, result)
end
else
transactions.each do |t|
log(:transaction => TransactionFeed.find(t), :person => TransactionFeed.find(t).person, :user => user, :message => "Success(dry run)")
end
end
end
def logging_results(t, user, result)
if result.success?
UserFirstDirectDepositIncentive.create!(:transaction_feed_id => t, :user_id => user.id)
log(:transaction => TransactionFeed.find(t), :person => TransactionFeed.find(t).person, :user => user, :message => "Success")
else
log(:transaction => TransactionFeed.find(t), :person => TransactionFeed.find(t).person, :user => user, :message => "Unable to load incentive amount on card")
end
end
def to_csv
@to_csv ||= FasterCSV.generate do |csv|
csv << header
@report.each do |row|
csv << [ row[:transaction_created_at], row[:transaction_id], row[:cardholder_name], row[:cardholder_ssn], row[:cardholder_created_at], row[:cardholder_card], row[:user_name], row[:user_login], row[:user_store], row[:user_card], row[:message] ]
end
end
end
def incentives_store
@incentives_agent.stores.find_by_name("INCENTIVES")
end
def incentives_user
incentives_store.users.find_by_login("incentives")
end
def incentive_amount(user, count)
amount = (user.stores.last.region.region_setting.settings["first_dd_teller_incentive"])
if amount.blank?
return count * BigDecimal("0.0")
else
return count * BigDecimal(amount)
end
end
def log(options = {})
transaction = TransactionFeed.find(options[:transaction])
person = options [:person]
user = options[:user]
message = options[:message]
store = user.stores.last
card_account = user.user_incentive_card_account_association.card_account rescue nil
card = card_account ? card_account.cards.last.to_s : nil
@report << { :transaction_created_at => transaction.created_at.to_s(:american_with_time), :transaction_id => transaction.id, :cardholder_name => person.to_s, :cardholder_ssn => person.send(:decrypt_ssn), :cardholder_created_at => person.created_at.to_s(:american_with_time),
:cardholder_card => transaction.card.to_s, :user_name => user.to_s, :user_store => store.to_s, :user_card => card, :message => message, :user_login => user.login }
end
def should_get_incentive? (user, count)
if (self.agent_enabled_first_dd_incentive?(user) && incentive_amount(user, count) > 0)
return true
else
return false
end
end
def agent_enabled_first_dd_incentive?(user)
user.agent.agent_setting.first_dd_teller_incentive == true
end
def incentive_not_paid_yet?(user, transaction)
UserFirstDirectDepositIncentive.for_user_and_card(user, TransactionFeed.find(transaction).card_id).empty?
end
def has_card_account_association? (user)
card_account_association = user.user_incentive_card_account_association
if card_account_association && card_account_association.card_account
return true
else
return false
end
end
def header
["Transaction At", "Transaction ID", "Cardholder Name", "Cardholder SSN", "Cardholder Created", "Cardholder Card", "Teller Name", "Teller Login", "Teller Store", "Teller Card", "Message"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment