Class: InterLibraryLoansController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/inter_library_loans_controller.rb

Instance Method Summary collapse

Instance Method Details

#acceptObject



248
249
# File 'app/controllers/inter_library_loans_controller.rb', line 248

def accept
end

#accept_itemObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'app/controllers/inter_library_loans_controller.rb', line 251

def accept_item
  return nil unless request.xhr?
  begin 
    library = current_user.library
    item_identifier = params[:item_identifier]
    @item = Item.where(:item_identifier => item_identifier).first
    @loan = InterLibraryLoan.in_process.find(:first, :conditions => ['item_id = ? AND received_at IS NULL', @item.id]) if @item
    if @item.nil? || @loan.nil?
      render :json => {:error => t('inter_library_loan.no_loan')}
      return false
    end
    unless @loan.to_library == current_user.library
      render :json => {:error => t('inter_library_loan.wrong_library')}
      return false
    end
    InterLibraryLoan.transaction do
      @reserve = Reserve.waiting.find(:first, :conditions => ["item_id = ? AND state = ? AND receipt_library_id = ?", @item.id, "in_process", library.id])
      if @reserve
        @reserve.sm_retain!
      else
        @item.checkin!
        @item.set_next_reservation
      end
      @loan.received_at = Time.zone.now
      @loan.sm_receive!
      @loan.save
    end
    if @item
      message = t('inter_library_loan.successfully_accept', :item_identifier => item_identifier)
      html = render_to_string :partial => "accept_item"
      render :json => {:success => 1, :html => html, :message => message}
    end
  rescue Exception => e
    logger.error "Failed to accept item: #{e}"
    render :json => {:error => t('inter_library_loan.failed_accept')}
  end
end

#createObject

POST /inter_library_loans POST /inter_library_loans.json



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/inter_library_loans_controller.rb', line 102

def create
  @inter_library_loan = InterLibraryLoan.new(params[:inter_library_loan])
  item = Item.where(:item_identifier => params[:inter_library_loan][:item_identifier]).first
  @inter_library_loan.item = item

  respond_to do |format|
    if @inter_library_loan.save
      @inter_library_loan.sm_request!
      flash[:notice] = t('controller.successfully_created', :model => t('activerecord.models.inter_library_loan'))
      format.html { redirect_to(@inter_library_loan) }
      format.json { render :json => @inter_library_loan, :status => :created, :location => @inter_library_loan }
    else
      @current_library = @inter_library_loan.from_library
      @libraries = LibraryGroup.first.real_libraries
      @reasons = InterLibraryLoan.reasons
#        @libraries.reject!{|library| library == current_user.library}
      format.html { render :action => "new" }
      format.json { render :json => @inter_library_loan.errors, :status => :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /inter_library_loans/1 DELETE /inter_library_loans/1.json



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/inter_library_loans_controller.rb', line 148

def destroy
  @inter_library_loan = InterLibraryLoan.find(params[:id])
  @inter_library_loan.destroy

  respond_to do |format|
    if @item
      format.html { redirect_to item_inter_library_loans_url(@item) }
      format.json { head :no_content }
    else
      format.html { redirect_to(inter_library_loans_url) }
      format.json { head :no_content }
    end
  end
end

#download_fileObject



289
290
291
292
293
294
295
296
297
298
299
# File 'app/controllers/inter_library_loans_controller.rb', line 289

def download_file
  #TODO fullpath -> filename 
  path = params[:path]
  if File.exist?(path)
    #send_file path, :type => "application/pdf", :disposition => 'attachment'
    send_file path, :type => "application/pdf", :disposition => 'inline'
  else
    logger.warn "not exist file. path:#{path}"
    render :pickup and return
  end
end

#editObject

GET /inter_library_loans/1/edit



93
94
95
96
97
98
# File 'app/controllers/inter_library_loans_controller.rb', line 93

def edit
  @inter_library_loan = InterLibraryLoan.find(params[:id])
  @libraries = LibraryGroup.first.real_libraries
  @reasons = InterLibraryLoan.reasons
#    @libraries.reject!{|library| library == current_user.library}
end

#export_loan_listsObject



163
164
165
166
# File 'app/controllers/inter_library_loans_controller.rb', line 163

def export_loan_lists
  @libraries = Library.real.all
  @selected_library = params[:library] || [current_user.library.id]
end

#get_loan_listsObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/inter_library_loans_controller.rb', line 168

def get_loan_lists
  @selected_library = params[:library] || []

  # check checked
  if @selected_library.empty?
    flash[:message] = t('inter_library_loan.no_library')
    @libraries = Library.real.all
    render :export_loan_lists; return false
  end
  # check date_exist?
  @loans = InterLibraryLoan.loan_items
  if @loans.blank?
    flash[:message] = t('inter_library_loan.no_loan')
    @libraries = Library.real.all
    render :export_loan_lists; return false
  end

  begin
    report = InterLibraryLoan.get_loan_lists(@loans, @selected_library)
    if report.page
      send_data report.generate, :filename => "loan_lists.pdf", :type => 'application/pdf', :disposition => 'attachment'
      logger.error "created report: #{Time.now}"
      return true
    else
      flash[:message] = t('inter_library_loan.no_loan')
      @libraries = Library.real.all
      render :export_loan_lists; return false
    end
  rescue Exception => e
    logger.error "failed #{e}"
    @libraries = Library.real.all
    render :export_loan_lists; return false
  end
end

#indexObject

GET /inter_library_loans GET /inter_library_loans.json



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/inter_library_loans_controller.rb', line 10

def index
  @from_library = @to_library = Library.real.all
  @reasons = InterLibraryLoan.reasons
  @selected_from_library = @selected_to_library = Library.real.all.inject([]){|ids, library| ids << library.id}
  @selected_reason = InterLibraryLoan.reasons.inject([]){|ids, reason| ids << reason[1]}
  if params[:commit]
    @selected_from_library = params[:from_library] ? params[:from_library].inject([]){|ids, id| ids << id.to_i} : []
    @selected_to_library = params[:to_library] ? params[:to_library].inject([]){|ids, id| ids << id.to_i} : []
    @selected_reason = params[:reason] ? params[:reason].inject([]){|ids, id| ids << id.to_i} : []
  end
  from_library = @selected_from_library
  to_library = @selected_to_library
  reason = @selected_reason
  # check conditions
  flash[:notice] = t('item_list.no_list_condition') if @selected_from_library.blank? or @selected_to_library.blank? or @selected_reason.blank?

  # query
  query = params[:query].to_s.strip
  @query = query.dup
  query = "#{query}*" if query.size == 1

  page = params[:page] || 1
  if @item
    if @query.present?
      @inter_library_loans = @item.inter_library_loans.search do
        fulltext query
        with(:from_library_id, from_library_id)
        with(:to_library_id, to_library)
        with(:reason, reason)
        paginate :page => page.to_i, :per_page => InterLibraryLoan.default_per_page
      end.results
    else
      @inter_library_loans = @item.inter_library_loans.where(:from_library_id => @selected_from_library, :to_library_id => @selected_to_library, :reason => @selected_reason).page(page)
    end
  else
    if @query.present?
      @inter_library_loans = InterLibraryLoan.search do
        fulltext query
        with(:from_library_id, from_library)
        with(:to_library_id, to_library)
        with(:reason, reason) 
        #with(:reason).equal_to 2
        paginate :page => page.to_i, :per_page => InterLibraryLoan.default_per_page
      end.results
    else
      @inter_library_loans = InterLibraryLoan.where(:from_library_id => @selected_from_library, :to_library_id => @selected_to_library, :reason => @selected_reason).page(page)
    end
  end
  respond_to do |format|
    format.html # index.html.erb
    format.json { render :json => @inter_library_loans }
    format.rss  { render :layout => false }
    format.atom
  end
end

#newObject

GET /inter_library_loans/new GET /inter_library_loans/new.json



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/inter_library_loans_controller.rb', line 79

def new
  @inter_library_loan = InterLibraryLoan.new
  @current_library = current_user.library
  @libraries = LibraryGroup.first.real_libraries
  @reasons = InterLibraryLoan.reasons
#    @libraries.reject!{|library| library == current_user.library}

  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @inter_library_loan }
  end
end

#outputObject



301
302
303
304
305
306
307
308
309
# File 'app/controllers/inter_library_loans_controller.rb', line 301

def output
  @loan = InterLibraryLoan.find(params[:id])
  if @loan.nil?
    flash[:message] = t('inter_library_loan.no_loan') 
    return false
  end
  file = InterLibraryLoan.get_loan_report(@loan)
  send_data file, :filename => "loan.pdf", :type => 'application/pdf', :disposition => 'attachment'
end

#pickup_itemObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/controllers/inter_library_loans_controller.rb', line 203

def pickup_item
  library = current_user.library
  item_identifier = params[:item_identifier_tmp].strip
  @pickup_item = Item.where(:item_identifier => item_identifier).first

  # check item_exist?
  unless @pickup_item
    flash[:message] = t('inter_library_loan.no_item') 
    render :pickup and return false
  end
  # check loan_item_exist?
  @loan = InterLibraryLoan.in_process.find(:first, :conditions => ['item_id = ?', @pickup_item.id])
  unless @loan
    flash[:message] = t('inter_library_loan.no_loan') 
    render :pickup and return false
  end

  begin
    # pick up item
    @pickup_item.circulation_status = CirculationStatus.find(:first, :conditions => ['name = ?', "In Transit Between Library Locations"])  
    @pickup_item.save
    @loan.shipped_at = Time.zone.now
    @loan.sm_ship!
    @loan.save

    report = InterLibraryLoan.get_pickup_item_file(@pickup_item, @loan)
    # check dir
    out_dir = "#{Rails.root}/private/system/inter_library_loans/"
    FileUtils.mkdir_p(out_dir) unless FileTest.exist?(out_dir)
    # make pdf
    pdf = "loan_item.pdf"
    report.generate_file(out_dir + pdf)

    flash[:message] = t('inter_library_loan.successfully_pickup', :item_identifier => item_identifier)
    flash[:path] = out_dir + pdf

    logger.error "created report: #{Time.now}"
    render :pickup
  rescue Exception => e
    logger.error "failed #{e}"
    flash[:message] = t('inter_library_loan.failed_pickup')
    render :pickup and return false
  end
end

#showObject

GET /inter_library_loans/1 GET /inter_library_loans/1.json



68
69
70
71
72
73
74
75
# File 'app/controllers/inter_library_loans_controller.rb', line 68

def show
  @inter_library_loan = InterLibraryLoan.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render :json => @inter_library_loan }
  end
end

#updateObject

PUT /inter_library_loans/1 PUT /inter_library_loans/1.json



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/inter_library_loans_controller.rb', line 126

def update
  @inter_library_loan = InterLibraryLoan.find(params[:id])
  @item = @inter_library_loan.item

  respond_to do |format|
    if @inter_library_loan.update_attributes(params[:inter_library_loan])
      flash[:notice] = t('controller.successfully_updated', :model => t('activerecord.models.inter_library_loan'))
      format.html { redirect_to(@inter_library_loan) }
      format.json { head :no_content }
    else
      @inter_library_loan.item = @item
      @libraries = LibraryGroup.first.real_libraries
      @reasons = InterLibraryLoan.reasons
#        @libraries.reject!{|library| library == current_user.library}
      format.html { render :action => "edit" }
      format.json { render :json => @inter_library_loan.errors, :status => :unprocessable_entity }
    end
  end
end