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, :income]
6 before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index, :income]
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'
54 # Find all purchases between :begin_date, and :end_date and sum up the total income
55 # from RentablePurchases, MerchandisePurchases. Print both sums, and the total sum.
56 @begin_date = Date.new params[:begin_date]['(1i)'].to_i, params[:begin_date]['(2i)'].to_i, params[:begin_date]['(3i)'].to_i
57 @end_date = Date.new params[:end_date]['(1i)'].to_i, params[:end_date]['(2i)'].to_i, params[:end_date]['(3i)'].to_i
58 merchandises = MerchandisePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date])
59 rentables = RentablePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date])
61 @merch_count = merchandises.length
62 @rent_count = rentables.length
63 @merch_sum = merchandises.sum(&:price)
64 @rent_sum = rentables.sum(&:price)
65 @total = @merch_sum + @rent_sum
66 render :action => 'income_results'
68 render :action => 'income'
73 # enter a customer id here
74 render :action => 'begin'
75 session[:total] = 0.00
80 # Set the customer's debt to $0.00. They MUST pay you before
81 # checking anything else out, of course
82 @customer = Customer.find_by_id(session[:customer_id])
86 session[:customer_id] = nil
89 redirect_to :action => :begin
93 if Customer.find_by_id(params[:customer_id])
94 session[:customer_id] = params[:customer_id]
95 redirect_to :action => :menu
97 flash[:notice] = "Customer ID is invalid"
98 redirect_to :action => :begin
103 @customer = Customer.find_by_id(session[:customer_id])
104 @total_price = session[:total]
105 @items = session[:items]
106 render :action => 'menu'
111 @customer = Customer.find_by_id(session[:customer_id])
112 @rentable = Rentable.find_by_id(params[:rentable_id])
115 flash[:notice] = "Customer ID is invalid"
116 redirect_to :action => :begin
121 flash[:notice] = "Rentable ID is invalid"
122 redirect_to :action => :rent
126 if @rentable.checkedout?
127 flash[:notice] = "This #{@rentable.type} is already checked out!"
128 redirect_to :action => :rent
132 # Check Rentable Policies
133 @maxvideos = RentablePolicy.find_by_name("MaxVideos")
134 if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
135 flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
136 redirect_to :action => :rent
140 @maxgames = RentablePolicy.find_by_name("MaxGames")
141 if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
142 flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
143 redirect_to :action => :rent
147 @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
148 if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
149 flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
150 redirect_to :action => :rent
154 @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
155 if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
156 flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
157 redirect_to :action => :rent
162 checkout = Coitem.new
163 checkout.customer = @customer
164 checkout.rentable = @rentable
165 checkout.out_date = Time.now.to_date
166 checkout.due_date = @rentable.due_date
169 # Actually record the purchase
170 purchase = RentablePurchase.new
171 purchase.customer_id = session[:customer_id][0]
172 purchase.date = Time.now.to_date
173 purchase.price = @rentable.calculated_price
174 purchase.rentable = @rentable
177 # Add te session variables
178 session[:total] += @rentable.calculated_price
179 session[:items].push @rentable
181 flash[:notice] = "Successfully made purchase"
182 redirect_to :action => :menu
184 render :action => 'rent'
190 @customer = Customer.find_by_id(session[:customer_id])
191 @merchandise = Merchandise.find_by_id(params[:merchandise_id])
194 flash[:notice] = "Customer ID is invalid"
195 redirect_to :action => :begin
200 flash[:notice] = "Merchandise ID is invalid"
201 redirect_to :action => :buy_merch
205 if @merchandise.quantity < 1
206 flash[:notice] = "The system thinks we are out of this merchandise item!"
207 redirect_to :action => :buy_merch
211 # Actually record the purchase
212 purchase = MerchandisePurchase.new
213 purchase.customer = @customer
214 purchase.date = Time.now.to_date
215 purchase.price = @merchandise.price
216 purchase.merchandise = @merchandise
217 purchase.quantity = 1
218 @merchandise.quantity -= 1
220 # Add to session variables
221 session[:total] += @merchandise.price
222 session[:items].push @merchandise
224 # Save both the merchandise (we changed the quantity) and the purchase to the log
228 flash[:notice] = "Successfully made purchase"
229 redirect_to :action => :menu
231 render :action => 'buy_merch'