Fix LIKE clauses in searches to match anywhere in the field
[cs356-p2-videostore.git] / app / controllers / coitem_controller.rb
1 class CoitemController < ApplicationController
2   layout "admin"
3
4   # Make sure that the user has logged in before they can take any
5   # action on checked out items
6   before_filter :authorize
7
8   def index
9     render :action => 'index'
10   end
11
12   # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
13   verify :method => :post, :only => [ :destroy, :create, :update ],
14          :redirect_to => { :action => :list }
15
16   def list
17     @coitem_pages, @coitems = paginate :coitems, :per_page => 10
18   end
19
20   def show
21     @coitem = Coitem.find(params[:id])
22   end
23
24   # We should never create new checked out items via the web interface, so remove
25   # the new and create methods.
26
27   def edit
28     @coitem = Coitem.find(params[:id])
29   end
30
31   def update
32     @coitem = Coitem.find(params[:id])
33     if @coitem.update_attributes(params[:coitem])
34       flash[:notice] = 'Coitem was successfully updated.'
35       redirect_to :action => 'show', :id => @coitem
36     else
37       render :action => 'edit'
38     end
39   end
40
41   # We should never delete a checked out item directly via the web interface, so
42   # remove the destroy method.
43
44   # Awesome, paginating overdue list, ordered by customer
45   def overdue
46     @coitem_pages, @coitems = paginate :coitems, :per_page => 50, :conditions => ["due_date < ?", Time.now.to_date], :order => "customer_id"
47     render :action => 'overdue'
48   end
49
50   def filterbycustomerid
51     query = params[:id]
52     @coitem_pages, @coitems = paginate :coitems, :per_page => 20, :conditions => ["customer_id = ?", query]
53     render :action => 'list'
54   end
55
56   def return
57     if request.post?
58       rentable_id = params[:rentable_id]
59       @rentable = Rentable.find_by_id(rentable_id)
60
61       if @rentable.nil?
62         flash[:notice] = "Unable to find this rentable"
63         redirect_to :action => :return
64         return
65       end
66
67       @coitem = Coitem.find_by_rentable_id(rentable_id)
68       if @coitem.nil?
69         flash[:notice] = "This item is not checked out!"
70         redirect_to :action => :return
71         return
72       end
73
74       # Check if the item is overdue
75       if @coitem.overdue?
76         @coitem.customer.debt += @coitem.late_fee
77         @coitem.customer.save
78       end
79
80       # Delete the row
81       @coitem.destroy
82
83       flash[:notice] = "Successfully returned item"
84       redirect_to :action => :return, :method => :get
85     else
86       render :action => 'return'
87     end
88   end
89 end