Created
June 11, 2009 16:38
-
-
Save c3mediagroup/128040 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def premiums | |
# Chart used for Agency, Unit and Producer Run Rate charts. | |
# Use this as a template for cleaning up/creating other chart methods. | |
require 'enumerator' | |
whom = current_account | |
whom = User.find(params[:advisor_id]) if params[:advisor_id] | |
whom = Team.find(params[:team_id]) if params[:team_id] | |
goal = whom.premiums_goal(@year_num) | |
min_goal = whom.premiums_minimum_goal(@year_num) | |
# -- Set up Selected Year Data | |
chart_data = if params[:advisor_id] | |
PaymentEvent.year(@year_num).for_advisor(whom).monthlies.to_chart_data | |
elsif params[:team_id] | |
PaymentEvent.year(@year_num).for_team(whom).monthlies.to_chart_data | |
else | |
PaymentEvent.year(@year_num).monthlies.to_chart_data | |
end | |
total_premiums = chart_data.collect{|d| d.nil? ? nil : d[:total] } | |
cumulative_totals = points_to_cumulative(total_premiums) | |
runrate_series = [] | |
cumulative_totals.each_with_index do |cd, index| | |
runrate_series << (cd/(index+1)*12) rescue cd | |
end | |
cum_total_series = Time.now.year == @year_num ? cumulative_totals[0..Time.now.month-1] : cumulative_totals | |
# -- Set up Previous-to-Selected Year Data | |
prev_chart_data = if params[:advisor_id] | |
PaymentEvent.year(@year_num-1).for_advisor(whom).monthlies.to_chart_data | |
elsif params[:team_id] | |
PaymentEvent.year(@year_num-1).for_team(whom).monthlies.to_chart_data | |
else | |
PaymentEvent.year(@year_num-1).monthlies.to_chart_data | |
end | |
prev_total_premiums = prev_chart_data.collect{|d| d.nil? ? nil : d[:total] } | |
prev_cumulative_totals = points_to_cumulative(prev_total_premiums) | |
prev_runrate_series = [] | |
prev_cumulative_totals.each_with_index do |cd, index| | |
prev_runrate_series << (cd/(index+1)*12) rescue cd | |
end | |
prev_cum_total_series = points_to_cumulative(prev_total_premiums) | |
# -- Set up Chart Display | |
chart = Ziya::Charts::Mixed.new(LICENSE, "advisor_premium") | |
chart.add :theme , "statusroom" | |
chart.add :chart_types, %w[line line line line line line line line] | |
colors = [GREEN_CLR, LT_GREEN_CLR, RED_CLR, LT_RED_CLR, LT_BLUE_CLR, BLUE_CLR, LT_PURPLE_CLR, PURPLE_CLR] | |
chart.add(:user_data, :colors, colors.join(',')) | |
series_explode = [THIN_LINE, THIN_LINE, THIN_LINE, THIN_LINE, MED_LINE, THICK_LINE, MED_LINE, HEAVY_LINE] | |
chart.add :user_data, :series_explode, series_explode | |
ceiling = ([goal] + prev_runrate_series + runrate_series).compact.max * Y_AXIS_HEADROOM | |
chart.add(:user_data, :ceiling, (ceiling - ceiling % Y_AXIS_ROUND)) | |
chart.add(:axis_category_text, [@year_num] + MONTHS) | |
chart.add(:series, 'Annual Target', [goal]*13) | |
chart.add(:series, 'Target Cum.', ([goal]*13).enum_for(:each_with_index).collect {|g,i| g/12*i }) | |
chart.add(:series, 'Min Target', [min_goal]*13) | |
chart.add(:series, 'Min Target Cum.', ([min_goal]*13).enum_for(:each_with_index).collect {|g,i| g/12*i }) | |
chart.add(:series, "#{@year_num-1} Cumm Total", [0] + prev_cum_total_series) | |
chart.add(:series, "#{@year_num} Cumm Total", [0] + cum_total_series) | |
chart.add(:series, "#{@year_num-1} Run Rate", [0] + prev_runrate_series) | |
if @year_num.to_i == Time.now.year | |
chart.add(:series, "#{@year_num} Run Rate", [0] + runrate_series[0..Time.now.month-1]) | |
else | |
chart.add(:series, "#{@year_num} Run Rate", [0] + runrate_series) | |
end | |
respond_to do |wants| | |
wants.xml { render :xml => chart.to_xml } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment