1 class PurchaseController < ApplicationController
4 # Make sure that a user logs in before doing any action here
5 before_filter :authorize, :except => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
6 before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
9 render :action => 'index'
13 @purchase_pages, @purchases = paginate :purchases, :per_page => 100
22 when :customer, "customer"
23 redirect_to :action => 'filterbycust', :id => id
25 date = Date.new id['(1i)'].to_i, id['(2i)'].to_i, id['(3i)'].to_i
26 redirect_to :action => 'filterbydate', :id => date.to_s
33 render :action => 'filter'
38 @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["customer_id = ?", params[:id]]
39 render :action => 'list'
43 @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["date = ?", params[:id]]
44 render :action => 'list'
48 @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["type = ?", params[:id]]
49 render :action => 'list'
53 # enter a customer id here
54 render :action => 'begin'
55 session[:total] = 0.00
60 # Set the customer's debt to $0.00. They MUST pay you before
61 # checking anything else out, of course
62 @customer = Customer.find_by_id(session[:customer_id])
66 session[:customer_id] = nil
69 redirect_to :action => :begin
73 if Customer.find_by_id(params[:customer_id])
74 session[:customer_id] = params[:customer_id]
75 redirect_to :action => :menu
77 flash[:notice] = "Customer ID is invalid"
78 redirect_to :action => :begin
83 @customer = Customer.find_by_id(session[:customer_id])
84 @total_price = session[:total]
85 @items = session[:items]
86 render :action => 'menu'
91 @customer = Customer.find_by_id(session[:customer_id])
92 @rentable = Rentable.find_by_id(params[:rentable_id])
95 flash[:notice] = "Customer ID is invalid"
96 redirect_to :action => :begin
101 flash[:notice] = "Rentable ID is invalid"
102 redirect_to :action => :rent
106 if @rentable.checkedout?
107 flash[:notice] = "This #{@rentable.type} is already checked out!"
108 redirect_to :action => :rent
112 # Check Rentable Policies
113 @maxvideos = RentablePolicy.find_by_name("MaxVideos")
114 if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
115 flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
116 redirect_to :action => :rent
120 @maxgames = RentablePolicy.find_by_name("MaxGames")
121 if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
122 flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
123 redirect_to :action => :rent
127 @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
128 if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
129 flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
130 redirect_to :action => :rent
134 @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
135 if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
136 flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
137 redirect_to :action => :rent
142 checkout = Coitem.new
143 checkout.customer = @customer
144 checkout.rentable = @rentable
145 checkout.out_date = Time.now.to_date
146 checkout.due_date = @rentable.due_date
149 # Actually record the purchase
150 purchase = RentablePurchase.new
151 purchase.customer_id = session[:customer_id]
152 purchase.date = Time.now.to_date
153 purchase.price = @rentable.calculated_price
154 purchase.rentable = @rentable
157 # Add te session variables
158 session[:total] += @rentable.calculated_price
159 session[:items].push @rentable
161 flash[:notice] = "Successfully made purchase"
162 redirect_to :action => :menu
164 render :action => 'rent'
170 @customer = Customer.find_by_id(session[:customer_id])
171 @merchandise = Merchandise.find_by_id(params[:merchandise_id])
174 flash[:notice] = "Customer ID is invalid"
175 redirect_to :action => :begin
180 flash[:notice] = "Merchandise ID is invalid"
181 redirect_to :action => :buy_merch
185 if @merchandise.quantity < 1
186 flash[:notice] = "The system thinks we are out of this merchandise item!"
187 redirect_to :action => :buy_merch
191 # Actually record the purchase
192 purchase = MerchandisePurchase.new
193 purchase.customer = @customer
194 purchase.date = Time.now.to_date
195 purchase.price = @merchandise.price
196 purchase.merchandise = @merchandise
197 purchase.quantity = 1
198 @merchandise.quantity -= 1
200 # Add to session variables
201 session[:total] += @merchandise.price
202 session[:items].push @merchandise
204 # Save both the merchandise (we changed the quantity) and the purchase to the log
208 flash[:notice] = "Successfully made purchase"
209 redirect_to :action => :menu
211 render :action => 'buy_merch'