Add RentablePolicy, to get set up for adding policies
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
index 1516498..704e38b 100644 (file)
@@ -12,6 +12,7 @@ class PurchaseController < ApplicationController
     # enter a customer id here
     render :action => 'begin'
     session[:total] = 0.00
+    session[:items] = []
   end
 
   def customer_ok
@@ -27,6 +28,7 @@ class PurchaseController < ApplicationController
   def menu
     @customer = Customer.find_by_id(session[:customer_id])
     @total_price = session[:total]
+    @items = session[:items]
     render :action => 'menu'
   end
 
@@ -50,15 +52,32 @@ class PurchaseController < ApplicationController
       return
     end
 
+    if @rentable.checkedout?
+      flash[:error] = "This #{@rentable.type} is already checked out!"
+      redirect_to :action => :rent_begin
+      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]
     purchase.date = Time.now.to_date
     purchase.price = @rentable.calculated_price
-    session[:total] += @rentable.calculated_price
     purchase.rentable = @rentable
     purchase.save!
 
+    # Add te session variables
+    session[:total] += @rentable.calculated_price
+    session[:items].push @rentable
+
     flash[:notice] = "Successfully made purchase"
     redirect_to :action => :menu
   end
@@ -94,11 +113,14 @@ class PurchaseController < ApplicationController
     purchase.customer_id = session[:customer_id]
     purchase.date = Time.now.to_date
     purchase.price = @merchandise.price
-    session[:total] += @merchandise.price
     purchase.merchandise = @merchandise
     purchase.quantity = 1
     @merchandise.quantity -= 1
 
+    # Add to session variables
+    session[:total] += @merchandise.price
+    session[:items].push @merchandise
+
     # Save both the merchandise (we changed the quantity) and the purchase to the log
     @merchandise.save!
     purchase.save!