Skip to content

Instantly share code, notes, and snippets.

@inscapist
Created June 22, 2016 09:27
Show Gist options
  • Save inscapist/f034bb74932a43caccd4b66cd22249de to your computer and use it in GitHub Desktop.
Save inscapist/f034bb74932a43caccd4b66cd22249de to your computer and use it in GitHub Desktop.
How to get the repayment rollover number
class ReinvestReporter
attr_accessor :investor_hash, :investor_states, :loan_hash, :loan_states, :loan_assignment
def initialize
investor_vars = calc_investor_variables
@investor_hash = investor_vars[0]
@investor_states = investor_vars[1]
loan_vars = calc_loan_variables
@loan_hash = loan_vars[0]
@loan_states = loan_vars[1]
@loan_assignment = []
end
def calc_investor_variables
month_start = Date.today.beginning_of_month
month_end = Date.today.end_of_month
disbursements = Disbursement.where('date(created_at) >= ?', month_start).where('date(created_at) <= ?', month_end)
investor_hash = {}
investor_states = {}
disbursements.each do |d|
l = d.investment.loan
criteria = "grade#{l.grade.gsub(/\+/, 'PLUS')}_tenure#{l.tenure_out}"
if d.investment.investor.user.investment_setting.activate_rollover
if investor_hash[criteria].blank?
investor_hash[criteria] = []
end
investor_hash[criteria].push d.investment.investor_id
if investor_states[d.investment.investor_id].blank?
investor_states[d.investment.investor_id] = {
repayment_amount: 0,
cap_amount: d.investment.investor.user.investment_setting.rollover_cap,
allocated_amount: 0
}
end
investor_states[d.investment.investor_id][:repayment_amount] += (d.principal_amount + d.interest_amount)
end
end
[investor_hash, investor_states]
end
def calc_loan_variables
loan_hash = {}
loan_states = {}
Loan.bidding.each do |l|
criteria = "grade#{l.grade.gsub(/\+/, 'PLUS')}_tenure#{l.tenure_out}"
if loan_hash[criteria].blank?
loan_hash[criteria] = []
end
loan_hash[criteria].push l.id
if loan_states[criteria].blank?
loan_states[l.id] = {
available_bid_amount: l.funding_amount_to_complete,
allocated_amount: 0
}
end
end
[loan_hash, loan_states]
end
def get_possible_criteria(criteria)
grade = criteria.split(/_/)[0].gsub(/grade/,'').gsub(/PLUS/, '+')
tenure = criteria.split(/_/)[1].gsub(/tenure/,'').to_i
grades = %W[A+ A B+ B C D E]
index = grades.index(grade)
equal_or_better_grades = []
loop do
equal_or_better_grades.push grades[index]
break if index == 0
index -= 1
end
criteria_list = []
equal_or_better_grades.each do |g|
criteria_list << "grade#{g.gsub(/\+/, 'PLUS')}_tenure#{tenure}"
end
criteria_list
end
def create_new_assignment(investor_id, loan_id)
investor_state = @investor_states[investor_id]
loan_state = @loan_states[loan_id]
unit_of_investment = 500000
amt_available_to_bid = loan_state[:available_bid_amount] - loan_state[:allocated_amount]
investor_balance = investor_state[:repayment_amount] - investor_state[:allocated_amount]
per_loan_cap = (investor_state[:cap_amount] > 0 && investor_state[:cap_amount] < unit_of_investment)? unit_of_investment : investor_state[:cap_amount]
if per_loan_cap.blank? || per_loan_cap <= 0
invest_amount = [amt_available_to_bid, investor_balance].min
else
invest_amount = [amt_available_to_bid, investor_balance, per_loan_cap].min
end
invest_amount = invest_amount - (invest_amount % unit_of_investment)
if invest_amount >= unit_of_investment
@loan_assignment.push [loan_id, investor_id, invest_amount]
investor_state[:allocated_amount] += invest_amount
loan_state[:allocated_amount] += invest_amount
end
end
def export!
@investor_hash.each do |original_criteria, investors|
feasible_criteria = get_possible_criteria(original_criteria)
investors.shuffle!
feasible_criteria.each do |cri|
if @loan_hash[cri].present?
loans = @loan_hash[cri]
investors.each do |investor_id|
loans.each do |loan_id|
puts "\nDUMP"
puts investor_id
puts loan_id
create_new_assignment(investor_id, loan_id)
end
end
end
end
end
@loan_assignment
end
private :create_new_assignment, :calc_investor_variables, :calc_loan_variables, :get_possible_criteria
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment