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 $logger->info("marking circ for item $barcode as claims returned".
307 (($backdate) ? " with backdate $backdate" : ''));
309 my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0]
310 or return $e->die_event;
312 my $circ = $e->search_action_circulation(
313 {checkin_time => undef, target_copy => $copy->id})->[0]
314 or return $e->die_event;
316 my $patron = $e->retrieve_actor_user($circ->usr);
317 my $max_count = $U->ou_ancestor_setting_value(
318 $circ->circ_lib, 'circ.max_patron_claim_return_count', $e);
320 # If the patron has too instances of many claims returned,
321 # require an override to continue. A configured max of
322 # 0 means all attempts require an override
323 if(defined $max_count and $patron->claims_returned_count >= $max_count) {
325 if($self->api_name =~ /override/) {
327 # see if we're allowed to override
328 return $e->die_event unless
329 $e->allowed('SET_CIRC_CLAIMS_RETURNED.override', $circ->circ_lib);
333 # exit early and return the max claims return event
335 return OpenILS::Event->new(
336 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT',
338 patron_count => $patron->claims_returned_count,
339 max_count => $max_count
345 $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib)
346 or return $e->die_event;
348 $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
349 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
352 # make it look like the circ stopped at the cliams returned time
353 $circ->stop_fines_time(clense_ISO8601($backdate));
354 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
358 $e->update_action_circulation($circ) or return $e->die_event;
362 # see if we need to also mark the copy as missing
363 if($U->ou_ancestor_setting_value($circ->circ_lib, 'circ.claim_return.mark_missing')) {
364 return $apputils->simplereq(
366 'open-ils.circ.mark_item_missing',
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 },
896 'checkin_workstation',
902 order_by => { circ => 'xact_start desc' },
922 __PACKAGE__->register_method(
923 method => 'mark_item',
924 api_name => 'open-ils.circ.mark_item_damaged',
926 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
927 @param authtoken The login session key
928 @param copy_id The ID of the copy to mark as damaged
929 @return 1 on success - Event otherwise.
932 __PACKAGE__->register_method(
933 method => 'mark_item',
934 api_name => 'open-ils.circ.mark_item_missing',
936 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
937 @param authtoken The login session key
938 @param copy_id The ID of the copy to mark as missing
939 @return 1 on success - Event otherwise.
942 __PACKAGE__->register_method(
943 method => 'mark_item',
944 api_name => 'open-ils.circ.mark_item_bindery',
946 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
947 @param authtoken The login session key
948 @param copy_id The ID of the copy to mark as bindery
949 @return 1 on success - Event otherwise.
952 __PACKAGE__->register_method(
953 method => 'mark_item',
954 api_name => 'open-ils.circ.mark_item_on_order',
956 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
957 @param authtoken The login session key
958 @param copy_id The ID of the copy to mark as on order
959 @return 1 on success - Event otherwise.
962 __PACKAGE__->register_method(
963 method => 'mark_item',
964 api_name => 'open-ils.circ.mark_item_ill',
966 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
967 @param authtoken The login session key
968 @param copy_id The ID of the copy to mark as inter-library loan
969 @return 1 on success - Event otherwise.
972 __PACKAGE__->register_method(
973 method => 'mark_item',
974 api_name => 'open-ils.circ.mark_item_cataloging',
976 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
977 @param authtoken The login session key
978 @param copy_id The ID of the copy to mark as cataloging
979 @return 1 on success - Event otherwise.
982 __PACKAGE__->register_method(
983 method => 'mark_item',
984 api_name => 'open-ils.circ.mark_item_reserves',
986 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
987 @param authtoken The login session key
988 @param copy_id The ID of the copy to mark as reserves
989 @return 1 on success - Event otherwise.
992 __PACKAGE__->register_method(
993 method => 'mark_item',
994 api_name => 'open-ils.circ.mark_item_discard',
996 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
997 @param authtoken The login session key
998 @param copy_id The ID of the copy to mark as discard
999 @return 1 on success - Event otherwise.
1004 my( $self, $conn, $auth, $copy_id, $args ) = @_;
1005 my $e = new_editor(authtoken=>$auth, xact =>1);
1006 return $e->die_event unless $e->checkauth;
1009 my $copy = $e->retrieve_asset_copy([
1011 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1012 or return $e->die_event;
1015 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1016 $copy->circ_lib : $copy->call_number->owning_lib;
1018 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
1021 my $perm = 'MARK_ITEM_MISSING';
1022 my $stat = OILS_COPY_STATUS_MISSING;
1024 if( $self->api_name =~ /damaged/ ) {
1025 $perm = 'MARK_ITEM_DAMAGED';
1026 $stat = OILS_COPY_STATUS_DAMAGED;
1027 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1028 return $evt if $evt;
1030 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1031 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1033 } elsif ( $self->api_name =~ /bindery/ ) {
1034 $perm = 'MARK_ITEM_BINDERY';
1035 $stat = OILS_COPY_STATUS_BINDERY;
1036 } elsif ( $self->api_name =~ /on_order/ ) {
1037 $perm = 'MARK_ITEM_ON_ORDER';
1038 $stat = OILS_COPY_STATUS_ON_ORDER;
1039 } elsif ( $self->api_name =~ /ill/ ) {
1040 $perm = 'MARK_ITEM_ILL';
1041 $stat = OILS_COPY_STATUS_ILL;
1042 } elsif ( $self->api_name =~ /cataloging/ ) {
1043 $perm = 'MARK_ITEM_CATALOGING';
1044 $stat = OILS_COPY_STATUS_CATALOGING;
1045 } elsif ( $self->api_name =~ /reserves/ ) {
1046 $perm = 'MARK_ITEM_RESERVES';
1047 $stat = OILS_COPY_STATUS_RESERVES;
1048 } elsif ( $self->api_name =~ /discard/ ) {
1049 $perm = 'MARK_ITEM_DISCARD';
1050 $stat = OILS_COPY_STATUS_DISCARD;
1054 $copy->status($stat);
1055 $copy->edit_date('now');
1056 $copy->editor($e->requestor->id);
1058 $e->update_asset_copy($copy) or return $e->die_event;
1060 my $holds = $e->search_action_hold_request(
1062 current_copy => $copy->id,
1063 fulfillment_time => undef,
1064 cancel_time => undef,
1070 $logger->debug("resetting holds that target the marked copy");
1071 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1076 sub handle_mark_damaged {
1077 my($e, $copy, $owning_lib, $args) = @_;
1079 my $apply = $args->{apply_fines} || '';
1080 return undef if $apply eq 'noapply';
1082 # grab the last circulation
1083 my $circ = $e->search_action_circulation([
1084 { target_copy => $copy->id},
1086 order_by => {circ => "xact_start DESC"},
1088 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1092 return undef unless $circ;
1094 my $charge_price = $U->ou_ancestor_setting_value(
1095 $owning_lib, 'circ.charge_on_damaged', $e);
1097 my $proc_fee = $U->ou_ancestor_setting_value(
1098 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1100 return undef unless $charge_price or $proc_fee;
1102 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1103 my $total = $copy_price + $proc_fee;
1107 if($charge_price and $copy_price) {
1108 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1109 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1110 return $evt if $evt;
1114 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1115 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1116 return $evt if $evt;
1119 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1120 return $evt if $evt;
1122 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1123 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1128 return OpenILS::Event->new('DAMAGE_CHARGE',
1142 # ----------------------------------------------------------------------
1143 __PACKAGE__->register_method(
1144 method => 'magic_fetch',
1145 api_name => 'open-ils.agent.fetch'
1148 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1151 my( $self, $conn, $auth, $args ) = @_;
1152 my $e = new_editor( authtoken => $auth );
1153 return $e->event unless $e->checkauth;
1155 my $hint = $$args{hint};
1156 my $id = $$args{id};
1158 # Is the call allowed to fetch this type of object?
1159 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1161 # Find the class the implements the given hint
1162 my ($class) = grep {
1163 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1165 $class =~ s/Fieldmapper:://og;
1166 $class =~ s/::/_/og;
1167 my $method = "retrieve_$class";
1169 my $obj = $e->$method($id) or return $e->event;
1172 # ----------------------------------------------------------------------
1175 __PACKAGE__->register_method(
1176 method => "fleshed_circ_retrieve",
1178 api_name => "open-ils.circ.fleshed.retrieve",);
1180 sub fleshed_circ_retrieve {
1181 my( $self, $client, $id ) = @_;
1182 my $e = new_editor();
1183 my $circ = $e->retrieve_action_circulation(
1189 circ => [ qw/ target_copy / ],
1190 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1191 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1192 acn => [ qw/ record / ],
1196 ) or return $e->event;
1198 my $copy = $circ->target_copy;
1199 my $vol = $copy->call_number;
1200 my $rec = $circ->target_copy->call_number->record;
1202 $vol->record($rec->id);
1203 $copy->call_number($vol->id);
1204 $circ->target_copy($copy->id);
1208 if( $rec->id == OILS_PRECAT_RECORD ) {
1212 $mvr = $U->record_to_mvr($rec);
1213 $rec->marc(''); # drop the bulky marc data
1227 __PACKAGE__->register_method(
1228 method => "test_batch_circ_events",
1229 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1232 # method for testing the behavior of a given event definition
1233 sub test_batch_circ_events {
1234 my($self, $conn, $auth, $event_def, $barcode) = @_;
1236 my $e = new_editor(authtoken => $auth);
1237 return $e->event unless $e->checkauth;
1238 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1240 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1241 or return $e->event;
1243 my $circ = $e->search_action_circulation(
1244 {target_copy => $copy->id, checkin_time => undef})->[0]
1245 or return $e->event;
1247 return undef unless $circ;
1249 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1254 __PACKAGE__->register_method(
1255 method => "user_payments_list",
1256 api_name => "open-ils.circ.user_payments.filtered.batch",
1259 desc => q/Returns a fleshed, date-limited set of all payments a user
1260 has made. By default, ordered by payment date. Optionally
1261 ordered by other columns in the top-level "mp" object/,
1263 {desc => 'Authentication token', type => 'string'},
1264 {desc => 'User ID', type => 'number'},
1265 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1267 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1268 and the related fully-realized payment object (e.g money.cash_payment)/}
1272 sub user_payments_list {
1273 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1275 my $e = new_editor(authtoken => $auth);
1276 return $e->event unless $e->checkauth;
1278 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1279 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1281 $order_by ||= ['payment_ts'];
1283 # all payments by user, between start_date and end_date
1284 my $payments = $e->json_query({
1285 select => {mp => ['id']},
1289 fkey => 'xact', field => 'id'}
1293 '+mbt' => {usr => $user_id},
1294 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1296 order_by => {mp => $order_by}
1299 for my $payment_id (@$payments) {
1300 my $payment = $e->retrieve_money_payment([
1308 'credit_card_payment',
1322 $conn->respond($payment);
1329 __PACKAGE__->register_method(
1330 method => "retrieve_circ_chain",
1331 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1334 desc => q/Given a circulation, this returns all circulation objects
1335 that are part of the same chain of renewals./,
1337 {desc => 'Authentication token', type => 'string'},
1338 {desc => 'Circ ID', type => 'number'},
1340 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1344 sub retrieve_circ_chain {
1345 my($self, $conn, $auth, $circ_id) = @_;
1347 my $e = new_editor(authtoken => $auth);
1348 return $e->event unless $e->checkauth;
1350 # grab the base circ and all parent (previous) circs by fleshing
1351 my $base_circ = $e->retrieve_action_circulation([
1355 flesh_fields => {circ => ['parent_circ']}
1357 ]) or return $e->event;
1359 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $base_circ->circ_lib);
1361 # send each circ to the caller, starting with the oldest circulation
1363 my $circ = $base_circ;
1365 push(@chain, $circ);
1367 # unflesh for consistency
1368 my $parent = $circ->parent_circ;
1369 $circ->parent_circ($parent->id) if $parent;
1373 $conn->respond($_) for reverse(@chain);
1375 # base circ may not be the end of the chain. see if there are any subsequent circs
1377 $conn->respond($circ) while ($circ = $e->search_action_circulation({parent_circ => $circ->id})->[0]);
1384 # {"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}}