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 use OpenILS::Utils::Editor;
27 #my $logger = "OpenSRF::Utils::Logger";
30 # ------------------------------------------------------------------------
31 # Top level Circ package;
32 # ------------------------------------------------------------------------
36 OpenILS::Application::Circ::Circulate->initialize();
40 __PACKAGE__->register_method(
41 method => 'retrieve_circ',
42 api_name => 'open-ils.circ.retrieve',
44 Retrieve a circ object by id
45 @param authtoken Login session key
46 @pararm circid The id of the circ object
50 my( $s, $c, $a, $i ) = @_;
51 my($reqr, $evt) = $U->checksesperm($a, 'VIEW_CIRCULATIONS');
54 ($circ, $evt) = $U->fetch_circulation($i);
60 # ------------------------------------------------------------------------
61 # Returns an array of {circ, record} hashes checked out by the user.
62 # ------------------------------------------------------------------------
63 __PACKAGE__->register_method(
64 method => "checkouts_by_user",
65 api_name => "open-ils.circ.actor.user.checked_out",
67 Returns a list of open circulations as a pile of objects. each object
68 contains the relevant copy, circ, and record
71 sub checkouts_by_user {
72 my( $self, $client, $user_session, $user_id ) = @_;
74 my( $requestor, $target, $copy, $record, $evt );
76 ( $requestor, $target, $evt ) =
77 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
80 my $circs = $apputils->simplereq(
82 "open-ils.storage.direct.action.open_circulation.search.atomic",
83 { usr => $target->id, checkin_time => undef } );
84 # { usr => $target->id } );
87 for my $circ (@$circs) {
89 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
92 $logger->debug("Retrieving record for copy " . $circ->target_copy);
94 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
97 my $mods = $apputils->record_to_mvr($record);
99 push( @results, { copy => $copy, circ => $circ, record => $mods } );
108 __PACKAGE__->register_method(
109 method => "checkouts_by_user_slim",
110 api_name => "open-ils.circ.actor.user.checked_out.slim",
111 NOTES => <<" NOTES");
112 Returns a list of open circulation objects
115 sub checkouts_by_user_slim {
116 my( $self, $client, $user_session, $user_id ) = @_;
118 my( $requestor, $target, $copy, $record, $evt );
120 ( $requestor, $target, $evt ) =
121 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
124 $logger->debug( 'User ' . $requestor->id .
125 " retrieving checked out items for user " . $target->id );
127 # XXX Make the call correct..
128 return $apputils->simplereq(
130 "open-ils.storage.direct.action.open_circulation.search.atomic",
131 { usr => $target->id, checkin_time => undef } );
132 # { usr => $target->id } );
136 __PACKAGE__->register_method(
137 method => "checkouts_by_user_opac",
138 api_name => "open-ils.circ.actor.user.checked_out.opac",);
140 sub checkouts_by_user_opac {
141 my( $self, $client, $auth, $user_id ) = @_;
143 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
144 return $e->event unless $e->checkauth;
145 $user_id ||= $e->requestor->id;
146 return $e->event unless
147 my $patron = $e->retrieve_actor_user($user_id);
150 my $search = {usr => $user_id, stop_fines => undef};
151 if( $user_id ne $e->requestor->id ) {
152 $data = $e->search_action_circulation(
153 $search, {checkperm=>1, permorg=>$patron->home_ou});
156 $data = $e->search_action_circulation($search);
159 return $e->event if $e->event;
168 __PACKAGE__->register_method(
169 method => "title_from_transaction",
170 api_name => "open-ils.circ.circ_transaction.find_title",
171 NOTES => <<" NOTES");
172 Returns a mods object for the title that is linked to from the
173 copy from the hold that created the given transaction
176 sub title_from_transaction {
177 my( $self, $client, $login_session, $transactionid ) = @_;
179 my( $user, $circ, $title, $evt );
181 ( $user, $evt ) = $apputils->checkses( $login_session );
184 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
187 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
190 return $apputils->record_to_mvr($title);
194 __PACKAGE__->register_method(
195 method => "set_circ_lost",
196 api_name => "open-ils.circ.circulation.set_lost",
197 NOTES => <<" NOTES");
198 Params are login, barcode
199 login must have SET_CIRC_LOST perms
200 Sets a circulation to lost
203 __PACKAGE__->register_method(
204 method => "set_circ_lost",
205 api_name => "open-ils.circ.circulation.set_claims_returned",
206 NOTES => <<" NOTES");
207 Params are login, barcode
208 login must have SET_CIRC_MISSING perms
209 Sets a circulation to lost
213 my( $self, $client, $login, $args ) = @_;
214 my( $user, $circ, $copy, $evt );
216 my $barcode = $$args{barcode};
217 my $backdate = $$args{backdate};
219 ( $user, $evt ) = $U->checkses($login);
222 # Grab the related copy
223 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
226 my $isclaims = $self->api_name =~ /claims_returned/;
227 my $islost = $self->api_name =~ /lost/;
228 my $session = $U->start_db_session();
230 # grab the circulation
231 ( $circ ) = $U->fetch_open_circulation( $copy->id );
232 return 1 unless $circ;
235 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
240 $evt = _set_circ_claims_returned(
241 $user, $circ, $session, $backdate );
244 # $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
245 # return $evt if $evt;
246 # $circ->stop_fines("CLAIMSRETURNED");
248 # $logger->activity("user ".$user->id." marking circ". $circ->id. " as claims returned");
250 # # allow the caller to backdate the circulation and void any fines
251 # # that occurred after the backdate
253 # OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
254 # $backdate, $circ, $user, $session );
258 # ($patron, $evt) = $U->fetch_user($circ->usr);
259 # return $evt if $evt;
260 # $patron->claims_returned_count(
261 # $patron->claims_returned_count + 1 );
263 # my $stat = $U->storagereq(
264 # 'open-ils.storage.direct.actor.user.update', $patron );
265 # return $U->DB_UPDATE_FAILED($patron) unless $stat;
269 $circ->stop_fines_time('now') unless $circ->stop_fines_time('now');
270 my $s = $session->request(
271 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
273 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
274 $U->commit_db_session($session);
280 my( $copy, $circ, $reqr, $session ) = @_;
282 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
285 $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
286 " lost for circ ". $circ->id. " and checking for necessary charges");
288 my $newstat = $U->copy_status_from_name('lost');
289 if( $copy->status ne $newstat->id ) {
291 $copy->status($newstat);
295 session => $session);
298 # if the copy has a price defined and/or a processing fee, bill the patron
299 my $amount = $copy->price || 0;
300 my $owner = $U->fetch_copy_owner($copy->id);
301 $logger->info("circ fetching org settings for $owner to determine processing fee");
302 my $settings = $U->simplereq(
304 'open-ils.actor.org_unit.settings.retrieve', $owner );
305 my $f = $settings->{'circ.processing_fee'} || 0;
310 $logger->activity("The system is charging $amount ".
311 "for lost materials on circulation ".$circ->id);
313 my $bill = Fieldmapper::money::billing->new;
315 $bill->xact( $circ->id );
316 $bill->amount( $amount );
317 $bill->billing_type('Lost materials'); # - these strings should be configurable some day
318 $bill->note('SYSTEM GENERATED');
320 my $id = $session->request(
321 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
323 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
326 $circ->stop_fines("LOST");
330 sub _set_circ_claims_returned {
331 my( $reqr, $circ, $session, $backdate ) = @_;
333 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
335 $circ->stop_fines("CLAIMSRETURNED");
337 $logger->activity("user ".$reqr->id.
338 " marking circ". $circ->id. " as claims returned");
340 # allow the caller to backdate the circulation and void any fines
341 # that occurred after the backdate
343 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
344 $backdate, $circ, $reqr, $session );
352 __PACKAGE__->register_method (
353 method => 'set_circ_due_date',
354 api_name => 'open-ils.circ.circulation.due_date.update',
356 Updates the due_date on the given circ
358 @param circid The id of the circ to update
359 @param date The timestamp of the new due date
363 sub set_circ_due_date {
364 my( $s, $c, $authtoken, $circid, $date ) = @_;
365 my ($circ, $evt) = $U->fetch_circulation($circid);
369 ($reqr, $evt) = $U->checkses($authtoken);
372 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
375 $date = clense_ISO8601($date);
376 $logger->activity("user ".$reqr->id.
377 " updating due_date on circ $circid: $date");
379 $circ->due_date($date);
380 my $stat = $U->storagereq(
381 'open-ils.storage.direct.action.circulation.update', $circ);
382 return $U->DB_UPDATE_FAILED unless defined $stat;
387 __PACKAGE__->register_method(
388 method => "create_in_house_use",
389 api_name => 'open-ils.circ.in_house_use.create',
391 Creates an in-house use action.
392 @param $authtoken The login session key
393 @param params A hash of params including
394 'location' The org unit id where the in-house use occurs
395 'copyid' The copy in question
396 'count' The number of in-house uses to apply to this copy
397 @return An array of id's representing the id's of the newly created
398 in-house use objects or an event on an error
401 sub create_in_house_use {
402 my( $self, $client, $authtoken, $params ) = @_;
404 my( $staff, $evt, $copy );
405 my $org = $params->{location};
406 my $copyid = $params->{copyid};
407 my $count = $params->{count} || 1;
408 my $use_time = $params->{use_time} || 'now';
411 my $barcode = $params->{barcode};
412 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
417 ($staff, $evt) = $U->checkses($authtoken);
420 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
423 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
426 $logger->activity("User " . $staff->id .
427 " creating $count in-house use(s) for copy $copyid at location $org");
429 if( $use_time ne 'now' ) {
430 $use_time = clense_ISO8601($use_time);
431 $logger->debug("in_house_use setting use time to $use_time");
436 my $ihu = Fieldmapper::action::in_house_use->new;
439 $ihu->staff($staff->id);
440 $ihu->org_unit($org);
441 $ihu->use_time($use_time);
443 my $id = $U->simplereq(
445 'open-ils.storage.direct.action.in_house_use.create', $ihu );
447 return $U->DB_UPDATE_FAILED($ihu) unless $id;
456 __PACKAGE__->register_method(
457 method => "view_circs",
458 api_name => "open-ils.circ.copy_checkout_history.retrieve",
460 Retrieves the last X circs for a given copy
461 @param authtoken The login session key
462 @param copyid The copy to check
463 @param count How far to go back in the item history
464 @return An array of circ ids
468 my( $self, $client, $authtoken, $copyid, $count ) = @_;
470 my( $requestor, $evt ) = $U->checksesperm(
471 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
474 return [] unless $count;
476 my $circs = $U->storagereq(
477 'open-ils.storage.direct.action.circulation.search_where.atomic',
479 target_copy => $copyid,
480 # opac_renewal => 'f',
481 # desk_renewal => 'f',
482 # phone_renewal => 'f',
486 order_by => "xact_start DESC"
490 # push(@users, $_->usr) for @$circs;
497 __PACKAGE__->register_method(
498 method => "circ_count",
499 api_name => "open-ils.circ.circulation.count",
501 Returns the number of times the item has circulated
502 @param copyid The copy to check
506 my( $self, $client, $copyid ) = @_;
507 my $circs = $U->storagereq(
508 'open-ils.storage.id_list.action.circulation.search.target_copy.atomic', $copyid );
509 return scalar @$circs;
514 __PACKAGE__->register_method(
515 method => 'fetch_notes',
516 api_name => 'open-ils.circ.copy_note.retrieve.all',
518 Returns an array of copy note objects.
519 @param args A named hash of parameters including:
520 authtoken : Required if viewing non-public notes
521 itemid : The id of the item whose notes we want to retrieve
522 pub : True if all the caller wants are public notes
523 @return An array of note objects
526 __PACKAGE__->register_method(
527 method => 'fetch_notes',
528 api_name => 'open-ils.circ.call_number_note.retrieve.all',
529 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
531 __PACKAGE__->register_method(
532 method => 'fetch_notes',
533 api_name => 'open-ils.circ.title_note.retrieve.all',
534 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
537 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
539 my( $self, $connection, $args ) = @_;
541 my $id = $$args{itemid};
542 my $authtoken = $$args{authtoken};
545 if( $self->api_name =~ /copy/ ) {
547 return $U->storagereq(
548 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
549 { owning_copy => $id, pub => 't' } );
551 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
553 return $U->storagereq(
554 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
557 } elsif( $self->api_name =~ /call_number/ ) {
559 return $U->storagereq(
560 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
561 { call_number => $id, pub => 't' } );
563 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
565 return $U->storagereq(
566 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
569 } elsif( $self->api_name =~ /title/ ) {
571 return $U->storagereq(
572 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
573 { record => $id, pub => 't' } );
575 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
577 return $U->storagereq(
578 'open-ils.storage.direct.biblio.record_note.search.record.atomic', $id );
585 __PACKAGE__->register_method(
586 method => 'has_notes',
587 api_name => 'open-ils.circ.copy.has_notes');
588 __PACKAGE__->register_method(
589 method => 'has_notes',
590 api_name => 'open-ils.circ.call_number.has_notes');
591 __PACKAGE__->register_method(
592 method => 'has_notes',
593 api_name => 'open-ils.circ.title.has_notes');
597 my( $self, $conn, $authtoken, $id ) = @_;
598 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
599 return $editor->event unless $editor->checkauth;
601 my $n = $editor->search_asset_copy_note(
602 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
604 $n = $editor->search_asset_call_number_note(
605 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
607 $n = $editor->search_biblio_record_note(
608 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
613 __PACKAGE__->register_method(
614 method => 'create_copy_note',
615 api_name => 'open-ils.circ.copy_note.create',
617 Creates a new copy note
618 @param authtoken The login session key
619 @param note The note object to create
620 @return The id of the new note object
623 sub create_copy_note {
624 my( $self, $connection, $authtoken, $note ) = @_;
625 my( $cnowner, $requestor, $evt );
627 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
629 ($requestor, $evt) = $U->checkses($authtoken);
631 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
634 $note->create_date('now');
635 $note->creator($requestor->id);
636 $note->pub( ($note->pub) ? 't' : 'f' );
638 my $id = $U->storagereq(
639 'open-ils.storage.direct.asset.copy_note.create', $note );
640 return $U->DB_UPDATE_FAILED($note) unless $id;
642 $logger->activity("User ".$requestor->id." created a new copy ".
643 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
648 __PACKAGE__->register_method(
649 method => 'delete_copy_note',
650 api_name => 'open-ils.circ.copy_note.delete',
652 Deletes an existing copy note
653 @param authtoken The login session key
654 @param noteid The id of the note to delete
655 @return 1 on success - Event otherwise.
658 sub delete_copy_note {
659 my( $self, $conn, $authtoken, $noteid ) = @_;
660 my( $requestor, $note, $owner, $evt );
662 ($requestor, $evt) = $U->checkses($authtoken);
665 ($note, $evt) = $U->fetch_copy_note($noteid);
668 if( $note->creator ne $requestor->id ) {
669 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
671 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
675 my $stat = $U->storagereq(
676 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
677 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
679 $logger->activity("User ".$requestor->id." deleted copy note $noteid");