Open-Source-Access-Control-.../app/controllers/payments_controller.rb

147 lines
3.9 KiB
Ruby
Raw Normal View History

2013-02-12 08:58:17 +00:00
class PaymentsController < ApplicationController
load_and_authorize_resource :payment
#load_and_authorize_resource :user, :through => :payment
2013-02-12 08:58:17 +00:00
before_filter :authenticate_user!
# Load users and certs based on current ability
before_filter do
@users = User.where(:hidden => false).where("member_level > 10").accessible_by(current_ability).sort_by(&:name_with_payee_and_member_level)
2013-02-12 08:58:17 +00:00
end
before_filter :only => [:create, :update] do
@payment.created_by = current_user.id
end
# GET /payments
# GET /payments.json
def index
@payments = @payments.order("date DESC")
2013-09-28 10:33:36 +00:00
@graph = { :members => chart("members"),
:total => chart("total"),
:basic => chart("basic"),
:associate => chart("associate")}
2013-09-28 09:42:30 +00:00
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @payments }
end
end
2013-09-28 10:33:36 +00:00
# Private method for index charts
def chart name
chart_name = name || "total"
2013-09-28 09:42:30 +00:00
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 }
2013-09-28 08:30:52 +00:00
@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
2013-09-28 09:42:30 +00:00
@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
2013-09-28 08:30:52 +00:00
else
2013-09-28 09:42:30 +00:00
amount # Output dollars to count amount
2013-09-28 08:30:52 +00:00
end
2013-09-28 09:42:30 +00:00
else
0
2013-09-28 08:30:52 +00:00
end
2013-09-28 09:42:30 +00:00
}]
2013-09-28 08:30:52 +00:00
end
end
2013-02-12 08:58:17 +00:00
2013-09-28 10:33:36 +00:00
return @payments_by_month
2013-02-12 08:58:17 +00:00
end
2013-09-28 09:42:30 +00:00
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
2013-02-12 08:58:17 +00:00
# GET /payments/1
# GET /payments/1.json
def show
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @payment }
end
end
# GET /payments/new
# GET /payments/new.json
def new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @payment }
end
end
# GET /payments/1/edit
def edit
end
# POST /payments
# POST /payments.json
def create
Rails.logger.warn "payment:"
Rails.logger.warn @payment.inspect
respond_to do |format|
if @payment.save
format.html { redirect_to payments_url, :notice => 'Payment was successfully created.' }
format.json { render :json => @payment, :status => :created, :location => @payment }
else
format.html { render :action => "new" }
format.json { render :json => @payment.errors, :status => :unprocessable_entity }
end
end
end
# PUT /payments/1
# PUT /payments/1.json
def update
respond_to do |format|
if @payment.update_attributes(params[:payment])
format.html { redirect_to payments_url, :notice => 'Payment was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @payment.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /payments/1
# DELETE /payments/1.json
def destroy
@payment.destroy
respond_to do |format|
format.html { redirect_to payments_url }
format.json { head :no_content }
end
end
end