1 package OpenILS::Application::Circ;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
4 use strict; use warnings;
6 use OpenILS::Application::Circ::Circulate;
7 use OpenILS::Application::Circ::Survey;
8 use OpenILS::Application::Circ::StatCat;
9 use OpenILS::Application::Circ::Holds;
10 use OpenILS::Application::Circ::HoldNotify;
11 use OpenILS::Application::Circ::CreditCard;
12 use OpenILS::Application::Circ::Money;
13 use OpenILS::Application::Circ::NonCat;
14 use OpenILS::Application::Circ::CopyLocations;
15 use OpenILS::Application::Circ::CircCommon;
18 use DateTime::Format::ISO8601;
20 use OpenILS::Application::AppUtils;
22 use OpenSRF::Utils qw/:datetime/;
23 use OpenSRF::AppSession;
24 use OpenILS::Utils::ModsParser;
26 use OpenSRF::EX qw(:try);
27 use OpenSRF::Utils::Logger qw(:logger);
28 use OpenILS::Utils::Fieldmapper;
29 use OpenILS::Utils::Editor;
30 use OpenILS::Utils::CStoreEditor q/:funcs/;
31 use OpenILS::Const qw/:const/;
32 use OpenSRF::Utils::SettingsClient;
33 use OpenILS::Application::Cat::AssetCommon;
35 my $apputils = "OpenILS::Application::AppUtils";
39 # ------------------------------------------------------------------------
40 # Top level Circ package;
41 # ------------------------------------------------------------------------
45 OpenILS::Application::Circ::Circulate->initialize();
49 __PACKAGE__->register_method(
50 method => 'retrieve_circ',
51 api_name => 'open-ils.circ.retrieve',
53 Retrieve a circ object by id
54 @param authtoken Login session key
55 @pararm circid The id of the circ object
59 my( $s, $c, $a, $i ) = @_;
60 my $e = new_editor(authtoken => $a);
61 return $e->event unless $e->checkauth;
62 my $circ = $e->retrieve_action_circulation($i) or return $e->event;
63 if( $e->requestor->id ne $circ->usr ) {
64 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
70 __PACKAGE__->register_method(
71 method => 'fetch_circ_mods',
72 api_name => 'open-ils.circ.circ_modifier.retrieve.all');
74 my($self, $conn, $args) = @_;
75 my $mods = new_editor()->retrieve_all_config_circ_modifier;
76 return [ map {$_->code} @$mods ] unless $$args{full};
80 __PACKAGE__->register_method(
81 method => 'fetch_bill_types',
82 api_name => 'open-ils.circ.billing_type.retrieve.all');
83 sub fetch_bill_types {
84 my $conf = OpenSRF::Utils::SettingsClient->new;
85 return $conf->config_value(
86 'apps', 'open-ils.circ', 'app_settings', 'billing_types', 'type' );
90 __PACKAGE__->register_method(
91 method => 'ranged_billing_types',
92 api_name => 'open-ils.circ.billing_type.ranged.retrieve.all');
94 sub ranged_billing_types {
95 my($self, $conn, $auth, $org_id, $depth) = @_;
96 my $e = new_editor(authtoken => $auth);
97 return $e->event unless $e->checkauth;
98 return $e->event unless $e->allowed('VIEW_BILLING_TYPE', $org_id);
99 return $e->search_config_billing_type(
100 {owner => $U->get_org_full_path($org_id, $depth)});
105 # ------------------------------------------------------------------------
106 # Returns an array of {circ, record} hashes checked out by the user.
107 # ------------------------------------------------------------------------
108 __PACKAGE__->register_method(
109 method => "checkouts_by_user",
110 api_name => "open-ils.circ.actor.user.checked_out",
112 NOTES => <<" NOTES");
113 Returns a list of open circulations as a pile of objects. Each object
114 contains the relevant copy, circ, and record
117 sub checkouts_by_user {
118 my($self, $client, $auth, $user_id) = @_;
120 my $e = new_editor(authtoken=>$auth);
121 return $e->event unless $e->checkauth;
123 my $circ_ids = $e->search_action_circulation(
125 checkin_time => undef,
127 {stop_fines => undef},
128 {stop_fines => ['MAXFINES','LONGOVERDUE']}
134 for my $id (@$circ_ids) {
135 my $circ = $e->retrieve_action_circulation([
139 circ => ['target_copy'],
140 acp => ['call_number'],
146 # un-flesh for consistency
147 my $c = $circ->target_copy;
148 $circ->target_copy($c->id);
150 my $cn = $c->call_number;
151 $c->call_number($cn->id);
159 record => $U->record_to_mvr($t)
169 __PACKAGE__->register_method(
170 method => "checkouts_by_user_slim",
171 api_name => "open-ils.circ.actor.user.checked_out.slim",
172 NOTES => <<" NOTES");
173 Returns a list of open circulation objects
177 sub checkouts_by_user_slim {
178 my( $self, $client, $user_session, $user_id ) = @_;
180 my( $requestor, $target, $copy, $record, $evt );
182 ( $requestor, $target, $evt ) =
183 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
186 $logger->debug( 'User ' . $requestor->id .
187 " retrieving checked out items for user " . $target->id );
189 # XXX Make the call correct..
190 return $apputils->simplereq(
192 "open-ils.cstore.direct.action.open_circulation.search.atomic",
193 { usr => $target->id, checkin_time => undef } );
194 # { usr => $target->id } );
198 __PACKAGE__->register_method(
199 method => "checkouts_by_user_opac",
200 api_name => "open-ils.circ.actor.user.checked_out.opac",);
203 sub checkouts_by_user_opac {
204 my( $self, $client, $auth, $user_id ) = @_;
206 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
207 return $e->event unless $e->checkauth;
208 $user_id ||= $e->requestor->id;
209 return $e->event unless
210 my $patron = $e->retrieve_actor_user($user_id);
213 my $search = {usr => $user_id, stop_fines => undef};
215 if( $user_id ne $e->requestor->id ) {
216 $data = $e->search_action_circulation(
217 $search, {checkperm=>1, permorg=>$patron->home_ou})
221 $data = $e->search_action_circulation($search);
228 __PACKAGE__->register_method(
229 method => "title_from_transaction",
230 api_name => "open-ils.circ.circ_transaction.find_title",
231 NOTES => <<" NOTES");
232 Returns a mods object for the title that is linked to from the
233 copy from the hold that created the given transaction
236 sub title_from_transaction {
237 my( $self, $client, $login_session, $transactionid ) = @_;
239 my( $user, $circ, $title, $evt );
241 ( $user, $evt ) = $apputils->checkses( $login_session );
244 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
247 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
250 return $apputils->record_to_mvr($title);
255 __PACKAGE__->register_method(
256 method => "new_set_circ_lost",
257 api_name => "open-ils.circ.circulation.set_lost",
259 Sets the copy and related open circulation to lost
261 @param args : barcode
266 # ---------------------------------------------------------------------
267 # Sets a circulation to lost. updates copy status to lost
268 # applies copy and/or prcoessing fees depending on org settings
269 # ---------------------------------------------------------------------
270 sub new_set_circ_lost {
271 my( $self, $conn, $auth, $args ) = @_;
273 my $e = new_editor(authtoken=>$auth, xact=>1);
274 return $e->die_event unless $e->checkauth;
276 my $copy = $e->search_asset_copy({barcode=>$$args{barcode}, deleted=>'f'})->[0]
277 or return $e->die_event;
279 my $evt = OpenILS::Application::Cat::AssetCommon->set_item_lost($e, $copy->id);
287 __PACKAGE__->register_method(
288 method => "set_circ_claims_returned",
289 api_name => "open-ils.circ.circulation.set_claims_returned",
291 desc => q/Sets the circ for a given item as claims returned
292 If a backdate is provided, overdue fines will be voided
293 back to the backdate/,
295 {desc => 'Authentication token', type => 'string'},
296 {desc => 'Arguments, including "barcode" and optional "backdate"', type => 'object'}
298 return => {desc => q/1 on success, failure event on error, and
299 PATRON_EXCEEDS_CLAIMS_RETURN_COUNT if the patron exceeds the
300 configured claims return maximum/}
304 __PACKAGE__->register_method(
305 method => "set_circ_claims_returned",
306 api_name => "open-ils.circ.circulation.set_claims_returned.override",
308 desc => q/This adds support for overrideing the configured max
309 claims returned amount.
310 @see open-ils.circ.circulation.set_claims_returned./,
314 sub set_circ_claims_returned {
315 my( $self, $conn, $auth, $args ) = @_;
317 my $e = new_editor(authtoken=>$auth, xact=>1);
318 return $e->die_event unless $e->checkauth;
320 my $barcode = $$args{barcode};
321 my $backdate = $$args{backdate};
323 my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0]
324 or return $e->die_event;
326 my $circ = $e->search_action_circulation(
327 {checkin_time => undef, target_copy => $copy->id})->[0]
328 or return $e->die_event;
330 $backdate = $circ->due_date if $$args{use_due_date};
332 $logger->info("marking circ for item $barcode as claims returned".
333 (($backdate) ? " with backdate $backdate" : ''));
335 my $patron = $e->retrieve_actor_user($circ->usr);
336 my $max_count = $U->ou_ancestor_setting_value(
337 $circ->circ_lib, 'circ.max_patron_claim_return_count', $e);
339 # If the patron has too instances of many claims returned,
340 # require an override to continue. A configured max of
341 # 0 means all attempts require an override
342 if(defined $max_count and $patron->claims_returned_count >= $max_count) {
344 if($self->api_name =~ /override/) {
346 # see if we're allowed to override
347 return $e->die_event unless
348 $e->allowed('SET_CIRC_CLAIMS_RETURNED.override', $circ->circ_lib);
352 # exit early and return the max claims return event
354 return OpenILS::Event->new(
355 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT',
357 patron_count => $patron->claims_returned_count,
358 max_count => $max_count
364 $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib)
365 or return $e->die_event;
367 $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
368 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
371 $backdate = cleanse_ISO8601($backdate);
373 my $original_date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($circ->due_date));
374 my $new_date = DateTime::Format::ISO8601->new->parse_datetime($backdate);
375 $backdate = $new_date->ymd . 'T' . $original_date->strftime('%T%z');
376 if ($backdate =~ /^(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d-\d\d)(\d\d)$/) {
377 $backdate = "$1:$2"; # put a colon in the timestamp component for DateTime::Format::ISO8601->parse_datetime
380 # make it look like the circ stopped at the cliams returned time
381 $circ->stop_fines_time($backdate);
382 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
386 $e->update_action_circulation($circ) or return $e->die_event;
388 # see if there is a configured post-claims-return copy status
389 if(my $stat = $U->ou_ancestor_setting_value($circ->circ_lib, 'circ.claim_return.copy_status')) {
390 $copy->status($stat);
391 $copy->edit_date('now');
392 $copy->editor($e->requestor->id);
393 $e->update_asset_copy($copy) or return $e->die_event;
401 __PACKAGE__->register_method(
402 method => "post_checkin_backdate_circ",
403 api_name => "open-ils.circ.post_checkin_backdate",
405 desc => q/Back-date an already checked in circulation/,
407 {desc => 'Authentication token', type => 'string'},
408 {desc => 'Circ ID', type => 'number'},
409 {desc => 'ISO8601 backdate', type => 'string'},
411 return => {desc => q/1 on success, failure event on error/}
415 __PACKAGE__->register_method(
416 method => "post_checkin_backdate_circ",
417 api_name => "open-ils.circ.post_checkin_backdate.batch",
420 desc => q/@see open-ils.circ.post_checkin_backdate. Batch mode/,
422 {desc => 'Authentication token', type => 'string'},
423 {desc => 'List of Circ ID', type => 'array'},
424 {desc => 'ISO8601 backdate', type => 'string'},
426 return => {desc => q/Set of: 1 on success, failure event on error/}
431 sub post_checkin_backdate_circ {
432 my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
433 my $e = new_editor(authtoken=>$auth);
434 return $e->die_event unless $e->checkauth;
435 if($self->api_name =~ /batch/) {
436 foreach my $c (@$circ_id) {
437 $conn->respond(post_checkin_backdate_circ_impl($e, $c, $backdate));
440 $conn->respond_complete(post_checkin_backdate_circ_impl($e, $circ_id, $backdate));
448 sub post_checkin_backdate_circ_impl {
449 my($e, $circ_id, $backdate) = @_;
453 my $circ = $e->retrieve_action_circulation($circ_id)
454 or return $e->die_event;
456 # anyone with checkin perms can backdate (more restrictive?)
457 return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
459 # don't allow back-dating an open circulation
460 return OpenILS::Event->new('BAD_PARAMS') unless
461 $backdate and $circ->checkin_time;
463 # update the checkin and stop_fines times to reflect the new backdate
464 $circ->stop_fines_time(cleanse_ISO8601($backdate));
465 $circ->checkin_time(cleanse_ISO8601($backdate));
466 $e->update_action_circulation($circ) or return $e->die_event;
468 # now void the overdues "erased" by the back-dating
469 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
472 # If the circ was closed before and the balance owned !=0, re-open the transaction
473 $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
482 __PACKAGE__->register_method (
483 method => 'set_circ_due_date',
484 api_name => 'open-ils.circ.circulation.due_date.update',
486 Updates the due_date on the given circ
488 @param circid The id of the circ to update
489 @param date The timestamp of the new due date
493 sub set_circ_due_date {
494 my( $self, $conn, $auth, $circ_id, $date ) = @_;
496 my $e = new_editor(xact=>1, authtoken=>$auth);
497 return $e->die_event unless $e->checkauth;
498 my $circ = $e->retrieve_action_circulation($circ_id)
499 or return $e->die_event;
501 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
502 $date = cleanse_ISO8601($date);
504 if (!(interval_to_seconds($circ->duration) % 86400)) { # duration is divisible by days
505 my $original_date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($circ->due_date));
506 my $new_date = DateTime::Format::ISO8601->new->parse_datetime($date);
507 $date = $new_date->ymd . 'T' . $original_date->strftime('%T%z');
508 if ($date =~ /^(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d-\d\d)(\d\d)$/) {
509 $date = "$1:$2"; # put a colon in the timestamp component for DateTime::Format::ISO8601->parse_datetime
513 $circ->due_date($date);
514 $e->update_action_circulation($circ) or return $e->die_event;
521 __PACKAGE__->register_method(
522 method => "create_in_house_use",
523 api_name => 'open-ils.circ.in_house_use.create',
525 Creates an in-house use action.
526 @param $authtoken The login session key
527 @param params A hash of params including
528 'location' The org unit id where the in-house use occurs
529 'copyid' The copy in question
530 'count' The number of in-house uses to apply to this copy
531 @return An array of id's representing the id's of the newly created
532 in-house use objects or an event on an error
535 __PACKAGE__->register_method(
536 method => "create_in_house_use",
537 api_name => 'open-ils.circ.non_cat_in_house_use.create',
541 sub create_in_house_use {
542 my( $self, $client, $auth, $params ) = @_;
545 my $org = $params->{location};
546 my $copyid = $params->{copyid};
547 my $count = $params->{count} || 1;
548 my $nc_type = $params->{non_cat_type};
549 my $use_time = $params->{use_time} || 'now';
551 my $e = new_editor(xact=>1,authtoken=>$auth);
552 return $e->event unless $e->checkauth;
553 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
555 my $non_cat = 1 if $self->api_name =~ /non_cat/;
559 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
561 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
567 if( $use_time ne 'now' ) {
568 $use_time = cleanse_ISO8601($use_time);
569 $logger->debug("in_house_use setting use time to $use_time");
580 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
581 $ihu->item_type($nc_type);
582 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
583 $cmeth = "create_action_non_cat_in_house_use";
586 $ihu = Fieldmapper::action::in_house_use->new;
588 $method = 'open-ils.storage.direct.action.in_house_use.create';
589 $cmeth = "create_action_in_house_use";
592 $ihu->staff($e->requestor->id);
593 $ihu->org_unit($org);
594 $ihu->use_time($use_time);
596 $ihu = $e->$cmeth($ihu) or return $e->event;
597 push( @ids, $ihu->id );
608 __PACKAGE__->register_method(
609 method => "view_circs",
610 api_name => "open-ils.circ.copy_checkout_history.retrieve",
612 Retrieves the last X circs for a given copy
613 @param authtoken The login session key
614 @param copyid The copy to check
615 @param count How far to go back in the item history
616 @return An array of circ ids
619 # ----------------------------------------------------------------------
620 # Returns $count most recent circs. If count exceeds the configured
621 # max, use the configured max instead
622 # ----------------------------------------------------------------------
624 my( $self, $client, $authtoken, $copyid, $count ) = @_;
626 my $e = new_editor(authtoken => $authtoken);
627 return $e->event unless $e->checkauth;
629 my $copy = $e->retrieve_asset_copy([
632 flesh_fields => {acp => ['call_number']}
634 ]) or return $e->event;
636 return $e->event unless $e->allowed(
637 'VIEW_COPY_CHECKOUT_HISTORY',
638 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
639 $copy->circ_lib : $copy->call_number->owning_lib);
641 my $max_history = $U->ou_ancestor_setting_value(
642 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
644 if(defined $max_history) {
645 $count = $max_history unless defined $count and $count < $max_history;
647 $count = 4 unless defined $count;
650 return $e->search_action_circulation([
651 {target_copy => $copyid},
652 {limit => $count, order_by => { circ => "xact_start DESC" }}
657 __PACKAGE__->register_method(
658 method => "circ_count",
659 api_name => "open-ils.circ.circulation.count",
661 Returns the number of times the item has circulated
662 @param copyid The copy to check
666 my( $self, $client, $copyid, $range ) = @_;
667 my $e = OpenILS::Utils::Editor->new;
668 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
673 __PACKAGE__->register_method(
674 method => 'fetch_notes',
676 api_name => 'open-ils.circ.copy_note.retrieve.all',
678 Returns an array of copy note objects.
679 @param args A named hash of parameters including:
680 authtoken : Required if viewing non-public notes
681 itemid : The id of the item whose notes we want to retrieve
682 pub : True if all the caller wants are public notes
683 @return An array of note objects
686 __PACKAGE__->register_method(
687 method => 'fetch_notes',
688 api_name => 'open-ils.circ.call_number_note.retrieve.all',
689 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
691 __PACKAGE__->register_method(
692 method => 'fetch_notes',
693 api_name => 'open-ils.circ.title_note.retrieve.all',
694 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
697 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
699 my( $self, $connection, $args ) = @_;
701 my $id = $$args{itemid};
702 my $authtoken = $$args{authtoken};
705 if( $self->api_name =~ /copy/ ) {
707 return $U->cstorereq(
708 'open-ils.cstore.direct.asset.copy_note.search.atomic',
709 { owning_copy => $id, pub => 't' } );
711 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
713 return $U->cstorereq(
714 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
717 } elsif( $self->api_name =~ /call_number/ ) {
719 return $U->cstorereq(
720 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
721 { call_number => $id, pub => 't' } );
723 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
725 return $U->cstorereq(
726 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
729 } elsif( $self->api_name =~ /title/ ) {
731 return $U->cstorereq(
732 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
733 { record => $id, pub => 't' } );
735 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
737 return $U->cstorereq(
738 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
745 __PACKAGE__->register_method(
746 method => 'has_notes',
747 api_name => 'open-ils.circ.copy.has_notes');
748 __PACKAGE__->register_method(
749 method => 'has_notes',
750 api_name => 'open-ils.circ.call_number.has_notes');
751 __PACKAGE__->register_method(
752 method => 'has_notes',
753 api_name => 'open-ils.circ.title.has_notes');
757 my( $self, $conn, $authtoken, $id ) = @_;
758 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
759 return $editor->event unless $editor->checkauth;
761 my $n = $editor->search_asset_copy_note(
762 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
764 $n = $editor->search_asset_call_number_note(
765 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
767 $n = $editor->search_biblio_record_note(
768 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
775 __PACKAGE__->register_method(
776 method => 'create_copy_note',
777 api_name => 'open-ils.circ.copy_note.create',
779 Creates a new copy note
780 @param authtoken The login session key
781 @param note The note object to create
782 @return The id of the new note object
785 sub create_copy_note {
786 my( $self, $connection, $authtoken, $note ) = @_;
788 my $e = new_editor(xact=>1, authtoken=>$authtoken);
789 return $e->event unless $e->checkauth;
790 my $copy = $e->retrieve_asset_copy(
794 flesh_fields => { 'acp' => ['call_number'] }
799 return $e->event unless
800 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
802 $note->create_date('now');
803 $note->creator($e->requestor->id);
804 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
807 $e->create_asset_copy_note($note) or return $e->event;
813 __PACKAGE__->register_method(
814 method => 'delete_copy_note',
815 api_name => 'open-ils.circ.copy_note.delete',
817 Deletes an existing copy note
818 @param authtoken The login session key
819 @param noteid The id of the note to delete
820 @return 1 on success - Event otherwise.
822 sub delete_copy_note {
823 my( $self, $conn, $authtoken, $noteid ) = @_;
825 my $e = new_editor(xact=>1, authtoken=>$authtoken);
826 return $e->die_event unless $e->checkauth;
828 my $note = $e->retrieve_asset_copy_note([
832 'acpn' => [ 'owning_copy' ],
833 'acp' => [ 'call_number' ],
836 ]) or return $e->die_event;
838 if( $note->creator ne $e->requestor->id ) {
839 return $e->die_event unless
840 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
843 $e->delete_asset_copy_note($note) or return $e->die_event;
849 __PACKAGE__->register_method(
850 method => 'age_hold_rules',
851 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
855 my( $self, $conn ) = @_;
856 return new_editor()->retrieve_all_config_rules_age_hold_protect();
861 __PACKAGE__->register_method(
862 method => 'copy_details_barcode',
864 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
865 sub copy_details_barcode {
866 my( $self, $conn, $auth, $barcode ) = @_;
867 my $e = new_editor();
868 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
869 return $e->event unless $cid;
870 return copy_details( $self, $conn, $auth, $cid );
874 __PACKAGE__->register_method(
875 method => 'copy_details',
876 api_name => 'open-ils.circ.copy_details.retrieve');
879 my( $self, $conn, $auth, $copy_id ) = @_;
880 my $e = new_editor(authtoken=>$auth);
881 return $e->event unless $e->checkauth;
883 my $flesh = { flesh => 1 };
885 my $copy = $e->retrieve_asset_copy(
891 acp => ['call_number'],
895 ]) or return $e->event;
898 # De-flesh the copy for backwards compatibility
900 my $vol = $copy->call_number;
902 $copy->call_number($vol->id);
903 my $record = $vol->record;
905 $vol->record($record->id);
906 $mvr = $U->record_to_mvr($record);
911 my $hold = $e->search_action_hold_request(
913 current_copy => $copy_id,
914 capture_time => { "!=" => undef },
915 fulfillment_time => undef,
916 cancel_time => undef,
920 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
922 my $transit = $e->search_action_transit_copy(
923 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
925 # find the latest circ, open or closed
926 my $circ = $e->search_action_circulation(
928 { target_copy => $copy_id },
934 'checkin_workstation',
937 'recurring_fine_rule'
940 order_by => { circ => 'xact_start desc' },
960 __PACKAGE__->register_method(
961 method => 'mark_item',
962 api_name => 'open-ils.circ.mark_item_damaged',
964 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
965 @param authtoken The login session key
966 @param copy_id The ID of the copy to mark as damaged
967 @return 1 on success - Event otherwise.
970 __PACKAGE__->register_method(
971 method => 'mark_item',
972 api_name => 'open-ils.circ.mark_item_missing',
974 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
975 @param authtoken The login session key
976 @param copy_id The ID of the copy to mark as missing
977 @return 1 on success - Event otherwise.
980 __PACKAGE__->register_method(
981 method => 'mark_item',
982 api_name => 'open-ils.circ.mark_item_bindery',
984 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
985 @param authtoken The login session key
986 @param copy_id The ID of the copy to mark as bindery
987 @return 1 on success - Event otherwise.
990 __PACKAGE__->register_method(
991 method => 'mark_item',
992 api_name => 'open-ils.circ.mark_item_on_order',
994 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
995 @param authtoken The login session key
996 @param copy_id The ID of the copy to mark as on order
997 @return 1 on success - Event otherwise.
1000 __PACKAGE__->register_method(
1001 method => 'mark_item',
1002 api_name => 'open-ils.circ.mark_item_ill',
1004 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
1005 @param authtoken The login session key
1006 @param copy_id The ID of the copy to mark as inter-library loan
1007 @return 1 on success - Event otherwise.
1010 __PACKAGE__->register_method(
1011 method => 'mark_item',
1012 api_name => 'open-ils.circ.mark_item_cataloging',
1014 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
1015 @param authtoken The login session key
1016 @param copy_id The ID of the copy to mark as cataloging
1017 @return 1 on success - Event otherwise.
1020 __PACKAGE__->register_method(
1021 method => 'mark_item',
1022 api_name => 'open-ils.circ.mark_item_reserves',
1024 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
1025 @param authtoken The login session key
1026 @param copy_id The ID of the copy to mark as reserves
1027 @return 1 on success - Event otherwise.
1030 __PACKAGE__->register_method(
1031 method => 'mark_item',
1032 api_name => 'open-ils.circ.mark_item_discard',
1034 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
1035 @param authtoken The login session key
1036 @param copy_id The ID of the copy to mark as discard
1037 @return 1 on success - Event otherwise.
1042 my( $self, $conn, $auth, $copy_id, $args ) = @_;
1043 my $e = new_editor(authtoken=>$auth, xact =>1);
1044 return $e->die_event unless $e->checkauth;
1047 my $copy = $e->retrieve_asset_copy([
1049 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1050 or return $e->die_event;
1053 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1054 $copy->circ_lib : $copy->call_number->owning_lib;
1056 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
1059 my $perm = 'MARK_ITEM_MISSING';
1060 my $stat = OILS_COPY_STATUS_MISSING;
1062 if( $self->api_name =~ /damaged/ ) {
1063 $perm = 'MARK_ITEM_DAMAGED';
1064 $stat = OILS_COPY_STATUS_DAMAGED;
1065 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1066 return $evt if $evt;
1068 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1069 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1071 } elsif ( $self->api_name =~ /bindery/ ) {
1072 $perm = 'MARK_ITEM_BINDERY';
1073 $stat = OILS_COPY_STATUS_BINDERY;
1074 } elsif ( $self->api_name =~ /on_order/ ) {
1075 $perm = 'MARK_ITEM_ON_ORDER';
1076 $stat = OILS_COPY_STATUS_ON_ORDER;
1077 } elsif ( $self->api_name =~ /ill/ ) {
1078 $perm = 'MARK_ITEM_ILL';
1079 $stat = OILS_COPY_STATUS_ILL;
1080 } elsif ( $self->api_name =~ /cataloging/ ) {
1081 $perm = 'MARK_ITEM_CATALOGING';
1082 $stat = OILS_COPY_STATUS_CATALOGING;
1083 } elsif ( $self->api_name =~ /reserves/ ) {
1084 $perm = 'MARK_ITEM_RESERVES';
1085 $stat = OILS_COPY_STATUS_RESERVES;
1086 } elsif ( $self->api_name =~ /discard/ ) {
1087 $perm = 'MARK_ITEM_DISCARD';
1088 $stat = OILS_COPY_STATUS_DISCARD;
1092 $copy->status($stat);
1093 $copy->edit_date('now');
1094 $copy->editor($e->requestor->id);
1096 $e->update_asset_copy($copy) or return $e->die_event;
1098 my $holds = $e->search_action_hold_request(
1100 current_copy => $copy->id,
1101 fulfillment_time => undef,
1102 cancel_time => undef,
1108 $logger->debug("resetting holds that target the marked copy");
1109 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1114 sub handle_mark_damaged {
1115 my($e, $copy, $owning_lib, $args) = @_;
1117 my $apply = $args->{apply_fines} || '';
1118 return undef if $apply eq 'noapply';
1120 my $new_amount = $args->{override_amount};
1121 my $new_btype = $args->{override_btype};
1122 my $new_note = $args->{override_note};
1124 # grab the last circulation
1125 my $circ = $e->search_action_circulation([
1126 { target_copy => $copy->id},
1128 order_by => {circ => "xact_start DESC"},
1130 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1134 return undef unless $circ;
1136 my $charge_price = $U->ou_ancestor_setting_value(
1137 $owning_lib, 'circ.charge_on_damaged', $e);
1139 my $proc_fee = $U->ou_ancestor_setting_value(
1140 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1142 my $void_overdue = $U->ou_ancestor_setting_value(
1143 $owning_lib, 'circ.damaged.void_ovedue', $e) || 0;
1145 return undef unless $charge_price or $proc_fee;
1147 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1148 my $total = $copy_price + $proc_fee;
1152 if($new_amount and $new_btype) {
1154 # Allow staff to override the amount to charge for a damaged item
1155 # Consider the case where the item is only partially damaged
1156 # This value is meant to take the place of the item price and
1157 # optional processing fee.
1159 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1160 $e, $new_amount, $new_btype, 'Damaged Item Override', $circ->id, $new_note);
1161 return $evt if $evt;
1165 if($charge_price and $copy_price) {
1166 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1167 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1168 return $evt if $evt;
1172 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1173 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1174 return $evt if $evt;
1178 # the assumption is that you would not void the overdues unless you
1179 # were also charging for the item and/or applying a processing fee
1181 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
1182 return $evt if $evt;
1185 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1186 return $evt if $evt;
1188 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1189 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1194 return OpenILS::Event->new('DAMAGE_CHARGE',
1208 # ----------------------------------------------------------------------
1209 __PACKAGE__->register_method(
1210 method => 'magic_fetch',
1211 api_name => 'open-ils.agent.fetch'
1214 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1217 my( $self, $conn, $auth, $args ) = @_;
1218 my $e = new_editor( authtoken => $auth );
1219 return $e->event unless $e->checkauth;
1221 my $hint = $$args{hint};
1222 my $id = $$args{id};
1224 # Is the call allowed to fetch this type of object?
1225 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1227 # Find the class the implements the given hint
1228 my ($class) = grep {
1229 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1231 $class =~ s/Fieldmapper:://og;
1232 $class =~ s/::/_/og;
1233 my $method = "retrieve_$class";
1235 my $obj = $e->$method($id) or return $e->event;
1238 # ----------------------------------------------------------------------
1241 __PACKAGE__->register_method(
1242 method => "fleshed_circ_retrieve",
1244 api_name => "open-ils.circ.fleshed.retrieve",);
1246 sub fleshed_circ_retrieve {
1247 my( $self, $client, $id ) = @_;
1248 my $e = new_editor();
1249 my $circ = $e->retrieve_action_circulation(
1255 circ => [ qw/ target_copy / ],
1256 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1257 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1258 acn => [ qw/ record / ],
1262 ) or return $e->event;
1264 my $copy = $circ->target_copy;
1265 my $vol = $copy->call_number;
1266 my $rec = $circ->target_copy->call_number->record;
1268 $vol->record($rec->id);
1269 $copy->call_number($vol->id);
1270 $circ->target_copy($copy->id);
1274 if( $rec->id == OILS_PRECAT_RECORD ) {
1278 $mvr = $U->record_to_mvr($rec);
1279 $rec->marc(''); # drop the bulky marc data
1293 __PACKAGE__->register_method(
1294 method => "test_batch_circ_events",
1295 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1298 # method for testing the behavior of a given event definition
1299 sub test_batch_circ_events {
1300 my($self, $conn, $auth, $event_def, $barcode) = @_;
1302 my $e = new_editor(authtoken => $auth);
1303 return $e->event unless $e->checkauth;
1304 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1306 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1307 or return $e->event;
1309 my $circ = $e->search_action_circulation(
1310 {target_copy => $copy->id, checkin_time => undef})->[0]
1311 or return $e->event;
1313 return undef unless $circ;
1315 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1319 __PACKAGE__->register_method(
1320 method => "fire_circ_events",
1321 api_name => "open-ils.circ.fire_circ_trigger_events",
1323 General event def runner for circ objects. If no event def ID
1324 is provided, the hook will be used to find the best event_def
1325 match based on the context org unit
1329 __PACKAGE__->register_method(
1330 method => "fire_circ_events",
1331 api_name => "open-ils.circ.fire_hold_trigger_events",
1333 General event def runner for hold objects. If no event def ID
1334 is provided, the hook will be used to find the best event_def
1335 match based on the context org unit
1339 __PACKAGE__->register_method(
1340 method => "fire_circ_events",
1341 api_name => "open-ils.circ.fire_user_trigger_events",
1343 General event def runner for user objects. If no event def ID
1344 is provided, the hook will be used to find the best event_def
1345 match based on the context org unit
1350 sub fire_circ_events {
1351 my($self, $conn, $auth, $org_id, $event_def, $hook, $granularity, $target_ids, $user_data) = @_;
1353 my $e = new_editor(authtoken => $auth);
1354 return $e->event unless $e->checkauth;
1358 if($self->api_name =~ /hold/) {
1359 return $e->event unless $e->allowed('VIEW_HOLD', $org_id);
1360 $targets = $e->batch_retrieve_action_hold_request($target_ids);
1361 } elsif($self->api_name =~ /user/) {
1362 return $e->event unless $e->allowed('VIEW_USER', $org_id);
1363 $targets = $e->batch_retrieve_actor_user($target_ids);
1365 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $org_id);
1366 $targets = $e->batch_retrieve_action_circulation($target_ids);
1369 return undef unless @$targets;
1370 return $U->fire_object_event($event_def, $hook, $targets, $org_id, $granularity, $user_data);
1373 __PACKAGE__->register_method(
1374 method => "user_payments_list",
1375 api_name => "open-ils.circ.user_payments.filtered.batch",
1378 desc => q/Returns a fleshed, date-limited set of all payments a user
1379 has made. By default, ordered by payment date. Optionally
1380 ordered by other columns in the top-level "mp" object/,
1382 {desc => 'Authentication token', type => 'string'},
1383 {desc => 'User ID', type => 'number'},
1384 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1386 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1387 and the related fully-realized payment object (e.g money.cash_payment)/}
1391 sub user_payments_list {
1392 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1394 my $e = new_editor(authtoken => $auth);
1395 return $e->event unless $e->checkauth;
1397 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1398 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1400 $order_by ||= ['payment_ts'];
1402 # all payments by user, between start_date and end_date
1403 my $payments = $e->json_query({
1404 select => {mp => ['id']},
1408 fkey => 'xact', field => 'id'}
1412 '+mbt' => {usr => $user_id},
1413 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1415 order_by => {mp => $order_by}
1418 for my $payment_id (@$payments) {
1419 my $payment = $e->retrieve_money_payment([
1427 'credit_card_payment',
1442 $conn->respond($payment);
1449 __PACKAGE__->register_method(
1450 method => "retrieve_circ_chain",
1451 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1454 desc => q/Given a circulation, this returns all circulation objects
1455 that are part of the same chain of renewals./,
1457 {desc => 'Authentication token', type => 'string'},
1458 {desc => 'Circ ID', type => 'number'},
1460 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1464 __PACKAGE__->register_method(
1465 method => "retrieve_circ_chain",
1466 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ.summary",
1468 desc => q/Given a circulation, this returns a summary of the circulation objects
1469 that are part of the same chain of renewals./,
1471 {desc => 'Authentication token', type => 'string'},
1472 {desc => 'Circ ID', type => 'number'},
1474 return => {desc => q/Circulation Chain Summary/}
1478 sub retrieve_circ_chain {
1479 my($self, $conn, $auth, $circ_id) = @_;
1481 my $e = new_editor(authtoken => $auth);
1482 return $e->event unless $e->checkauth;
1483 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1485 if($self->api_name =~ /summary/) {
1486 return $U->create_circ_chain_summary($e, $circ_id);
1490 my $chain = $e->json_query({from => ['action.circ_chain', $circ_id]});
1492 for my $circ_info (@$chain) {
1493 my $circ = Fieldmapper::action::circulation->new;
1494 $circ->$_($circ_info->{$_}) for keys %$circ_info;
1495 $conn->respond($circ);
1502 __PACKAGE__->register_method(
1503 method => "retrieve_prev_circ_chain",
1504 api_name => "open-ils.circ.prev_renewal_chain.retrieve_by_circ",
1507 desc => q/Given a circulation, this returns all circulation objects
1508 that are part of the previous chain of renewals./,
1510 {desc => 'Authentication token', type => 'string'},
1511 {desc => 'Circ ID', type => 'number'},
1513 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1517 __PACKAGE__->register_method(
1518 method => "retrieve_prev_circ_chain",
1519 api_name => "open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary",
1521 desc => q/Given a circulation, this returns a summary of the circulation objects
1522 that are part of the previous chain of renewals./,
1524 {desc => 'Authentication token', type => 'string'},
1525 {desc => 'Circ ID', type => 'number'},
1527 return => {desc => q/Object containing Circulation Chain Summary and User Id/}
1531 sub retrieve_prev_circ_chain {
1532 my($self, $conn, $auth, $circ_id) = @_;
1534 my $e = new_editor(authtoken => $auth);
1535 return $e->event unless $e->checkauth;
1536 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1538 if($self->api_name =~ /summary/) {
1539 my $first_circ = $e->json_query({from => ['action.circ_chain', $circ_id]})->[0];
1540 my $target_copy = $$first_circ{'target_copy'};
1541 my $usr = $$first_circ{'usr'};
1542 my $last_circ_from_prev_chain = $e->json_query({
1543 'select' => { 'circ' => ['id'] },
1546 target_copy => $target_copy,
1547 xact_start => { '<' => $$first_circ{'xact_start'} }
1549 'order_by' => [{ 'class'=>'circ', 'field'=>'xact_start', 'direction'=>'desc' }],
1552 return undef unless $last_circ_from_prev_chain;
1553 return undef unless $$last_circ_from_prev_chain{'id'};
1554 my $sum = $e->json_query({from => ['action.summarize_circ_chain', $$last_circ_from_prev_chain{'id'}]})->[0];
1555 return undef unless $sum;
1556 my $obj = Fieldmapper::action::circ_chain_summary->new;
1557 $obj->$_($sum->{$_}) for keys %$sum;
1558 return { 'summary' => $obj, 'usr' => $usr };
1562 my $first_circ = $e->json_query({from => ['action.circ_chain', $circ_id]})->[0];
1563 my $target_copy = $$first_circ{'target_copy'};
1564 my $last_circ_from_prev_chain = $e->json_query({
1565 'select' => { 'circ' => ['id'] },
1568 target_copy => $target_copy,
1569 xact_start => { '<' => $$first_circ{'xact_start'} }
1571 'order_by' => [{ 'class'=>'circ', 'field'=>'xact_start', 'direction'=>'desc' }],
1574 return undef unless $last_circ_from_prev_chain;
1575 return undef unless $$last_circ_from_prev_chain{'id'};
1576 my $chain = $e->json_query({from => ['action.circ_chain', $$last_circ_from_prev_chain{'id'}]});
1578 for my $circ_info (@$chain) {
1579 my $circ = Fieldmapper::action::circulation->new;
1580 $circ->$_($circ_info->{$_}) for keys %$circ_info;
1581 $conn->respond($circ);
1591 # {"select":{"acp":["id"],"circ":[{"aggregate":true,"transform":"count","alias":"count","column":"id"}]},"from":{"acp":{"circ":{"field":"target_copy","fkey":"id","type":"left"},"acn"{"field":"id","fkey":"call_number"}}},"where":{"+acn":{"record":200057}}