X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=app%2Fcontrollers%2Fpurchase_controller.rb;fp=app%2Fcontrollers%2Fpurchase_controller.rb;h=0f7e22fa1d79267903c7f463ad1e9df57b977e39;hb=b12881a928516bca02b2bd8ce1bbe9288840c76f;hp=2ac4dae677dcd11bab2741914b671afc436bb447;hpb=bbf13350a73e14c5448f6fb0077126b7d92cdb48;p=cs356-p2-videostore.git diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb index 2ac4dae..0f7e22f 100644 --- a/app/controllers/purchase_controller.rb +++ b/app/controllers/purchase_controller.rb @@ -1,14 +1,52 @@ class PurchaseController < ApplicationController + layout "admin" # Make sure that a user logs in before doing any action here - before_filter :authorize + before_filter :authorize, :except => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index] + before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index] 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 begin @@ -18,12 +56,25 @@ class PurchaseController < ApplicationController session[:items] = [] end + def success_end + # Set the customer's debt to $0.00. They MUST pay you before + # checking anything else out, of course + @customer = Customer.find_by_id(session[:customer_id]) + @customer.debt = 0.00 + @customer.save + + session[:customer_id] = nil + session[:total] = nil + session[:items] = nil + redirect_to :action => :begin + end + def customer_ok if Customer.find_by_id(params[:customer_id]) 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 @@ -35,129 +86,130 @@ class PurchaseController < ApplicationController render :action => 'menu' end - def rent_begin - render :action => 'rent_begin' - end - - def rent_validate - @customer = Customer.find_by_id(session[:customer_id]) - @rentable = Rentable.find_by_id(params[:rentable_id]) - - if @customer.nil? - flash[:error] = "Customer ID is invalid" - redirect_to :action => :begin - return - end - - if @rentable.nil? - flash[:error] = "Rentable ID is invalid" - redirect_to :action => :rent_begin - 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[:error] = "#{@maxvideos.description} LIMIT REACHED" - redirect_to :action => :rent_begin - return - end - - @maxgames = RentablePolicy.find_by_name("MaxGames") - if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value - flash[:error] = "#{@maxgames.description} LIMIT REACHED" - redirect_to :action => :rent_begin - return - end - - @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos") - if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value - flash[:error] = "#{@maxoverduevideos.description} LIMIT REACHED" - redirect_to :action => :rent_begin - return - end - - @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames") - if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value - flash[:error] = "#{@maxoverduegames.description} LIMIT REACHED" - redirect_to :action => :rent_begin - return + def rent + if request.post? + @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 @rentable.nil? + flash[:notice] = "Rentable ID is invalid" + redirect_to :action => :rent + return + end + + if @rentable.checkedout? + flash[:notice] = "This #{@rentable.type} is already checked out!" + redirect_to :action => :rent + 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 + + @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 + + @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 + + @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 + + # 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 + else + render :action => 'rent' 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 - def buy_begin - render :action => 'buy_begin' - 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 - - if @merchandise.nil? - flash[:error] = "Merchandise ID is invalid" - redirect_to :action => :buy_begin - return - end - - if @merchandise.quantity < 1 - flash[:error] = "The system thinks we are out of this merchandise item!" - redirect_to :action => :buy_begin - return + 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 = @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 - - # 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 end + end