1 package OpenILS::Application::Circ;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
5 use OpenILS::Application::Circ::Circulate;
6 use OpenILS::Application::Circ::Rules;
7 use OpenILS::Application::Circ::Survey;
8 use OpenILS::Application::Circ::StatCat;
9 use OpenILS::Application::Circ::Holds;
10 use OpenILS::Application::Circ::Money;
11 use OpenILS::Application::Circ::NonCat;
12 use OpenILS::Application::Circ::CopyLocations;
14 use OpenILS::Application::AppUtils;
15 my $apputils = "OpenILS::Application::AppUtils";
18 use OpenILS::Utils::ModsParser;
20 use OpenSRF::EX qw(:try);
21 use OpenSRF::Utils::Logger qw(:logger);
22 #my $logger = "OpenSRF::Utils::Logger";
25 # ------------------------------------------------------------------------
26 # Top level Circ package;
27 # ------------------------------------------------------------------------
31 OpenILS::Application::Circ::Rules->initialize();
32 OpenILS::Application::Circ::Circulate->initialize();
36 # ------------------------------------------------------------------------
37 # Returns an array of {circ, record} hashes checked out by the user.
38 # ------------------------------------------------------------------------
39 __PACKAGE__->register_method(
40 method => "checkouts_by_user",
41 api_name => "open-ils.circ.actor.user.checked_out",
43 Returns a list of open circulations as a pile of objects. each object
44 contains the relevant copy, circ, and record
47 sub checkouts_by_user {
48 my( $self, $client, $user_session, $user_id ) = @_;
50 my( $requestor, $target, $copy, $record, $evt );
52 ( $requestor, $target, $evt ) =
53 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
56 my $circs = $apputils->simplereq(
58 "open-ils.storage.direct.action.open_circulation.search.atomic",
59 { usr => $target->id, checkin_time => undef } );
60 # { usr => $target->id } );
63 for my $circ (@$circs) {
65 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
68 $logger->debug("Retrieving record for copy " . $circ->target_copy);
70 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
73 my $mods = $apputils->record_to_mvr($record);
75 push( @results, { copy => $copy, circ => $circ, record => $mods } );
84 __PACKAGE__->register_method(
85 method => "checkouts_by_user_slim",
86 api_name => "open-ils.circ.actor.user.checked_out.slim",
88 Returns a list of open circulation objects
91 sub checkouts_by_user_slim {
92 my( $self, $client, $user_session, $user_id ) = @_;
94 my( $requestor, $target, $copy, $record, $evt );
96 ( $requestor, $target, $evt ) =
97 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
100 $logger->debug( 'User ' . $requestor->id .
101 " retrieving checked out items for user " . $target->id );
103 # XXX Make the call correct..
104 return $apputils->simplereq(
106 "open-ils.storage.direct.action.open_circulation.search.atomic",
107 { usr => $target->id, checkin_time => undef } );
108 # { usr => $target->id } );
114 __PACKAGE__->register_method(
115 method => "title_from_transaction",
116 api_name => "open-ils.circ.circ_transaction.find_title",
117 NOTES => <<" NOTES");
118 Returns a mods object for the title that is linked to from the
119 copy from the hold that created the given transaction
122 sub title_from_transaction {
123 my( $self, $client, $login_session, $transactionid ) = @_;
125 my( $user, $circ, $title, $evt );
127 ( $user, $evt ) = $apputils->checkses( $login_session );
130 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
133 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
136 return $apputils->record_to_mvr($title);
140 __PACKAGE__->register_method(
141 method => "set_circ_lost",
142 api_name => "open-ils.circ.circulation.set_lost",
143 NOTES => <<" NOTES");
144 Params are login, circid
145 login must have SET_CIRC_LOST perms
146 Sets a circulation to lost
149 __PACKAGE__->register_method(
150 method => "set_circ_lost",
151 api_name => "open-ils.circ.circulation.set_claims_returned",
152 NOTES => <<" NOTES");
153 Params are login, circid
154 login must have SET_CIRC_MISSING perms
155 Sets a circulation to lost
159 my( $self, $client, $login, $circid ) = @_;
160 my( $user, $circ, $evt );
162 ( $user, $evt ) = $apputils->checkses($login);
165 ( $circ, $evt ) = $apputils->fetch_circulation( $circid );
168 if($self->api_name =~ /lost/) {
169 if($evt = $apputils->checkperms(
170 $user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
173 $circ->stop_fines("LOST");
176 # XXX Back date the checkin time so the patron has no fines
177 if($self->api_name =~ /claims_returned/) {
178 if($evt = $apputils->checkperms(
179 $user->id, $circ->circ_lib, "SET_CIRC_CLAIMS_RETURNED")) {
182 $circ->stop_fines("CLAIMSRETURNED");
185 my $s = $apputils->simplereq(
187 "open-ils.storage.direct.action.circulation.update", $circ );
189 if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
192 __PACKAGE__->register_method(
193 method => "create_in_house_use",
194 api_name => 'open-ils.circ.in_house_use.create',
196 Creates an in-house use action.
197 @param $authtoken The login session key
198 @param params A hash of params including
199 'location' The org unit id where the in-house use occurs
200 'copyid' The copy in question
201 'count' The number of in-house uses to apply to this copy
202 @return An array of id's representing the id's of the newly created
203 in-house use objects or an event on an error
206 sub create_in_house_use {
207 my( $self, $client, $authtoken, $params ) = @_;
209 my( $staff, $evt, $copy );
210 my $org = $params->{location};
211 my $copyid = $params->{copyid};
212 my $count = $params->{count} || 1;
213 my $use_time = $params->{use_time} || 'now';
215 ($staff, $evt) = $U->checkses($authtoken);
218 ($copy, $evt) = $U->fetch_copy($copyid);
221 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
224 $logger->activity("User " . $staff->id .
225 " creating $count in-house use(s) for copy $copyid at location $org");
227 if( $use_time ne 'now' ) {
228 my @dates = split(/ /, $use_time);
229 my ($y,$m,$d) = split(/-/, $dates[0]);
230 my ($h,$min,$s) = split(/:/, $dates[1]);
231 $logger->debug("in_house_use setting use time to $use_time");
232 $use_time = OpenILS::Application::Circ::Circulate::_create_date_stamp($s,$min,$h,$d,$m,$y);
237 my $ihu = Fieldmapper::action::in_house_use->new;
240 $ihu->staff($staff->id);
241 $ihu->org_unit($org);
242 $ihu->use_time($use_time);
244 my $id = $U->simplereq(
246 'open-ils.storage.direct.action.in_house_use.create', $ihu );
248 return $U->DB_UPDATE_FAILED($ihu) unless $id;
257 __PACKAGE__->register_method(
258 method => "view_circ_patrons",
259 api_name => "open-ils.circ.copy_checkout_history.retrieve",
261 Retrieves the last X users who checked out a given copy
262 @param authtoken The login session key
263 @param copyid The copy to check
264 @param count How far to go back in the item history
265 @return An array of patron ids
268 sub view_circ_patrons {
269 my( $self, $client, $authtoken, $copyid, $count ) = @_;
271 my( $requestor, $evt ) = $U->checksesperm(
272 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
275 return [] unless $count;
277 my $circs = $U->storagereq(
278 'open-ils.storage.direct.action.circulation.search_where.atomic',
280 target_copy => $copyid,
283 phone_renewal => 'f',
287 order_by => "xact_start DESC"
292 push(@users, $_->usr) for @$circs;
298 __PACKAGE__->register_method(
299 method => 'fetch_notes',
300 api_name => 'open-ils.circ.copy_note.retrieve.all',
302 Returns an array of copy note objects.
303 @param args A named hash of parameters including:
304 authtoken : Required if viewing non-public notes
305 itemid : The id of the item whose notes we want to retrieve
306 pub : True if all the caller wants are public notes
307 @return An array of note objects
310 __PACKAGE__->register_method(
311 method => 'fetch_notes',
312 api_name => 'open-ils.circ.call_number_note.retrieve.all',
313 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
315 __PACKAGE__->register_method(
316 method => 'fetch_notes',
317 api_name => 'open-ils.circ.title_note.retrieve.all',
318 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
320 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
322 my( $self, $connection, $args ) = @_;
324 my $id = $$args{itemid};
325 my $authtoken = $$args{authtoken};
328 if( $self->api_name =~ /copy/ ) {
330 return $U->storagereq(
331 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
332 { owning_copy => $id, pub => 't' } );
334 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_COPY_NOTES');
336 return $U->storagereq(
337 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
340 } elsif( $self->api_name =~ /call_number/ ) {
342 return $U->storagereq(
343 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
344 { call_number => $id, pub => 't' } );
346 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_VOLUME_NOTES');
348 return $U->storagereq(
349 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
352 } elsif( $self->api_name =~ /title/ ) {
354 return $U->storagereq(
355 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
356 { record => $id, pub => 't' } );
358 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_TITLE_NOTES');
360 return $U->storagereq(
361 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
368 __PACKAGE__->register_method(
369 method => 'create_copy_note',
370 api_name => 'open-ils.circ.copy_note.create',
372 Creates a new copy note
373 @param authtoken The login session key
374 @param note The note object to create
375 @return The id of the new note object
378 sub create_copy_note {
379 my( $self, $connection, $authtoken, $note ) = @_;
380 my( $cnowner, $requestor, $evt );
382 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
384 ($requestor, $evt) = $U->checkses($authtoken);
386 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
389 $note->create_date('now');
390 $note->pub( ($note->pub) ? 't' : 'f' );
392 my $id = $U->storagereq(
393 'open-ils.storage.direct.asset.copy_note.create', $note );
394 return $U->DB_UPDATE_FAILED($note) unless $id;
396 $logger->activity("User ".$requestor->id." created a new copy ".
397 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
402 __PACKAGE__->register_method(
403 method => 'delete_copy_note',
404 api_name => 'open-ils.circ.copy_note.delete',
406 Deletes an existing copy note
407 @param authtoken The login session key
408 @param noteid The id of the note to delete
409 @return 1 on success - Event otherwise.
412 sub delete_copy_note {
413 my( $self, $conn, $authtoken, $noteid ) = @_;
414 my( $requestor, $note, $owner, $evt );
416 ($requestor, $evt) = $U->checkses($authtoken);
419 ($note, $evt) = $U->fetch_copy_note($noteid);
422 if( $note->creator ne $requestor->id ) {
423 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
425 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
429 my $stat = $U->storagereq(
430 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
431 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
433 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
438 __PACKAGE__->register_method(
439 method => 'note_batch',
440 api_name => 'open-ils.circ.biblio_notes.public.batch.retrieve',
442 Returns a set of notes for a given set of titles, volumes, and copies.
443 @param titleid The id of the title who's notes are retrieving
444 @return A list like so:
446 "titles" : [ { id : $id, notes : [ n1, n2 ] },... ]
447 "volumes" : [ { id : $id, notes : [ n1, n2 ] },... ]
448 "copies" : [ { id : $id, notes : [ n1, n2 ] },... ]
454 my( $self, $conn, $titleid ) = @_;
457 my $cns = $U->storagereq(
458 'open-ils.storage.id_list.asset.call_number.search_where.atomic',
459 { record => $titleid, deleted => 'f' } );
460 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
463 my $copyids = $U->storagereq(
464 #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
465 'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
466 push(@copies, @$copyids);
469 return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
481 my $titles = (ref($$args{titles})) ? $$args{titles} : [];
482 my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
483 my $copies = (ref($$args{copies})) ? $$args{copies} : [];
485 for my $title (@$titles) {
486 my $notes = $U->storagereq(
487 'open-ils.storage.direct.biblio.record_note.search_where.atomic',
488 { record => $title, pub => 't' });
489 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
492 for my $volume (@$volumes) {
493 my $notes = $U->storagereq(
494 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
495 { call_number => $volume, pub => 't' });
496 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
500 for my $copy (@$copies) {
501 $logger->debug("Fetching copy notes for copy $copy");
502 my $notes = $U->storagereq(
503 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
504 { owning_copy => $copy, pub => 't' });
505 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;