Add Income tracking to the Purchase System
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
index 4492862..a8bfa8f 100644 (file)
@@ -57,12 +57,15 @@ class PurchaseController < ApplicationController
       @end_date = Date.new params[:end_date]['(1i)'].to_i, params[:end_date]['(2i)'].to_i, params[:end_date]['(3i)'].to_i
       merchandises = MerchandisePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date])
       rentables = RentablePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date])
+      late_fees = LateFeePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date])
 
       @merch_count = merchandises.length
       @rent_count = rentables.length
+      @late_count = late_fees.length
       @merch_sum = merchandises.sum(&:price)
       @rent_sum = rentables.sum(&:price)
-      @total = @merch_sum + @rent_sum
+      @late_sum = late_fees.sum(&:price)
+      @total = @merch_sum + @rent_sum + @late_sum
       render :action => 'income_results'
     else
       render :action => 'income'
@@ -84,6 +87,15 @@ class PurchaseController < ApplicationController
     @items = session[:items]
     @time = Time.now
 
+    # Record a Late Fee Payment if we need to
+    if @debt
+      purchase = LateFeePurchase.new
+      purchase.customer = @customer
+      purchase.date = Time.now.to_date
+      purchase.price = @debt
+      purchase.save
+    end
+
     # Set the customer's debt to $0.00, she paid us
     @customer = Customer.find_by_id(session[:customer_id])
     @customer.debt = 0.00
@@ -177,7 +189,7 @@ class PurchaseController < ApplicationController
 
       # Actually record the purchase
       purchase = RentablePurchase.new
-      purchase.customer_id = session[:customer_id][0]
+      purchase.customer = @customer
       purchase.date = Time.now.to_date
       purchase.price = @rentable.calculated_price
       purchase.rentable = @rentable