Skip to content

Instantly share code, notes, and snippets.

@lcowell
Created January 12, 2012 01:05
Show Gist options
  • Save lcowell/1597816 to your computer and use it in GitHub Desktop.
Save lcowell/1597816 to your computer and use it in GitHub Desktop.
class Deal < AR
attr_reader :minimum_order, :board_lot_size, :unit_value, :allocation_value
end
# last level allocation rate
# planned allocations
# deal
class DealAllocation < AR
belongs_to :deal
has_many :expressions_of_interest
has_many :allocation_plans
attr_accessor :remaining_units
def by_rank(rank)
expressions_of_interest.select {|e| e.rank == rank}
end
def rank_total(rank)
by_rank(rank).sum(&:units_calculated)
end
def partial_rank
expressions_of_interest.select {|eoi| [:partial, :no].include? eoi.within_deal_allocation? }.first.eoi_member_rank
end
end
class Allocation::ExpressionOfInterest
belongs_to :deal_allocation
belongs_to :expression_of_interest, :class_name => "Admin::ExpressionOfInterest"
belongs_to :allocation_plan_item
def in_partial_rank?
deal_allocation.partial_rank == self.eoi_member_rank
end
def initialize
allocation_plan_item = Allocation::AllocationPlanItem.plan_for_rank(self.rank)
end
# expression_of_interest
def date_of_interest
expression_of_interest.created_at
end
# expression_of_interest
def eoi_member_rank
expression_of_interest.member
end
# expression_of_interest
def eoi_amount_requested
expression_of_interest.eoi_amount_requested
end
# expression_of_interest
def eoi_units_requested
expression_of_interest.eoi_units_requested
end
# deal
def amount_calculated
units_calculated * deal.unit_price
end
# allocation_plan, #deal <-- could use expression of interest insead of deal
def units_calculated
if in_partial_rank?
units_calculated_by_percent
else
units_calculated_by_strategy
end
end
def units_calculated_by_percent(allocation_percentage = 1.0, remaining_percentage = 0.37)
requested_units * remaining_percentage * allocation_percentage
# result = alloction_plan.percentage_strategy(requested_units, allocation_percentage)
return 0 if result == 0
[result.rounded_down(board_lot_size), minimum].max
end
def units_calculated_by_strategy
result = allocation_plan.calculate(requested_units)
return 0 if result == 0
[result.rounded_down(board_lot_size), minimum].max
end
# deal
def percentage_of_deal_unit_allocation
(units_calculated / deal.total_unit_allocation).as_percent
end
# deal_allocation
def cumulative_percentage_of_deal_unit_allocation
if deal_allocation.previous_expression_of_interest(self)
deal_allocation.previous_expression_of_interest(self).cumulative_percentage_of_deal_unit_allocation + percentage_of_deal_unit_allocation
else
percentage_of_deal_unit_allocation
end
end
def within_deal_allocation?
if cumulative_percentage_of_deal_unit_allocation < 100
:yes
elsif cumulative_percentage_of_deal_unit_allocation - percentage_of_deal_unit_allocation < 100
:partial
else
:no
end
end
end
class Allocation::AllocationPlanItem
attr_reader :rank
attr_reader :rank_plan, :rank_demand
def self.plan_for_rank(rank)
# looks up the plan for a rank
end
end
class Allocation::InvestorRankPlan
attr_accessor :units
attr_reader :percentage
def calculate(units)
# result will be rounded to the board lot size
(units * percentage).rounded(board_lot_size)
end
end
class Allocation::InvestorRankEOIDemand
attr_reader :value
def units
value * something
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment