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::Money;
12 use OpenILS::Application::Circ::NonCat;
13 use OpenILS::Application::Circ::CopyLocations;
14 use OpenILS::Application::Circ::CircCommon;
17 use DateTime::Format::ISO8601;
19 use OpenILS::Application::AppUtils;
21 use OpenSRF::Utils qw/:datetime/;
22 use OpenSRF::AppSession;
23 use OpenILS::Utils::ModsParser;
25 use OpenSRF::EX qw(:try);
26 use OpenSRF::Utils::Logger qw(:logger);
27 use OpenILS::Utils::Fieldmapper;
28 use OpenILS::Utils::Editor;
29 use OpenILS::Utils::CStoreEditor q/:funcs/;
30 use OpenILS::Const qw/:const/;
31 use OpenSRF::Utils::SettingsClient;
32 use OpenILS::Application::Cat::AssetCommon;
34 my $apputils = "OpenILS::Application::AppUtils";
38 # ------------------------------------------------------------------------
39 # Top level Circ package;
40 # ------------------------------------------------------------------------
44 OpenILS::Application::Circ::Circulate->initialize();
48 __PACKAGE__->register_method(
49 method => 'retrieve_circ',
50 api_name => 'open-ils.circ.retrieve',
52 Retrieve a circ object by id
53 @param authtoken Login session key
54 @pararm circid The id of the circ object
58 my( $s, $c, $a, $i ) = @_;
59 my $e = new_editor(authtoken => $a);
60 return $e->event unless $e->checkauth;
61 my $circ = $e->retrieve_action_circulation($i) or return $e->event;
62 if( $e->requestor->id ne $circ->usr ) {
63 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
69 __PACKAGE__->register_method(
70 method => 'fetch_circ_mods',
71 api_name => 'open-ils.circ.circ_modifier.retrieve.all');
73 my($self, $conn, $args) = @_;
74 my $mods = new_editor()->retrieve_all_config_circ_modifier;
75 return [ map {$_->code} @$mods ] unless $$args{full};
79 __PACKAGE__->register_method(
80 method => 'fetch_bill_types',
81 api_name => 'open-ils.circ.billing_type.retrieve.all');
82 sub fetch_bill_types {
83 my $conf = OpenSRF::Utils::SettingsClient->new;
84 return $conf->config_value(
85 'apps', 'open-ils.circ', 'app_settings', 'billing_types', 'type' );
89 __PACKAGE__->register_method(
90 method => 'ranged_billing_types',
91 api_name => 'open-ils.circ.billing_type.ranged.retrieve.all');
93 sub ranged_billing_types {
94 my($self, $conn, $auth, $org_id, $depth) = @_;
95 my $e = new_editor(authtoken => $auth);
96 return $e->event unless $e->checkauth;
97 return $e->event unless $e->allowed('VIEW_BILLING_TYPE', $org_id);
98 return $e->search_config_billing_type(
99 {owner => $U->get_org_full_path($org_id, $depth)});
104 # ------------------------------------------------------------------------
105 # Returns an array of {circ, record} hashes checked out by the user.
106 # ------------------------------------------------------------------------
107 __PACKAGE__->register_method(
108 method => "checkouts_by_user",
109 api_name => "open-ils.circ.actor.user.checked_out",
110 NOTES => <<" NOTES");
111 Returns a list of open circulations as a pile of objects. Each object
112 contains the relevant copy, circ, and record
115 sub checkouts_by_user {
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 my $circs = $apputils->simplereq(
126 "open-ils.cstore.direct.action.open_circulation.search.atomic",
127 { usr => $target->id, checkin_time => undef } );
128 # { usr => $target->id } );
131 for my $circ (@$circs) {
133 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
136 $logger->debug("Retrieving record for copy " . $circ->target_copy);
138 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
141 my $mods = $apputils->record_to_mvr($record);
143 push( @results, { copy => $copy, circ => $circ, record => $mods } );
152 __PACKAGE__->register_method(
153 method => "checkouts_by_user_slim",
154 api_name => "open-ils.circ.actor.user.checked_out.slim",
155 NOTES => <<" NOTES");
156 Returns a list of open circulation objects
160 sub checkouts_by_user_slim {
161 my( $self, $client, $user_session, $user_id ) = @_;
163 my( $requestor, $target, $copy, $record, $evt );
165 ( $requestor, $target, $evt ) =
166 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
169 $logger->debug( 'User ' . $requestor->id .
170 " retrieving checked out items for user " . $target->id );
172 # XXX Make the call correct..
173 return $apputils->simplereq(
175 "open-ils.cstore.direct.action.open_circulation.search.atomic",
176 { usr => $target->id, checkin_time => undef } );
177 # { usr => $target->id } );
181 __PACKAGE__->register_method(
182 method => "checkouts_by_user_opac",
183 api_name => "open-ils.circ.actor.user.checked_out.opac",);
186 sub checkouts_by_user_opac {
187 my( $self, $client, $auth, $user_id ) = @_;
189 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
190 return $e->event unless $e->checkauth;
191 $user_id ||= $e->requestor->id;
192 return $e->event unless
193 my $patron = $e->retrieve_actor_user($user_id);
196 my $search = {usr => $user_id, stop_fines => undef};
198 if( $user_id ne $e->requestor->id ) {
199 $data = $e->search_action_circulation(
200 $search, {checkperm=>1, permorg=>$patron->home_ou})
204 $data = $e->search_action_circulation($search);
211 __PACKAGE__->register_method(
212 method => "title_from_transaction",
213 api_name => "open-ils.circ.circ_transaction.find_title",
214 NOTES => <<" NOTES");
215 Returns a mods object for the title that is linked to from the
216 copy from the hold that created the given transaction
219 sub title_from_transaction {
220 my( $self, $client, $login_session, $transactionid ) = @_;
222 my( $user, $circ, $title, $evt );
224 ( $user, $evt ) = $apputils->checkses( $login_session );
227 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
230 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
233 return $apputils->record_to_mvr($title);
238 __PACKAGE__->register_method(
239 method => "new_set_circ_lost",
240 api_name => "open-ils.circ.circulation.set_lost",
242 Sets the copy and related open circulation to lost
244 @param args : barcode
249 # ---------------------------------------------------------------------
250 # Sets a circulation to lost. updates copy status to lost
251 # applies copy and/or prcoessing fees depending on org settings
252 # ---------------------------------------------------------------------
253 sub new_set_circ_lost {
254 my( $self, $conn, $auth, $args ) = @_;
256 my $e = new_editor(authtoken=>$auth, xact=>1);
257 return $e->die_event unless $e->checkauth;
259 my $copy = $e->search_asset_copy({barcode=>$$args{barcode}, deleted=>'f'})->[0]
260 or return $e->die_event;
262 my $evt = OpenILS::Application::Cat::AssetCommon->set_item_lost($e, $copy->id);
270 __PACKAGE__->register_method(
271 method => "set_circ_claims_returned",
272 api_name => "open-ils.circ.circulation.set_claims_returned",
274 desc => q/Sets the circ for a given item as claims returned
275 If a backdate is provided, overdue fines will be voided
276 back to the backdate/,
278 {desc => 'Authentication token', type => 'string'},
279 {desc => 'Arguments, including "barcode" and optional "backdate"', type => 'object'}
281 return => {desc => q/1 on success, failure event on error, and
282 PATRON_EXCEEDS_CLAIMS_RETURN_COUNT if the patron exceeds the
283 configured claims return maximum/}
287 __PACKAGE__->register_method(
288 method => "set_circ_claims_returned",
289 api_name => "open-ils.circ.circulation.set_claims_returned.override",
291 desc => q/This adds support for overrideing the configured max
292 claims returned amount.
293 @see open-ils.circ.circulation.set_claims_returned./,
297 sub set_circ_claims_returned {
298 my( $self, $conn, $auth, $args ) = @_;
300 my $e = new_editor(authtoken=>$auth, xact=>1);
301 return $e->die_event unless $e->checkauth;
303 my $barcode = $$args{barcode};
304 my $backdate = $$args{backdate};
306 my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0]
307 or return $e->die_event;
309 my $circ = $e->search_action_circulation(
310 {checkin_time => undef, target_copy => $copy->id})->[0]
311 or return $e->die_event;
313 $backdate = $circ->due_date if $$args{use_due_date};
315 $logger->info("marking circ for item $barcode as claims returned".
316 (($backdate) ? " with backdate $backdate" : ''));
318 my $patron = $e->retrieve_actor_user($circ->usr);
319 my $max_count = $U->ou_ancestor_setting_value(
320 $circ->circ_lib, 'circ.max_patron_claim_return_count', $e);
322 # If the patron has too instances of many claims returned,
323 # require an override to continue. A configured max of
324 # 0 means all attempts require an override
325 if(defined $max_count and $patron->claims_returned_count >= $max_count) {
327 if($self->api_name =~ /override/) {
329 # see if we're allowed to override
330 return $e->die_event unless
331 $e->allowed('SET_CIRC_CLAIMS_RETURNED.override', $circ->circ_lib);
335 # exit early and return the max claims return event
337 return OpenILS::Event->new(
338 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT',
340 patron_count => $patron->claims_returned_count,
341 max_count => $max_count
347 $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib)
348 or return $e->die_event;
350 $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
351 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
354 # make it look like the circ stopped at the cliams returned time
355 $circ->stop_fines_time(clense_ISO8601($backdate));
356 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
360 $e->update_action_circulation($circ) or return $e->die_event;
362 # see if there is a configured post-claims-return copy status
363 if(my $stat = $U->ou_ancestor_setting_value($circ->circ_lib, 'circ.claim_return.copy_status')) {
364 $copy->status($stat);
365 $copy->edit_date('now');
366 $copy->editor($e->requestor->id);
367 $e->update_asset_copy($copy) or return $e->die_event;
375 __PACKAGE__->register_method(
376 method => "post_checkin_backdate_circ",
377 api_name => "open-ils.circ.post_checkin_backdate",
379 desc => q/Back-date an already checked in circulation/,
381 {desc => 'Authentication token', type => 'string'},
382 {desc => 'Circ ID', type => 'number'},
383 {desc => 'ISO8601 backdate', type => 'string'},
385 return => {desc => q/1 on success, failure event on error/}
389 __PACKAGE__->register_method(
390 method => "post_checkin_backdate_circ",
391 api_name => "open-ils.circ.post_checkin_backdate.batch",
394 desc => q/@see open-ils.circ.post_checkin_backdate. Batch mode/,
396 {desc => 'Authentication token', type => 'string'},
397 {desc => 'List of Circ ID', type => 'array'},
398 {desc => 'ISO8601 backdate', type => 'string'},
400 return => {desc => q/Set of: 1 on success, failure event on error/}
405 sub post_checkin_backdate_circ {
406 my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
407 my $e = new_editor(authtoken=>$auth);
408 return $e->die_event unless $e->checkauth;
409 if($self->api_name =~ /batch/) {
410 foreach my $c (@$circ_id) {
411 $conn->respond(post_checkin_backdate_circ_impl($e, $c, $backdate));
414 $conn->respond_complete(post_checkin_backdate_circ_impl($e, $circ_id, $backdate));
422 sub post_checkin_backdate_circ_impl {
423 my($e, $circ_id, $backdate) = @_;
427 my $circ = $e->retrieve_action_circulation($circ_id)
428 or return $e->die_event;
430 # anyone with checkin perms can backdate (more restrictive?)
431 return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
433 # don't allow back-dating an open circulation
434 return OpenILS::Event->new('BAD_PARAMS') unless
435 $backdate and $circ->checkin_time;
437 # update the checkin and stop_fines times to reflect the new backdate
438 $circ->stop_fines_time(clense_ISO8601($backdate));
439 $circ->checkin_time(clense_ISO8601($backdate));
440 $e->update_action_circulation($circ) or return $e->die_event;
442 # now void the overdues "erased" by the back-dating
443 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
446 # If the circ was closed before and the balance owned !=0, re-open the transaction
447 $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
456 __PACKAGE__->register_method (
457 method => 'set_circ_due_date',
458 api_name => 'open-ils.circ.circulation.due_date.update',
460 Updates the due_date on the given circ
462 @param circid The id of the circ to update
463 @param date The timestamp of the new due date
467 sub set_circ_due_date {
468 my( $self, $conn, $auth, $circ_id, $date ) = @_;
470 my $e = new_editor(xact=>1, authtoken=>$auth);
471 return $e->die_event unless $e->checkauth;
472 my $circ = $e->retrieve_action_circulation($circ_id)
473 or return $e->die_event;
475 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
476 $date = clense_ISO8601($date);
477 $circ->due_date($date);
478 $e->update_action_circulation($circ) or return $e->die_event;
485 __PACKAGE__->register_method(
486 method => "create_in_house_use",
487 api_name => 'open-ils.circ.in_house_use.create',
489 Creates an in-house use action.
490 @param $authtoken The login session key
491 @param params A hash of params including
492 'location' The org unit id where the in-house use occurs
493 'copyid' The copy in question
494 'count' The number of in-house uses to apply to this copy
495 @return An array of id's representing the id's of the newly created
496 in-house use objects or an event on an error
499 __PACKAGE__->register_method(
500 method => "create_in_house_use",
501 api_name => 'open-ils.circ.non_cat_in_house_use.create',
505 sub create_in_house_use {
506 my( $self, $client, $auth, $params ) = @_;
509 my $org = $params->{location};
510 my $copyid = $params->{copyid};
511 my $count = $params->{count} || 1;
512 my $nc_type = $params->{non_cat_type};
513 my $use_time = $params->{use_time} || 'now';
515 my $e = new_editor(xact=>1,authtoken=>$auth);
516 return $e->event unless $e->checkauth;
517 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
519 my $non_cat = 1 if $self->api_name =~ /non_cat/;
523 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
525 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
531 if( $use_time ne 'now' ) {
532 $use_time = clense_ISO8601($use_time);
533 $logger->debug("in_house_use setting use time to $use_time");
544 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
545 $ihu->item_type($nc_type);
546 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
547 $cmeth = "create_action_non_cat_in_house_use";
550 $ihu = Fieldmapper::action::in_house_use->new;
552 $method = 'open-ils.storage.direct.action.in_house_use.create';
553 $cmeth = "create_action_in_house_use";
556 $ihu->staff($e->requestor->id);
557 $ihu->org_unit($org);
558 $ihu->use_time($use_time);
560 $ihu = $e->$cmeth($ihu) or return $e->event;
561 push( @ids, $ihu->id );
572 __PACKAGE__->register_method(
573 method => "view_circs",
574 api_name => "open-ils.circ.copy_checkout_history.retrieve",
576 Retrieves the last X circs for a given copy
577 @param authtoken The login session key
578 @param copyid The copy to check
579 @param count How far to go back in the item history
580 @return An array of circ ids
583 # ----------------------------------------------------------------------
584 # Returns $count most recent circs. If count exceeds the configured
585 # max, use the configured max instead
586 # ----------------------------------------------------------------------
588 my( $self, $client, $authtoken, $copyid, $count ) = @_;
590 my $e = new_editor(authtoken => $authtoken);
591 return $e->event unless $e->checkauth;
593 my $copy = $e->retrieve_asset_copy([
596 flesh_fields => {acp => ['call_number']}
598 ]) or return $e->event;
600 return $e->event unless $e->allowed(
601 'VIEW_COPY_CHECKOUT_HISTORY',
602 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
603 $copy->circ_lib : $copy->call_number->owning_lib);
605 my $max_history = $U->ou_ancestor_setting_value(
606 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
608 if(defined $max_history) {
609 $count = $max_history unless defined $count and $count < $max_history;
611 $count = 4 unless defined $count;
614 return $e->search_action_circulation([
615 {target_copy => $copyid},
616 {limit => $count, order_by => { circ => "xact_start DESC" }}
621 __PACKAGE__->register_method(
622 method => "circ_count",
623 api_name => "open-ils.circ.circulation.count",
625 Returns the number of times the item has circulated
626 @param copyid The copy to check
630 my( $self, $client, $copyid, $range ) = @_;
631 my $e = OpenILS::Utils::Editor->new;
632 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
637 __PACKAGE__->register_method(
638 method => 'fetch_notes',
639 api_name => 'open-ils.circ.copy_note.retrieve.all',
641 Returns an array of copy note objects.
642 @param args A named hash of parameters including:
643 authtoken : Required if viewing non-public notes
644 itemid : The id of the item whose notes we want to retrieve
645 pub : True if all the caller wants are public notes
646 @return An array of note objects
649 __PACKAGE__->register_method(
650 method => 'fetch_notes',
651 api_name => 'open-ils.circ.call_number_note.retrieve.all',
652 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
654 __PACKAGE__->register_method(
655 method => 'fetch_notes',
656 api_name => 'open-ils.circ.title_note.retrieve.all',
657 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
660 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
662 my( $self, $connection, $args ) = @_;
664 my $id = $$args{itemid};
665 my $authtoken = $$args{authtoken};
668 if( $self->api_name =~ /copy/ ) {
670 return $U->cstorereq(
671 'open-ils.cstore.direct.asset.copy_note.search.atomic',
672 { owning_copy => $id, pub => 't' } );
674 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
676 return $U->cstorereq(
677 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
680 } elsif( $self->api_name =~ /call_number/ ) {
682 return $U->cstorereq(
683 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
684 { call_number => $id, pub => 't' } );
686 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
688 return $U->cstorereq(
689 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
692 } elsif( $self->api_name =~ /title/ ) {
694 return $U->cstorereq(
695 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
696 { record => $id, pub => 't' } );
698 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
700 return $U->cstorereq(
701 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
708 __PACKAGE__->register_method(
709 method => 'has_notes',
710 api_name => 'open-ils.circ.copy.has_notes');
711 __PACKAGE__->register_method(
712 method => 'has_notes',
713 api_name => 'open-ils.circ.call_number.has_notes');
714 __PACKAGE__->register_method(
715 method => 'has_notes',
716 api_name => 'open-ils.circ.title.has_notes');
720 my( $self, $conn, $authtoken, $id ) = @_;
721 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
722 return $editor->event unless $editor->checkauth;
724 my $n = $editor->search_asset_copy_note(
725 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
727 $n = $editor->search_asset_call_number_note(
728 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
730 $n = $editor->search_biblio_record_note(
731 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
738 __PACKAGE__->register_method(
739 method => 'create_copy_note',
740 api_name => 'open-ils.circ.copy_note.create',
742 Creates a new copy note
743 @param authtoken The login session key
744 @param note The note object to create
745 @return The id of the new note object
748 sub create_copy_note {
749 my( $self, $connection, $authtoken, $note ) = @_;
751 my $e = new_editor(xact=>1, authtoken=>$authtoken);
752 return $e->event unless $e->checkauth;
753 my $copy = $e->retrieve_asset_copy(
757 flesh_fields => { 'acp' => ['call_number'] }
762 return $e->event unless
763 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
765 $note->create_date('now');
766 $note->creator($e->requestor->id);
767 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
770 $e->create_asset_copy_note($note) or return $e->event;
776 __PACKAGE__->register_method(
777 method => 'delete_copy_note',
778 api_name => 'open-ils.circ.copy_note.delete',
780 Deletes an existing copy note
781 @param authtoken The login session key
782 @param noteid The id of the note to delete
783 @return 1 on success - Event otherwise.
785 sub delete_copy_note {
786 my( $self, $conn, $authtoken, $noteid ) = @_;
788 my $e = new_editor(xact=>1, authtoken=>$authtoken);
789 return $e->die_event unless $e->checkauth;
791 my $note = $e->retrieve_asset_copy_note([
795 'acpn' => [ 'owning_copy' ],
796 'acp' => [ 'call_number' ],
799 ]) or return $e->die_event;
801 if( $note->creator ne $e->requestor->id ) {
802 return $e->die_event unless
803 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
806 $e->delete_asset_copy_note($note) or return $e->die_event;
812 __PACKAGE__->register_method(
813 method => 'age_hold_rules',
814 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
818 my( $self, $conn ) = @_;
819 return new_editor()->retrieve_all_config_rules_age_hold_protect();
824 __PACKAGE__->register_method(
825 method => 'copy_details_barcode',
827 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
828 sub copy_details_barcode {
829 my( $self, $conn, $auth, $barcode ) = @_;
830 my $e = new_editor();
831 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
832 return $e->event unless $cid;
833 return copy_details( $self, $conn, $auth, $cid );
837 __PACKAGE__->register_method(
838 method => 'copy_details',
839 api_name => 'open-ils.circ.copy_details.retrieve');
842 my( $self, $conn, $auth, $copy_id ) = @_;
843 my $e = new_editor(authtoken=>$auth);
844 return $e->event unless $e->checkauth;
846 my $flesh = { flesh => 1 };
848 my $copy = $e->retrieve_asset_copy(
854 acp => ['call_number'],
858 ]) or return $e->event;
861 # De-flesh the copy for backwards compatibility
863 my $vol = $copy->call_number;
865 $copy->call_number($vol->id);
866 my $record = $vol->record;
868 $vol->record($record->id);
869 $mvr = $U->record_to_mvr($record);
874 my $hold = $e->search_action_hold_request(
876 current_copy => $copy_id,
877 capture_time => { "!=" => undef },
878 fulfillment_time => undef,
879 cancel_time => undef,
883 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
885 my $transit = $e->search_action_transit_copy(
886 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
888 # find the latest circ, open or closed
889 my $circ = $e->search_action_circulation(
891 { target_copy => $copy_id },
897 'checkin_workstation',
900 'recurring_fine_rule'
903 order_by => { circ => 'xact_start desc' },
923 __PACKAGE__->register_method(
924 method => 'mark_item',
925 api_name => 'open-ils.circ.mark_item_damaged',
927 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
928 @param authtoken The login session key
929 @param copy_id The ID of the copy to mark as damaged
930 @return 1 on success - Event otherwise.
933 __PACKAGE__->register_method(
934 method => 'mark_item',
935 api_name => 'open-ils.circ.mark_item_missing',
937 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
938 @param authtoken The login session key
939 @param copy_id The ID of the copy to mark as missing
940 @return 1 on success - Event otherwise.
943 __PACKAGE__->register_method(
944 method => 'mark_item',
945 api_name => 'open-ils.circ.mark_item_bindery',
947 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
948 @param authtoken The login session key
949 @param copy_id The ID of the copy to mark as bindery
950 @return 1 on success - Event otherwise.
953 __PACKAGE__->register_method(
954 method => 'mark_item',
955 api_name => 'open-ils.circ.mark_item_on_order',
957 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
958 @param authtoken The login session key
959 @param copy_id The ID of the copy to mark as on order
960 @return 1 on success - Event otherwise.
963 __PACKAGE__->register_method(
964 method => 'mark_item',
965 api_name => 'open-ils.circ.mark_item_ill',
967 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
968 @param authtoken The login session key
969 @param copy_id The ID of the copy to mark as inter-library loan
970 @return 1 on success - Event otherwise.
973 __PACKAGE__->register_method(
974 method => 'mark_item',
975 api_name => 'open-ils.circ.mark_item_cataloging',
977 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
978 @param authtoken The login session key
979 @param copy_id The ID of the copy to mark as cataloging
980 @return 1 on success - Event otherwise.
983 __PACKAGE__->register_method(
984 method => 'mark_item',
985 api_name => 'open-ils.circ.mark_item_reserves',
987 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
988 @param authtoken The login session key
989 @param copy_id The ID of the copy to mark as reserves
990 @return 1 on success - Event otherwise.
993 __PACKAGE__->register_method(
994 method => 'mark_item',
995 api_name => 'open-ils.circ.mark_item_discard',
997 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
998 @param authtoken The login session key
999 @param copy_id The ID of the copy to mark as discard
1000 @return 1 on success - Event otherwise.
1005 my( $self, $conn, $auth, $copy_id, $args ) = @_;
1006 my $e = new_editor(authtoken=>$auth, xact =>1);
1007 return $e->die_event unless $e->checkauth;
1010 my $copy = $e->retrieve_asset_copy([
1012 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1013 or return $e->die_event;
1016 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1017 $copy->circ_lib : $copy->call_number->owning_lib;
1019 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
1022 my $perm = 'MARK_ITEM_MISSING';
1023 my $stat = OILS_COPY_STATUS_MISSING;
1025 if( $self->api_name =~ /damaged/ ) {
1026 $perm = 'MARK_ITEM_DAMAGED';
1027 $stat = OILS_COPY_STATUS_DAMAGED;
1028 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1029 return $evt if $evt;
1031 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1032 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1034 } elsif ( $self->api_name =~ /bindery/ ) {
1035 $perm = 'MARK_ITEM_BINDERY';
1036 $stat = OILS_COPY_STATUS_BINDERY;
1037 } elsif ( $self->api_name =~ /on_order/ ) {
1038 $perm = 'MARK_ITEM_ON_ORDER';
1039 $stat = OILS_COPY_STATUS_ON_ORDER;
1040 } elsif ( $self->api_name =~ /ill/ ) {
1041 $perm = 'MARK_ITEM_ILL';
1042 $stat = OILS_COPY_STATUS_ILL;
1043 } elsif ( $self->api_name =~ /cataloging/ ) {
1044 $perm = 'MARK_ITEM_CATALOGING';
1045 $stat = OILS_COPY_STATUS_CATALOGING;
1046 } elsif ( $self->api_name =~ /reserves/ ) {
1047 $perm = 'MARK_ITEM_RESERVES';
1048 $stat = OILS_COPY_STATUS_RESERVES;
1049 } elsif ( $self->api_name =~ /discard/ ) {
1050 $perm = 'MARK_ITEM_DISCARD';
1051 $stat = OILS_COPY_STATUS_DISCARD;
1055 $copy->status($stat);
1056 $copy->edit_date('now');
1057 $copy->editor($e->requestor->id);
1059 $e->update_asset_copy($copy) or return $e->die_event;
1061 my $holds = $e->search_action_hold_request(
1063 current_copy => $copy->id,
1064 fulfillment_time => undef,
1065 cancel_time => undef,
1071 $logger->debug("resetting holds that target the marked copy");
1072 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1077 sub handle_mark_damaged {
1078 my($e, $copy, $owning_lib, $args) = @_;
1080 my $apply = $args->{apply_fines} || '';
1081 return undef if $apply eq 'noapply';
1083 my $new_amount = $args->{override_amount};
1084 my $new_btype = $args->{override_btype};
1085 my $new_note = $args->{override_note};
1087 # grab the last circulation
1088 my $circ = $e->search_action_circulation([
1089 { target_copy => $copy->id},
1091 order_by => {circ => "xact_start DESC"},
1093 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1097 return undef unless $circ;
1099 my $charge_price = $U->ou_ancestor_setting_value(
1100 $owning_lib, 'circ.charge_on_damaged', $e);
1102 my $proc_fee = $U->ou_ancestor_setting_value(
1103 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1105 my $void_overdue = $U->ou_ancestor_setting_value(
1106 $owning_lib, 'circ.damaged.void_ovedue', $e) || 0;
1108 return undef unless $charge_price or $proc_fee;
1110 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1111 my $total = $copy_price + $proc_fee;
1115 if($new_amount and $new_btype) {
1117 # Allow staff to override the amount to charge for a damaged item
1118 # Consider the case where the item is only partially damaged
1119 # This value is meant to take the place of the item price and
1120 # optional processing fee.
1122 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1123 $e, $new_amount, $new_btype, 'Damaged Item Override', $circ->id, $new_note);
1124 return $evt if $evt;
1128 if($charge_price and $copy_price) {
1129 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1130 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1131 return $evt if $evt;
1135 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1136 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1137 return $evt if $evt;
1141 # the assumption is that you would not void the overdues unless you
1142 # were also charging for the item and/or applying a processing fee
1144 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
1145 return $evt if $evt;
1148 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1149 return $evt if $evt;
1151 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1152 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1157 return OpenILS::Event->new('DAMAGE_CHARGE',
1171 # ----------------------------------------------------------------------
1172 __PACKAGE__->register_method(
1173 method => 'magic_fetch',
1174 api_name => 'open-ils.agent.fetch'
1177 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1180 my( $self, $conn, $auth, $args ) = @_;
1181 my $e = new_editor( authtoken => $auth );
1182 return $e->event unless $e->checkauth;
1184 my $hint = $$args{hint};
1185 my $id = $$args{id};
1187 # Is the call allowed to fetch this type of object?
1188 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1190 # Find the class the implements the given hint
1191 my ($class) = grep {
1192 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1194 $class =~ s/Fieldmapper:://og;
1195 $class =~ s/::/_/og;
1196 my $method = "retrieve_$class";
1198 my $obj = $e->$method($id) or return $e->event;
1201 # ----------------------------------------------------------------------
1204 __PACKAGE__->register_method(
1205 method => "fleshed_circ_retrieve",
1207 api_name => "open-ils.circ.fleshed.retrieve",);
1209 sub fleshed_circ_retrieve {
1210 my( $self, $client, $id ) = @_;
1211 my $e = new_editor();
1212 my $circ = $e->retrieve_action_circulation(
1218 circ => [ qw/ target_copy / ],
1219 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1220 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1221 acn => [ qw/ record / ],
1225 ) or return $e->event;
1227 my $copy = $circ->target_copy;
1228 my $vol = $copy->call_number;
1229 my $rec = $circ->target_copy->call_number->record;
1231 $vol->record($rec->id);
1232 $copy->call_number($vol->id);
1233 $circ->target_copy($copy->id);
1237 if( $rec->id == OILS_PRECAT_RECORD ) {
1241 $mvr = $U->record_to_mvr($rec);
1242 $rec->marc(''); # drop the bulky marc data
1256 __PACKAGE__->register_method(
1257 method => "test_batch_circ_events",
1258 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1261 # method for testing the behavior of a given event definition
1262 sub test_batch_circ_events {
1263 my($self, $conn, $auth, $event_def, $barcode) = @_;
1265 my $e = new_editor(authtoken => $auth);
1266 return $e->event unless $e->checkauth;
1267 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1269 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1270 or return $e->event;
1272 my $circ = $e->search_action_circulation(
1273 {target_copy => $copy->id, checkin_time => undef})->[0]
1274 or return $e->event;
1276 return undef unless $circ;
1278 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1283 __PACKAGE__->register_method(
1284 method => "user_payments_list",
1285 api_name => "open-ils.circ.user_payments.filtered.batch",
1288 desc => q/Returns a fleshed, date-limited set of all payments a user
1289 has made. By default, ordered by payment date. Optionally
1290 ordered by other columns in the top-level "mp" object/,
1292 {desc => 'Authentication token', type => 'string'},
1293 {desc => 'User ID', type => 'number'},
1294 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1296 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1297 and the related fully-realized payment object (e.g money.cash_payment)/}
1301 sub user_payments_list {
1302 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1304 my $e = new_editor(authtoken => $auth);
1305 return $e->event unless $e->checkauth;
1307 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1308 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1310 $order_by ||= ['payment_ts'];
1312 # all payments by user, between start_date and end_date
1313 my $payments = $e->json_query({
1314 select => {mp => ['id']},
1318 fkey => 'xact', field => 'id'}
1322 '+mbt' => {usr => $user_id},
1323 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1325 order_by => {mp => $order_by}
1328 for my $payment_id (@$payments) {
1329 my $payment = $e->retrieve_money_payment([
1337 'credit_card_payment',
1351 $conn->respond($payment);
1358 __PACKAGE__->register_method(
1359 method => "retrieve_circ_chain",
1360 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1363 desc => q/Given a circulation, this returns all circulation objects
1364 that are part of the same chain of renewals./,
1366 {desc => 'Authentication token', type => 'string'},
1367 {desc => 'Circ ID', type => 'number'},
1369 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1373 sub retrieve_circ_chain {
1374 my($self, $conn, $auth, $circ_id) = @_;
1376 my $e = new_editor(authtoken => $auth);
1377 return $e->event unless $e->checkauth;
1379 # grab the base circ and all parent (previous) circs by fleshing
1380 my $base_circ = $e->retrieve_action_circulation([
1384 flesh_fields => {circ => [qw/parent_circ workstation checkin_workstation/]}
1386 ]) or return $e->event;
1388 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $base_circ->circ_lib);
1390 # send each circ to the caller, starting with the oldest circulation
1392 my $circ = $base_circ;
1394 push(@chain, $circ);
1396 # unflesh for consistency
1397 my $parent = $circ->parent_circ;
1398 $circ->parent_circ($parent->id) if $parent;
1402 $conn->respond($_) for reverse(@chain);
1404 # base circ may not be the end of the chain. see if there are any subsequent circs
1406 $conn->respond($circ) while ($circ = $e->search_action_circulation([
1407 {parent_circ => $circ->id},
1408 {flesh => 1, flesh_fields => {circ => [qw/workstation checkin_workstation/]}}
1416 # {"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}}