Add Purchase system
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb
new file mode 100644 (file)
index 0000000..1516498
--- /dev/null
@@ -0,0 +1,109 @@
+class PurchaseController < ApplicationController
+
+  def index
+    redirect_to :action => :begin
+  end
+
+  def list
+    @purchase_pages, @purchase = paginate :purchases, :per_page => 100
+  end
+
+  def begin
+    # enter a customer id here
+    render :action => 'begin'
+    session[:total] = 0.00
+  end
+
+  def customer_ok
+    if Customer.find_by_id(params[:customer_id])
+      session[:customer_id] = params[:customer_id]
+      redirect_to :action => :menu
+    else
+      flash[:error] = "Customer ID is invalid"
+      redirect_to :action => :begin
+    end
+  end
+
+  def menu
+    @customer = Customer.find_by_id(session[:customer_id])
+    @total_price = session[:total]
+    render :action => 'menu'
+  end
+
+  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
+
+    # 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!
+
+    flash[:notice] = "Successfully made purchase"
+    redirect_to :action => :menu
+  end
+
+  def buy_begin
+    render :action => 'buy_begin'
+  end
+
+  def buy_validate
+    @customer = Customer.find_by_id(session[:customer_id])
+    @merchandise = Merchandise.find_by_id(params[:merchandise_id])
+  
+    if @customer.nil?
+      flash[:error] = "Customer ID is invalid"
+      redirect_to :action => :begin
+      return
+    end
+
+    if @merchandise.nil?
+      flash[:error] = "Merchandise ID is invalid"
+      redirect_to :action => :buy_begin
+      return
+    end
+
+    if @merchandise.quantity < 1
+      flash[:error] = "The system thinks we are out of this merchandise item!"
+      redirect_to :action => :buy_begin
+      return
+    end
+
+    # Actually record the purchase
+    purchase = MerchandisePurchase.new
+    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
+
+    # Save both the merchandise (we changed the quantity) and the purchase to the log
+    @merchandise.save!
+    purchase.save!
+
+    flash[:notice] = "Successfully made purchase"
+    redirect_to :action => :menu
+  end
+end