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
18 if Customer.find_by_id(params[:customer_id])
19 session[:customer_id] = params[:customer_id]
20 redirect_to :action => :menu
22 flash[:error] = "Customer ID is invalid"
23 redirect_to :action => :begin
28 @customer = Customer.find_by_id(session[:customer_id])
29 @total_price = session[:total]
30 render :action => 'menu'
34 render :action => 'rent_begin'
38 @customer = Customer.find_by_id(session[:customer_id])
39 @rentable = Rentable.find_by_id(params[:rentable_id])
42 flash[:error] = "Customer ID is invalid"
43 redirect_to :action => :begin
48 flash[:error] = "Rentable ID is invalid"
49 redirect_to :action => :rent_begin
53 if @rentable.checkedout?
54 flash[:error] = "This #{@rentable.type} is already checked out!"
55 redirect_to :action => :rent_begin
61 checkout.customer = @customer
62 checkout.rentable = @rentable
63 checkout.out_date = Time.now.to_date
64 checkout.due_date = @rentable.due_date
67 # Actually record the purchase
68 purchase = RentablePurchase.new
69 purchase.customer_id = session[:customer_id]
70 purchase.date = Time.now.to_date
71 purchase.price = @rentable.calculated_price
72 session[:total] += @rentable.calculated_price
73 purchase.rentable = @rentable
76 flash[:notice] = "Successfully made purchase"
77 redirect_to :action => :menu
81 render :action => 'buy_begin'
85 @customer = Customer.find_by_id(session[:customer_id])
86 @merchandise = Merchandise.find_by_id(params[:merchandise_id])
89 flash[:error] = "Customer ID is invalid"
90 redirect_to :action => :begin
95 flash[:error] = "Merchandise ID is invalid"
96 redirect_to :action => :buy_begin
100 if @merchandise.quantity < 1
101 flash[:error] = "The system thinks we are out of this merchandise item!"
102 redirect_to :action => :buy_begin
106 # Actually record the purchase
107 purchase = MerchandisePurchase.new
108 purchase.customer_id = session[:customer_id]
109 purchase.date = Time.now.to_date
110 purchase.price = @merchandise.price
111 session[:total] += @merchandise.price
112 purchase.merchandise = @merchandise
113 purchase.quantity = 1
114 @merchandise.quantity -= 1
116 # Save both the merchandise (we changed the quantity) and the purchase to the log
120 flash[:notice] = "Successfully made purchase"
121 redirect_to :action => :menu