Skip to content

Instantly share code, notes, and snippets.

@hdubugras
Created July 30, 2014 21:42
Show Gist options
  • Save hdubugras/df8918a91024711852bd to your computer and use it in GitHub Desktop.
Save hdubugras/df8918a91024711852bd to your computer and use it in GitHub Desktop.
Calculate installments
MDR_1X = '3'
MDR_3X = '3.35'
MDR_6X = '3.35'
MDR_12X = '4.8'
ANTECIPATION_FEE = '0'
FREE_INSTALLMENTS = 3
PASS_MDR = :full
def calculate_installments(amount, installments)
if installments == 1
fee = MDR_1X
elsif installments > 1 && installments <= 3
fee = MDR_3X
elsif installments > 3 && installments <= 6
fee = MDR_6X
elsif installments > 6 && installments <= 12
fee = MDR_12X
end
if PASS_MDR == :full
liquid_value = amount / (1 - fee.to_f/100)
elsif PASS_MDR == :partial
liquid_value = amount / (1 - (fee.to_f - MDR_1X.to_f)/100)
else
liquid_value = amount * (1 - mdr_1x.to_f/100)
end
if installments > FREE_INSTALLMENTS
total_value = liquid_value / (1 - (ANTECIPATION_FEE.to_f/100) * (installments + 1)/2)
else
total_value = amount
end
end
amount = ARGV[0].to_f
puts "Repassando MDR total? #{(PASS_MDR == :full) ? 'SIM' : 'NÃO'}"
puts "Repassando MDR parcial? #{(PASS_MDR == :partial) ? 'SIM' : 'NÃO'}"
puts "Quantas parcelas sem juros? #{FREE_INSTALLMENTS}"
12.times do |i|
total_value = calculate_installments(amount, i+1).round(2)
each_installment = (total_value / (i+1)).round(2)
puts "#{i+1} Parcela(s) - Valor total: #{total_value}, Valor por parcela: #{each_installment}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment