Add Income to Purchases
[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 success_end
80     # Set the customer's debt to $0.00. They MUST pay you before
81     # checking anything else out, of course
82     @customer = Customer.find_by_id(session[:customer_id])
83     @customer.debt = 0.00
84     @customer.save
85
86     session[:customer_id] = nil
87     session[:total] = nil
88     session[:items] = nil
89     redirect_to :action => :begin
90   end
91
92   def customer_ok
93     if Customer.find_by_id(params[:customer_id])
94       session[:customer_id] = params[:customer_id]
95       redirect_to :action => :menu
96     else
97       flash[:notice] = "Customer ID is invalid"
98       redirect_to :action => :begin
99     end
100   end
101
102   def menu
103     @customer = Customer.find_by_id(session[:customer_id])
104     @total_price = session[:total]
105     @items = session[:items]
106     render :action => 'menu'
107   end
108
109   def rent
110     if request.post?
111       @customer = Customer.find_by_id(session[:customer_id])
112       @rentable = Rentable.find_by_id(params[:rentable_id])
113
114       if @customer.nil?
115         flash[:notice] = "Customer ID is invalid"
116         redirect_to :action => :begin
117         return
118       end
119
120       if @rentable.nil?
121         flash[:notice] = "Rentable ID is invalid"
122         redirect_to :action => :rent
123         return
124       end
125
126       if @rentable.checkedout?
127         flash[:notice] = "This #{@rentable.type} is already checked out!"
128         redirect_to :action => :rent
129         return
130       end
131
132       # Check Rentable Policies
133       @maxvideos = RentablePolicy.find_by_name("MaxVideos")
134       if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
135         flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
136         redirect_to :action => :rent
137         return
138       end
139
140       @maxgames = RentablePolicy.find_by_name("MaxGames")
141       if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
142         flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
143         redirect_to :action => :rent
144         return
145       end
146
147       @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
148       if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
149         flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
150         redirect_to :action => :rent
151         return
152       end
153
154       @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
155       if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
156         flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
157         redirect_to :action => :rent
158         return
159       end
160
161       # Check out the item
162       checkout = Coitem.new
163       checkout.customer = @customer
164       checkout.rentable = @rentable
165       checkout.out_date = Time.now.to_date
166       checkout.due_date = @rentable.due_date
167       checkout.save!
168
169       # Actually record the purchase
170       purchase = RentablePurchase.new
171       purchase.customer_id = session[:customer_id][0]
172       purchase.date = Time.now.to_date
173       purchase.price = @rentable.calculated_price
174       purchase.rentable = @rentable
175       purchase.save!
176
177       # Add te session variables
178       session[:total] += @rentable.calculated_price
179       session[:items].push @rentable
180
181       flash[:notice] = "Successfully made purchase"
182       redirect_to :action => :menu
183     else
184       render :action => 'rent'
185     end
186   end
187
188   def buy_merch
189     if request.post?
190       @customer = Customer.find_by_id(session[:customer_id])
191       @merchandise = Merchandise.find_by_id(params[:merchandise_id])
192     
193       if @customer.nil?
194         flash[:notice] = "Customer ID is invalid"
195         redirect_to :action => :begin
196         return
197       end
198
199       if @merchandise.nil?
200         flash[:notice] = "Merchandise ID is invalid"
201         redirect_to :action => :buy_merch
202         return
203       end
204
205       if @merchandise.quantity < 1
206         flash[:notice] = "The system thinks we are out of this merchandise item!"
207         redirect_to :action => :buy_merch
208         return
209       end
210
211       # Actually record the purchase
212       purchase = MerchandisePurchase.new
213       purchase.customer = @customer
214       purchase.date = Time.now.to_date
215       purchase.price = @merchandise.price
216       purchase.merchandise = @merchandise
217       purchase.quantity = 1
218       @merchandise.quantity -= 1
219
220       # Add to session variables
221       session[:total] += @merchandise.price
222       session[:items].push @merchandise
223
224       # Save both the merchandise (we changed the quantity) and the purchase to the log
225       @merchandise.save!
226       purchase.save!
227
228       flash[:notice] = "Successfully made purchase"
229       redirect_to :action => :menu
230     else
231       render :action => 'buy_merch'
232     end
233   end
234
235 end