Prettify the Purchase system
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
1 class PurchaseController < ApplicationController
2   layout "admin"
3
4   # Make sure that a user logs in before doing any action here
5   before_filter :authorize, :except => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
6   before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
7
8   def index
9     render :action => 'index'
10   end
11
12   def list
13     @purchase_pages, @purchases = paginate :purchases, :per_page => 100
14   end
15
16   def filter
17     if request.post?
18       type = params[:type]
19       id = params[:id]
20
21       case type
22         when :customer, "customer"
23           redirect_to :action => 'filterbycust', :id => id
24         when :date, "date"
25           date = Date.new id['(1i)'].to_i, id['(2i)'].to_i, id['(3i)'].to_i
26           redirect_to :action => 'filterbydate', :id => date.to_s
27       end
28     else
29       @type = params[:type]
30       if @type.nil?
31         @type = "all"
32       end
33       render :action => 'filter'
34     end
35   end
36
37   def filterbycust
38     @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["customer_id = ?", params[:id]]
39     render :action => 'list'
40   end
41
42   def filterbydate
43     @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["date = ?", params[:id]]
44     render :action => 'list'
45   end
46
47   def filterbytype
48     @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["type = ?", params[:id]]
49     render :action => 'list'
50   end
51
52   def begin
53     # enter a customer id here
54     render :action => 'begin'
55     session[:total] = 0.00
56     session[:items] = []
57   end
58
59   def success_end
60     # Set the customer's debt to $0.00. They MUST pay you before
61     # checking anything else out, of course
62     @customer = Customer.find_by_id(session[:customer_id])
63     @customer.debt = 0.00
64     @customer.save
65
66     session[:customer_id] = nil
67     session[:total] = nil
68     session[:items] = nil
69     redirect_to :action => :begin
70   end
71
72   def customer_ok
73     if Customer.find_by_id(params[:customer_id])
74       session[:customer_id] = params[:customer_id]
75       redirect_to :action => :menu
76     else
77       flash[:notice] = "Customer ID is invalid"
78       redirect_to :action => :begin
79     end
80   end
81
82   def menu
83     @customer = Customer.find_by_id(session[:customer_id])
84     @total_price = session[:total]
85     @items = session[:items]
86     render :action => 'menu'
87   end
88
89   def rent
90     if request.post?
91       @customer = Customer.find_by_id(session[:customer_id])
92       @rentable = Rentable.find_by_id(params[:rentable_id])
93
94       if @customer.nil?
95         flash[:notice] = "Customer ID is invalid"
96         redirect_to :action => :begin
97         return
98       end
99
100       if @rentable.nil?
101         flash[:notice] = "Rentable ID is invalid"
102         redirect_to :action => :rent
103         return
104       end
105
106       if @rentable.checkedout?
107         flash[:notice] = "This #{@rentable.type} is already checked out!"
108         redirect_to :action => :rent
109         return
110       end
111
112       # Check Rentable Policies
113       @maxvideos = RentablePolicy.find_by_name("MaxVideos")
114       if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
115         flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
116         redirect_to :action => :rent
117         return
118       end
119
120       @maxgames = RentablePolicy.find_by_name("MaxGames")
121       if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
122         flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
123         redirect_to :action => :rent
124         return
125       end
126
127       @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
128       if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
129         flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
130         redirect_to :action => :rent
131         return
132       end
133
134       @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
135       if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
136         flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
137         redirect_to :action => :rent
138         return
139       end
140
141       # Check out the item
142       checkout = Coitem.new
143       checkout.customer = @customer
144       checkout.rentable = @rentable
145       checkout.out_date = Time.now.to_date
146       checkout.due_date = @rentable.due_date
147       checkout.save!
148
149       # Actually record the purchase
150       purchase = RentablePurchase.new
151       purchase.customer_id = session[:customer_id]
152       purchase.date = Time.now.to_date
153       purchase.price = @rentable.calculated_price
154       purchase.rentable = @rentable
155       purchase.save!
156
157       # Add te session variables
158       session[:total] += @rentable.calculated_price
159       session[:items].push @rentable
160
161       flash[:notice] = "Successfully made purchase"
162       redirect_to :action => :menu
163     else
164       render :action => 'rent'
165     end
166   end
167
168   def buy_merch
169     if request.post?
170       @customer = Customer.find_by_id(session[:customer_id])
171       @merchandise = Merchandise.find_by_id(params[:merchandise_id])
172     
173       if @customer.nil?
174         flash[:notice] = "Customer ID is invalid"
175         redirect_to :action => :begin
176         return
177       end
178
179       if @merchandise.nil?
180         flash[:notice] = "Merchandise ID is invalid"
181         redirect_to :action => :buy_merch
182         return
183       end
184
185       if @merchandise.quantity < 1
186         flash[:notice] = "The system thinks we are out of this merchandise item!"
187         redirect_to :action => :buy_merch
188         return
189       end
190
191       # Actually record the purchase
192       purchase = MerchandisePurchase.new
193       purchase.customer = @customer
194       purchase.date = Time.now.to_date
195       purchase.price = @merchandise.price
196       purchase.merchandise = @merchandise
197       purchase.quantity = 1
198       @merchandise.quantity -= 1
199
200       # Add to session variables
201       session[:total] += @merchandise.price
202       session[:items].push @merchandise
203
204       # Save both the merchandise (we changed the quantity) and the purchase to the log
205       @merchandise.save!
206       purchase.save!
207
208       flash[:notice] = "Successfully made purchase"
209       redirect_to :action => :menu
210     else
211       render :action => 'buy_merch'
212     end
213   end
214
215 end