Lots of stuff, I got too tired to keep perfect revision history
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
index a8bfa8f..37b7401 100644 (file)
@@ -77,18 +77,20 @@ class PurchaseController < ApplicationController
     render :action => 'begin'
     session[:total] = 0.00
     session[:items] = []
+    session[:bonus] = nil
   end
 
-  def reciept
+  def receipt
     # Save important data, since we're gonna wipe it out now
     @customer = Customer.find_by_id(session[:customer_id])
     @debt = @customer.debt
     @total = session[:total] + @debt
     @items = session[:items]
+    @bonus = session[:bonus]
     @time = Time.now
 
     # Record a Late Fee Payment if we need to
-    if @debt
+    if not @debt.zero?
       purchase = LateFeePurchase.new
       purchase.customer = @customer
       purchase.date = Time.now.to_date
@@ -105,9 +107,10 @@ class PurchaseController < ApplicationController
     session[:customer_id] = nil
     session[:total] = nil
     session[:items] = nil
+    session[:bonus] = nil
 
-    # Show the reciept
-    render :action => 'reciept'
+    # Show the receipt
+    render :action => 'receipt'
   end
 
   def customer_ok
@@ -179,6 +182,51 @@ class PurchaseController < ApplicationController
         return
       end
 
+      # Check for a Bonus
+      for bonus in BonusPolicy.find_all_by_bonus_type(@rentable.class.to_s)
+        # Find the earliest the period for this bonus extends
+        since_date = Time.now.advance(:days => (-1 * bonus.days)).to_date
+
+        # Find the date of the latest bonus in this period
+        last_bonus_date = BonusPurchase.last_bonus_date(@customer, @rentable.class, since_date)
+
+        # Find the number of rentals of this type in the period since the last bonus
+        count = 0
+        for rental in RentablePurchase.find_all_by_customer_id(@customer, :conditions => ["date > ?", last_bonus_date])
+          if rental.rentable.class == @rentable.class
+            count += 1
+          end
+        end
+
+        # We are eligible for a bonus!
+        if count >= bonus.number
+
+          # 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!
+
+          # Record the BonusPurchase
+          purchase = BonusPurchase.new
+          purchase.customer = @customer
+          purchase.date = Time.now.to_date
+          purchase.price = @rentable.calculated_price
+          purchase.rentable = @rentable
+          purchase.save!
+
+          # Add to session variables
+          session[:items].push @rentable
+          session[:bonus] = @rentable
+
+          flash[:notice] = "Successfully made bonus purchase"
+          redirect_to :action => :menu
+          return
+        end
+      end
+      
       # Check out the item
       checkout = Coitem.new
       checkout.customer = @customer