From b12881a928516bca02b2bd8ce1bbe9288840c76f Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Sun, 25 Nov 2007 17:29:11 -0800 Subject: [PATCH] Prettify the Purchase system Signed-off-by: Ira W. Snyder --- app/controllers/purchase_controller.rb | 300 ++++++++++-------- app/models/purchase.rb | 8 + app/views/layouts/admin.rhtml | 1 + app/views/layouts/purchase.rhtml | 1 - app/views/purchase/buy.rhtml | 2 - .../{buy_begin.rhtml => buy_merch.rhtml} | 2 +- app/views/purchase/filter.rhtml | 16 + app/views/purchase/index.rhtml | 8 + app/views/purchase/list.rhtml | 26 +- app/views/purchase/menu.rhtml | 14 +- app/views/purchase/rent.rhtml | 12 +- db/development.sqlite3 | Bin 24576 -> 25600 bytes 12 files changed, 243 insertions(+), 147 deletions(-) delete mode 100644 app/views/purchase/buy.rhtml rename app/views/purchase/{buy_begin.rhtml => buy_merch.rhtml} (82%) create mode 100644 app/views/purchase/filter.rhtml create mode 100644 app/views/purchase/index.rhtml diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb index 2ac4dae..0f7e22f 100644 --- a/app/controllers/purchase_controller.rb +++ b/app/controllers/purchase_controller.rb @@ -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 diff --git a/app/models/purchase.rb b/app/models/purchase.rb index ff07e41..26cf518 100644 --- a/app/models/purchase.rb +++ b/app/models/purchase.rb @@ -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 diff --git a/app/views/layouts/admin.rhtml b/app/views/layouts/admin.rhtml index a4ad91a..cb8f1e2 100644 --- a/app/views/layouts/admin.rhtml +++ b/app/views/layouts/admin.rhtml @@ -20,6 +20,7 @@

<%= link_to "Game Maintenence", :controller => 'game', :action => 'index' %>

<%= link_to "Checked Out Items", :controller => 'coitem', :action => 'index' %>

<%= link_to "Customer Maintenence", :controller => 'customer', :action => 'index' %>

+

<%= link_to "Financial Information", :controller => 'purchase', :action => 'index' %>


<%= link_to "Logout", :controller => 'login', :action => 'logout' %>

diff --git a/app/views/layouts/purchase.rhtml b/app/views/layouts/purchase.rhtml index 0423231..e9350f5 100644 --- a/app/views/layouts/purchase.rhtml +++ b/app/views/layouts/purchase.rhtml @@ -6,7 +6,6 @@ Purchase: <%= controller.action_name %> <%= stylesheet_link_tag 'scaffold' %> - <%= stylesheet_link_tag 'videostore', :media => "all" %> diff --git a/app/views/purchase/buy.rhtml b/app/views/purchase/buy.rhtml deleted file mode 100644 index 61c6d0f..0000000 --- a/app/views/purchase/buy.rhtml +++ /dev/null @@ -1,2 +0,0 @@ -

Purchase#buy

-

Find me in app/views/purchase/buy.rhtml

diff --git a/app/views/purchase/buy_begin.rhtml b/app/views/purchase/buy_merch.rhtml similarity index 82% rename from app/views/purchase/buy_begin.rhtml rename to app/views/purchase/buy_merch.rhtml index 7ea2912..de2509e 100644 --- a/app/views/purchase/buy_begin.rhtml +++ b/app/views/purchase/buy_merch.rhtml @@ -3,7 +3,7 @@

Please read the item's ID number off of the bar code, and type it into the box below.

-<%= start_form_tag :action => 'buy_validate'%> +<%= start_form_tag :action => 'buy_merch'%> <%= text_field 'merchandise_id', nil %> <%= submit_tag 'Ok' %> <%= end_form_tag %> diff --git a/app/views/purchase/filter.rhtml b/app/views/purchase/filter.rhtml new file mode 100644 index 0000000..3e5be12 --- /dev/null +++ b/app/views/purchase/filter.rhtml @@ -0,0 +1,16 @@ +

Filter Purchases

+ +<% 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 index 0000000..8ea7cef --- /dev/null +++ b/app/views/purchase/index.rhtml @@ -0,0 +1,8 @@ +

Purchase Information

+ +

Actions

+ diff --git a/app/views/purchase/list.rhtml b/app/views/purchase/list.rhtml index 593b7a6..4c6e1ec 100644 --- a/app/views/purchase/list.rhtml +++ b/app/views/purchase/list.rhtml @@ -1,20 +1,23 @@

Listing purchases

- +
- <% for column in Purchase.content_columns %> - - <% end %> + + + + + + <% for purchase in @purchases %> - <% for column in Purchase.content_columns %> - - <% end %> - - - + + + + + + <% end %>
<%= column.human_name %>DateQuantityTypeTitlePriceCustomer
<%=h purchase.send(column.name) %><%= link_to 'Show', :action => 'show', :id => purchase %><%= link_to 'Edit', :action => 'edit', :id => purchase %><%= link_to 'Destroy', { :action => 'destroy', :id => purchase }, :confirm => 'Are you sure?', :method => :post %><%=link_to purchase.date, :action => 'filterbydate', :id => purchase.date %><%=h purchase.quantity %><%=link_to purchase.type, :action => 'filterbytype', :id => purchase.type %><%=h purchase.title %><%=h purchase.price %><%=link_to purchase.customer.name, :action => 'filterbycust', :id => purchase.customer_id %>
@@ -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 %> -
- -<%= link_to 'New purchase', :action => 'new' %> diff --git a/app/views/purchase/menu.rhtml b/app/views/purchase/menu.rhtml index 6f5abd1..e2e01b5 100644 --- a/app/views/purchase/menu.rhtml +++ b/app/views/purchase/menu.rhtml @@ -4,7 +4,9 @@

Items Purchased

@@ -18,9 +20,13 @@ <% end %> +

Actions

-<%=link_to 'End Purchase', :action => 'index' %> diff --git a/app/views/purchase/rent.rhtml b/app/views/purchase/rent.rhtml index 076d2be..07b2973 100644 --- a/app/views/purchase/rent.rhtml +++ b/app/views/purchase/rent.rhtml @@ -1,2 +1,10 @@ -

Purchase#rent

-

Find me in app/views/purchase/rent.rhtml

+

Check out a Rentable Item

+ +

Please read the item's ID number off of the bar code, and type it into +the box below.

+ +<%= start_form_tag :action => 'rent'%> +<%= text_field 'rentable_id', nil %> + <%= submit_tag 'Ok' %> +<%= end_form_tag %> + diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 5dff97849d345a10ad62e7d3f94da65eb718f913..0a336e5d64035d3727cd4a1bfec1e45439a461ac 100644 GIT binary patch delta 482 zcmZoTz}Rqtae_2&Bm)Ep04bJ@8nd_<88#p0vSXA4@;RA4F)(K_FJtat)?@Zze#Cr) zS%B%&#>N{=HZnYH42+D5icIWA1_tK3hK9OErf`;p3^zoUksB_CBs*D;f0~>O7gPx= zHYJ-M@v}-uXt9VgvNEVMF$U)*=Hw^@1P5VuLV2qO1r}7PzPy^JIS;WmZ#Wb}c4^JR^w7VhYr?$be^J06Q-;(8)qTEDXew zn?-Csa5GCVSWIU3FkrS&a|ap0Y|6~KIl*IrxB$p|EX*Mw@2vrOZ)2kiGn1A#!gMc$ O?VeEExwSmtRssO4lwKSF delta 203 zcmZoT!Pszsae_3j5d#v~s4uqe5 zV4keUKTQs(Nsf_GQIV0;$iTo{*U(Vc$QaHt-Ta83RYF3aMU;`1L7j;)I5#mTML7T7Fe^MRXLoFQQ{yNAKVCe6(W9t*?)(PAv* -- 2.25.1