1 class PurchaseController < ApplicationController
3 # Make sure that a user logs in before doing any action here
4 before_filter :authorize
7 redirect_to :action => :begin
11 @purchase_pages, @purchase = paginate :purchases, :per_page => 100
15 # enter a customer id here
16 render :action => 'begin'
17 session[:total] = 0.00
22 if Customer.find_by_id(params[:customer_id])
23 session[:customer_id] = params[:customer_id]
24 redirect_to :action => :menu
26 flash[:error] = "Customer ID is invalid"
27 redirect_to :action => :begin
32 @customer = Customer.find_by_id(session[:customer_id])
33 @total_price = session[:total]
34 @items = session[:items]
35 render :action => 'menu'
39 render :action => 'rent_begin'
43 @customer = Customer.find_by_id(session[:customer_id])
44 @rentable = Rentable.find_by_id(params[:rentable_id])
47 flash[:error] = "Customer ID is invalid"
48 redirect_to :action => :begin
53 flash[:error] = "Rentable ID is invalid"
54 redirect_to :action => :rent_begin
58 if @rentable.checkedout?
59 flash[:error] = "This #{@rentable.type} is already checked out!"
60 redirect_to :action => :rent_begin
64 # Check Rentable Policies
65 @maxvideos = RentablePolicy.find_by_name("MaxVideos")
66 if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
67 flash[:error] = "#{@maxvideos.description} LIMIT REACHED"
68 redirect_to :action => :rent_begin
72 @maxgames = RentablePolicy.find_by_name("MaxGames")
73 if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
74 flash[:error] = "#{@maxgames.description} LIMIT REACHED"
75 redirect_to :action => :rent_begin
79 @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
80 if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
81 flash[:error] = "#{@maxoverduevideos.description} LIMIT REACHED"
82 redirect_to :action => :rent_begin
86 @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
87 if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
88 flash[:error] = "#{@maxoverduegames.description} LIMIT REACHED"
89 redirect_to :action => :rent_begin
95 checkout.customer = @customer
96 checkout.rentable = @rentable
97 checkout.out_date = Time.now.to_date
98 checkout.due_date = @rentable.due_date
101 # Actually record the purchase
102 purchase = RentablePurchase.new
103 purchase.customer_id = session[:customer_id]
104 purchase.date = Time.now.to_date
105 purchase.price = @rentable.calculated_price
106 purchase.rentable = @rentable
109 # Add te session variables
110 session[:total] += @rentable.calculated_price
111 session[:items].push @rentable
113 flash[:notice] = "Successfully made purchase"
114 redirect_to :action => :menu
118 render :action => 'buy_begin'
122 @customer = Customer.find_by_id(session[:customer_id])
123 @merchandise = Merchandise.find_by_id(params[:merchandise_id])
126 flash[:error] = "Customer ID is invalid"
127 redirect_to :action => :begin
132 flash[:error] = "Merchandise ID is invalid"
133 redirect_to :action => :buy_begin
137 if @merchandise.quantity < 1
138 flash[:error] = "The system thinks we are out of this merchandise item!"
139 redirect_to :action => :buy_begin
143 # Actually record the purchase
144 purchase = MerchandisePurchase.new
145 purchase.customer_id = session[:customer_id]
146 purchase.date = Time.now.to_date
147 purchase.price = @merchandise.price
148 purchase.merchandise = @merchandise
149 purchase.quantity = 1
150 @merchandise.quantity -= 1
152 # Add to session variables
153 session[:total] += @merchandise.price
154 session[:items].push @merchandise
156 # Save both the merchandise (we changed the quantity) and the purchase to the log
160 flash[:notice] = "Successfully made purchase"
161 redirect_to :action => :menu