Add a space between the period and 'Days'
[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, :income]
6   before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index, :income]
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 income
53     if request.post?
54       # Find all purchases between :begin_date, and :end_date and sum up the total income
55       # from RentablePurchases, MerchandisePurchases. Print both sums, and the total sum.
56       @begin_date = Date.new params[:begin_date]['(1i)'].to_i, params[:begin_date]['(2i)'].to_i, params[:begin_date]['(3i)'].to_i
57       @end_date = Date.new params[:end_date]['(1i)'].to_i, params[:end_date]['(2i)'].to_i, params[:end_date]['(3i)'].to_i
58       merchandises = MerchandisePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date])
59       rentables = RentablePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date])
60
61       @merch_count = merchandises.length
62       @rent_count = rentables.length
63       @merch_sum = merchandises.sum(&:price)
64       @rent_sum = rentables.sum(&:price)
65       @total = @merch_sum + @rent_sum
66       render :action => 'income_results'
67     else
68       render :action => 'income'
69     end
70   end
71
72   def begin
73     # enter a customer id here
74     render :action => 'begin'
75     session[:total] = 0.00
76     session[:items] = []
77   end
78
79   def reciept
80     # Save important data, since we're gonna wipe it out now
81     @customer = Customer.find_by_id(session[:customer_id])
82     @debt = @customer.debt
83     @total = session[:total] + @debt
84     @items = session[:items]
85     @time = Time.now
86
87     # Set the customer's debt to $0.00, she paid us
88     @customer = Customer.find_by_id(session[:customer_id])
89     @customer.debt = 0.00
90     @customer.save
91
92     # Wipe session data
93     session[:customer_id] = nil
94     session[:total] = nil
95     session[:items] = nil
96
97     # Show the reciept
98     render :action => 'reciept'
99   end
100
101   def customer_ok
102     if Customer.find_by_id(params[:customer_id])
103       session[:customer_id] = params[:customer_id]
104       redirect_to :action => :menu
105     else
106       flash[:notice] = "Customer ID is invalid"
107       redirect_to :action => :begin
108     end
109   end
110
111   def menu
112     @customer = Customer.find_by_id(session[:customer_id])
113     @total_price = session[:total]
114     @items = session[:items]
115     render :action => 'menu'
116   end
117
118   def rent
119     if request.post?
120       @customer = Customer.find_by_id(session[:customer_id])
121       @rentable = Rentable.find_by_id(params[:rentable_id])
122
123       if @customer.nil?
124         flash[:notice] = "Customer ID is invalid"
125         redirect_to :action => :begin
126         return
127       end
128
129       if @rentable.nil?
130         flash[:notice] = "Rentable ID is invalid"
131         redirect_to :action => :rent
132         return
133       end
134
135       if @rentable.checkedout?
136         flash[:notice] = "This #{@rentable.type} is already checked out!"
137         redirect_to :action => :rent
138         return
139       end
140
141       # Check Rentable Policies
142       @maxvideos = RentablePolicy.find_by_name("MaxVideos")
143       if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
144         flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
145         redirect_to :action => :rent
146         return
147       end
148
149       @maxgames = RentablePolicy.find_by_name("MaxGames")
150       if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
151         flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
152         redirect_to :action => :rent
153         return
154       end
155
156       @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
157       if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
158         flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
159         redirect_to :action => :rent
160         return
161       end
162
163       @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
164       if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
165         flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
166         redirect_to :action => :rent
167         return
168       end
169
170       # Check out the item
171       checkout = Coitem.new
172       checkout.customer = @customer
173       checkout.rentable = @rentable
174       checkout.out_date = Time.now.to_date
175       checkout.due_date = @rentable.due_date
176       checkout.save!
177
178       # Actually record the purchase
179       purchase = RentablePurchase.new
180       purchase.customer_id = session[:customer_id][0]
181       purchase.date = Time.now.to_date
182       purchase.price = @rentable.calculated_price
183       purchase.rentable = @rentable
184       purchase.save!
185
186       # Add to session variables
187       session[:total] += @rentable.calculated_price
188       session[:items].push @rentable
189
190       flash[:notice] = "Successfully made purchase"
191       redirect_to :action => :menu
192     else
193       render :action => 'rent'
194     end
195   end
196
197   def buy_merch
198     if request.post?
199       @customer = Customer.find_by_id(session[:customer_id])
200       @merchandise = Merchandise.find_by_id(params[:merchandise_id])
201     
202       if @customer.nil?
203         flash[:notice] = "Customer ID is invalid"
204         redirect_to :action => :begin
205         return
206       end
207
208       if @merchandise.nil?
209         flash[:notice] = "Merchandise ID is invalid"
210         redirect_to :action => :buy_merch
211         return
212       end
213
214       if @merchandise.quantity < 1
215         flash[:notice] = "The system thinks we are out of this merchandise item!"
216         redirect_to :action => :buy_merch
217         return
218       end
219
220       # Actually record the purchase
221       purchase = MerchandisePurchase.new
222       purchase.customer = @customer
223       purchase.date = Time.now.to_date
224       purchase.price = @merchandise.price
225       purchase.merchandise = @merchandise
226       purchase.quantity = 1
227       @merchandise.quantity -= 1
228
229       # Add to session variables
230       session[:total] += @merchandise.price
231       session[:items].push @merchandise
232
233       # Save both the merchandise (we changed the quantity) and the purchase to the log
234       @merchandise.save!
235       purchase.save!
236
237       flash[:notice] = "Successfully made purchase"
238       redirect_to :action => :menu
239     else
240       render :action => 'buy_merch'
241     end
242   end
243
244 end