Prettify the Purchase system
authorIra W. Snyder <devel@irasnyder.com>
Mon, 26 Nov 2007 01:29:11 +0000 (17:29 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Mon, 26 Nov 2007 01:29:11 +0000 (17:29 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
12 files changed:
app/controllers/purchase_controller.rb
app/models/purchase.rb
app/views/layouts/admin.rhtml
app/views/layouts/purchase.rhtml
app/views/purchase/buy.rhtml [deleted file]
app/views/purchase/buy_merch.rhtml [moved from app/views/purchase/buy_begin.rhtml with 82% similarity]
app/views/purchase/filter.rhtml [new file with mode: 0644]
app/views/purchase/index.rhtml [new file with mode: 0644]
app/views/purchase/list.rhtml
app/views/purchase/menu.rhtml
app/views/purchase/rent.rhtml
db/development.sqlite3

index 2ac4dae..0f7e22f 100644 (file)
@@ -1,14 +1,52 @@
 class PurchaseController < ApplicationController
+  layout "admin"
 
   # Make sure that a user logs in before doing any action here
-  before_filter :authorize
+  before_filter :authorize, :except => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
+  before_filter :manager, :only => [:filter, :filterbycust, :filterbydate, :filterbytype, :list, :index]
 
   def index
-    redirect_to :action => :begin
+    render :action => 'index'
   end
 
   def list
-    @purchase_pages, @purchase = paginate :purchases, :per_page => 100
+    @purchase_pages, @purchases = paginate :purchases, :per_page => 100
+  end
+
+  def filter
+    if request.post?
+      type = params[:type]
+      id = params[:id]
+
+      case type
+        when :customer, "customer"
+          redirect_to :action => 'filterbycust', :id => id
+        when :date, "date"
+          date = Date.new id['(1i)'].to_i, id['(2i)'].to_i, id['(3i)'].to_i
+          redirect_to :action => 'filterbydate', :id => date.to_s
+      end
+    else
+      @type = params[:type]
+      if @type.nil?
+        @type = "all"
+      end
+      render :action => 'filter'
+    end
+  end
+
+  def filterbycust
+    @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["customer_id = ?", params[:id]]
+    render :action => 'list'
+  end
+
+  def filterbydate
+    @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["date = ?", params[:id]]
+    render :action => 'list'
+  end
+
+  def filterbytype
+    @purchase_pages, @purchases = paginate :purchases, :per_page => 100, :conditions => ["type = ?", params[:id]]
+    render :action => 'list'
   end
 
   def begin
@@ -18,12 +56,25 @@ class PurchaseController < ApplicationController
     session[:items] = []
   end
 
+  def success_end
+    # Set the customer's debt to $0.00. They MUST pay you before
+    # checking anything else out, of course
+    @customer = Customer.find_by_id(session[:customer_id])
+    @customer.debt = 0.00
+    @customer.save
+
+    session[:customer_id] = nil
+    session[:total] = nil
+    session[:items] = nil
+    redirect_to :action => :begin
+  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"
+      flash[:notice] = "Customer ID is invalid"
       redirect_to :action => :begin
     end
   end
@@ -35,129 +86,130 @@ class PurchaseController < ApplicationController
     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
-
-    if @rentable.checkedout?
-      flash[:error] = "This #{@rentable.type} is already checked out!"
-      redirect_to :action => :rent_begin
-      return
-    end
-
-    # Check Rentable Policies
-    @maxvideos = RentablePolicy.find_by_name("MaxVideos")
-    if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
-      flash[:error] = "#{@maxvideos.description} LIMIT REACHED"
-      redirect_to :action => :rent_begin
-      return
-    end
-
-    @maxgames = RentablePolicy.find_by_name("MaxGames")
-    if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
-      flash[:error] = "#{@maxgames.description} LIMIT REACHED"
-      redirect_to :action => :rent_begin
-      return
-    end
-
-    @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
-    if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
-      flash[:error] = "#{@maxoverduevideos.description} LIMIT REACHED"
-      redirect_to :action => :rent_begin
-      return
-    end
-
-    @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
-    if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
-      flash[:error] = "#{@maxoverduegames.description} LIMIT REACHED"
-      redirect_to :action => :rent_begin
-      return
+  def rent
+    if request.post?
+      @customer = Customer.find_by_id(session[:customer_id])
+      @rentable = Rentable.find_by_id(params[:rentable_id])
+
+      if @customer.nil?
+        flash[:notice] = "Customer ID is invalid"
+        redirect_to :action => :begin
+        return
+      end
+
+      if @rentable.nil?
+        flash[:notice] = "Rentable ID is invalid"
+        redirect_to :action => :rent
+        return
+      end
+
+      if @rentable.checkedout?
+        flash[:notice] = "This #{@rentable.type} is already checked out!"
+        redirect_to :action => :rent
+        return
+      end
+
+      # Check Rentable Policies
+      @maxvideos = RentablePolicy.find_by_name("MaxVideos")
+      if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
+        flash[:notice] = "#{@maxvideos.description} LIMIT REACHED"
+        redirect_to :action => :rent
+        return
+      end
+
+      @maxgames = RentablePolicy.find_by_name("MaxGames")
+      if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
+        flash[:notice] = "#{@maxgames.description} LIMIT REACHED"
+        redirect_to :action => :rent
+        return
+      end
+
+      @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
+      if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
+        flash[:notice] = "#{@maxoverduevideos.description} LIMIT REACHED"
+        redirect_to :action => :rent
+        return
+      end
+
+      @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
+      if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
+        flash[:notice] = "#{@maxoverduegames.description} LIMIT REACHED"
+        redirect_to :action => :rent
+        return
+      end
+
+      # 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!
+
+      # Actually record the purchase
+      purchase = RentablePurchase.new
+      purchase.customer_id = session[:customer_id]
+      purchase.date = Time.now.to_date
+      purchase.price = @rentable.calculated_price
+      purchase.rentable = @rentable
+      purchase.save!
+
+      # Add te session variables
+      session[:total] += @rentable.calculated_price
+      session[:items].push @rentable
+
+      flash[:notice] = "Successfully made purchase"
+      redirect_to :action => :menu
+    else
+      render :action => 'rent'
     end
-
-    # 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!
-
-    # Actually record the purchase
-    purchase = RentablePurchase.new
-    purchase.customer_id = session[:customer_id]
-    purchase.date = Time.now.to_date
-    purchase.price = @rentable.calculated_price
-    purchase.rentable = @rentable
-    purchase.save!
-
-    # Add te session variables
-    session[:total] += @rentable.calculated_price
-    session[:items].push @rentable
-
-    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
+  def buy_merch
+    if request.post?
+      @customer = Customer.find_by_id(session[:customer_id])
+      @merchandise = Merchandise.find_by_id(params[:merchandise_id])
+    
+      if @customer.nil?
+        flash[:notice] = "Customer ID is invalid"
+        redirect_to :action => :begin
+        return
+      end
+
+      if @merchandise.nil?
+        flash[:notice] = "Merchandise ID is invalid"
+        redirect_to :action => :buy_merch
+        return
+      end
+
+      if @merchandise.quantity < 1
+        flash[:notice] = "The system thinks we are out of this merchandise item!"
+        redirect_to :action => :buy_merch
+        return
+      end
+
+      # Actually record the purchase
+      purchase = MerchandisePurchase.new
+      purchase.customer = @customer
+      purchase.date = Time.now.to_date
+      purchase.price = @merchandise.price
+      purchase.merchandise = @merchandise
+      purchase.quantity = 1
+      @merchandise.quantity -= 1
+
+      # Add to session variables
+      session[:total] += @merchandise.price
+      session[:items].push @merchandise
+
+      # 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
+    else
+      render :action => 'buy_merch'
     end
-
-    # Actually record the purchase
-    purchase = MerchandisePurchase.new
-    purchase.customer_id = session[:customer_id]
-    purchase.date = Time.now.to_date
-    purchase.price = @merchandise.price
-    purchase.merchandise = @merchandise
-    purchase.quantity = 1
-    @merchandise.quantity -= 1
-
-    # Add to session variables
-    session[:total] += @merchandise.price
-    session[:items].push @merchandise
-
-    # 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
index ff07e41..26cf518 100644 (file)
@@ -6,6 +6,14 @@ class Purchase < ActiveRecord::Base
   validates_presence_of :price
   validates_numericality_of :price
 
+  def title
+    if type == MerchandisePurchase
+      return merchandise.title
+    else
+      return rentable.title
+    end
+  end
+
   protected
   def validate
     errors.add(:price, "cannot be negative") if price < 0
index a4ad91a..cb8f1e2 100644 (file)
@@ -20,6 +20,7 @@
       <p><%= link_to "Game Maintenence", :controller => 'game', :action => 'index' %></p>
       <p><%= link_to "Checked Out Items", :controller => 'coitem', :action => 'index' %></p>
       <p><%= link_to "Customer Maintenence", :controller => 'customer', :action => 'index' %></p>
+      <p><%= link_to "Financial Information", :controller => 'purchase', :action => 'index' %></p>
       <br/>
       <p><%= link_to "Logout", :controller => 'login', :action => 'logout' %></p>
     </div>
index 0423231..e9350f5 100644 (file)
@@ -6,7 +6,6 @@
   <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
   <title>Purchase: <%= controller.action_name %></title>
   <%= stylesheet_link_tag 'scaffold' %>
-  <%= stylesheet_link_tag 'videostore', :media => "all" %>
 </head>
 <body>
 
diff --git a/app/views/purchase/buy.rhtml b/app/views/purchase/buy.rhtml
deleted file mode 100644 (file)
index 61c6d0f..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-<h1>Purchase#buy</h1>
-<p>Find me in app/views/purchase/buy.rhtml</p>
similarity index 82%
rename from app/views/purchase/buy_begin.rhtml
rename to app/views/purchase/buy_merch.rhtml
index 7ea2912..de2509e 100644 (file)
@@ -3,7 +3,7 @@
 <p>Please read the item's ID number off of the bar code, and type it into
 the box below.</p>
 
-<%= start_form_tag :action => 'buy_validate'%>
+<%= start_form_tag :action => 'buy_merch'%>
 <%= text_field 'merchandise_id', nil %>
   <%= submit_tag 'Ok' %></form>
 <%= end_form_tag %>
diff --git a/app/views/purchase/filter.rhtml b/app/views/purchase/filter.rhtml
new file mode 100644 (file)
index 0000000..3e5be12
--- /dev/null
@@ -0,0 +1,16 @@
+<h1>Filter Purchases</h1>
+
+<% if @type == "customer" or @type == "all" %>
+  <%= start_form_tag :action => 'filter', :type => :customer %>
+    Customer ID: <%= text_field 'id', nil  %>
+    <%= submit_tag 'Ok' %>
+  <%= end_form_tag %>
+<% end %>
+
+<% if @type == "date" or @type == "all" %>
+  <%= start_form_tag :action => 'filter', :type => :date %>
+    Date: <%= date_select 'id', nil %>
+    <%= submit_tag 'Ok' %>
+  <%= end_form_tag %>
+<% end %>
+
diff --git a/app/views/purchase/index.rhtml b/app/views/purchase/index.rhtml
new file mode 100644 (file)
index 0000000..8ea7cef
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>Purchase Information</h1>
+
+<h3>Actions</h3>
+<ul>
+  <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>
+</ul>
index 593b7a6..4c6e1ec 100644 (file)
@@ -1,20 +1,23 @@
 <h1>Listing purchases</h1>
 
-<table>
+<table border="1">
   <tr>
-  <% for column in Purchase.content_columns %>
-    <th><%= column.human_name %></th>
-  <% end %>
+    <th>Date</th>
+    <th>Quantity</th>
+    <th>Type</th>
+    <th>Title</th>
+    <th>Price</th>
+    <th>Customer</th>
   </tr>
   
 <% for purchase in @purchases %>
   <tr>
-  <% for column in Purchase.content_columns %>
-    <td><%=h purchase.send(column.name) %></td>
-  <% end %>
-    <td><%= link_to 'Show', :action => 'show', :id => purchase %></td>
-    <td><%= link_to 'Edit', :action => 'edit', :id => purchase %></td>
-    <td><%= link_to 'Destroy', { :action => 'destroy', :id => purchase }, :confirm => 'Are you sure?', :method => :post %></td>
+    <td><%=link_to purchase.date, :action => 'filterbydate', :id => purchase.date %></td>
+    <td><%=h purchase.quantity %></td>
+    <td><%=link_to purchase.type, :action => 'filterbytype', :id => purchase.type %></td>
+    <td><%=h purchase.title %></td>
+    <td><%=h purchase.price %></td>
+    <td><%=link_to purchase.customer.name, :action => 'filterbycust', :id => purchase.customer_id %></td>
   </tr>
 <% end %>
 </table>
@@ -22,6 +25,3 @@
 <%= link_to 'Previous page', { :page => @purchase_pages.current.previous } if @purchase_pages.current.previous %>
 <%= link_to 'Next page', { :page => @purchase_pages.current.next } if @purchase_pages.current.next %> 
 
-<br />
-
-<%= link_to 'New purchase', :action => 'new' %>
index 6f5abd1..e2e01b5 100644 (file)
@@ -4,7 +4,9 @@
 <ul>
   <li>Customer ID: <%= @customer.id %></li>
   <li>Customer Name: <%= @customer.name %></li>
-  <li>Total Price: <%= @total_price %></li>
+  <li>Customer Debt: <%= number_to_currency(@customer.debt) %></li>
+  <li>Price for this transaction: <%= number_to_currency(@total_price) %></li>
+  <li>Total Amount Owed: <%= number_to_currency(@customer.debt + @total_price) %></li>
 </ul>
 
 <h3>Items Purchased</h3>
 </ul>
 <% end %>
 
+<h3>Actions</h3>
 <ul>
-  <li><%=link_to 'Rent an Item', :action => 'rent_begin' %></li>
-  <li><%=link_to 'Buy Merchandise', :action => 'buy_begin' %></li>
+  <li><%=link_to 'Rent an Item', :action => 'rent' %></li>
+  <li><%=link_to 'Buy Merchandise', :action => 'buy_merch' %></li>
+  <br/>
+  <br/>
+  <br/>
+  <li><%=link_to 'End Purchase', :action => 'success_end' %></li>
 </ul>
 
-<%=link_to 'End Purchase', :action => 'index' %>
index 076d2be..07b2973 100644 (file)
@@ -1,2 +1,10 @@
-<h1>Purchase#rent</h1>
-<p>Find me in app/views/purchase/rent.rhtml</p>
+<h1>Check out a Rentable Item</h1>
+
+<p>Please read the item's ID number off of the bar code, and type it into
+the box below.</p>
+
+<%= start_form_tag :action => 'rent'%>
+<%= text_field 'rentable_id', nil %>
+  <%= submit_tag 'Ok' %></form>
+<%= end_form_tag %>
+
index 5dff978..0a336e5 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ