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 use OpenILS::Utils::Fieldmapper;
26 #my $logger = "OpenSRF::Utils::Logger";
29 # ------------------------------------------------------------------------
30 # Top level Circ package;
31 # ------------------------------------------------------------------------
35 OpenILS::Application::Circ::Circulate->initialize();
39 __PACKAGE__->register_method(
40 method => 'retrieve_circ',
41 api_name => 'open-ils.circ.retrieve',
43 Retrieve a circ object by id
44 @param authtoken Login session key
45 @pararm circid The id of the circ object
49 my( $s, $c, $a, $i ) = @_;
50 my($reqr, $evt) = $U->checksesperm($a, 'VIEW_CIRCULATIONS');
53 ($circ, $evt) = $U->fetch_circulation($i);
59 # ------------------------------------------------------------------------
60 # Returns an array of {circ, record} hashes checked out by the user.
61 # ------------------------------------------------------------------------
62 __PACKAGE__->register_method(
63 method => "checkouts_by_user",
64 api_name => "open-ils.circ.actor.user.checked_out",
66 Returns a list of open circulations as a pile of objects. each object
67 contains the relevant copy, circ, and record
70 sub checkouts_by_user {
71 my( $self, $client, $user_session, $user_id ) = @_;
73 my( $requestor, $target, $copy, $record, $evt );
75 ( $requestor, $target, $evt ) =
76 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
79 my $circs = $apputils->simplereq(
81 "open-ils.storage.direct.action.open_circulation.search.atomic",
82 { usr => $target->id, checkin_time => undef } );
83 # { usr => $target->id } );
86 for my $circ (@$circs) {
88 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
91 $logger->debug("Retrieving record for copy " . $circ->target_copy);
93 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
96 my $mods = $apputils->record_to_mvr($record);
98 push( @results, { copy => $copy, circ => $circ, record => $mods } );
107 __PACKAGE__->register_method(
108 method => "checkouts_by_user_slim",
109 api_name => "open-ils.circ.actor.user.checked_out.slim",
110 NOTES => <<" NOTES");
111 Returns a list of open circulation objects
114 sub checkouts_by_user_slim {
115 my( $self, $client, $user_session, $user_id ) = @_;
117 my( $requestor, $target, $copy, $record, $evt );
119 ( $requestor, $target, $evt ) =
120 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
123 $logger->debug( 'User ' . $requestor->id .
124 " retrieving checked out items for user " . $target->id );
126 # XXX Make the call correct..
127 return $apputils->simplereq(
129 "open-ils.storage.direct.action.open_circulation.search.atomic",
130 { usr => $target->id, checkin_time => undef } );
131 # { usr => $target->id } );
137 __PACKAGE__->register_method(
138 method => "title_from_transaction",
139 api_name => "open-ils.circ.circ_transaction.find_title",
140 NOTES => <<" NOTES");
141 Returns a mods object for the title that is linked to from the
142 copy from the hold that created the given transaction
145 sub title_from_transaction {
146 my( $self, $client, $login_session, $transactionid ) = @_;
148 my( $user, $circ, $title, $evt );
150 ( $user, $evt ) = $apputils->checkses( $login_session );
153 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
156 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
159 return $apputils->record_to_mvr($title);
163 __PACKAGE__->register_method(
164 method => "set_circ_lost",
165 api_name => "open-ils.circ.circulation.set_lost",
166 NOTES => <<" NOTES");
167 Params are login, barcode
168 login must have SET_CIRC_LOST perms
169 Sets a circulation to lost
172 __PACKAGE__->register_method(
173 method => "set_circ_lost",
174 api_name => "open-ils.circ.circulation.set_claims_returned",
175 NOTES => <<" NOTES");
176 Params are login, barcode
177 login must have SET_CIRC_MISSING perms
178 Sets a circulation to lost
182 my( $self, $client, $login, $args ) = @_;
183 my( $user, $circ, $copy, $evt );
185 my $barcode = $$args{barcode};
186 my $backdate = $$args{backdate};
188 ( $user, $evt ) = $U->checkses($login);
191 # Grab the related copy
192 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
195 my $isclaims = $self->api_name =~ /claims_returned/;
196 my $islost = $self->api_name =~ /lost/;
197 my $session = $U->start_db_session();
199 # grab the circulation
200 ( $circ ) = $U->fetch_open_circulation( $copy->id );
201 return 1 unless $circ;
204 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
209 $evt = _set_circ_claims_returned(
210 $user, $circ, $session, $backdate );
213 # $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
214 # return $evt if $evt;
215 # $circ->stop_fines("CLAIMSRETURNED");
217 # $logger->activity("user ".$user->id." marking circ". $circ->id. " as claims returned");
219 # # allow the caller to backdate the circulation and void any fines
220 # # that occurred after the backdate
222 # OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
223 # $backdate, $circ, $user, $session );
227 # ($patron, $evt) = $U->fetch_user($circ->usr);
228 # return $evt if $evt;
229 # $patron->claims_returned_count(
230 # $patron->claims_returned_count + 1 );
232 # my $stat = $U->storagereq(
233 # 'open-ils.storage.direct.actor.user.update', $patron );
234 # return $U->DB_UPDATE_FAILED($patron) unless $stat;
238 $circ->stop_fines_time('now') unless $circ->stop_fines_time('now');
239 my $s = $session->request(
240 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
242 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
243 $U->commit_db_session($session);
249 my( $copy, $circ, $reqr, $session ) = @_;
251 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
254 $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
255 " lost for circ ". $circ->id. " and checking for necessary charges");
257 my $newstat = $U->copy_status_from_name('lost');
258 if( $copy->status ne $newstat->id ) {
260 $copy->status($newstat);
264 session => $session);
267 # if the copy has a price defined and/or a processing fee, bill the patron
268 my $amount = $copy->price || 0;
269 my $owner = $U->fetch_copy_owner($copy->id);
270 $logger->info("circ fetching org settings for $owner to determine processing fee");
271 my $settings = $U->simplereq(
273 'open-ils.actor.org_unit.settings.retrieve', $owner );
274 my $f = $settings->{'circ.processing_fee'} || 0;
279 $logger->activity("The system is charging $amount ".
280 "for lost materials on circulation ".$circ->id);
282 my $bill = Fieldmapper::money::billing->new;
284 $bill->xact( $circ->id );
285 $bill->amount( $amount );
286 $bill->billing_type('Lost materials'); # - these strings should be configurable some day
287 $bill->note('SYSTEM GENERATED');
289 my $id = $session->request(
290 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
292 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
295 $circ->stop_fines("LOST");
299 sub _set_circ_claims_returned {
300 my( $reqr, $circ, $session, $backdate ) = @_;
302 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
304 $circ->stop_fines("CLAIMSRETURNED");
306 $logger->activity("user ".$reqr->id.
307 " marking circ". $circ->id. " as claims returned");
309 # allow the caller to backdate the circulation and void any fines
310 # that occurred after the backdate
312 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
313 $backdate, $circ, $reqr, $session );
321 __PACKAGE__->register_method (
322 method => 'set_circ_due_date',
323 api_name => 'open-ils.circ.circulation.due_date.update',
325 Updates the due_date on the given circ
327 @param circid The id of the circ to update
328 @param date The timestamp of the new due date
332 sub set_circ_due_date {
333 my( $s, $c, $authtoken, $circid, $date ) = @_;
334 my ($circ, $evt) = $U->fetch_circulation($circid);
338 ($reqr, $evt) = $U->checkses($authtoken);
341 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
344 $date = clense_ISO8601($date);
345 $logger->activity("user ".$reqr->id.
346 " updating due_date on circ $circid: $date");
348 $circ->due_date($date);
349 my $stat = $U->storagereq(
350 'open-ils.storage.direct.action.circulation.update', $circ);
351 return $U->DB_UPDATE_FAILED unless defined $stat;
356 __PACKAGE__->register_method(
357 method => "create_in_house_use",
358 api_name => 'open-ils.circ.in_house_use.create',
360 Creates an in-house use action.
361 @param $authtoken The login session key
362 @param params A hash of params including
363 'location' The org unit id where the in-house use occurs
364 'copyid' The copy in question
365 'count' The number of in-house uses to apply to this copy
366 @return An array of id's representing the id's of the newly created
367 in-house use objects or an event on an error
370 sub create_in_house_use {
371 my( $self, $client, $authtoken, $params ) = @_;
373 my( $staff, $evt, $copy );
374 my $org = $params->{location};
375 my $copyid = $params->{copyid};
376 my $count = $params->{count} || 1;
377 my $use_time = $params->{use_time} || 'now';
380 my $barcode = $params->{barcode};
381 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
386 ($staff, $evt) = $U->checkses($authtoken);
389 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
392 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
395 $logger->activity("User " . $staff->id .
396 " creating $count in-house use(s) for copy $copyid at location $org");
398 if( $use_time ne 'now' ) {
399 $use_time = clense_ISO8601($use_time);
400 $logger->debug("in_house_use setting use time to $use_time");
405 my $ihu = Fieldmapper::action::in_house_use->new;
408 $ihu->staff($staff->id);
409 $ihu->org_unit($org);
410 $ihu->use_time($use_time);
412 my $id = $U->simplereq(
414 'open-ils.storage.direct.action.in_house_use.create', $ihu );
416 return $U->DB_UPDATE_FAILED($ihu) unless $id;
425 __PACKAGE__->register_method(
426 method => "view_circ_patrons",
427 api_name => "open-ils.circ.copy_checkout_history.retrieve",
429 Retrieves the last X users who checked out a given copy
430 @param authtoken The login session key
431 @param copyid The copy to check
432 @param count How far to go back in the item history
433 @return An array of patron ids
436 sub view_circ_patrons {
437 my( $self, $client, $authtoken, $copyid, $count ) = @_;
439 my( $requestor, $evt ) = $U->checksesperm(
440 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
443 return [] unless $count;
445 my $circs = $U->storagereq(
446 'open-ils.storage.direct.action.circulation.search_where.atomic',
448 target_copy => $copyid,
451 phone_renewal => 'f',
455 order_by => "xact_start DESC"
460 push(@users, $_->usr) for @$circs;
466 __PACKAGE__->register_method(
467 method => 'fetch_notes',
468 api_name => 'open-ils.circ.copy_note.retrieve.all',
470 Returns an array of copy note objects.
471 @param args A named hash of parameters including:
472 authtoken : Required if viewing non-public notes
473 itemid : The id of the item whose notes we want to retrieve
474 pub : True if all the caller wants are public notes
475 @return An array of note objects
478 __PACKAGE__->register_method(
479 method => 'fetch_notes',
480 api_name => 'open-ils.circ.call_number_note.retrieve.all',
481 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
483 __PACKAGE__->register_method(
484 method => 'fetch_notes',
485 api_name => 'open-ils.circ.title_note.retrieve.all',
486 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
489 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
491 my( $self, $connection, $args ) = @_;
493 my $id = $$args{itemid};
494 my $authtoken = $$args{authtoken};
497 if( $self->api_name =~ /copy/ ) {
499 return $U->storagereq(
500 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
501 { owning_copy => $id, pub => 't' } );
503 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
505 return $U->storagereq(
506 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
509 } elsif( $self->api_name =~ /call_number/ ) {
511 return $U->storagereq(
512 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
513 { call_number => $id, pub => 't' } );
515 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
517 return $U->storagereq(
518 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
521 } elsif( $self->api_name =~ /title/ ) {
523 return $U->storagereq(
524 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
525 { record => $id, pub => 't' } );
527 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
529 return $U->storagereq(
530 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
537 __PACKAGE__->register_method(
538 method => 'create_copy_note',
539 api_name => 'open-ils.circ.copy_note.create',
541 Creates a new copy note
542 @param authtoken The login session key
543 @param note The note object to create
544 @return The id of the new note object
547 sub create_copy_note {
548 my( $self, $connection, $authtoken, $note ) = @_;
549 my( $cnowner, $requestor, $evt );
551 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
553 ($requestor, $evt) = $U->checkses($authtoken);
555 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
558 $note->create_date('now');
559 $note->creator($requestor->id);
560 $note->pub( ($note->pub) ? 't' : 'f' );
562 my $id = $U->storagereq(
563 'open-ils.storage.direct.asset.copy_note.create', $note );
564 return $U->DB_UPDATE_FAILED($note) unless $id;
566 $logger->activity("User ".$requestor->id." created a new copy ".
567 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
572 __PACKAGE__->register_method(
573 method => 'delete_copy_note',
574 api_name => 'open-ils.circ.copy_note.delete',
576 Deletes an existing copy note
577 @param authtoken The login session key
578 @param noteid The id of the note to delete
579 @return 1 on success - Event otherwise.
582 sub delete_copy_note {
583 my( $self, $conn, $authtoken, $noteid ) = @_;
584 my( $requestor, $note, $owner, $evt );
586 ($requestor, $evt) = $U->checkses($authtoken);
589 ($note, $evt) = $U->fetch_copy_note($noteid);
592 if( $note->creator ne $requestor->id ) {
593 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
595 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
599 my $stat = $U->storagereq(
600 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
601 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
603 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
607 =head this method is really inefficient - get rid of me
609 __PACKAGE__->register_method(
610 method => 'note_batch',
611 api_name => 'open-ils.circ.biblio_notes.public.batch.retrieve',
613 Returns a set of notes for a given set of titles, volumes, and copies.
614 @param titleid The id of the title who's notes are retrieving
615 @return A list like so:
617 "titles" : [ { id : $id, notes : [ n1, n2 ] },... ]
618 "volumes" : [ { id : $id, notes : [ n1, n2 ] },... ]
619 "copies" : [ { id : $id, notes : [ n1, n2 ] },... ]
625 my( $self, $conn, $titleid ) = @_;
628 my $cns = $U->storagereq(
629 'open-ils.storage.id_list.asset.call_number.search_where.atomic',
630 { record => $titleid, deleted => 'f' } );
631 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
634 my $copyids = $U->storagereq(
635 #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
636 'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
637 push(@copies, @$copyids);
640 return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
652 my $titles = (ref($$args{titles})) ? $$args{titles} : [];
653 my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
654 my $copies = (ref($$args{copies})) ? $$args{copies} : [];
656 for my $title (@$titles) {
657 my $notes = $U->storagereq(
658 'open-ils.storage.direct.biblio.record_note.search_where.atomic',
659 { record => $title, pub => 't' });
660 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
663 for my $volume (@$volumes) {
664 my $notes = $U->storagereq(
665 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
666 { call_number => $volume, pub => 't' });
667 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
671 for my $copy (@$copies) {
672 $logger->debug("Fetching copy notes for copy $copy");
673 my $notes = $U->storagereq(
674 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
675 { owning_copy => $copy, pub => 't' });
676 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;