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;
15 use DateTime::Format::ISO8601;
17 use OpenILS::Application::AppUtils;
18 my $apputils = "OpenILS::Application::AppUtils";
20 use OpenSRF::Utils qw/:datetime/;
21 use OpenILS::Utils::ModsParser;
23 use OpenSRF::EX qw(:try);
24 use OpenSRF::Utils::Logger qw(:logger);
25 #my $logger = "OpenSRF::Utils::Logger";
28 # ------------------------------------------------------------------------
29 # Top level Circ package;
30 # ------------------------------------------------------------------------
34 OpenILS::Application::Circ::Circulate->initialize();
38 __PACKAGE__->register_method(
39 method => 'retrieve_circ',
40 api_name => 'open-ils.circ.retrieve',
42 Retrieve a circ object by id
43 @param authtoken Login session key
44 @pararm circid The id of the circ object
48 my( $s, $c, $a, $i ) = @_;
49 my($reqr, $evt) = $U->checksesperm($a, 'VIEW_CIRCULATIONS');
52 ($circ, $evt) = $U->fetch_circulation($i);
58 # ------------------------------------------------------------------------
59 # Returns an array of {circ, record} hashes checked out by the user.
60 # ------------------------------------------------------------------------
61 __PACKAGE__->register_method(
62 method => "checkouts_by_user",
63 api_name => "open-ils.circ.actor.user.checked_out",
65 Returns a list of open circulations as a pile of objects. each object
66 contains the relevant copy, circ, and record
69 sub checkouts_by_user {
70 my( $self, $client, $user_session, $user_id ) = @_;
72 my( $requestor, $target, $copy, $record, $evt );
74 ( $requestor, $target, $evt ) =
75 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
78 my $circs = $apputils->simplereq(
80 "open-ils.storage.direct.action.open_circulation.search.atomic",
81 { usr => $target->id, checkin_time => undef } );
82 # { usr => $target->id } );
85 for my $circ (@$circs) {
87 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
90 $logger->debug("Retrieving record for copy " . $circ->target_copy);
92 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
95 my $mods = $apputils->record_to_mvr($record);
97 push( @results, { copy => $copy, circ => $circ, record => $mods } );
106 __PACKAGE__->register_method(
107 method => "checkouts_by_user_slim",
108 api_name => "open-ils.circ.actor.user.checked_out.slim",
109 NOTES => <<" NOTES");
110 Returns a list of open circulation objects
113 sub checkouts_by_user_slim {
114 my( $self, $client, $user_session, $user_id ) = @_;
116 my( $requestor, $target, $copy, $record, $evt );
118 ( $requestor, $target, $evt ) =
119 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
122 $logger->debug( 'User ' . $requestor->id .
123 " retrieving checked out items for user " . $target->id );
125 # XXX Make the call correct..
126 return $apputils->simplereq(
128 "open-ils.storage.direct.action.open_circulation.search.atomic",
129 { usr => $target->id, checkin_time => undef } );
130 # { usr => $target->id } );
136 __PACKAGE__->register_method(
137 method => "title_from_transaction",
138 api_name => "open-ils.circ.circ_transaction.find_title",
139 NOTES => <<" NOTES");
140 Returns a mods object for the title that is linked to from the
141 copy from the hold that created the given transaction
144 sub title_from_transaction {
145 my( $self, $client, $login_session, $transactionid ) = @_;
147 my( $user, $circ, $title, $evt );
149 ( $user, $evt ) = $apputils->checkses( $login_session );
152 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
155 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
158 return $apputils->record_to_mvr($title);
162 __PACKAGE__->register_method(
163 method => "set_circ_lost",
164 api_name => "open-ils.circ.circulation.set_lost",
165 NOTES => <<" NOTES");
166 Params are login, barcode
167 login must have SET_CIRC_LOST perms
168 Sets a circulation to lost
171 __PACKAGE__->register_method(
172 method => "set_circ_lost",
173 api_name => "open-ils.circ.circulation.set_claims_returned",
174 NOTES => <<" NOTES");
175 Params are login, barcode
176 login must have SET_CIRC_MISSING perms
177 Sets a circulation to lost
181 my( $self, $client, $login, $args ) = @_;
182 my( $user, $circ, $copy, $evt );
184 my $barcode = $$args{barcode};
185 my $backdate = $$args{backdate};
187 ( $user, $evt ) = $U->checkses($login);
190 # Grab the related copy
191 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
194 my $isclaims = $self->api_name =~ /claims_returned/;
195 my $islost = $self->api_name =~ /lost/;
196 my $session = $U->start_db_session();
198 # grab the circulation
199 ( $circ ) = $U->fetch_open_circulation( $copy->id );
200 return 1 unless $circ;
203 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
209 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
211 $circ->stop_fines("CLAIMSRETURNED");
213 # allow the caller to backdate the circulation and void any fines
214 # that occurred after the backdate
216 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
217 $backdate, $circ, $user, $session );
221 my $s = $session->request(
222 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
224 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
225 $U->commit_db_session($session);
231 my( $copy, $circ, $reqr, $session ) = @_;
232 my $newstat = $U->copy_status_from_name('lost');
233 if( $copy->status ne $newstat->id ) {
234 $copy->status($newstat);
235 $U->update_copy(copy => $copy, editor => $user->id, session => $session);
238 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_LOST');
240 $circ->stop_fines("LOST");
244 __PACKAGE__->register_method (
245 method => 'set_circ_due_date',
246 api_name => 'open-ils.circ.circulation.due_date.update',
248 Updates the due_date on the given circ
250 @param circid The id of the circ to update
251 @param date The timestamp of the new due date
255 sub set_circ_due_date {
256 my( $s, $c, $authtoken, $circid, $date ) = @_;
257 my ($circ, $evt) = $U->fetch_circulation($circid);
261 ($reqr, $evt) = $U->checkses($authtoken);
264 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
267 $date = clense_ISO8601($date);
268 $logger->activity("user ".$reqr->id.
269 " updating due_date on circ $circid: $date");
271 $circ->due_date($date);
272 my $stat = $U->storagereq(
273 'open-ils.storage.direct.action.circulation.update', $circ);
274 return $U->DB_UPDATE_FAILED unless defined $stat;
279 __PACKAGE__->register_method(
280 method => "create_in_house_use",
281 api_name => 'open-ils.circ.in_house_use.create',
283 Creates an in-house use action.
284 @param $authtoken The login session key
285 @param params A hash of params including
286 'location' The org unit id where the in-house use occurs
287 'copyid' The copy in question
288 'count' The number of in-house uses to apply to this copy
289 @return An array of id's representing the id's of the newly created
290 in-house use objects or an event on an error
293 sub create_in_house_use {
294 my( $self, $client, $authtoken, $params ) = @_;
296 my( $staff, $evt, $copy );
297 my $org = $params->{location};
298 my $copyid = $params->{copyid};
299 my $count = $params->{count} || 1;
300 my $use_time = $params->{use_time} || 'now';
303 my $barcode = $params->{barcode};
304 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
309 ($staff, $evt) = $U->checkses($authtoken);
312 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
315 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
318 $logger->activity("User " . $staff->id .
319 " creating $count in-house use(s) for copy $copyid at location $org");
321 if( $use_time ne 'now' ) {
322 $use_time = clense_ISO8601($use_time);
323 $logger->debug("in_house_use setting use time to $use_time");
328 my $ihu = Fieldmapper::action::in_house_use->new;
331 $ihu->staff($staff->id);
332 $ihu->org_unit($org);
333 $ihu->use_time($use_time);
335 my $id = $U->simplereq(
337 'open-ils.storage.direct.action.in_house_use.create', $ihu );
339 return $U->DB_UPDATE_FAILED($ihu) unless $id;
348 __PACKAGE__->register_method(
349 method => "view_circ_patrons",
350 api_name => "open-ils.circ.copy_checkout_history.retrieve",
352 Retrieves the last X users who checked out a given copy
353 @param authtoken The login session key
354 @param copyid The copy to check
355 @param count How far to go back in the item history
356 @return An array of patron ids
359 sub view_circ_patrons {
360 my( $self, $client, $authtoken, $copyid, $count ) = @_;
362 my( $requestor, $evt ) = $U->checksesperm(
363 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
366 return [] unless $count;
368 my $circs = $U->storagereq(
369 'open-ils.storage.direct.action.circulation.search_where.atomic',
371 target_copy => $copyid,
374 phone_renewal => 'f',
378 order_by => "xact_start DESC"
383 push(@users, $_->usr) for @$circs;
389 __PACKAGE__->register_method(
390 method => 'fetch_notes',
391 api_name => 'open-ils.circ.copy_note.retrieve.all',
393 Returns an array of copy note objects.
394 @param args A named hash of parameters including:
395 authtoken : Required if viewing non-public notes
396 itemid : The id of the item whose notes we want to retrieve
397 pub : True if all the caller wants are public notes
398 @return An array of note objects
401 __PACKAGE__->register_method(
402 method => 'fetch_notes',
403 api_name => 'open-ils.circ.call_number_note.retrieve.all',
404 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
406 __PACKAGE__->register_method(
407 method => 'fetch_notes',
408 api_name => 'open-ils.circ.title_note.retrieve.all',
409 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
412 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
414 my( $self, $connection, $args ) = @_;
416 my $id = $$args{itemid};
417 my $authtoken = $$args{authtoken};
420 if( $self->api_name =~ /copy/ ) {
422 return $U->storagereq(
423 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
424 { owning_copy => $id, pub => 't' } );
426 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
428 return $U->storagereq(
429 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
432 } elsif( $self->api_name =~ /call_number/ ) {
434 return $U->storagereq(
435 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
436 { call_number => $id, pub => 't' } );
438 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
440 return $U->storagereq(
441 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
444 } elsif( $self->api_name =~ /title/ ) {
446 return $U->storagereq(
447 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
448 { record => $id, pub => 't' } );
450 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
452 return $U->storagereq(
453 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
460 __PACKAGE__->register_method(
461 method => 'create_copy_note',
462 api_name => 'open-ils.circ.copy_note.create',
464 Creates a new copy note
465 @param authtoken The login session key
466 @param note The note object to create
467 @return The id of the new note object
470 sub create_copy_note {
471 my( $self, $connection, $authtoken, $note ) = @_;
472 my( $cnowner, $requestor, $evt );
474 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
476 ($requestor, $evt) = $U->checkses($authtoken);
478 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
481 $note->create_date('now');
482 $note->pub( ($note->pub) ? 't' : 'f' );
484 my $id = $U->storagereq(
485 'open-ils.storage.direct.asset.copy_note.create', $note );
486 return $U->DB_UPDATE_FAILED($note) unless $id;
488 $logger->activity("User ".$requestor->id." created a new copy ".
489 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
494 __PACKAGE__->register_method(
495 method => 'delete_copy_note',
496 api_name => 'open-ils.circ.copy_note.delete',
498 Deletes an existing copy note
499 @param authtoken The login session key
500 @param noteid The id of the note to delete
501 @return 1 on success - Event otherwise.
504 sub delete_copy_note {
505 my( $self, $conn, $authtoken, $noteid ) = @_;
506 my( $requestor, $note, $owner, $evt );
508 ($requestor, $evt) = $U->checkses($authtoken);
511 ($note, $evt) = $U->fetch_copy_note($noteid);
514 if( $note->creator ne $requestor->id ) {
515 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
517 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
521 my $stat = $U->storagereq(
522 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
523 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
525 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
529 =head this method is really inefficient - get rid of me
531 __PACKAGE__->register_method(
532 method => 'note_batch',
533 api_name => 'open-ils.circ.biblio_notes.public.batch.retrieve',
535 Returns a set of notes for a given set of titles, volumes, and copies.
536 @param titleid The id of the title who's notes are retrieving
537 @return A list like so:
539 "titles" : [ { id : $id, notes : [ n1, n2 ] },... ]
540 "volumes" : [ { id : $id, notes : [ n1, n2 ] },... ]
541 "copies" : [ { id : $id, notes : [ n1, n2 ] },... ]
547 my( $self, $conn, $titleid ) = @_;
550 my $cns = $U->storagereq(
551 'open-ils.storage.id_list.asset.call_number.search_where.atomic',
552 { record => $titleid, deleted => 'f' } );
553 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
556 my $copyids = $U->storagereq(
557 #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
558 'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
559 push(@copies, @$copyids);
562 return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
574 my $titles = (ref($$args{titles})) ? $$args{titles} : [];
575 my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
576 my $copies = (ref($$args{copies})) ? $$args{copies} : [];
578 for my $title (@$titles) {
579 my $notes = $U->storagereq(
580 'open-ils.storage.direct.biblio.record_note.search_where.atomic',
581 { record => $title, pub => 't' });
582 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
585 for my $volume (@$volumes) {
586 my $notes = $U->storagereq(
587 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
588 { call_number => $volume, pub => 't' });
589 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
593 for my $copy (@$copies) {
594 $logger->debug("Fetching copy notes for copy $copy");
595 my $notes = $U->storagereq(
596 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
597 { owning_copy => $copy, pub => 't' });
598 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;