enju_trunk_ill 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/javascripts/enju_trunk_ill/application.js +15 -0
  5. data/app/assets/stylesheets/enju_trunk_ill/application.css +13 -0
  6. data/app/controllers/enju_trunk_ill/application_controller.rb +4 -0
  7. data/app/controllers/inter_library_loans_controller.rb +310 -0
  8. data/app/helpers/enju_trunk_ill/application_helper.rb +4 -0
  9. data/app/models/inter_library_loan.rb +250 -0
  10. data/app/views/inter_library_loans/_accept_item.html.erb +5 -0
  11. data/app/views/inter_library_loans/accept.html.erb +57 -0
  12. data/app/views/inter_library_loans/edit.html.erb +92 -0
  13. data/app/views/inter_library_loans/export_loan_lists.html.erb +50 -0
  14. data/app/views/inter_library_loans/index.atom.builder +11 -0
  15. data/app/views/inter_library_loans/index.html.erb +116 -0
  16. data/app/views/inter_library_loans/index.rss.builder +31 -0
  17. data/app/views/inter_library_loans/loan_list.tlf +1 -0
  18. data/app/views/inter_library_loans/move_item.tlf +1 -0
  19. data/app/views/inter_library_loans/new.html.erb +52 -0
  20. data/app/views/inter_library_loans/pickup.html.erb +35 -0
  21. data/app/views/inter_library_loans/show.html.erb +61 -0
  22. data/app/views/layouts/enju_trunk_ill/application.html.erb +14 -0
  23. data/config/routes.rb +2 -0
  24. data/lib/enju_trunk_ill.rb +4 -0
  25. data/lib/enju_trunk_ill/engine.rb +5 -0
  26. data/lib/enju_trunk_ill/version.rb +3 -0
  27. data/lib/tasks/enju_trunk_ill_tasks.rake +4 -0
  28. data/test/dummy/README.rdoc +261 -0
  29. data/test/dummy/Rakefile +7 -0
  30. data/test/dummy/app/assets/javascripts/application.js +15 -0
  31. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  32. data/test/dummy/app/controllers/application_controller.rb +3 -0
  33. data/test/dummy/app/helpers/application_helper.rb +2 -0
  34. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  35. data/test/dummy/config.ru +4 -0
  36. data/test/dummy/config/application.rb +59 -0
  37. data/test/dummy/config/boot.rb +10 -0
  38. data/test/dummy/config/database.yml +25 -0
  39. data/test/dummy/config/environment.rb +5 -0
  40. data/test/dummy/config/environments/development.rb +37 -0
  41. data/test/dummy/config/environments/production.rb +67 -0
  42. data/test/dummy/config/environments/test.rb +37 -0
  43. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  44. data/test/dummy/config/initializers/inflections.rb +15 -0
  45. data/test/dummy/config/initializers/mime_types.rb +5 -0
  46. data/test/dummy/config/initializers/secret_token.rb +7 -0
  47. data/test/dummy/config/initializers/session_store.rb +8 -0
  48. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/test/dummy/config/locales/en.yml +5 -0
  50. data/test/dummy/config/routes.rb +4 -0
  51. data/test/dummy/public/404.html +26 -0
  52. data/test/dummy/public/422.html +26 -0
  53. data/test/dummy/public/500.html +25 -0
  54. data/test/dummy/public/favicon.ico +0 -0
  55. data/test/dummy/script/rails +6 -0
  56. data/test/enju_trunk_ill_test.rb +7 -0
  57. data/test/integration/navigation_test.rb +10 -0
  58. data/test/test_helper.rb +15 -0
  59. metadata +172 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = EnjuTrunkIll
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'EnjuTrunkIll'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module EnjuTrunkIll
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,310 @@
1
+ class InterLibraryLoansController < ApplicationController
2
+ before_filter :check_client_ip_address
3
+ load_and_authorize_resource
4
+ before_filter :get_item
5
+ before_filter :store_page, :only => :index
6
+ cache_sweeper :page_sweeper, :only => [:create, :update, :destroy]
7
+
8
+ # GET /inter_library_loans
9
+ # GET /inter_library_loans.json
10
+ def index
11
+ @from_library = @to_library = Library.real.all
12
+ @reasons = InterLibraryLoan.reasons
13
+ @selected_from_library = @selected_to_library = Library.real.all.inject([]){|ids, library| ids << library.id}
14
+ @selected_reason = InterLibraryLoan.reasons.inject([]){|ids, reason| ids << reason[1]}
15
+ if params[:commit]
16
+ @selected_from_library = params[:from_library] ? params[:from_library].inject([]){|ids, id| ids << id.to_i} : []
17
+ @selected_to_library = params[:to_library] ? params[:to_library].inject([]){|ids, id| ids << id.to_i} : []
18
+ @selected_reason = params[:reason] ? params[:reason].inject([]){|ids, id| ids << id.to_i} : []
19
+ end
20
+ from_library = @selected_from_library
21
+ to_library = @selected_to_library
22
+ reason = @selected_reason
23
+ # check conditions
24
+ flash[:notice] = t('item_list.no_list_condition') if @selected_from_library.blank? or @selected_to_library.blank? or @selected_reason.blank?
25
+
26
+ # query
27
+ query = params[:query].to_s.strip
28
+ @query = query.dup
29
+ query = "#{query}*" if query.size == 1
30
+
31
+ page = params[:page] || 1
32
+ if @item
33
+ if @query.present?
34
+ @inter_library_loans = @item.inter_library_loans.search do
35
+ fulltext query
36
+ with(:from_library_id, from_library_id)
37
+ with(:to_library_id, to_library)
38
+ with(:reason, reason)
39
+ paginate :page => page.to_i, :per_page => InterLibraryLoan.default_per_page
40
+ end.results
41
+ else
42
+ @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)
43
+ end
44
+ else
45
+ if @query.present?
46
+ @inter_library_loans = InterLibraryLoan.search do
47
+ fulltext query
48
+ with(:from_library_id, from_library)
49
+ with(:to_library_id, to_library)
50
+ with(:reason, reason)
51
+ #with(:reason).equal_to 2
52
+ paginate :page => page.to_i, :per_page => InterLibraryLoan.default_per_page
53
+ end.results
54
+ else
55
+ @inter_library_loans = InterLibraryLoan.where(:from_library_id => @selected_from_library, :to_library_id => @selected_to_library, :reason => @selected_reason).page(page)
56
+ end
57
+ end
58
+ respond_to do |format|
59
+ format.html # index.html.erb
60
+ format.json { render :json => @inter_library_loans }
61
+ format.rss { render :layout => false }
62
+ format.atom
63
+ end
64
+ end
65
+
66
+ # GET /inter_library_loans/1
67
+ # GET /inter_library_loans/1.json
68
+ def show
69
+ @inter_library_loan = InterLibraryLoan.find(params[:id])
70
+
71
+ respond_to do |format|
72
+ format.html # show.html.erb
73
+ format.json { render :json => @inter_library_loan }
74
+ end
75
+ end
76
+
77
+ # GET /inter_library_loans/new
78
+ # GET /inter_library_loans/new.json
79
+ def new
80
+ @inter_library_loan = InterLibraryLoan.new
81
+ @current_library = current_user.library
82
+ @libraries = LibraryGroup.first.real_libraries
83
+ @reasons = InterLibraryLoan.reasons
84
+ # @libraries.reject!{|library| library == current_user.library}
85
+
86
+ respond_to do |format|
87
+ format.html # new.html.erb
88
+ format.json { render :json => @inter_library_loan }
89
+ end
90
+ end
91
+
92
+ # GET /inter_library_loans/1/edit
93
+ def edit
94
+ @inter_library_loan = InterLibraryLoan.find(params[:id])
95
+ @libraries = LibraryGroup.first.real_libraries
96
+ @reasons = InterLibraryLoan.reasons
97
+ # @libraries.reject!{|library| library == current_user.library}
98
+ end
99
+
100
+ # POST /inter_library_loans
101
+ # POST /inter_library_loans.json
102
+ def create
103
+ @inter_library_loan = InterLibraryLoan.new(params[:inter_library_loan])
104
+ item = Item.where(:item_identifier => params[:inter_library_loan][:item_identifier]).first
105
+ @inter_library_loan.item = item
106
+
107
+ respond_to do |format|
108
+ if @inter_library_loan.save
109
+ @inter_library_loan.sm_request!
110
+ flash[:notice] = t('controller.successfully_created', :model => t('activerecord.models.inter_library_loan'))
111
+ format.html { redirect_to(@inter_library_loan) }
112
+ format.json { render :json => @inter_library_loan, :status => :created, :location => @inter_library_loan }
113
+ else
114
+ @current_library = @inter_library_loan.from_library
115
+ @libraries = LibraryGroup.first.real_libraries
116
+ @reasons = InterLibraryLoan.reasons
117
+ # @libraries.reject!{|library| library == current_user.library}
118
+ format.html { render :action => "new" }
119
+ format.json { render :json => @inter_library_loan.errors, :status => :unprocessable_entity }
120
+ end
121
+ end
122
+ end
123
+
124
+ # PUT /inter_library_loans/1
125
+ # PUT /inter_library_loans/1.json
126
+ def update
127
+ @inter_library_loan = InterLibraryLoan.find(params[:id])
128
+ @item = @inter_library_loan.item
129
+
130
+ respond_to do |format|
131
+ if @inter_library_loan.update_attributes(params[:inter_library_loan])
132
+ flash[:notice] = t('controller.successfully_updated', :model => t('activerecord.models.inter_library_loan'))
133
+ format.html { redirect_to(@inter_library_loan) }
134
+ format.json { head :no_content }
135
+ else
136
+ @inter_library_loan.item = @item
137
+ @libraries = LibraryGroup.first.real_libraries
138
+ @reasons = InterLibraryLoan.reasons
139
+ # @libraries.reject!{|library| library == current_user.library}
140
+ format.html { render :action => "edit" }
141
+ format.json { render :json => @inter_library_loan.errors, :status => :unprocessable_entity }
142
+ end
143
+ end
144
+ end
145
+
146
+ # DELETE /inter_library_loans/1
147
+ # DELETE /inter_library_loans/1.json
148
+ def destroy
149
+ @inter_library_loan = InterLibraryLoan.find(params[:id])
150
+ @inter_library_loan.destroy
151
+
152
+ respond_to do |format|
153
+ if @item
154
+ format.html { redirect_to item_inter_library_loans_url(@item) }
155
+ format.json { head :no_content }
156
+ else
157
+ format.html { redirect_to(inter_library_loans_url) }
158
+ format.json { head :no_content }
159
+ end
160
+ end
161
+ end
162
+
163
+ def export_loan_lists
164
+ @libraries = Library.real.all
165
+ @selected_library = params[:library] || [current_user.library.id]
166
+ end
167
+
168
+ def get_loan_lists
169
+ @selected_library = params[:library] || []
170
+
171
+ # check checked
172
+ if @selected_library.empty?
173
+ flash[:message] = t('inter_library_loan.no_library')
174
+ @libraries = Library.real.all
175
+ render :export_loan_lists; return false
176
+ end
177
+ # check date_exist?
178
+ @loans = InterLibraryLoan.loan_items
179
+ if @loans.blank?
180
+ flash[:message] = t('inter_library_loan.no_loan')
181
+ @libraries = Library.real.all
182
+ render :export_loan_lists; return false
183
+ end
184
+
185
+ begin
186
+ report = InterLibraryLoan.get_loan_lists(@loans, @selected_library)
187
+ if report.page
188
+ send_data report.generate, :filename => "loan_lists.pdf", :type => 'application/pdf', :disposition => 'attachment'
189
+ logger.error "created report: #{Time.now}"
190
+ return true
191
+ else
192
+ flash[:message] = t('inter_library_loan.no_loan')
193
+ @libraries = Library.real.all
194
+ render :export_loan_lists; return false
195
+ end
196
+ rescue Exception => e
197
+ logger.error "failed #{e}"
198
+ @libraries = Library.real.all
199
+ render :export_loan_lists; return false
200
+ end
201
+ end
202
+
203
+ def pickup_item
204
+ library = current_user.library
205
+ item_identifier = params[:item_identifier_tmp].strip
206
+ @pickup_item = Item.where(:item_identifier => item_identifier).first
207
+
208
+ # check item_exist?
209
+ unless @pickup_item
210
+ flash[:message] = t('inter_library_loan.no_item')
211
+ render :pickup and return false
212
+ end
213
+ # check loan_item_exist?
214
+ @loan = InterLibraryLoan.in_process.find(:first, :conditions => ['item_id = ?', @pickup_item.id])
215
+ unless @loan
216
+ flash[:message] = t('inter_library_loan.no_loan')
217
+ render :pickup and return false
218
+ end
219
+
220
+ begin
221
+ # pick up item
222
+ @pickup_item.circulation_status = CirculationStatus.find(:first, :conditions => ['name = ?', "In Transit Between Library Locations"])
223
+ @pickup_item.save
224
+ @loan.shipped_at = Time.zone.now
225
+ @loan.sm_ship!
226
+ @loan.save
227
+
228
+ report = InterLibraryLoan.get_pickup_item_file(@pickup_item, @loan)
229
+ # check dir
230
+ out_dir = "#{Rails.root}/private/system/inter_library_loans/"
231
+ FileUtils.mkdir_p(out_dir) unless FileTest.exist?(out_dir)
232
+ # make pdf
233
+ pdf = "loan_item.pdf"
234
+ report.generate_file(out_dir + pdf)
235
+
236
+ flash[:message] = t('inter_library_loan.successfully_pickup', :item_identifier => item_identifier)
237
+ flash[:path] = out_dir + pdf
238
+
239
+ logger.error "created report: #{Time.now}"
240
+ render :pickup
241
+ rescue Exception => e
242
+ logger.error "failed #{e}"
243
+ flash[:message] = t('inter_library_loan.failed_pickup')
244
+ render :pickup and return false
245
+ end
246
+ end
247
+
248
+ def accept
249
+ end
250
+
251
+ def accept_item
252
+ return nil unless request.xhr?
253
+ begin
254
+ library = current_user.library
255
+ item_identifier = params[:item_identifier]
256
+ @item = Item.where(:item_identifier => item_identifier).first
257
+ @loan = InterLibraryLoan.in_process.find(:first, :conditions => ['item_id = ? AND received_at IS NULL', @item.id]) if @item
258
+ if @item.nil? || @loan.nil?
259
+ render :json => {:error => t('inter_library_loan.no_loan')}
260
+ return false
261
+ end
262
+ unless @loan.to_library == current_user.library
263
+ render :json => {:error => t('inter_library_loan.wrong_library')}
264
+ return false
265
+ end
266
+ InterLibraryLoan.transaction do
267
+ @reserve = Reserve.waiting.find(:first, :conditions => ["item_id = ? AND state = ? AND receipt_library_id = ?", @item.id, "in_process", library.id])
268
+ if @reserve
269
+ @reserve.sm_retain!
270
+ else
271
+ @item.checkin!
272
+ @item.set_next_reservation
273
+ end
274
+ @loan.received_at = Time.zone.now
275
+ @loan.sm_receive!
276
+ @loan.save
277
+ end
278
+ if @item
279
+ message = t('inter_library_loan.successfully_accept', :item_identifier => item_identifier)
280
+ html = render_to_string :partial => "accept_item"
281
+ render :json => {:success => 1, :html => html, :message => message}
282
+ end
283
+ rescue Exception => e
284
+ logger.error "Failed to accept item: #{e}"
285
+ render :json => {:error => t('inter_library_loan.failed_accept')}
286
+ end
287
+ end
288
+
289
+ def download_file
290
+ #TODO fullpath -> filename
291
+ path = params[:path]
292
+ if File.exist?(path)
293
+ #send_file path, :type => "application/pdf", :disposition => 'attachment'
294
+ send_file path, :type => "application/pdf", :disposition => 'inline'
295
+ else
296
+ logger.warn "not exist file. path:#{path}"
297
+ render :pickup and return
298
+ end
299
+ end
300
+
301
+ def output
302
+ @loan = InterLibraryLoan.find(params[:id])
303
+ if @loan.nil?
304
+ flash[:message] = t('inter_library_loan.no_loan')
305
+ return false
306
+ end
307
+ file = InterLibraryLoan.get_loan_report(@loan)
308
+ send_data file, :filename => "loan.pdf", :type => 'application/pdf', :disposition => 'attachment'
309
+ end
310
+ end
@@ -0,0 +1,4 @@
1
+ module EnjuTrunkIll
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,250 @@
1
+ class InterLibraryLoan < ActiveRecord::Base
2
+ attr_accessible :item_id, :from_library_id, :to_library_id, :requested_at, :reason
3
+
4
+ self.extend ItemsHelper
5
+ default_scope :order => "created_at DESC"
6
+ scope :completed, where(:state => 'return_received')
7
+ #scope :processing, lambda {|item, to_library| {:conditions => ['item_id = ? AND to_library_id = ? AND state != ?', item.id, wto_library.id, 'return_received']}}
8
+ scope :processing, lambda {|item, to_library| {:conditions => ['item_id = ? AND to_library_id = ?', item.id, to_library.id]}}
9
+ scope :in_process, :order => "created_at ASC"
10
+
11
+ belongs_to :item, :validate => true
12
+ #belongs_to :reserve
13
+ belongs_to :to_library, :foreign_key => 'to_library_id', :class_name => 'Library', :validate => true
14
+ belongs_to :from_library, :foreign_key => 'from_library_id', :class_name => 'Library', :validate => true
15
+
16
+ validates_presence_of :item_id, :from_library_id, :to_library_id
17
+ validates_associated :item, :from_library, :to_library
18
+
19
+ paginates_per 10
20
+
21
+ attr_accessor :item_identifier
22
+
23
+ state_machine :initial => :pending do
24
+ before_transition :pending => :requested, :do => :do_request
25
+ before_transition :requested => :shipped, :do => :ship
26
+ before_transition :shipped => :received, :do => :receive
27
+ before_transition :received => :return_shipped, :do => :return_ship
28
+ before_transition :return_shipped => :return_received, :do => :return_receive
29
+
30
+ event :sm_request do
31
+ transition :pending => :requested
32
+ end
33
+
34
+ event :sm_ship do
35
+ transition :requested => :shipped
36
+ end
37
+
38
+ event :sm_receive do
39
+ transition :shipped => :received
40
+ end
41
+
42
+ event :sm_return_ship do
43
+ transition :received => :return_shipped
44
+ end
45
+
46
+ event :sm_return_receive do
47
+ transition :return_shipped => :return_received
48
+ end
49
+ end
50
+
51
+ searchable do
52
+ text :item_identifier do
53
+ self.item.item_identifier if self.item
54
+ end
55
+ text :title do
56
+ titles = []
57
+ titles << self.item.manifestation.original_title if self.item
58
+ titles << self.item.manifestation.title_transcription if self.item
59
+ end
60
+ string :state
61
+ integer :item_id
62
+ integer :to_library_id
63
+ integer :from_library_id
64
+ integer :reason
65
+ time :requested_at
66
+ time :shipped_at
67
+ time :received_at
68
+ time :return_shipped_at
69
+ time :return_received_at
70
+ time :created_at
71
+ time :updated_at
72
+ end
73
+
74
+ def do_request
75
+ InterLibraryLoan.transaction do
76
+ self.item.update_attribute(:circulation_status, CirculationStatus.where(:name => 'Recalled').first)
77
+ self.update_attribute(:requested_at, Time.zone.now)
78
+ end
79
+ end
80
+
81
+ def ship
82
+ InterLibraryLoan.transaction do
83
+ self.item.update_attributes({:circulation_status => CirculationStatus.where(:name => 'In Transit Between Library Locations').first})
84
+ self.update_attributes({:shipped_at => Time.zone.now})
85
+ end
86
+ end
87
+
88
+ def receive
89
+ InterLibraryLoan.transaction do
90
+ # self.item.update_attributes({:circulation_status => CirculationStatus.where(:name => 'In Process').first})
91
+ self.update_attributes({:received_at => Time.zone.now})
92
+ end
93
+ end
94
+
95
+ def return_ship
96
+ InterLibraryLoan.transaction do
97
+ self.item.update_attributes({:circulation_status => CirculationStatus.where(:name => 'In Transit Between Library Locations').first})
98
+ self.update_attributes({:return_shipped_at => Time.zone.now})
99
+ end
100
+ end
101
+
102
+ def return_receive
103
+ InterLibraryLoan.transaction do
104
+ # TODO: 'Waiting To Be Reshelved'
105
+ self.item.update_attributes({:circulation_status => CirculationStatus.where(:name => 'Available On Shelf').first})
106
+ self.update_attributes({:return_received_at => Time.zone.now})
107
+ end
108
+ end
109
+
110
+ def request_for_reserve(item, to_library)
111
+ self.update_attributes(:item_id => item.id, :to_library_id => to_library.id, :from_library_id => item.shelf.library.id, :requested_at => Time.zone.now, :reason => 1)
112
+ self.sm_request!
113
+ end
114
+
115
+ def request_for_checkin(item, from_library)
116
+ self.update_attributes(:item_id => item.id, :from_library_id => from_library.id, :to_library_id => item.shelf.library.id, :requested_at => Time.zone.now, :reason => 2)
117
+ self.sm_request!
118
+ end
119
+
120
+ def self.loan_items
121
+ loans = []
122
+ item_ids = InterLibraryLoan.select(:item_id).where(:received_at => nil).inject([]){|ids, loan| ids << loan.item_id}.uniq
123
+ item_ids.each do |id|
124
+ loans << InterLibraryLoan.where(:item_id => id, :received_at => nil).order("reason DESC, created_at ASC").first
125
+ end
126
+ return loans
127
+ end
128
+
129
+ def self.reasons
130
+ reasons = [[I18n.t('inter_library_loan.checkout'), 1],
131
+ [I18n.t('inter_library_loan.checkin'), 2]]
132
+ return reasons
133
+ end
134
+
135
+ def self.get_loan_report(inter_library_loan)
136
+ @loan = inter_library_loan
137
+ begin
138
+ report = ThinReports::Report.new :layout => "#{Rails.root.to_s}/app/views/inter_library_loans/move_item"
139
+ report.start_new_page
140
+ report.page.item(:export_date).value(Time.now)
141
+ report.page.item(:title).value(@loan.item.manifestation.original_title)
142
+ report.page.item(:call_number).value(call_numberformat(@loan.item))
143
+ report.page.item(:from_library).value(@loan.from_library.display_name.localize)
144
+ report.page.item(:to_library).value(@loan.to_library.display_name.localize)
145
+ report.page.item(:reason).value(I18n.t('inter_library_loan.checkout')) if @loan.reason == 1
146
+ report.page.item(:reason).value(I18n.t('inter_library_loan.checkin')) if @loan.reason == 2
147
+ reserve = Reserve.waiting.where(:item_id => @loan.item_id, :receipt_library_id => @loan.to_library_id, :state => 'in_process').first
148
+ if reserve
149
+ report.page.item(:user_title).show
150
+ report.page.item(:reserve_user).value(reserve.user.username) if reserve.user
151
+ report.page.item(:expire_date_title).show
152
+ report.page.item(:reserve_expire_date).value(reserve.expired_at)
153
+ end
154
+ logger.error "created report: #{Time.now}"
155
+ return report.generate
156
+ rescue Exception => e
157
+ logger.error "failed #{e}"
158
+ return false
159
+ end
160
+ end
161
+
162
+ def self.get_loan_lists(loans, library_ids)
163
+ report = ThinReports::Report.new :layout => "#{Rails.root.to_s}/app/views/inter_library_loans/loan_list"
164
+
165
+ report.events.on :page_create do |e|
166
+ e.page.item(:page).value(e.page.no)
167
+ end
168
+ report.events.on :generate do |e|
169
+ e.pages.each do |page|
170
+ page.item(:total).value(e.report.page_count)
171
+ end
172
+ end
173
+
174
+ library_ids.each do |library_id|
175
+ library = Library.find(library_id) rescue nil
176
+ to_libraries = InterLibraryLoan.where(:from_library_id => library_id).inject([]){|libraries, data| libraries << Library.find(data.to_library_id)}
177
+ next if to_libraries.blank?
178
+ to_libraries.uniq.each do |to_library|
179
+ report.start_new_page
180
+ report.page.item(:date).value(Time.now)
181
+ report.page.item(:library).value(library.display_name.localize)
182
+ report.page.item(:library_move_to).value(to_library.display_name.localize)
183
+ loans.each do |loan|
184
+ if loan.from_library_id == library.id && loan.to_library_id == to_library.id && loan.reason == 1
185
+ report.page.list(:list).add_row do |row|
186
+ row.item(:reason).value(I18n.t('inter_library_loan.checkout'))
187
+ row.item(:item_identifier).value(loan.item.item_identifier)
188
+ row.item(:shelf).value(loan.item.shelf.display_name) if loan.item.shelf
189
+ row.item(:call_number).value(call_numberformat(loan.item))
190
+ row.item(:title).value(loan.item.manifestation.original_title) if loan.item.manifestation
191
+ end
192
+ end
193
+ end
194
+ loans.each do |loan|
195
+ if loan.from_library_id == library.id && loan.to_library_id == to_library.id && loan.reason == 2
196
+ report.page.list(:list).add_row do |row|
197
+ row.item(:reason).value(I18n.t('inter_library_loan.checkin'))
198
+ row.item(:item_identifier).value(loan.item.item_identifier)
199
+ row.item(:shelf).value(loan.item.shelf.display_name) if loan.item.shelf
200
+ row.item(:call_number).value(call_numberformat(loan.item))
201
+ row.item(:title).value(loan.item.manifestation.original_title) if loan.item.manifestation
202
+ end
203
+ end
204
+ end
205
+ end
206
+ end
207
+ logger.error report.page
208
+ return report
209
+ end
210
+
211
+ def self.get_pickup_item_file(pickup_item, loan)
212
+ report = ThinReports::Report.new :layout => "#{Rails.root.to_s}/app/views/inter_library_loans/move_item"
213
+ report.start_new_page
214
+ report.page.item(:export_date).value(Time.now)
215
+ report.page.item(:title).value(pickup_item.manifestation.original_title)
216
+ report.page.item(:call_number).value(call_numberformat(pickup_item))
217
+ report.page.item(:from_library).value(loan.from_library.display_name.localize)
218
+ report.page.item(:to_library).value(loan.to_library.display_name.localize)
219
+ report.page.item(:reason).value(I18n.t('inter_library_loan.checkout')) if loan.reason == 1
220
+ report.page.item(:reason).value(I18n.t('inter_library_loan.checkin')) if loan.reason == 2
221
+ reserve = Reserve.waiting.where(:item_id => loan.item_id, :receipt_library_id => loan.to_library_id, :state => 'in_process').first
222
+ if reserve
223
+ report.page.item(:user_title).show
224
+ report.page.item(:reserve_user).value(reserve.user.username) if reserve.user
225
+ report.page.item(:expire_date_title).show
226
+ report.page.item(:reserve_expire_date).value(reserve.expired_at)
227
+ end
228
+ return report
229
+ end
230
+ end
231
+
232
+ # == Schema Information
233
+ #
234
+ # Table name: inter_library_loans
235
+ #
236
+ # id :integer not null, primary key
237
+ # item_id :integer not null
238
+ # to_library_id :integer not null
239
+ # requested_at :datetime
240
+ # shipped_at :datetime
241
+ # received_at :datetime
242
+ # return_shipped_at :datetime
243
+ # return_received_at :datetime
244
+ # deleted_at :datetime
245
+ # state :string(255)
246
+ # created_at :datetime
247
+ # updated_at :datetime
248
+ # from_library_id :integer not null
249
+ #
250
+