From 70d3675bba6ee586e1010a915e36758d2bab3637 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Fri, 23 Nov 2007 00:33:13 -0800 Subject: [PATCH] Add the capability to return checked out items Signed-off-by: Ira W. Snyder --- app/controllers/coitem_controller.rb | 34 +++++++++++++++++++++++++++ app/models/coitem.rb | 6 +++++ app/views/coitem/return.rhtml | 8 +++++++ app/views/layouts/coitem.rhtml | 1 + db/development.sqlite3 | Bin 17408 -> 17408 bytes 5 files changed, 49 insertions(+) create mode 100644 app/views/coitem/return.rhtml 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 5ddceca9996e69b3e2e0569b4fba3083fb01c398..d7f8a65baae6465cdd5386792d277b7877b7f723 100644 GIT binary patch delta 194 zcmZqZU~K4MoFL8Uu~FtJJ1+wR12eM_1G5nGQs&H!jUSjq@)RTWRT({t5*32;DpOL6 zOpMJHCjaINW8|Ei$vvBgfd#1J5pxy@ZWiE)Vg~XVrcKV{mjW>=Cig%Xa+6o_OK|~3 zEHcbOjG~j-b(EPHMJG1CWKt8F W+-RfBYRt{3CJ1DyY);mlD+U0(wJFX3 delta 201 zcmZqZU~K4MoFL6;w^8OPJ1=t(12fZa2BzQ4OPMn_Hoj%z31wtwP>j@9W%MjcoV<}+ zcd|FPtONrPh%1Jgt1`MI<|zc{Ri>mCnHZZZOwQz<&BMx^#lZZC`3Une=8nw*JWlCfjmqX^-ML}_