mirror of
https://github.com/fspc/BikeShed-1.git
synced 2026-09-16 08:31:36 -04:00
Added credit tracking and stats
Added credit_conversions table so conversions can change over time Added default credit conversion fixture Added User#transaction_logs Added User#total_credits (earned minus spent) Added User#total_credits_spent (through transaction_logs) Added User#total_earned_credits (complicated query which finds the conversion rate for a log depending on its created_at date)
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
class CreditConversion < ActiveRecord::Base
|
||||
attr_accessible :conversion
|
||||
end
|
||||
+39
-1
@@ -10,7 +10,8 @@ class User < ActiveRecord::Base
|
||||
:first_name, :last_name, :bike_id,
|
||||
:user_profiles_attributes, :username
|
||||
|
||||
has_many :transactions
|
||||
has_many :transactions, as: :customer
|
||||
has_many :transaction_logs, through: :transactions, source: :logs
|
||||
has_many :user_profiles
|
||||
accepts_nested_attributes_for :user_profiles, allow_destroy: false
|
||||
|
||||
@@ -56,6 +57,43 @@ class User < ActiveRecord::Base
|
||||
WHERE bike_status_id = #{status_id}")
|
||||
end
|
||||
|
||||
def total_credits
|
||||
total_earned_credits - total_credits_spent
|
||||
end
|
||||
|
||||
def total_credits_spent
|
||||
log_action = ::ActsAsLoggable::TransactionAction.find_by_action("TIME")
|
||||
transaction_logs.
|
||||
where( "log_action_id = ? AND log_action_type = ?",
|
||||
log_action.id, log_action.class.to_s).
|
||||
sum{ |r| r.description.to_i }.round(2)
|
||||
end
|
||||
|
||||
def total_earned_credits
|
||||
log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN")
|
||||
|
||||
# Find the first credit conversion which has a created_at date before the
|
||||
# log's created_at date and join it to the log's row so we can calculate
|
||||
# the credits earned from that log entry (each log could have a different
|
||||
# conversion rate)
|
||||
#
|
||||
# The DISTINCT ON, and ORDER BY are important to getting the
|
||||
# single conversion rate that applies to the respective log.
|
||||
::ActsAsLoggable::Log.find_by_sql("
|
||||
SELECT DISTINCT ON (logs.created_at) start_date, end_date,
|
||||
conversion.conversion, conversion.created_at
|
||||
FROM logs
|
||||
INNER JOIN(
|
||||
SELECT conversion, created_at
|
||||
FROM credit_conversions
|
||||
) AS conversion ON logs.created_at > conversion.created_at
|
||||
WHERE logs.loggable_id = #{self.id}
|
||||
AND logs.loggable_type = 'User'
|
||||
AND (log_action_id != #{log_action.id} AND log_action_type = '#{log_action.class.to_s}')
|
||||
ORDER BY logs.created_at, conversion.created_at DESC").
|
||||
sum{ |l| ((l.end_date - l.start_date)/3600) * l.conversion.to_i}.round(2)
|
||||
end
|
||||
|
||||
def total_hours
|
||||
log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN")
|
||||
logs.where("log_action_id != ? AND log_action_type = ?", log_action.id, log_action.class.to_s).sum { |l| (l.end_date - l.start_date)/3600 }.round(2)
|
||||
|
||||
Reference in New Issue
Block a user