- def rent_begin
- render :action => 'rent_begin'
- end
-
- def rent_validate
- @customer = Customer.find_by_id(session[:customer_id])
- @rentable = Rentable.find_by_id(params[:rentable_id])
-
- if @customer.nil?
- flash[:error] = "Customer ID is invalid"
- redirect_to :action => :begin
- return
- end
-
- if @rentable.nil?
- flash[:error] = "Rentable ID is invalid"
- redirect_to :action => :rent_begin
- return
- end
-
- if @rentable.checkedout?
- flash[:error] = "This #{@rentable.type} is already checked out!"
- redirect_to :action => :rent_begin
- return
- end
-
- # Check Rentable Policies
- @maxvideos = RentablePolicy.find_by_name("MaxVideos")
- if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
- flash[:error] = "#{@maxvideos.description} LIMIT REACHED"
- redirect_to :action => :rent_begin
- return
- end
-
- @maxgames = RentablePolicy.find_by_name("MaxGames")
- if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
- flash[:error] = "#{@maxgames.description} LIMIT REACHED"
- redirect_to :action => :rent_begin
- return
+ def rent
+ if request.post?
+ @customer = Customer.find_by_id(session[:customer_id])
+ @rentable = Rentable.find_by_id(params[:rentable_id])
+
+ if @customer.nil?
+ flash[:notice] = "Customer ID is invalid"
+ redirect_to :action => :begin
+ return
+ end
+
+ if @rentable.nil?
+ flash[:notice] = "Rentable ID is invalid"
+ redirect_to :action => :rent
+ return
+ end
+
+ if @rentable.checkedout?
+ flash[:notice] = "This #{@rentable.type} is already checked out!"
+ redirect_to :action => :rent
+ return
+ end
+
+ # Check Rentable Policies
+ @maxvideos = RentablePolicy.find_by_name("MaxVideos")
+ if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
+ flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
+ redirect_to :action => :rent
+ return
+ end
+
+ @maxgames = RentablePolicy.find_by_name("MaxGames")
+ if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
+ flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
+ redirect_to :action => :rent
+ return
+ end
+
+ @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
+ if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
+ flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
+ redirect_to :action => :rent
+ return
+ end
+
+ @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
+ if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
+ flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
+ redirect_to :action => :rent
+ return
+ end
+
+ # Check out the item
+ checkout = Coitem.new
+ checkout.customer = @customer
+ checkout.rentable = @rentable
+ checkout.out_date = Time.now.to_date
+ checkout.due_date = @rentable.due_date
+ checkout.save!
+
+ # Actually record the purchase
+ purchase = RentablePurchase.new
+ purchase.customer_id = session[:customer_id][0]
+ purchase.date = Time.now.to_date
+ purchase.price = @rentable.calculated_price
+ purchase.rentable = @rentable
+ purchase.save!
+
+ # Add to session variables
+ session[:total] += @rentable.calculated_price
+ session[:items].push @rentable
+
+ flash[:notice] = "Successfully made purchase"
+ redirect_to :action => :menu
+ else
+ render :action => 'rent'