Add RentablePolicy for MaxOverdueVideos and MaxOverdueGames
[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 Rentable Policies
62     @maxvideos = RentablePolicy.find_by_name("MaxVideos")
63     if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
64       flash[:error] = "#{@maxvideos.description} LIMIT REACHED"
65       redirect_to :action => :rent_begin
66       return
67     end
68
69     @maxgames = RentablePolicy.find_by_name("MaxGames")
70     if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
71       flash[:error] = "#{@maxgames.description} LIMIT REACHED"
72       redirect_to :action => :rent_begin
73       return
74     end
75
76     @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
77     if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
78       flash[:error] = "#{@maxoverduevideos.description} LIMIT REACHED"
79       redirect_to :action => :rent_begin
80       return
81     end
82
83     @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
84     if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
85       flash[:error] = "#{@maxoverduegames.description} LIMIT REACHED"
86       redirect_to :action => :rent_begin
87       return
88     end
89
90     # Check out the item
91     checkout = Coitem.new
92     checkout.customer = @customer
93     checkout.rentable = @rentable
94     checkout.out_date = Time.now.to_date
95     checkout.due_date = @rentable.due_date
96     checkout.save!
97
98     # Actually record the purchase
99     purchase = RentablePurchase.new
100     purchase.customer_id = session[:customer_id]
101     purchase.date = Time.now.to_date
102     purchase.price = @rentable.calculated_price
103     purchase.rentable = @rentable
104     purchase.save!
105
106     # Add te session variables
107     session[:total] += @rentable.calculated_price
108     session[:items].push @rentable
109
110     flash[:notice] = "Successfully made purchase"
111     redirect_to :action => :menu
112   end
113
114   def buy_begin
115     render :action => 'buy_begin'
116   end
117
118   def buy_validate
119     @customer = Customer.find_by_id(session[:customer_id])
120     @merchandise = Merchandise.find_by_id(params[:merchandise_id])
121   
122     if @customer.nil?
123       flash[:error] = "Customer ID is invalid"
124       redirect_to :action => :begin
125       return
126     end
127
128     if @merchandise.nil?
129       flash[:error] = "Merchandise ID is invalid"
130       redirect_to :action => :buy_begin
131       return
132     end
133
134     if @merchandise.quantity < 1
135       flash[:error] = "The system thinks we are out of this merchandise item!"
136       redirect_to :action => :buy_begin
137       return
138     end
139
140     # Actually record the purchase
141     purchase = MerchandisePurchase.new
142     purchase.customer_id = session[:customer_id]
143     purchase.date = Time.now.to_date
144     purchase.price = @merchandise.price
145     purchase.merchandise = @merchandise
146     purchase.quantity = 1
147     @merchandise.quantity -= 1
148
149     # Add to session variables
150     session[:total] += @merchandise.price
151     session[:items].push @merchandise
152
153     # Save both the merchandise (we changed the quantity) and the purchase to the log
154     @merchandise.save!
155     purchase.save!
156
157     flash[:notice] = "Successfully made purchase"
158     redirect_to :action => :menu
159   end
160 end