diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 9a42740..dd50979 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -16,28 +16,6 @@ class PaymentsController < ApplicationController # GET /payments.json def index @payments = @payments.order("date DESC") - payment_months = @payments.group_by{ |p| p.date.beginning_of_month } - @payments_by_month = [] - payment_months.each do |month| - # Only grab the last year from today - if month.first > (Date.today - 1.year) && month.first < Date.today - # Calculate sum of amounts for each month and store at end of month array - @payments_by_month << {:month => month.first, :sum => month.last.sum{|p| - if p.amount - p.amount.to_i - else - if p.user - Rails.logger.info p.user.member_level - p.user.member_level.to_i - else - Rails.logger.info p.inspect - Rails.logger.info p.user.inspect - 0 - end - end - }} - end - end respond_to do |format| format.html # index.html.erb @@ -45,6 +23,62 @@ class PaymentsController < ApplicationController end end + def chart + chart_name = params[:name] || "total" + if chart_name == "total" + chart_type = [25, 50, 100] + elsif chart_name == "members" + chart_type = [25, 50, 100] + elsif chart_name == "basic" + chart_type = [50] + elsif chart_name == "associate" + chart_type = [25] + else + chart_type = [] + end + + payment_months = @payments.sort_by(&:date).group_by{ |p| p.date.beginning_of_month } + @payments_by_month = [] + payment_months.each do |month| + # Only grab the last year from today + if month.first > (Date.today - 1.year) && month.first < Date.today + # Calculate sum of amounts for each month and store at end of month array + @payments_by_month << [month.first.to_time.to_i*1000, month.last.sum{|p| + amount = amount_or_level(p) + if chart_type.include?(amount) + if chart_name == "members" + 1 # Output 1 to count members + else + amount # Output dollars to count amount + end + else + 0 + end + }] + end + end + + respond_to do |format| + format.html # index.html.erb + format.json { render :json => @payments_by_month } + end + end + + def amount_or_level p + if p.amount + return p.amount.to_i + else + if p.user + Rails.logger.info p.user.member_level + return p.user.member_level.to_i + else + Rails.logger.info p.inspect + Rails.logger.info p.user.inspect + return 0 + end + end + end + # GET /payments/1 # GET /payments/1.json def show diff --git a/app/views/payments/index.html.erb b/app/views/payments/index.html.erb index 12f9468..aee18ae 100644 --- a/app/views/payments/index.html.erb +++ b/app/views/payments/index.html.erb @@ -1,4 +1,85 @@ -

Listing payments

+ + + + + +
+ + +

Listing payments

Create Payments: @@ -7,14 +88,6 @@ <%= link_to 'IPN', ipns_path %>

-

-

Payments by Month

-
-<% @payments_by_month.each do |month| %> -
<%= month[:month] %>
-
<%= month[:sum] %>
-<% end %> -
diff --git a/config/routes.rb b/config/routes.rb index 5f0355f..e0b6672 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ Dooraccess::Application.routes.draw do resources :paypal_csvs match 'paypal_csvs/:id/link' => 'paypal_csvs#link', :as => :link_paypal_csv + match 'payments/chart' => 'payments#chart', :as => :chart_payments resources :payments resources :user_certifications