704e38b78b0e03737cbcb732b15bae02096df0ff
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
1 class PurchaseController < ApplicationController
2
3   def index
4     redirect_to :action => :begin
5   end
6
7   def list
8     @purchase_pages, @purchase = paginate :purchases, :per_page => 100
9   end
10
11   def begin
12     # enter a customer id here
13     render :action => 'begin'
14     session[:total] = 0.00
15     session[:items] = []
16   end
17
18   def customer_ok
19     if Customer.find_by_id(params[:customer_id])
20       session[:customer_id] = params[:customer_id]
21       redirect_to :action => :menu
22     else
23       flash[:error] = "Customer ID is invalid"
24       redirect_to :action => :begin
25     end
26   end
27
28   def menu
29     @customer = Customer.find_by_id(session[:customer_id])
30     @total_price = session[:total]
31     @items = session[:items]
32     render :action => 'menu'
33   end
34
35   def rent_begin
36     render :action => 'rent_begin'
37   end
38
39   def rent_validate
40     @customer = Customer.find_by_id(session[:customer_id])
41     @rentable = Rentable.find_by_id(params[:rentable_id])
42
43     if @customer.nil?
44       flash[:error] = "Customer ID is invalid"
45       redirect_to :action => :begin
46       return
47     end
48
49     if @rentable.nil?
50       flash[:error] = "Rentable ID is invalid"
51       redirect_to :action => :rent_begin
52       return
53     end
54
55     if @rentable.checkedout?
56       flash[:error] = "This #{@rentable.type} is already checked out!"
57       redirect_to :action => :rent_begin
58       return
59     end
60
61     # Check out the item
62     checkout = Coitem.new
63     checkout.customer = @customer
64     checkout.rentable = @rentable
65     checkout.out_date = Time.now.to_date
66     checkout.due_date = @rentable.due_date
67     checkout.save!
68
69     # Actually record the purchase
70     purchase = RentablePurchase.new
71     purchase.customer_id = session[:customer_id]
72     purchase.date = Time.now.to_date
73     purchase.price = @rentable.calculated_price
74     purchase.rentable = @rentable
75     purchase.save!
76
77     # Add te session variables
78     session[:total] += @rentable.calculated_price
79     session[:items].push @rentable
80
81     flash[:notice] = "Successfully made purchase"
82     redirect_to :action => :menu
83   end
84
85   def buy_begin
86     render :action => 'buy_begin'
87   end
88
89   def buy_validate
90     @customer = Customer.find_by_id(session[:customer_id])
91     @merchandise = Merchandise.find_by_id(params[:merchandise_id])
92   
93     if @customer.nil?
94       flash[:error] = "Customer ID is invalid"
95       redirect_to :action => :begin
96       return
97     end
98
99     if @merchandise.nil?
100       flash[:error] = "Merchandise ID is invalid"
101       redirect_to :action => :buy_begin
102       return
103     end
104
105     if @merchandise.quantity < 1
106       flash[:error] = "The system thinks we are out of this merchandise item!"
107       redirect_to :action => :buy_begin
108       return
109     end
110
111     # Actually record the purchase
112     purchase = MerchandisePurchase.new
113     purchase.customer_id = session[:customer_id]
114     purchase.date = Time.now.to_date
115     purchase.price = @merchandise.price
116     purchase.merchandise = @merchandise
117     purchase.quantity = 1
118     @merchandise.quantity -= 1
119
120     # Add to session variables
121     session[:total] += @merchandise.price
122     session[:items].push @merchandise
123
124     # Save both the merchandise (we changed the quantity) and the purchase to the log
125     @merchandise.save!
126     purchase.save!
127
128     flash[:notice] = "Successfully made purchase"
129     redirect_to :action => :menu
130   end
131 end