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::Survey;
7 use OpenILS::Application::Circ::StatCat;
8 use OpenILS::Application::Circ::Holds;
9 use OpenILS::Application::Circ::Money;
10 use OpenILS::Application::Circ::NonCat;
11 use OpenILS::Application::Circ::CopyLocations;
14 use DateTime::Format::ISO8601;
16 use OpenILS::Application::AppUtils;
17 my $apputils = "OpenILS::Application::AppUtils";
19 use OpenSRF::Utils qw/:datetime/;
20 use OpenILS::Utils::ModsParser;
22 use OpenSRF::EX qw(:try);
23 use OpenSRF::Utils::Logger qw(:logger);
24 use OpenILS::Utils::Fieldmapper;
25 use OpenILS::Utils::Editor;
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
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};
152 if( $user_id ne $e->requestor->id ) {
153 $data = $e->search_action_circulation(
154 $search, {checkperm=>1, permorg=>$patron->home_ou})
158 $data = $e->search_action_circulation($search);
165 __PACKAGE__->register_method(
166 method => "title_from_transaction",
167 api_name => "open-ils.circ.circ_transaction.find_title",
168 NOTES => <<" NOTES");
169 Returns a mods object for the title that is linked to from the
170 copy from the hold that created the given transaction
173 sub title_from_transaction {
174 my( $self, $client, $login_session, $transactionid ) = @_;
176 my( $user, $circ, $title, $evt );
178 ( $user, $evt ) = $apputils->checkses( $login_session );
181 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
184 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
187 return $apputils->record_to_mvr($title);
191 __PACKAGE__->register_method(
192 method => "set_circ_lost",
193 api_name => "open-ils.circ.circulation.set_lost",
194 NOTES => <<" NOTES");
195 Params are login, barcode
196 login must have SET_CIRC_LOST perms
197 Sets a circulation to lost
200 __PACKAGE__->register_method(
201 method => "set_circ_lost",
202 api_name => "open-ils.circ.circulation.set_claims_returned",
203 NOTES => <<" NOTES");
204 Params are login, barcode
205 login must have SET_CIRC_MISSING perms
206 Sets a circulation to lost
210 my( $self, $client, $login, $args ) = @_;
211 my( $user, $circ, $copy, $evt );
213 my $barcode = $$args{barcode};
214 my $backdate = $$args{backdate};
216 ( $user, $evt ) = $U->checkses($login);
219 # Grab the related copy
220 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
223 my $isclaims = $self->api_name =~ /claims_returned/;
224 my $islost = $self->api_name =~ /lost/;
225 my $session = $U->start_db_session();
227 # grab the circulation
228 ( $circ ) = $U->fetch_open_circulation( $copy->id );
229 return 1 unless $circ;
232 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
237 $evt = _set_circ_claims_returned(
238 $user, $circ, $session, $backdate );
241 # $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
242 # return $evt if $evt;
243 # $circ->stop_fines("CLAIMSRETURNED");
245 # $logger->activity("user ".$user->id." marking circ". $circ->id. " as claims returned");
247 # # allow the caller to backdate the circulation and void any fines
248 # # that occurred after the backdate
250 # OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
251 # $backdate, $circ, $user, $session );
255 # ($patron, $evt) = $U->fetch_user($circ->usr);
256 # return $evt if $evt;
257 # $patron->claims_returned_count(
258 # $patron->claims_returned_count + 1 );
260 # my $stat = $U->storagereq(
261 # 'open-ils.storage.direct.actor.user.update', $patron );
262 # return $U->DB_UPDATE_FAILED($patron) unless $stat;
266 $circ->stop_fines_time('now') unless $circ->stop_fines_time('now');
267 my $s = $session->request(
268 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
270 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
271 $U->commit_db_session($session);
277 my( $copy, $circ, $reqr, $session ) = @_;
279 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
282 $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
283 " lost for circ ". $circ->id. " and checking for necessary charges");
285 my $newstat = $U->copy_status_from_name('lost');
286 if( $copy->status ne $newstat->id ) {
288 $copy->status($newstat);
292 session => $session);
295 # if the copy has a price defined and/or a processing fee, bill the patron
296 my $amount = $copy->price || 0;
297 my $owner = $U->fetch_copy_owner($copy->id);
298 $logger->info("circ fetching org settings for $owner to determine processing fee");
299 my $settings = $U->simplereq(
301 'open-ils.actor.org_unit.settings.retrieve', $owner );
302 my $f = $settings->{'circ.processing_fee'} || 0;
307 $logger->activity("The system is charging $amount ".
308 "for lost materials on circulation ".$circ->id);
310 my $bill = Fieldmapper::money::billing->new;
312 $bill->xact( $circ->id );
313 $bill->amount( $amount );
314 $bill->billing_type('Lost materials'); # - these strings should be configurable some day
315 $bill->note('SYSTEM GENERATED');
317 my $id = $session->request(
318 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
320 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
323 $circ->stop_fines("LOST");
327 sub _set_circ_claims_returned {
328 my( $reqr, $circ, $session, $backdate ) = @_;
330 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
332 $circ->stop_fines("CLAIMSRETURNED");
334 $logger->activity("user ".$reqr->id.
335 " marking circ". $circ->id. " as claims returned");
337 # allow the caller to backdate the circulation and void any fines
338 # that occurred after the backdate
340 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
341 $backdate, $circ, $reqr, $session );
349 __PACKAGE__->register_method (
350 method => 'set_circ_due_date',
351 api_name => 'open-ils.circ.circulation.due_date.update',
353 Updates the due_date on the given circ
355 @param circid The id of the circ to update
356 @param date The timestamp of the new due date
360 sub set_circ_due_date {
361 my( $s, $c, $authtoken, $circid, $date ) = @_;
362 my ($circ, $evt) = $U->fetch_circulation($circid);
366 ($reqr, $evt) = $U->checkses($authtoken);
369 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
372 $date = clense_ISO8601($date);
373 $logger->activity("user ".$reqr->id.
374 " updating due_date on circ $circid: $date");
376 $circ->due_date($date);
377 my $stat = $U->storagereq(
378 'open-ils.storage.direct.action.circulation.update', $circ);
379 return $U->DB_UPDATE_FAILED unless defined $stat;
384 __PACKAGE__->register_method(
385 method => "create_in_house_use",
386 api_name => 'open-ils.circ.in_house_use.create',
388 Creates an in-house use action.
389 @param $authtoken The login session key
390 @param params A hash of params including
391 'location' The org unit id where the in-house use occurs
392 'copyid' The copy in question
393 'count' The number of in-house uses to apply to this copy
394 @return An array of id's representing the id's of the newly created
395 in-house use objects or an event on an error
398 sub create_in_house_use {
399 my( $self, $client, $authtoken, $params ) = @_;
401 my( $staff, $evt, $copy );
402 my $org = $params->{location};
403 my $copyid = $params->{copyid};
404 my $count = $params->{count} || 1;
405 my $use_time = $params->{use_time} || 'now';
408 my $barcode = $params->{barcode};
409 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
414 ($staff, $evt) = $U->checkses($authtoken);
417 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
420 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
423 $logger->activity("User " . $staff->id .
424 " creating $count in-house use(s) for copy $copyid at location $org");
426 if( $use_time ne 'now' ) {
427 $use_time = clense_ISO8601($use_time);
428 $logger->debug("in_house_use setting use time to $use_time");
433 my $ihu = Fieldmapper::action::in_house_use->new;
436 $ihu->staff($staff->id);
437 $ihu->org_unit($org);
438 $ihu->use_time($use_time);
440 my $id = $U->simplereq(
442 'open-ils.storage.direct.action.in_house_use.create', $ihu );
444 return $U->DB_UPDATE_FAILED($ihu) unless $id;
453 __PACKAGE__->register_method(
454 method => "view_circs",
455 api_name => "open-ils.circ.copy_checkout_history.retrieve",
457 Retrieves the last X circs for a given copy
458 @param authtoken The login session key
459 @param copyid The copy to check
460 @param count How far to go back in the item history
461 @return An array of circ ids
465 my( $self, $client, $authtoken, $copyid, $count ) = @_;
467 my( $requestor, $evt ) = $U->checksesperm(
468 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
471 return [] unless $count;
473 my $circs = $U->storagereq(
474 'open-ils.storage.direct.action.circulation.search_where.atomic',
476 target_copy => $copyid,
477 # opac_renewal => 'f',
478 # desk_renewal => 'f',
479 # phone_renewal => 'f',
483 order_by => "xact_start DESC"
487 # push(@users, $_->usr) for @$circs;
494 __PACKAGE__->register_method(
495 method => "circ_count",
496 api_name => "open-ils.circ.circulation.count",
498 Returns the number of times the item has circulated
499 @param copyid The copy to check
503 my( $self, $client, $copyid ) = @_;
504 my $circs = $U->storagereq(
505 'open-ils.storage.id_list.action.circulation.search.target_copy.atomic', $copyid );
506 return scalar @$circs;
511 __PACKAGE__->register_method(
512 method => 'fetch_notes',
513 api_name => 'open-ils.circ.copy_note.retrieve.all',
515 Returns an array of copy note objects.
516 @param args A named hash of parameters including:
517 authtoken : Required if viewing non-public notes
518 itemid : The id of the item whose notes we want to retrieve
519 pub : True if all the caller wants are public notes
520 @return An array of note objects
523 __PACKAGE__->register_method(
524 method => 'fetch_notes',
525 api_name => 'open-ils.circ.call_number_note.retrieve.all',
526 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
528 __PACKAGE__->register_method(
529 method => 'fetch_notes',
530 api_name => 'open-ils.circ.title_note.retrieve.all',
531 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
534 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
536 my( $self, $connection, $args ) = @_;
538 my $id = $$args{itemid};
539 my $authtoken = $$args{authtoken};
542 if( $self->api_name =~ /copy/ ) {
544 return $U->storagereq(
545 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
546 { owning_copy => $id, pub => 't' } );
548 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
550 return $U->storagereq(
551 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
554 } elsif( $self->api_name =~ /call_number/ ) {
556 return $U->storagereq(
557 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
558 { call_number => $id, pub => 't' } );
560 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
562 return $U->storagereq(
563 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
566 } elsif( $self->api_name =~ /title/ ) {
568 return $U->storagereq(
569 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
570 { record => $id, pub => 't' } );
572 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
574 return $U->storagereq(
575 'open-ils.storage.direct.biblio.record_note.search.record.atomic', $id );
582 __PACKAGE__->register_method(
583 method => 'has_notes',
584 api_name => 'open-ils.circ.copy.has_notes');
585 __PACKAGE__->register_method(
586 method => 'has_notes',
587 api_name => 'open-ils.circ.call_number.has_notes');
588 __PACKAGE__->register_method(
589 method => 'has_notes',
590 api_name => 'open-ils.circ.title.has_notes');
594 my( $self, $conn, $authtoken, $id ) = @_;
595 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
596 return $editor->event unless $editor->checkauth;
598 my $n = $editor->search_asset_copy_note(
599 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
601 $n = $editor->search_asset_call_number_note(
602 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
604 $n = $editor->search_biblio_record_note(
605 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
610 __PACKAGE__->register_method(
611 method => 'create_copy_note',
612 api_name => 'open-ils.circ.copy_note.create',
614 Creates a new copy note
615 @param authtoken The login session key
616 @param note The note object to create
617 @return The id of the new note object
620 sub create_copy_note {
621 my( $self, $connection, $authtoken, $note ) = @_;
622 my( $cnowner, $requestor, $evt );
624 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
626 ($requestor, $evt) = $U->checkses($authtoken);
628 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
631 $note->create_date('now');
632 $note->creator($requestor->id);
633 $note->pub( ($note->pub) ? 't' : 'f' );
635 my $id = $U->storagereq(
636 'open-ils.storage.direct.asset.copy_note.create', $note );
637 return $U->DB_UPDATE_FAILED($note) unless $id;
639 $logger->activity("User ".$requestor->id." created a new copy ".
640 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
645 __PACKAGE__->register_method(
646 method => 'delete_copy_note',
647 api_name => 'open-ils.circ.copy_note.delete',
649 Deletes an existing copy note
650 @param authtoken The login session key
651 @param noteid The id of the note to delete
652 @return 1 on success - Event otherwise.
655 sub delete_copy_note {
656 my( $self, $conn, $authtoken, $noteid ) = @_;
657 my( $requestor, $note, $owner, $evt );
659 ($requestor, $evt) = $U->checkses($authtoken);
662 ($note, $evt) = $U->fetch_copy_note($noteid);
665 if( $note->creator ne $requestor->id ) {
666 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
668 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
672 my $stat = $U->storagereq(
673 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
674 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
676 $logger->activity("User ".$requestor->id." deleted copy note $noteid");