Add Income tracking to the Purchase System
authorIra W. Snyder <devel@irasnyder.com>
Tue, 27 Nov 2007 01:08:22 +0000 (17:08 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Tue, 27 Nov 2007 01:08:22 +0000 (17:08 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
app/controllers/purchase_controller.rb
app/models/late_fee_purchase.rb [new file with mode: 0644]
app/models/purchase.rb
app/views/purchase/income_results.rhtml
app/views/purchase/list.rhtml
db/development.sqlite3

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
diff --git a/app/models/late_fee_purchase.rb b/app/models/late_fee_purchase.rb
new file mode 100644 (file)
index 0000000..e427846
--- /dev/null
@@ -0,0 +1,4 @@
+class LateFeePurchase < Purchase
+  belongs_to :customer
+
+end
index 26cf518..b596aa2 100644 (file)
@@ -9,8 +9,10 @@ class Purchase < ActiveRecord::Base
   def title
     if type == MerchandisePurchase
       return merchandise.title
-    else
+    elsif type == RentablePurchase
       return rentable.title
+    else
+      return 'Late Fees'
     end
   end
 
index eafd83f..6c10b8c 100644 (file)
@@ -2,6 +2,11 @@
 
 <p><b>Number of Merchandise Items Sold: </b><%= @merch_count %></p>
 <p><b>Income from Merchandise: </b><%= number_to_currency(@merch_sum) %></p>
+<br/>
 <p><b>Number of Rentals: </b><%= @rent_count %></p>
 <p><b>Income from Rentals: </b><%= number_to_currency(@rent_sum) %></p>
+<br/>
+<p><b>Number of Late Fee Payments: </b><%= @late_count %></p>
+<p><b>Income from Late Fee Payments: </b><%= number_to_currency(@late_sum) %></p>
+<br/>
 <p><b>Total Income: </b><%= number_to_currency(@total) %></p>
index 6887d13..504bc61 100644 (file)
@@ -14,7 +14,7 @@
     <td><%=link_to purchase.date, :action => 'filterbydate', :id => purchase.date %></td>
     <td><%=link_to purchase.class, :action => 'filterbytype', :id => purchase.type %></td>
     <td><%=h purchase.title %></td>
-    <td><%=h purchase.price %></td>
+    <td><%=h number_to_currency(purchase.price) %></td>
     <td><%=link_to purchase.customer.name, :action => 'filterbycust', :id => purchase.customer_id %></td>
   </tr>
 <% end %>
index 74a0b45..694afec 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ