From 31c3621ad8a43715ecc02655d58b20099308fe50 Mon Sep 17 00:00:00 2001
From: "Ira W. Snyder" <devel@irasnyder.com>
Date: Mon, 26 Nov 2007 15:28:20 -0800
Subject: [PATCH] Add Income to Purchases

Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
---
 app/controllers/purchase_controller.rb  | 24 ++++++++++++++++++++++--
 app/views/purchase/income.rhtml         |  7 +++++++
 app/views/purchase/income_results.rhtml |  7 +++++++
 app/views/purchase/index.rhtml          |  1 +
 4 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 app/views/purchase/income.rhtml
 create mode 100644 app/views/purchase/income_results.rhtml

diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb
index 1c40069..6be4254 100644
--- a/app/controllers/purchase_controller.rb
+++ b/app/controllers/purchase_controller.rb
@@ -2,8 +2,8 @@ class PurchaseController < ApplicationController
   layout "admin"
 
   # Make sure that a user logs in before doing any action here
-  before_filter :authorize, :except => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
-  before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
+  before_filter :authorize, :except => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index, :income]
+  before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index, :income]
 
   def index
     render :action => 'index'
@@ -49,6 +49,26 @@ class PurchaseController < ApplicationController
     render :action => 'list'
   end
 
+  def income
+    if request.post?
+      # Find all purchases between :begin_date, and :end_date and sum up the total income
+      # from RentablePurchases, MerchandisePurchases. Print both sums, and the total sum.
+      @begin_date = Date.new params[:begin_date]['(1i)'].to_i, params[:begin_date]['(2i)'].to_i, params[:begin_date]['(3i)'].to_i
+      @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])
+
+      @merch_count = merchandises.length
+      @rent_count = rentables.length
+      @merch_sum = merchandises.sum(&:price)
+      @rent_sum = rentables.sum(&:price)
+      @total = @merch_sum + @rent_sum
+      render :action => 'income_results'
+    else
+      render :action => 'income'
+    end
+  end
+
   def begin
     # enter a customer id here
     render :action => 'begin'
diff --git a/app/views/purchase/income.rhtml b/app/views/purchase/income.rhtml
new file mode 100644
index 0000000..e906193
--- /dev/null
+++ b/app/views/purchase/income.rhtml
@@ -0,0 +1,7 @@
+<h1>Select Income Period</h1>
+
+<%= start_form_tag :action => 'income' %>
+  Start Date: <%= date_select 'begin_date', nil %><br/><br/>
+  End Date: <%= date_select 'end_date', nil %><br/><br/>
+  <%= submit_tag 'Ok' %>
+<%= end_form_tag %>
diff --git a/app/views/purchase/income_results.rhtml b/app/views/purchase/income_results.rhtml
new file mode 100644
index 0000000..eafd83f
--- /dev/null
+++ b/app/views/purchase/income_results.rhtml
@@ -0,0 +1,7 @@
+<h1>Income from <%= @begin_date %> to <%= @end_date %></h1>
+
+<p><b>Number of Merchandise Items Sold: </b><%= @merch_count %></p>
+<p><b>Income from Merchandise: </b><%= number_to_currency(@merch_sum) %></p>
+<p><b>Number of Rentals: </b><%= @rent_count %></p>
+<p><b>Income from Rentals: </b><%= number_to_currency(@rent_sum) %></p>
+<p><b>Total Income: </b><%= number_to_currency(@total) %></p>
diff --git a/app/views/purchase/index.rhtml b/app/views/purchase/index.rhtml
index 8ea7cef..a16d6a3 100644
--- a/app/views/purchase/index.rhtml
+++ b/app/views/purchase/index.rhtml
@@ -5,4 +5,5 @@
   <li><%= link_to 'List All Purchases', :action => 'list' %></li>
   <li><%= link_to 'List All Purchases by Date', :action => 'filter', :type => 'date' %></li>
   <li><%= link_to 'List All Purchases by a Customer', :action => 'filter', :type => 'customer' %></li>
+  <li><%= link_to 'Total Income by Date(s)', :action => 'income' %></li>
 </ul>
-- 
2.34.1