Add the capability to return checked out items
authorIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 08:33:13 +0000 (00:33 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 08:33:13 +0000 (00:33 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
app/controllers/coitem_controller.rb
app/models/coitem.rb
app/views/coitem/return.rhtml [new file with mode: 0644]
app/views/layouts/coitem.rhtml
db/development.sqlite3

index 73049f3..a524f05 100644 (file)
@@ -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
index 513625a..53bf312 100644 (file)
@@ -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 (file)
index 0000000..4a15185
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>Return a Rentable Item</h1>
+
+<%= start_form_tag :action => 'return_validate'%>
+<%= text_field 'rentable_id', nil  %>
+  <%= submit_tag 'Ok' %></form>
+<%= end_form_tag %>
+
+<br />
index 5b76991..191c4d2 100644 (file)
@@ -10,6 +10,7 @@
 <body>
 
 <p style="color: green"><%= flash[:notice] %></p>
+<p style="color: red"><%= flash[:error] %></p>
 
 <%= yield  %>
 
index 5ddceca..d7f8a65 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ