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
297 my $copy_price = $copy->price || 0;
299 # If the copy has a price configured, charge said price to the user
301 $evt = _make_bill($session, $copy_price, 'Lost Materials', $circ->id);
305 # if the location that owns the copy has a processing fee, charge the user
306 my $owner = $U->fetch_copy_owner($copy->id);
307 $logger->info("circ fetching org settings for $owner to determine processing fee");
309 my $settings = $U->simplereq(
310 'open-ils.actor', 'open-ils.actor.org_unit.settings.retrieve', $owner );
311 my $fee = $settings->{'circ.lost_materials_processing_fee'} || 0;
314 $evt = _make_bill($session, $fee, 'Lost Materials Processing Fee', $circ->id);
318 $circ->stop_fines("LOST");
323 my( $session, $amount, $type, $xactid ) = @_;
325 $logger->activity("The system is charging $amount ".
326 " [$type] for lost materials on circulation $xactid");
328 my $bill = Fieldmapper::money::billing->new;
330 $bill->xact($xactid);
331 $bill->amount($amount);
332 $bill->billing_type($type); # - XXX these strings should be configurable some day
333 $bill->note('SYSTEM GENERATED');
335 my $id = $session->request(
336 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
338 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
342 sub _set_circ_claims_returned {
343 my( $reqr, $circ, $session, $backdate ) = @_;
345 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
347 $circ->stop_fines("CLAIMSRETURNED");
349 $logger->activity("user ".$reqr->id.
350 " marking circ". $circ->id. " as claims returned");
352 # allow the caller to backdate the circulation and void any fines
353 # that occurred after the backdate
355 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
356 $backdate, $circ, $reqr, $session );
364 __PACKAGE__->register_method (
365 method => 'set_circ_due_date',
366 api_name => 'open-ils.circ.circulation.due_date.update',
368 Updates the due_date on the given circ
370 @param circid The id of the circ to update
371 @param date The timestamp of the new due date
375 sub set_circ_due_date {
376 my( $s, $c, $authtoken, $circid, $date ) = @_;
377 my ($circ, $evt) = $U->fetch_circulation($circid);
381 ($reqr, $evt) = $U->checkses($authtoken);
384 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
387 $date = clense_ISO8601($date);
388 $logger->activity("user ".$reqr->id.
389 " updating due_date on circ $circid: $date");
391 $circ->due_date($date);
392 my $stat = $U->storagereq(
393 'open-ils.storage.direct.action.circulation.update', $circ);
394 return $U->DB_UPDATE_FAILED unless defined $stat;
399 __PACKAGE__->register_method(
400 method => "create_in_house_use",
401 api_name => 'open-ils.circ.in_house_use.create',
403 Creates an in-house use action.
404 @param $authtoken The login session key
405 @param params A hash of params including
406 'location' The org unit id where the in-house use occurs
407 'copyid' The copy in question
408 'count' The number of in-house uses to apply to this copy
409 @return An array of id's representing the id's of the newly created
410 in-house use objects or an event on an error
413 sub create_in_house_use {
414 my( $self, $client, $authtoken, $params ) = @_;
416 my( $staff, $evt, $copy );
417 my $org = $params->{location};
418 my $copyid = $params->{copyid};
419 my $count = $params->{count} || 1;
420 my $use_time = $params->{use_time} || 'now';
423 my $barcode = $params->{barcode};
424 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
429 ($staff, $evt) = $U->checkses($authtoken);
432 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
435 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
438 $logger->activity("User " . $staff->id .
439 " creating $count in-house use(s) for copy $copyid at location $org");
441 if( $use_time ne 'now' ) {
442 $use_time = clense_ISO8601($use_time);
443 $logger->debug("in_house_use setting use time to $use_time");
448 my $ihu = Fieldmapper::action::in_house_use->new;
451 $ihu->staff($staff->id);
452 $ihu->org_unit($org);
453 $ihu->use_time($use_time);
455 my $id = $U->simplereq(
457 'open-ils.storage.direct.action.in_house_use.create', $ihu );
459 return $U->DB_UPDATE_FAILED($ihu) unless $id;
468 __PACKAGE__->register_method(
469 method => "view_circs",
470 api_name => "open-ils.circ.copy_checkout_history.retrieve",
472 Retrieves the last X circs for a given copy
473 @param authtoken The login session key
474 @param copyid The copy to check
475 @param count How far to go back in the item history
476 @return An array of circ ids
480 my( $self, $client, $authtoken, $copyid, $count ) = @_;
482 my( $requestor, $evt ) = $U->checksesperm(
483 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
486 return [] unless $count;
488 my $circs = $U->storagereq(
489 'open-ils.storage.direct.action.circulation.search_where.atomic',
491 target_copy => $copyid,
492 # opac_renewal => 'f',
493 # desk_renewal => 'f',
494 # phone_renewal => 'f',
498 order_by => "xact_start DESC"
502 # push(@users, $_->usr) for @$circs;
509 __PACKAGE__->register_method(
510 method => "circ_count",
511 api_name => "open-ils.circ.circulation.count",
513 Returns the number of times the item has circulated
514 @param copyid The copy to check
518 my( $self, $client, $copyid ) = @_;
519 my $e = OpenILS::Utils::Editor->new;
520 my $circs = $e->search_action_circulation({target_copy => $copyid}, {idlist=>1});
521 return scalar @$circs;
526 __PACKAGE__->register_method(
527 method => 'fetch_notes',
528 api_name => 'open-ils.circ.copy_note.retrieve.all',
530 Returns an array of copy note objects.
531 @param args A named hash of parameters including:
532 authtoken : Required if viewing non-public notes
533 itemid : The id of the item whose notes we want to retrieve
534 pub : True if all the caller wants are public notes
535 @return An array of note objects
538 __PACKAGE__->register_method(
539 method => 'fetch_notes',
540 api_name => 'open-ils.circ.call_number_note.retrieve.all',
541 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
543 __PACKAGE__->register_method(
544 method => 'fetch_notes',
545 api_name => 'open-ils.circ.title_note.retrieve.all',
546 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
549 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
551 my( $self, $connection, $args ) = @_;
553 my $id = $$args{itemid};
554 my $authtoken = $$args{authtoken};
557 if( $self->api_name =~ /copy/ ) {
559 return $U->storagereq(
560 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
561 { owning_copy => $id, pub => 't' } );
563 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
565 return $U->storagereq(
566 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
569 } elsif( $self->api_name =~ /call_number/ ) {
571 return $U->storagereq(
572 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
573 { call_number => $id, pub => 't' } );
575 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
577 return $U->storagereq(
578 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
581 } elsif( $self->api_name =~ /title/ ) {
583 return $U->storagereq(
584 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
585 { record => $id, pub => 't' } );
587 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
589 return $U->storagereq(
590 'open-ils.storage.direct.biblio.record_note.search.record.atomic', $id );
597 __PACKAGE__->register_method(
598 method => 'has_notes',
599 api_name => 'open-ils.circ.copy.has_notes');
600 __PACKAGE__->register_method(
601 method => 'has_notes',
602 api_name => 'open-ils.circ.call_number.has_notes');
603 __PACKAGE__->register_method(
604 method => 'has_notes',
605 api_name => 'open-ils.circ.title.has_notes');
609 my( $self, $conn, $authtoken, $id ) = @_;
610 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
611 return $editor->event unless $editor->checkauth;
613 my $n = $editor->search_asset_copy_note(
614 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
616 $n = $editor->search_asset_call_number_note(
617 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
619 $n = $editor->search_biblio_record_note(
620 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
625 __PACKAGE__->register_method(
626 method => 'create_copy_note',
627 api_name => 'open-ils.circ.copy_note.create',
629 Creates a new copy note
630 @param authtoken The login session key
631 @param note The note object to create
632 @return The id of the new note object
635 sub create_copy_note {
636 my( $self, $connection, $authtoken, $note ) = @_;
637 my( $cnowner, $requestor, $evt );
639 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
641 ($requestor, $evt) = $U->checkses($authtoken);
643 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
646 $note->create_date('now');
647 $note->creator($requestor->id);
648 $note->pub( ($note->pub) ? 't' : 'f' );
650 my $id = $U->storagereq(
651 'open-ils.storage.direct.asset.copy_note.create', $note );
652 return $U->DB_UPDATE_FAILED($note) unless $id;
654 $logger->activity("User ".$requestor->id." created a new copy ".
655 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
660 __PACKAGE__->register_method(
661 method => 'delete_copy_note',
662 api_name => 'open-ils.circ.copy_note.delete',
664 Deletes an existing copy note
665 @param authtoken The login session key
666 @param noteid The id of the note to delete
667 @return 1 on success - Event otherwise.
670 sub delete_copy_note {
671 my( $self, $conn, $authtoken, $noteid ) = @_;
672 my( $requestor, $note, $owner, $evt );
674 ($requestor, $evt) = $U->checkses($authtoken);
677 ($note, $evt) = $U->fetch_copy_note($noteid);
680 if( $note->creator ne $requestor->id ) {
681 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
683 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
687 my $stat = $U->storagereq(
688 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
689 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
691 $logger->activity("User ".$requestor->id." deleted copy note $noteid");