2ac4dae677dcd11bab2741914b671afc436bb447
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
1 class PurchaseController < ApplicationController
2
3   # Make sure that a user logs in before doing any action here
4   before_filter :authorize
5
6   def index
7     redirect_to :action => :begin
8   end
9
10   def list
11     @purchase_pages, @purchase = paginate :purchases, :per_page => 100
12   end
13
14   def begin
15     # enter a customer id here
16     render :action => 'begin'
17     session[:total] = 0.00
18     session[:items] = []
19   end
20
21   def customer_ok
22     if Customer.find_by_id(params[:customer_id])
23       session[:customer_id] = params[:customer_id]
24       redirect_to :action => :menu
25     else
26       flash[:error] = "Customer ID is invalid"
27       redirect_to :action => :begin
28     end
29   end
30
31   def menu
32     @customer = Customer.find_by_id(session[:customer_id])
33     @total_price = session[:total]
34     @items = session[:items]
35     render :action => 'menu'
36   end
37
38   def rent_begin
39     render :action => 'rent_begin'
40   end
41
42   def rent_validate
43     @customer = Customer.find_by_id(session[:customer_id])
44     @rentable = Rentable.find_by_id(params[:rentable_id])
45
46     if @customer.nil?
47       flash[:error] = "Customer ID is invalid"
48       redirect_to :action => :begin
49       return
50     end
51
52     if @rentable.nil?
53       flash[:error] = "Rentable ID is invalid"
54       redirect_to :action => :rent_begin
55       return
56     end
57
58     if @rentable.checkedout?
59       flash[:error] = "This #{@rentable.type} is already checked out!"
60       redirect_to :action => :rent_begin
61       return
62     end
63
64     # Check Rentable Policies
65     @maxvideos = RentablePolicy.find_by_name("MaxVideos")
66     if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
67       flash[:error] = "#{@maxvideos.description} LIMIT REACHED"
68       redirect_to :action => :rent_begin
69       return
70     end
71
72     @maxgames = RentablePolicy.find_by_name("MaxGames")
73     if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
74       flash[:error] = "#{@maxgames.description} LIMIT REACHED"
75       redirect_to :action => :rent_begin
76       return
77     end
78
79     @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
80     if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
81       flash[:error] = "#{@maxoverduevideos.description} LIMIT REACHED"
82       redirect_to :action => :rent_begin
83       return
84     end
85
86     @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
87     if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
88       flash[:error] = "#{@maxoverduegames.description} LIMIT REACHED"
89       redirect_to :action => :rent_begin
90       return
91     end
92
93     # Check out the item
94     checkout = Coitem.new
95     checkout.customer = @customer
96     checkout.rentable = @rentable
97     checkout.out_date = Time.now.to_date
98     checkout.due_date = @rentable.due_date
99     checkout.save!
100
101     # Actually record the purchase
102     purchase = RentablePurchase.new
103     purchase.customer_id = session[:customer_id]
104     purchase.date = Time.now.to_date
105     purchase.price = @rentable.calculated_price
106     purchase.rentable = @rentable
107     purchase.save!
108
109     # Add te session variables
110     session[:total] += @rentable.calculated_price
111     session[:items].push @rentable
112
113     flash[:notice] = "Successfully made purchase"
114     redirect_to :action => :menu
115   end
116
117   def buy_begin
118     render :action => 'buy_begin'
119   end
120
121   def buy_validate
122     @customer = Customer.find_by_id(session[:customer_id])
123     @merchandise = Merchandise.find_by_id(params[:merchandise_id])
124   
125     if @customer.nil?
126       flash[:error] = "Customer ID is invalid"
127       redirect_to :action => :begin
128       return
129     end
130
131     if @merchandise.nil?
132       flash[:error] = "Merchandise ID is invalid"
133       redirect_to :action => :buy_begin
134       return
135     end
136
137     if @merchandise.quantity < 1
138       flash[:error] = "The system thinks we are out of this merchandise item!"
139       redirect_to :action => :buy_begin
140       return
141     end
142
143     # Actually record the purchase
144     purchase = MerchandisePurchase.new
145     purchase.customer_id = session[:customer_id]
146     purchase.date = Time.now.to_date
147     purchase.price = @merchandise.price
148     purchase.merchandise = @merchandise
149     purchase.quantity = 1
150     @merchandise.quantity -= 1
151
152     # Add to session variables
153     session[:total] += @merchandise.price
154     session[:items].push @merchandise
155
156     # Save both the merchandise (we changed the quantity) and the purchase to the log
157     @merchandise.save!
158     purchase.save!
159
160     flash[:notice] = "Successfully made purchase"
161     redirect_to :action => :menu
162   end
163 end