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 # Save important data, since we're gonna wipe it out now
81 @customer = Customer.find_by_id(session[:customer_id])
82 @debt = @customer.debt
83 @total = session[:total] + @debt
84 @items = session[:items]
87 # Set the customer's debt to $0.00, she paid us
88 @customer = Customer.find_by_id(session[:customer_id])
93 session[:customer_id] = nil
98 render :action => 'reciept'
102 if Customer.find_by_id(params[:customer_id])
103 session[:customer_id] = params[:customer_id]
104 redirect_to :action => :menu
106 flash[:notice] = "Customer ID is invalid"
107 redirect_to :action => :begin
112 @customer = Customer.find_by_id(session[:customer_id])
113 @total_price = session[:total]
114 @items = session[:items]
115 render :action => 'menu'
120 @customer = Customer.find_by_id(session[:customer_id])
121 @rentable = Rentable.find_by_id(params[:rentable_id])
124 flash[:notice] = "Customer ID is invalid"
125 redirect_to :action => :begin
130 flash[:notice] = "Rentable ID is invalid"
131 redirect_to :action => :rent
135 if @rentable.checkedout?
136 flash[:notice] = "This #{@rentable.type} is already checked out!"
137 redirect_to :action => :rent
141 # Check Rentable Policies
142 @maxvideos = RentablePolicy.find_by_name("MaxVideos")
143 if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
144 flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
145 redirect_to :action => :rent
149 @maxgames = RentablePolicy.find_by_name("MaxGames")
150 if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
151 flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
152 redirect_to :action => :rent
156 @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
157 if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
158 flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
159 redirect_to :action => :rent
163 @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
164 if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
165 flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
166 redirect_to :action => :rent
171 checkout = Coitem.new
172 checkout.customer = @customer
173 checkout.rentable = @rentable
174 checkout.out_date = Time.now.to_date
175 checkout.due_date = @rentable.due_date
178 # Actually record the purchase
179 purchase = RentablePurchase.new
180 purchase.customer_id = session[:customer_id][0]
181 purchase.date = Time.now.to_date
182 purchase.price = @rentable.calculated_price
183 purchase.rentable = @rentable
186 # Add to session variables
187 session[:total] += @rentable.calculated_price
188 session[:items].push @rentable
190 flash[:notice] = "Successfully made purchase"
191 redirect_to :action => :menu
193 render :action => 'rent'
199 @customer = Customer.find_by_id(session[:customer_id])
200 @merchandise = Merchandise.find_by_id(params[:merchandise_id])
203 flash[:notice] = "Customer ID is invalid"
204 redirect_to :action => :begin
209 flash[:notice] = "Merchandise ID is invalid"
210 redirect_to :action => :buy_merch
214 if @merchandise.quantity < 1
215 flash[:notice] = "The system thinks we are out of this merchandise item!"
216 redirect_to :action => :buy_merch
220 # Actually record the purchase
221 purchase = MerchandisePurchase.new
222 purchase.customer = @customer
223 purchase.date = Time.now.to_date
224 purchase.price = @merchandise.price
225 purchase.merchandise = @merchandise
226 purchase.quantity = 1
227 @merchandise.quantity -= 1
229 # Add to session variables
230 session[:total] += @merchandise.price
231 session[:items].push @merchandise
233 # Save both the merchandise (we changed the quantity) and the purchase to the log
237 flash[:notice] = "Successfully made purchase"
238 redirect_to :action => :menu
240 render :action => 'buy_merch'