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();
199 # if setting to list, update the copy's statua
201 my $newstat = $U->copy_status_from_name('lost') if $islost;
202 if( $copy->status ne $newstat->id ) {
203 $copy->status($newstat);
204 $U->update_copy(copy => $copy, editor => $user->id, session => $session);
208 # grab the circulation
209 ( $circ ) = $U->fetch_open_circulation( $copy->id );
210 return 1 unless $circ;
214 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_LOST');
216 $circ->stop_fines("LOST");
221 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
223 $circ->stop_fines("CLAIMSRETURNED");
225 # allow the caller to backdate the circulation and void any fines
226 # that occurred after the backdate
228 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
229 $backdate, $circ, $user, $session );
233 my $s = $session->request(
234 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
236 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
237 $U->commit_db_session($session);
243 __PACKAGE__->register_method (
244 method => 'set_circ_due_date',
245 api_name => 'open-ils.circ.circulation.due_date.update',
247 Updates the due_date on the given circ
249 @param circid The id of the circ to update
250 @param date The timestamp of the new due date
254 sub set_circ_due_date {
255 my( $s, $c, $authtoken, $circid, $date ) = @_;
256 my ($circ, $evt) = $U->fetch_circulation($circid);
260 ($reqr, $evt) = $U->checkses($authtoken);
263 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
266 $date = clense_ISO8601($date);
267 $logger->activity("user ".$reqr->id.
268 " updating due_date on circ $circid: $date");
270 $circ->due_date($date);
271 my $stat = $U->storagereq(
272 'open-ils.storage.direct.action.circulation.update', $circ);
273 return $U->DB_UPDATE_FAILED unless defined $stat;
278 __PACKAGE__->register_method(
279 method => "create_in_house_use",
280 api_name => 'open-ils.circ.in_house_use.create',
282 Creates an in-house use action.
283 @param $authtoken The login session key
284 @param params A hash of params including
285 'location' The org unit id where the in-house use occurs
286 'copyid' The copy in question
287 'count' The number of in-house uses to apply to this copy
288 @return An array of id's representing the id's of the newly created
289 in-house use objects or an event on an error
292 sub create_in_house_use {
293 my( $self, $client, $authtoken, $params ) = @_;
295 my( $staff, $evt, $copy );
296 my $org = $params->{location};
297 my $copyid = $params->{copyid};
298 my $count = $params->{count} || 1;
299 my $use_time = $params->{use_time} || 'now';
302 my $barcode = $params->{barcode};
303 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
308 ($staff, $evt) = $U->checkses($authtoken);
311 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
314 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
317 $logger->activity("User " . $staff->id .
318 " creating $count in-house use(s) for copy $copyid at location $org");
320 if( $use_time ne 'now' ) {
321 $use_time = clense_ISO8601($use_time);
322 $logger->debug("in_house_use setting use time to $use_time");
327 my $ihu = Fieldmapper::action::in_house_use->new;
330 $ihu->staff($staff->id);
331 $ihu->org_unit($org);
332 $ihu->use_time($use_time);
334 my $id = $U->simplereq(
336 'open-ils.storage.direct.action.in_house_use.create', $ihu );
338 return $U->DB_UPDATE_FAILED($ihu) unless $id;
347 __PACKAGE__->register_method(
348 method => "view_circ_patrons",
349 api_name => "open-ils.circ.copy_checkout_history.retrieve",
351 Retrieves the last X users who checked out a given copy
352 @param authtoken The login session key
353 @param copyid The copy to check
354 @param count How far to go back in the item history
355 @return An array of patron ids
358 sub view_circ_patrons {
359 my( $self, $client, $authtoken, $copyid, $count ) = @_;
361 my( $requestor, $evt ) = $U->checksesperm(
362 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
365 return [] unless $count;
367 my $circs = $U->storagereq(
368 'open-ils.storage.direct.action.circulation.search_where.atomic',
370 target_copy => $copyid,
373 phone_renewal => 'f',
377 order_by => "xact_start DESC"
382 push(@users, $_->usr) for @$circs;
388 __PACKAGE__->register_method(
389 method => 'fetch_notes',
390 api_name => 'open-ils.circ.copy_note.retrieve.all',
392 Returns an array of copy note objects.
393 @param args A named hash of parameters including:
394 authtoken : Required if viewing non-public notes
395 itemid : The id of the item whose notes we want to retrieve
396 pub : True if all the caller wants are public notes
397 @return An array of note objects
400 __PACKAGE__->register_method(
401 method => 'fetch_notes',
402 api_name => 'open-ils.circ.call_number_note.retrieve.all',
403 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
405 __PACKAGE__->register_method(
406 method => 'fetch_notes',
407 api_name => 'open-ils.circ.title_note.retrieve.all',
408 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
411 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
413 my( $self, $connection, $args ) = @_;
415 my $id = $$args{itemid};
416 my $authtoken = $$args{authtoken};
419 if( $self->api_name =~ /copy/ ) {
421 return $U->storagereq(
422 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
423 { owning_copy => $id, pub => 't' } );
425 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
427 return $U->storagereq(
428 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
431 } elsif( $self->api_name =~ /call_number/ ) {
433 return $U->storagereq(
434 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
435 { call_number => $id, pub => 't' } );
437 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
439 return $U->storagereq(
440 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
443 } elsif( $self->api_name =~ /title/ ) {
445 return $U->storagereq(
446 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
447 { record => $id, pub => 't' } );
449 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
451 return $U->storagereq(
452 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
459 __PACKAGE__->register_method(
460 method => 'create_copy_note',
461 api_name => 'open-ils.circ.copy_note.create',
463 Creates a new copy note
464 @param authtoken The login session key
465 @param note The note object to create
466 @return The id of the new note object
469 sub create_copy_note {
470 my( $self, $connection, $authtoken, $note ) = @_;
471 my( $cnowner, $requestor, $evt );
473 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
475 ($requestor, $evt) = $U->checkses($authtoken);
477 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
480 $note->create_date('now');
481 $note->pub( ($note->pub) ? 't' : 'f' );
483 my $id = $U->storagereq(
484 'open-ils.storage.direct.asset.copy_note.create', $note );
485 return $U->DB_UPDATE_FAILED($note) unless $id;
487 $logger->activity("User ".$requestor->id." created a new copy ".
488 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
493 __PACKAGE__->register_method(
494 method => 'delete_copy_note',
495 api_name => 'open-ils.circ.copy_note.delete',
497 Deletes an existing copy note
498 @param authtoken The login session key
499 @param noteid The id of the note to delete
500 @return 1 on success - Event otherwise.
503 sub delete_copy_note {
504 my( $self, $conn, $authtoken, $noteid ) = @_;
505 my( $requestor, $note, $owner, $evt );
507 ($requestor, $evt) = $U->checkses($authtoken);
510 ($note, $evt) = $U->fetch_copy_note($noteid);
513 if( $note->creator ne $requestor->id ) {
514 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
516 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
520 my $stat = $U->storagereq(
521 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
522 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
524 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
528 =head this method is really inefficient - get rid of me
530 __PACKAGE__->register_method(
531 method => 'note_batch',
532 api_name => 'open-ils.circ.biblio_notes.public.batch.retrieve',
534 Returns a set of notes for a given set of titles, volumes, and copies.
535 @param titleid The id of the title who's notes are retrieving
536 @return A list like so:
538 "titles" : [ { id : $id, notes : [ n1, n2 ] },... ]
539 "volumes" : [ { id : $id, notes : [ n1, n2 ] },... ]
540 "copies" : [ { id : $id, notes : [ n1, n2 ] },... ]
546 my( $self, $conn, $titleid ) = @_;
549 my $cns = $U->storagereq(
550 'open-ils.storage.id_list.asset.call_number.search_where.atomic',
551 { record => $titleid, deleted => 'f' } );
552 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
555 my $copyids = $U->storagereq(
556 #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
557 'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
558 push(@copies, @$copyids);
561 return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
573 my $titles = (ref($$args{titles})) ? $$args{titles} : [];
574 my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
575 my $copies = (ref($$args{copies})) ? $$args{copies} : [];
577 for my $title (@$titles) {
578 my $notes = $U->storagereq(
579 'open-ils.storage.direct.biblio.record_note.search_where.atomic',
580 { record => $title, pub => 't' });
581 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
584 for my $volume (@$volumes) {
585 my $notes = $U->storagereq(
586 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
587 { call_number => $volume, pub => 't' });
588 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
592 for my $copy (@$copies) {
593 $logger->debug("Fetching copy notes for copy $copy");
594 my $notes = $U->storagereq(
595 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
596 { owning_copy => $copy, pub => 't' });
597 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;