From d1985dae126a3d8794583e01140a6688914d97b0 Mon Sep 17 00:00:00 2001
From: Will Bradley
Date: Sat, 28 Sep 2013 01:30:52 -0700
Subject: [PATCH] Adding payment stats
---
app/controllers/home_controller.rb | 6 ++++--
app/controllers/payments_controller.rb | 22 ++++++++++++++++++++++
app/views/home/index.html.erb | 2 ++
app/views/payments/index.html.erb | 9 +++++++++
4 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb
index 838f7de..01e5296 100644
--- a/app/controllers/home_controller.rb
+++ b/app/controllers/home_controller.rb
@@ -13,8 +13,10 @@ def index
@num_door_opens = DoorLog.where("key = 'G'").count
@today_door_opens = DoorLog.where("key = 'G' AND created_at > ?", DateTime.now - 1.day).count
@recent_door_opens = DoorLog.where("key = 'G' AND created_at > ?", DateTime.now - 7.days).count
- @num_door_denieds = DoorLog.where("key = 'f'").count
- @recent_door_denieds = DoorLog.where("key = 'f' AND created_at > ?", DateTime.now - 7.days).count
+ @num_door_denieds = DoorLog.where("key = 'D'").count
+ @recent_door_denieds = DoorLog.where("key = 'D' AND created_at > ?", DateTime.now - 7.days).count
+ @num_logins = User.sum('sign_in_count')
+ @recent_logins = User.where('current_sign_in_at > ?',Date.today - 7.days).count
@num_macs = Mac.count
@recent_macs = Mac.where("since > ?", DateTime.now - 1.day).count
diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb
index 7d07630..9a42740 100644
--- a/app/controllers/payments_controller.rb
+++ b/app/controllers/payments_controller.rb
@@ -16,6 +16,28 @@ 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
diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb
index 319a16f..800e2f3 100644
--- a/app/views/home/index.html.erb
+++ b/app/views/home/index.html.erb
@@ -35,6 +35,8 @@
<%= @num_door_opens %> (<%= @today_door_opens %> today, <%= @recent_door_opens %> in the last 7 days)
# of Door Accesses Denied:
<%= @num_door_denieds %> (<%= @recent_door_denieds %> in the last 7 days)
+ # of Logins:
+ <%= @num_logins %> (<%= @recent_logins %> users today)
# of Computers in this DB:
<%= @num_macs %> (<%= @recent_macs %> seen today)
diff --git a/app/views/payments/index.html.erb b/app/views/payments/index.html.erb
index 0a45b4a..12f9468 100644
--- a/app/views/payments/index.html.erb
+++ b/app/views/payments/index.html.erb
@@ -7,6 +7,15 @@
<%= link_to 'IPN', ipns_path %>
+
+
Payments by Month
+
+<% @payments_by_month.each do |month| %>
+ - <%= month[:month] %>
+ - <%= month[:sum] %>
+<% end %>
+
+