X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=app%2Fcontrollers%2Fpurchase_controller.rb;h=37b74012378cf88d003ba10a5e92fad19fca1b7a;hb=da871e3e27c31759e0d017c89ccf16a14db1a227;hp=704e38b78b0e03737cbcb732b15bae02096df0ff;hpb=74561aac0964ca7bd9283ad0a9b017e02431f845;p=cs356-p2-videostore.git diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb index 704e38b..37b7401 100644 --- a/app/controllers/purchase_controller.rb +++ b/app/controllers/purchase_controller.rb @@ -1,11 +1,75 @@ class PurchaseController < ApplicationController + layout "admin" + + # Make sure that a user logs in before doing any action here + before_filter :authorize, :except => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index, :income] + before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index, :income] def index - redirect_to :action => :begin + render :action => 'index' end def list - @purchase_pages, @purchase = paginate :purchases, :per_page => 100 + @purchase_pages, @purchases = paginate :purchases, :per_page => 100 + end + + def filter + if request.post? + type = params[:type] + id = params[:id] + + case type + when :customer, "customer" + redirect_to :action => 'filterbycust', :id => id + when :date, "date" + date = Date.new id['(1i)'].to_i, id['(2i)'].to_i, id['(3i)'].to_i + redirect_to :action => 'filterbydate', :id => date.to_s + end + else + @type = params[:type] + if @type.nil? + @type = "all" + end + render :action => 'filter' + end + end + + def filterbycust + @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["customer_id = ?", params[:id]] + render :action => 'list' + end + + def filterbydate + @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["date = ?", params[:id]] + render :action => 'list' + end + + def filterbytype + @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["type = ?", params[:id]] + render :action => 'list' + end + + def income + if request.post? + # Find all purchases between :begin_date, and :end_date and sum up the total income + # from RentablePurchases, MerchandisePurchases. Print both sums, and the total sum. + @begin_date = Date.new params[:begin_date]['(1i)'].to_i, params[:begin_date]['(2i)'].to_i, params[:begin_date]['(3i)'].to_i + @end_date = Date.new params[:end_date]['(1i)'].to_i, params[:end_date]['(2i)'].to_i, params[:end_date]['(3i)'].to_i + merchandises = MerchandisePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date]) + rentables = RentablePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date]) + late_fees = LateFeePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date]) + + @merch_count = merchandises.length + @rent_count = rentables.length + @late_count = late_fees.length + @merch_sum = merchandises.sum(&:price) + @rent_sum = rentables.sum(&:price) + @late_sum = late_fees.sum(&:price) + @total = @merch_sum + @rent_sum + @late_sum + render :action => 'income_results' + else + render :action => 'income' + end end def begin @@ -13,6 +77,40 @@ class PurchaseController < ApplicationController render :action => 'begin' session[:total] = 0.00 session[:items] = [] + session[:bonus] = nil + end + + def receipt + # Save important data, since we're gonna wipe it out now + @customer = Customer.find_by_id(session[:customer_id]) + @debt = @customer.debt + @total = session[:total] + @debt + @items = session[:items] + @bonus = session[:bonus] + @time = Time.now + + # Record a Late Fee Payment if we need to + if not @debt.zero? + purchase = LateFeePurchase.new + purchase.customer = @customer + purchase.date = Time.now.to_date + purchase.price = @debt + purchase.save + end + + # Set the customer's debt to $0.00, she paid us + @customer = Customer.find_by_id(session[:customer_id]) + @customer.debt = 0.00 + @customer.save + + # Wipe session data + session[:customer_id] = nil + session[:total] = nil + session[:items] = nil + session[:bonus] = nil + + # Show the receipt + render :action => 'receipt' end def customer_ok @@ -20,7 +118,7 @@ class PurchaseController < ApplicationController session[:customer_id] = params[:customer_id] redirect_to :action => :menu else - flash[:error] = "Customer ID is invalid" + flash[:notice] = "Customer ID is invalid" redirect_to :action => :begin end end @@ -32,100 +130,175 @@ class PurchaseController < ApplicationController render :action => 'menu' end - def rent_begin - render :action => 'rent_begin' - end + def rent + if request.post? + @customer = Customer.find_by_id(session[:customer_id]) + @rentable = Rentable.find_by_id(params[:rentable_id]) - def rent_validate - @customer = Customer.find_by_id(session[:customer_id]) - @rentable = Rentable.find_by_id(params[:rentable_id]) + if @customer.nil? + flash[:notice] = "Customer ID is invalid" + redirect_to :action => :begin + return + end - if @customer.nil? - flash[:error] = "Customer ID is invalid" - redirect_to :action => :begin - return - end + if @rentable.nil? + flash[:notice] = "Rentable ID is invalid" + redirect_to :action => :rent + return + end - if @rentable.nil? - flash[:error] = "Rentable ID is invalid" - redirect_to :action => :rent_begin - return - end + if @rentable.checkedout? + flash[:notice] = "This #{@rentable.type} is already checked out!" + redirect_to :action => :rent + return + end - if @rentable.checkedout? - flash[:error] = "This #{@rentable.type} is already checked out!" - redirect_to :action => :rent_begin - return - end + # Check Rentable Policies + @maxvideos = RentablePolicy.find_by_name("MaxVideos") + if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value + flash[:notice] = "#{@maxvideos.description} LIMIT REACHED" + redirect_to :action => :rent + return + end - # Check out the item - checkout = Coitem.new - checkout.customer = @customer - checkout.rentable = @rentable - checkout.out_date = Time.now.to_date - checkout.due_date = @rentable.due_date - checkout.save! - - # Actually record the purchase - purchase = RentablePurchase.new - purchase.customer_id = session[:customer_id] - purchase.date = Time.now.to_date - purchase.price = @rentable.calculated_price - purchase.rentable = @rentable - purchase.save! - - # Add te session variables - session[:total] += @rentable.calculated_price - session[:items].push @rentable - - flash[:notice] = "Successfully made purchase" - redirect_to :action => :menu - end + @maxgames = RentablePolicy.find_by_name("MaxGames") + if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value + flash[:notice] = "#{@maxgames.description} LIMIT REACHED" + redirect_to :action => :rent + return + end - def buy_begin - render :action => 'buy_begin' - end + @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos") + if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value + flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED" + redirect_to :action => :rent + return + end - def buy_validate - @customer = Customer.find_by_id(session[:customer_id]) - @merchandise = Merchandise.find_by_id(params[:merchandise_id]) - - if @customer.nil? - flash[:error] = "Customer ID is invalid" - redirect_to :action => :begin - return - end + @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames") + if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value + flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED" + redirect_to :action => :rent + return + end - if @merchandise.nil? - flash[:error] = "Merchandise ID is invalid" - redirect_to :action => :buy_begin - return - end + # Check for a Bonus + for bonus in BonusPolicy.find_all_by_bonus_type(@rentable.class.to_s) + # Find the earliest the period for this bonus extends + since_date = Time.now.advance(:days => (-1 * bonus.days)).to_date + + # Find the date of the latest bonus in this period + last_bonus_date = BonusPurchase.last_bonus_date(@customer, @rentable.class, since_date) + + # Find the number of rentals of this type in the period since the last bonus + count = 0 + for rental in RentablePurchase.find_all_by_customer_id(@customer, :conditions => ["date > ?", last_bonus_date]) + if rental.rentable.class == @rentable.class + count += 1 + end + end + + # We are eligible for a bonus! + if count >= bonus.number + + # Check out the item + checkout = Coitem.new + checkout.customer = @customer + checkout.rentable = @rentable + checkout.out_date = Time.now.to_date + checkout.due_date = @rentable.due_date + checkout.save! + + # Record the BonusPurchase + purchase = BonusPurchase.new + purchase.customer = @customer + purchase.date = Time.now.to_date + purchase.price = @rentable.calculated_price + purchase.rentable = @rentable + purchase.save! + + # Add to session variables + session[:items].push @rentable + session[:bonus] = @rentable + + flash[:notice] = "Successfully made bonus purchase" + redirect_to :action => :menu + return + end + end + + # Check out the item + checkout = Coitem.new + checkout.customer = @customer + checkout.rentable = @rentable + checkout.out_date = Time.now.to_date + checkout.due_date = @rentable.due_date + checkout.save! - if @merchandise.quantity < 1 - flash[:error] = "The system thinks we are out of this merchandise item!" - redirect_to :action => :buy_begin - return + # Actually record the purchase + purchase = RentablePurchase.new + purchase.customer = @customer + purchase.date = Time.now.to_date + purchase.price = @rentable.calculated_price + purchase.rentable = @rentable + purchase.save! + + # Add to session variables + session[:total] += @rentable.calculated_price + session[:items].push @rentable + + flash[:notice] = "Successfully made purchase" + redirect_to :action => :menu + else + render :action => 'rent' end + end + + def buy_merch + if request.post? + @customer = Customer.find_by_id(session[:customer_id]) + @merchandise = Merchandise.find_by_id(params[:merchandise_id]) + + if @customer.nil? + flash[:notice] = "Customer ID is invalid" + redirect_to :action => :begin + return + end + + if @merchandise.nil? + flash[:notice] = "Merchandise ID is invalid" + redirect_to :action => :buy_merch + return + end + + if @merchandise.quantity < 1 + flash[:notice] = "The system thinks we are out of this merchandise item!" + redirect_to :action => :buy_merch + return + end - # Actually record the purchase - purchase = MerchandisePurchase.new - purchase.customer_id = session[:customer_id] - purchase.date = Time.now.to_date - purchase.price = @merchandise.price - purchase.merchandise = @merchandise - purchase.quantity = 1 - @merchandise.quantity -= 1 - - # Add to session variables - session[:total] += @merchandise.price - session[:items].push @merchandise - - # Save both the merchandise (we changed the quantity) and the purchase to the log - @merchandise.save! - purchase.save! - - flash[:notice] = "Successfully made purchase" - redirect_to :action => :menu + # Actually record the purchase + purchase = MerchandisePurchase.new + purchase.customer = @customer + purchase.date = Time.now.to_date + purchase.price = @merchandise.price + purchase.merchandise = @merchandise + purchase.quantity = 1 + @merchandise.quantity -= 1 + + # Add to session variables + session[:total] += @merchandise.price + session[:items].push @merchandise + + # Save both the merchandise (we changed the quantity) and the purchase to the log + @merchandise.save! + purchase.save! + + flash[:notice] = "Successfully made purchase" + redirect_to :action => :menu + else + render :action => 'buy_merch' + end end + end