1 class PurchaseController < ApplicationController
4 redirect_to :action => :begin
8 @purchase_pages, @purchase = paginate :purchases, :per_page => 100
12 # enter a customer id here
13 render :action => 'begin'
14 session[:total] = 0.00
19 if Customer.find_by_id(params[:customer_id])
20 session[:customer_id] = params[:customer_id]
21 redirect_to :action => :menu
23 flash[:error] = "Customer ID is invalid"
24 redirect_to :action => :begin
29 @customer = Customer.find_by_id(session[:customer_id])
30 @total_price = session[:total]
31 @items = session[:items]
32 render :action => 'menu'
36 render :action => 'rent_begin'
40 @customer = Customer.find_by_id(session[:customer_id])
41 @rentable = Rentable.find_by_id(params[:rentable_id])
44 flash[:error] = "Customer ID is invalid"
45 redirect_to :action => :begin
50 flash[:error] = "Rentable ID is invalid"
51 redirect_to :action => :rent_begin
55 if @rentable.checkedout?
56 flash[:error] = "This #{@rentable.type} is already checked out!"
57 redirect_to :action => :rent_begin
61 # Check Rentable Policies
62 @maxvideos = RentablePolicy.find_by_name("MaxVideos")
63 if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
64 flash[:error] = "#{@maxvideos.description} LIMIT REACHED"
65 redirect_to :action => :rent_begin
69 @maxgames = RentablePolicy.find_by_name("MaxGames")
70 if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
71 flash[:error] = "#{@maxgames.description} LIMIT REACHED"
72 redirect_to :action => :rent_begin
78 checkout.customer = @customer
79 checkout.rentable = @rentable
80 checkout.out_date = Time.now.to_date
81 checkout.due_date = @rentable.due_date
84 # Actually record the purchase
85 purchase = RentablePurchase.new
86 purchase.customer_id = session[:customer_id]
87 purchase.date = Time.now.to_date
88 purchase.price = @rentable.calculated_price
89 purchase.rentable = @rentable
92 # Add te session variables
93 session[:total] += @rentable.calculated_price
94 session[:items].push @rentable
96 flash[:notice] = "Successfully made purchase"
97 redirect_to :action => :menu
101 render :action => 'buy_begin'
105 @customer = Customer.find_by_id(session[:customer_id])
106 @merchandise = Merchandise.find_by_id(params[:merchandise_id])
109 flash[:error] = "Customer ID is invalid"
110 redirect_to :action => :begin
115 flash[:error] = "Merchandise ID is invalid"
116 redirect_to :action => :buy_begin
120 if @merchandise.quantity < 1
121 flash[:error] = "The system thinks we are out of this merchandise item!"
122 redirect_to :action => :buy_begin
126 # Actually record the purchase
127 purchase = MerchandisePurchase.new
128 purchase.customer_id = session[:customer_id]
129 purchase.date = Time.now.to_date
130 purchase.price = @merchandise.price
131 purchase.merchandise = @merchandise
132 purchase.quantity = 1
133 @merchandise.quantity -= 1
135 # Add to session variables
136 session[:total] += @merchandise.price
137 session[:items].push @merchandise
139 # Save both the merchandise (we changed the quantity) and the purchase to the log
143 flash[:notice] = "Successfully made purchase"
144 redirect_to :action => :menu