2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'numeric'
|
|
|
|
|
|
|
|
module CalculateSuggestedAmounts
|
|
|
|
MIN = 25
|
2019-07-30 21:29:24 +00:00
|
|
|
MAX = 100_000_000
|
|
|
|
BRACKETS = [{ range: MIN...1000, delta: 100 },
|
|
|
|
{ range: 1000...5000, delta: 500 },
|
|
|
|
{ range: 5000...MAX, delta: 2500 }].freeze
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
# Calculates a set of suggested donation amounts based upon our internal special algorithm
|
|
|
|
# This is most useful for suggesting amounts a recurring donor could change to
|
|
|
|
# @return [Array<Integer>] suggested amounts for your donation
|
|
|
|
# @param [Number] amount the amount in cents to start from
|
|
|
|
def self.calculate(amount)
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ amount: amount }, amount: { required: true, is_a: Numeric, min: MIN, max: MAX })
|
2018-03-25 17:30:42 +00:00
|
|
|
result = []
|
|
|
|
|
|
|
|
step_down_val = step_down_value(amount)
|
2019-07-30 21:29:24 +00:00
|
|
|
result.push(step_down_val) unless step_down_val.nil?
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
higher_amounts = []
|
2019-07-30 21:29:24 +00:00
|
|
|
while higher_amounts.empty? || (higher_amounts.length < 3 && !higher_amounts.last.nil?)
|
2018-03-25 17:30:42 +00:00
|
|
|
if higher_amounts.empty?
|
|
|
|
higher_amounts.push(step_up_value(amount))
|
|
|
|
else
|
|
|
|
higher_amounts.push(step_up_value(higher_amounts.last))
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
result.concat(higher_amounts.reject(&:nil?))
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.step_down_value(amount)
|
|
|
|
initial_bracket = get_bracket_by_amount(amount)
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# check_floor_for_delta
|
2018-03-25 17:30:42 +00:00
|
|
|
delta_floor = amount.floor_for_delta(initial_bracket[:delta])
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# not on a delta, just send a floor
|
|
|
|
if delta_floor != amount
|
2018-03-25 17:30:42 +00:00
|
|
|
return delta_floor < MIN ? nil : delta_floor
|
|
|
|
end
|
|
|
|
|
|
|
|
potential_lower_amount = amount - initial_bracket[:delta]
|
|
|
|
|
|
|
|
# is potential_lower_amount < our MIN? if so, return nil
|
|
|
|
return nil if potential_lower_amount < MIN
|
|
|
|
|
|
|
|
new_bracket = get_bracket_by_amount(potential_lower_amount)
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# if in same bracket, potential_lower_amount is our step_down_value
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
return potential_lower_amount if initial_bracket == new_bracket
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# we're going to step down by our new bracket value then
|
|
|
|
amount - new_bracket[:delta]
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.step_up_value(amount)
|
|
|
|
bracket = get_bracket_by_amount(amount)
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# check_ceil_for_delta
|
2018-03-25 17:30:42 +00:00
|
|
|
delta_ceil = amount.ceil_for_delta(bracket[:delta])
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# not on a delta, just send a ceil
|
|
|
|
if delta_ceil != amount
|
2018-03-25 17:30:42 +00:00
|
|
|
return delta_ceil >= MAX ? nil : delta_ceil
|
|
|
|
end
|
|
|
|
|
|
|
|
potential_higher_amount = amount + bracket[:delta]
|
|
|
|
|
|
|
|
# is potential_lower_amount < our MIN? if so, return nil
|
|
|
|
return nil if potential_higher_amount >= MAX
|
|
|
|
|
|
|
|
potential_higher_amount
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_bracket_by_amount(amount)
|
2021-05-24 17:05:48 +00:00
|
|
|
BRACKETS.detect { |i| i[:range].cover?(amount) }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|