From: Ira W. Snyder Date: Fri, 23 Nov 2007 08:33:13 +0000 (-0800) Subject: Add the capability to return checked out items X-Git-Tag: turned-in~54 X-Git-Url: https://www.irasnyder.com/gitweb/?p=cs356-p2-videostore.git;a=commitdiff_plain;h=70d3675bba6ee586e1010a915e36758d2bab3637 Add the capability to return checked out items Signed-off-by: Ira W. Snyder --- diff --git a/app/controllers/coitem_controller.rb b/app/controllers/coitem_controller.rb index 73049f3..a524f05 100644 --- a/app/controllers/coitem_controller.rb +++ b/app/controllers/coitem_controller.rb @@ -54,4 +54,38 @@ class CoitemController < ApplicationController @coitem_pages, @coitems = paginate :coitems, :per_page => 50, :conditions => "due_date < DATE('NOW', 'LOCALTIME')", :order => "customer_id" render :action => 'list' end + + def return + render :action => 'return' + end + + def return_validate + rentable_id = params[:rentable_id] + @rentable = Rentable.find_by_id(rentable_id) + + if @rentable.nil? + flash[:error] = "Unable to find this rentable" + redirect_to :action => :return + return + end + + @coitem = Coitem.find_by_rentable_id(rentable_id) + if @coitem.nil? + flash[:error] = "This item is not checked out!" + redirect_to :action => :return + return + end + + # Check if the item is overdue + if @coitem.overdue? + @coitem.customer.debt += @coitem.late_fee + @coitem.customer.save + end + + # Delete the row + @coitem.destroy + + flash[:notice] = "Successfully returned item" + redirect_to :action => :return + end end diff --git a/app/models/coitem.rb b/app/models/coitem.rb index 513625a..53bf312 100644 --- a/app/models/coitem.rb +++ b/app/models/coitem.rb @@ -21,6 +21,12 @@ class Coitem < ActiveRecord::Base return due_date < Time.now.to_date end + def late_fee + # FIXME: this should be calculated better + days_late = Time.now.to_date - (due_date) + return 3 * days_late.to_i + end + protected def validate errors.add(:customer_id, "does not exist is the database") if customer.nil? diff --git a/app/views/coitem/return.rhtml b/app/views/coitem/return.rhtml new file mode 100644 index 0000000..4a15185 --- /dev/null +++ b/app/views/coitem/return.rhtml @@ -0,0 +1,8 @@ +

Return a Rentable Item

+ +<%= start_form_tag :action => 'return_validate'%> +<%= text_field 'rentable_id', nil %> + <%= submit_tag 'Ok' %> +<%= end_form_tag %> + +
diff --git a/app/views/layouts/coitem.rhtml b/app/views/layouts/coitem.rhtml index 5b76991..191c4d2 100644 --- a/app/views/layouts/coitem.rhtml +++ b/app/views/layouts/coitem.rhtml @@ -10,6 +10,7 @@

<%= flash[:notice] %>

+

<%= flash[:error] %>

<%= yield %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 5ddceca..d7f8a65 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ