4445c07b838352860e3cb499bebf4fe890954c22
[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   end
16
17   def customer_ok
18     if Customer.find_by_id(params[:customer_id])
19       session[:customer_id] = params[:customer_id]
20       redirect_to :action => :menu
21     else
22       flash[:error] = "Customer ID is invalid"
23       redirect_to :action => :begin
24     end
25   end
26
27   def menu
28     @customer = Customer.find_by_id(session[:customer_id])
29     @total_price = session[:total]
30     render :action => 'menu'
31   end
32
33   def rent_begin
34     render :action => 'rent_begin'
35   end
36
37   def rent_validate
38     @customer = Customer.find_by_id(session[:customer_id])
39     @rentable = Rentable.find_by_id(params[:rentable_id])
40
41     if @customer.nil?
42       flash[:error] = "Customer ID is invalid"
43       redirect_to :action => :begin
44       return
45     end
46
47     if @rentable.nil?
48       flash[:error] = "Rentable ID is invalid"
49       redirect_to :action => :rent_begin
50       return
51     end
52
53     if @rentable.checkedout?
54       flash[:error] = "This #{@rentable.type} is already checked out!"
55       redirect_to :action => :rent_begin
56       return
57     end
58
59     # Check out the item
60     checkout = Coitem.new
61     checkout.customer = @customer
62     checkout.rentable = @rentable
63     checkout.out_date = Time.now.to_date
64     checkout.due_date = @rentable.due_date
65     checkout.save!
66
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
74     purchase.save!
75
76     flash[:notice] = "Successfully made purchase"
77     redirect_to :action => :menu
78   end
79
80   def buy_begin
81     render :action => 'buy_begin'
82   end
83
84   def buy_validate
85     @customer = Customer.find_by_id(session[:customer_id])
86     @merchandise = Merchandise.find_by_id(params[:merchandise_id])
87   
88     if @customer.nil?
89       flash[:error] = "Customer ID is invalid"
90       redirect_to :action => :begin
91       return
92     end
93
94     if @merchandise.nil?
95       flash[:error] = "Merchandise ID is invalid"
96       redirect_to :action => :buy_begin
97       return
98     end
99
100     if @merchandise.quantity < 1
101       flash[:error] = "The system thinks we are out of this merchandise item!"
102       redirect_to :action => :buy_begin
103       return
104     end
105
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
115
116     # Save both the merchandise (we changed the quantity) and the purchase to the log
117     @merchandise.save!
118     purchase.save!
119
120     flash[:notice] = "Successfully made purchase"
121     redirect_to :action => :menu
122   end
123 end