Skip to content

Instantly share code, notes, and snippets.

@c3mediagroup
Created June 5, 2009 11:43
Show Gist options
  • Save c3mediagroup/124250 to your computer and use it in GitHub Desktop.
Save c3mediagroup/124250 to your computer and use it in GitHub Desktop.
class ChartsController < ApplicationController
LICENSE = 'LTAM663SPJLO.945CWK-2XOI1X0-7L'
PERFORMANCE_METRIC_0 = {:metric => 33500, :label => "APR"}
PERFORMANCE_METRIC_1 = {:metric => 88500, :label => "Leader's Club"}
PERFORMANCE_METRIC_2 = {:metric => 180000, :label => "Executive Club"}
PERFORMANCE_METRIC_3 = {:metric => 291500, :label => "President's Council"}
PERFORMANCE_METRIC_4 = {:metric => 518200, :label => "Chairman's Club"}
Y_AXIS_HEADROOM = 1 #TODO: Add rounding to nearest 1000
# caches_action :company_premiums, :company, :advisor, :top_performers
def company_premiums
chart = Ziya::Charts::Line.new(LICENSE, "company_premiums")
chart_data = []
if params[:range] == 'year'
chart.add(:axis_category_text, MONTHS)
chart_data = PaymentEvent.yearly_report(@year_num) if params[:year]
else
chart.add(:axis_category_text, (1..30).map(&:to_s))
chart_data = PaymentEvent.thirty_day_report
end
chart.add(:series, 'Premiums', chart_data)
chart.add :theme , "statusroom"
# chart.add :user_data, :text, ProductCategory.fixed.map(&:name) * ', '
respond_to do |wants|
wants.xml { render :xml => chart.to_xml }
end
end
def advisor_premiums
require 'enumerator'
advisor = User.find(params[:advisor_id])
chart = Ziya::Charts::Mixed.new(LICENSE, "advisor_premium")
chart.add :chart_types, %w[line line line line line line line]
goal = advisor.premiums_goal(@year_num)
min_goal = advisor.premiums_minimum_goal(@year_num)
premiums_runrate = []
total_premiums = PaymentEvent.year(@year_num).for_advisor(advisor).monthlies
# total_premiums = (1..12).inject([]) do |arr, month|
# monthly_performance = advisor.payment_events.year(@year_num).month(month).overall_performance
# arr << monthly_performance
# premiums_runrate << (monthly_performance*12/month)
# arr
# end
totals = [nil]*12
total_premiums.each do |pre|
totals[pre.month.to_i-1] = pre.total
end
# actual target cum, min cum, actual cum, min, target, runrate,
colors = %w[f0cd2d 6ba139 c13b2b c13b2b 6ba139 306787 ]
premiums_runrate = (1..12).inject([]) do |arr, month|
arr << ((BigDecimal(totals[month-1])*12/month) rescue 0)
arr
end
# premiums = advisor.payment_events.year(@year_num).by_month
chart.add(:axis_category_text, [''] + MONTHS)
if params[:filter][:report_type] == 'custom'
# chart.add(:series, 'Life', premiums[:life]) if params[:filter][:cat_life]
# chart.add(:series, 'LTC', premiums[:ltc]) if params[:filter][:cat_ltc]
# chart.add(:series, 'Di', premiums[:di]) if params[:filter][:cat_di]
# chart.add(:series, 'Total Premiums', [0] + total_premiums[0..Time.now.month-1]) if params[:filter][:cat_all]
else
chart.add(:series, 'Actual', [0] + totals[0..Time.now.month-1])
end
chart.add(:series, 'Target Cum.', ([goal]*13).enum_for(:each_with_index).collect {|g,i| g/12*i })
chart.add(:series, 'Min Target Cum.', ([min_goal]*13).enum_for(:each_with_index).collect {|g,i| g/12*i })
chart.add(:series, 'Min Target', [min_goal]*13)
chart.add(:series, 'Annual Target', [goal]*13)
chart.add(:series, 'Run Rate', [0] + premiums_runrate[0..Time.now.month-1])
chart.add :theme , "statusroom"
chart.add(:user_data, :colors, colors.join(','))
respond_to do |wants|
wants.xml { render :xml => chart.to_xml }
end
end
def company
category = params[:category]
case category
when 'fixed'
chart_years = current_account.fixed_product_years.find_all_by_year(@year_num)
when 'combined'
chart_years = current_account.categorical_years.find_all_by_year(@year_num)
else
chart_years = current_account.categorical_years.find(:all, :conditions => ['categories.name = ? AND years.year = ?', category, @year_num])
end
chart = Ziya::Charts::Mixed.new(LICENSE, "company_runrate")
chart_data = Chart.new(chart_years)
chart.add :chart_types, %w[line line line line line line column]
chart.add(:axis_category_text, [''] + MONTHS )
chart.add(:series, 'Target Cum.', [0] + chart_data.report[:'Target Cumulative'])
chart.add(:series, 'Min Target Cum.', [0] + chart_data.report[:'Minimum Target Cum.'])
chart.add(:series, 'Actual Cum.', [0] + chart_data.report[:'Actual Cumulative'])
chart.add(:series, 'Min Target', [chart_data.report[:'Annual Minimum' ].first] * 13)
chart.add(:series, 'Annual Target', [chart_data.report[:'Annual Target'].first] * 13)
chart.add(:series, 'Runrate', [0] + chart_data.report[:'Run Rate'])
chart.add(:series, 'Actual', [0] + chart_data.report[:'Actual'])
# chart.add(:series, 'Annual Min', chart_data.report[:'Annual Minimum'])
chart.add :theme , "statusroom"
# chart.add( :user_data, :colors, colors )
# chart.add(:series, 'Baseline', [-1]*12)
respond_to do |wants|
wants.xml { render :xml => chart.to_xml }
# wants.html {}
end
end
def advisor
advisor = User.advisor.find(params[:id])
category = params[:category]
case category
when 'fixed'
chart_years = advisor.fixed_product_years.find_all_by_year(@year_num)
chart_yaml = "advisor_runrate"
when 'combined'
chart_years = advisor.combined_product_years.find_all_by_year(@year_num)
chart_yaml = "advisor_runrate_combined"
when 'gdc'
chart_years = advisor.gdc_years.find_all_by_year(@year_num)
chart_yaml = "advisor_runrate"
else
chart_years = advisor.categorical_years.find(:all, :conditions => ['categories.name = ? AND years.year = ?', category, @year_num])
chart_yaml = "advisor_runrate"
end
chart = Ziya::Charts::Mixed.new(LICENSE, chart_yaml)
chart.add( :axis_category_text, [''] + MONTHS )
chart.add :theme , (params[:theme] || "statusroom")
if chart_years.blank?
chart.add(:series, [0]*13)
else
chart_data = Chart.new(chart_years)
perf_chart_types = []
series_explode = []
# APR, Leaders,
# club_colors = %w[777777 999999 aaaaaa cccccc eeeeee]
club_colors = %w[af9e56 bcae71 c8be8e d4cca9 e3deca]
# target cum, min cum, actual cum, min, target, runrate, actual
colors = %w[6ba139 c13b2b 306787 c13b2b 6ba139 306787 f0cd2d]
# colors = ['ff0000']*12 #ff0000 #bbffbb #ccffcc #ddffdd #eeffee]
if category == 'combined'
metrics = [PERFORMANCE_METRIC_0, PERFORMANCE_METRIC_1, PERFORMANCE_METRIC_2, PERFORMANCE_METRIC_3, PERFORMANCE_METRIC_4]
max_total = Year.calculate(:max, :total, :conditions => {:id => chart_years.map(&:id)})
# Grab the metrics less than
advisor_metrics = metrics.collect{|m| m if max_total > m[:metric] }.compact
# Grab the first metric greater than
advisor_metrics << metrics.detect {|m| m[:metric] > max_total }
advisor_metrics.each do |metric|
chart.add(:series, metric[:label], [metric[:metric]] * 13)
perf_chart_types << "area"
series_explode << 100
end
chart.add :chart_types, (perf_chart_types + %w[line line line line line line line])
# chart.add :user_data, :series_colors, club_colors[0..perf_chart_types.length-1]+colors
# chart.add :user_data, :series_color, '333333,aaaaaa'
# chart.add :user_data, :colors, club_colors[0..perf_chart_types.length-1]+colors
else
chart.add :chart_types, %w[line line line line line line line]
end
# series_explode = series_explode + [50, 50, 50, 100, 100, 100, 75]
series_explode = params[:small_lines] ? [20, 20, 20, 20, 20, 20, 20] : [50, 50, 50, 100, 100, 100, 75]
# chart.add(:series, 'Target Cum.', [0] + chart_data.report[:'Target Cumulative'])
# chart.add(:series, 'Min Target Cum.', [0] + chart_data.report[:'Minimum Target Cum.'])
# chart.add(:series, 'Actual Cum.', [0] + chart_data.report[:'Actual Cumulative'])
# chart.add(:series, 'Min Target', [chart_data.report[:'Annual Minimum' ].first] * 13)
# chart.add(:series, 'Annual Target', [chart_data.report[:'Annual Target'].first] * 13)
# chart.add(:series, 'Runrate', [0] + chart_data.report[:'Run Rate'])
# chart.add(:series, 'Actual', [0] + chart_data.report[:'Actual'])
chart.add(:series, 'Target Cumulative', [0] + chart_data.report[:'Target Cumulative'])
chart.add(:series, 'Minimum Target Cum.', [0] + chart_data.report[:'Minimum Target Cum.'])
chart.add(:series, 'Actual Cumulative', [0] + chart_data.report[:'Actual Cumulative'])
chart.add(:series, 'Annual Minimum', [chart_data.report[:'Annual Minimum' ].first] * 13)
chart.add(:series, 'Annual Target', [chart_data.report[:'Annual Target'].first] * 13)
chart.add(:series, 'Run Rate', [0] + chart_data.report[:'Run Rate'])
chart.add(:series, 'Actual', [0] + chart_data.report[:'Actual'])
chart.add :user_data, :series_explode, series_explode
chart.add :user_data, :colors, (club_colors[0..perf_chart_types.length-1]+colors).join(',')
# '333333,444444,555555,666666,777777,888888,999999,aaaaaa,bbbbbb,cccccc,dddddd,eeeeee'
end
respond_to do |wants|
wants.xml { render :text => chart.to_xml and return }
end
end
def advisor_premiums_by_category
advisor = User.advisor.find(params[:id])
chart = Ziya::Charts::Column.new(LICENSE, 'category_bar')
chart.add( :axis_category_text, MONTHS )
chart.add :theme , (params[:theme] || "statusroom")
colors = %w[2c5186 86892d ba8828]
total_premiums = PaymentEvent.year(@year_num).for_advisor(advisor).monthlies
totals = [nil]*12
lifes = [nil]*12
ltcs = [nil]*12
dis = [nil]*12
total_premiums.each do |pre|
totals[pre.month.to_i-1] = pre.total
lifes[pre.month.to_i-1] = pre.life_premium
ltcs[pre.month.to_i-1] = pre.lc_premium
dis[pre.month.to_i-1] = pre.di_premium
end
# premiums = advisor.payment_events.year(@year_num).by_month
chart.add(:series, 'Life', lifes)
chart.add(:series, 'LTC', ltcs)
chart.add(:series, 'DI', dis)
chart.add(:user_data, :colors, colors.join(','))
chart.add :user_data, :ceiling, (lifes + ltcs + dis).compact.max * Y_AXIS_HEADROOM
respond_to do |wants|
wants.xml { render :text => chart.to_xml and return }
end
end
def advisor_clubsumm
advisor = User.advisor.find(params[:id])
chart = Ziya::Charts::Column.new(LICENSE, 'bar_chart')
chart.add( :axis_category_text, MONTHS )
chart.add :theme , (params[:theme] || "statusroom")
colors = %w[6ba139 c13b2b 306787 c13b2b 6ba139 306787 f0cd2d]
# clubsumms = advisor.payment_events.year(@year_num).by_month
clubsumms = {}
# clubsumms[:life] = Chart.new(advisor.categorical_years.all(:conditions => ['categories.name = ? AND years.year = ?', 'life', @year_num])).report[:'Actual'] rescue []
# clubsumms[:ltc] = Chart.new(advisor.categorical_years.all(:conditions => ['categories.name = ? AND years.year = ?', 'ltc', @year_num])).report[:'Actual'] rescue []
# clubsumms[:di] = Chart.new(advisor.categorical_years.all(:conditions => ['categories.name = ? AND years.year = ?', 'disability', @year_num])).report[:'Actual'] rescue []
# clubsumms[:other] = Chart.new(advisor.categorical_years.all(:conditions => ['categories.name NOT IN (?) AND years.year = ?', ['life', 'ltc', 'disability'], @year_num])).report[:'Actual'] rescue []
clubsumms[:life] = advisor.categorical_years.period(@year_num).for_product('life').totals.by_month
clubsumms[:ltc] = advisor.categorical_years.period(@year_num).for_product('ltc').totals.by_month
clubsumms[:di] = advisor.categorical_years.period(@year_num).for_product('disability').totals.by_month
clubsumms[:other] = advisor.categorical_years.period(@year_num).for_product('club-credits-other').totals.by_month
chart.add(:series, 'Life', clubsumms[:life])
chart.add(:series, 'LTC', clubsumms[:ltc])
chart.add(:series, 'Di', clubsumms[:di])
chart.add(:series, 'Other', clubsumms[:other])
chart.add :user_data, :colors, colors.join(',')
respond_to do |wants|
wants.xml { render :text => chart.to_xml and return }
end
end
def team
advisors = Team.find(params[:id], :include => :members).members
category = params[:category]
case category
when 'fixed'
chart_years = Year.find(:all, :include => :category, :conditions => {:'categories.name' => Category::FIXED, :'categories.owner_id' => advisors.map(&:id), :'years.year' => @year_num})
when 'combined'
chart_years = advisor.categorical_years.find_all_by_year(@year_num)
else
chart_years = advisor.categorical_years.find(:all, :conditions => ['categories.name = ? AND years.year = ?', category, @year_num])
end
chart = Ziya::Charts::Mixed.new(LICENSE, "company_runrate")
chart.add :chart_types, %w[line line line line line line column]
chart.add( :axis_category_text, [''] + MONTHS )
chart.add :theme , "statusroom"
if chart_years.blank?
chart.add(:series, [0]*13)
else
chart_data = Chart.new(chart_years)
chart.add(:series, 'Target Cum.', [0] + chart_data.report[:'Target Cumulative'])
chart.add(:series, 'Min Target Cum.', [0] + chart_data.report[:'Minimum Target Cum.'])
chart.add(:series, 'Actual Cum.', [0] + chart_data.report[:'Actual Cumulative'])
chart.add(:series, 'Min Target', [chart_data.report[:'Annual Minimum' ].first] * 13)
chart.add(:series, 'Annual Target', [chart_data.report[:'Annual Target'].first] * 13)
chart.add(:series, 'Runrate', [0] + chart_data.report[:'Run Rate'])
chart.add(:series, 'Actual', [0] + chart_data.report[:'Actual'])
end
respond_to do |wants|
wants.xml { render :text => chart.to_xml and return }
end
end
def top_performers
chart = Ziya::Charts::PieThreed.new(LICENSE, "top_performers")
chart.add :axis_category_text, (toppers = User.advisors.top_performers(@year_num)[0..15]).map(&:display_name)
chart.add :series, "Top Performers", toppers.collect{|advisor| advisor.overall_performance(@year_num).to_i }
# TODO figure out more about this: chart.add :user_data, :url, update_pie_url
chart.add :series, "Region 2", [
{ :bevel => 'data', :value => 20 },
{ :bevel => 'data', :value => 10 },
{ :glow => 'glow1', :bevel => 'data', :value => 30 },
{ :bevel => 'data', :value => 50 },
{ :bevel => 'data', :value => 40 } ]
chart.add :theme , "statusroom"
respond_to do |fmt|
fmt.xml { render :xml => chart.to_xml }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment