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;
360 # see if there is a configured post-claims-return copy status
361 if(my $stat = $U->ou_ancestor_setting_value($circ->circ_lib, 'circ.claim_return.copy_status')) {
362 $copy->status($stat);
363 $copy->edit_date('now');
364 $copy->editor($e->requestor->id);
365 $e->update_asset_copy($copy) or return $e->die_event;
373 __PACKAGE__->register_method(
374 method => "post_checkin_backdate_circ",
375 api_name => "open-ils.circ.post_checkin_backdate",
377 desc => q/Back-date an already checked in circulation/,
379 {desc => 'Authentication token', type => 'string'},
380 {desc => 'Circ ID', type => 'number'},
381 {desc => 'ISO8601 backdate', type => 'string'},
383 return => {desc => q/1 on success, failure event on error/}
387 __PACKAGE__->register_method(
388 method => "post_checkin_backdate_circ",
389 api_name => "open-ils.circ.post_checkin_backdate.batch",
392 desc => q/@see open-ils.circ.post_checkin_backdate. Batch mode/,
394 {desc => 'Authentication token', type => 'string'},
395 {desc => 'List of Circ ID', type => 'array'},
396 {desc => 'ISO8601 backdate', type => 'string'},
398 return => {desc => q/Set of: 1 on success, failure event on error/}
403 sub post_checkin_backdate_circ {
404 my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
405 my $e = new_editor(authtoken=>$auth);
406 return $e->die_event unless $e->checkauth;
407 if($self->api_name =~ /batch/) {
408 foreach my $c (@$circ_id) {
409 $conn->respond(post_checkin_backdate_circ_impl($e, $c, $backdate));
412 $conn->respond_complete(post_checkin_backdate_circ_impl($e, $circ_id, $backdate));
420 sub post_checkin_backdate_circ_impl {
421 my($e, $circ_id, $backdate) = @_;
425 my $circ = $e->retrieve_action_circulation($circ_id)
426 or return $e->die_event;
428 # anyone with checkin perms can backdate (more restrictive?)
429 return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
431 # don't allow back-dating an open circulation
432 return OpenILS::Event->new('BAD_PARAMS') unless
433 $backdate and $circ->checkin_time;
435 # update the checkin and stop_fines times to reflect the new backdate
436 $circ->stop_fines_time(clense_ISO8601($backdate));
437 $circ->checkin_time(clense_ISO8601($backdate));
438 $e->update_action_circulation($circ) or return $e->die_event;
440 # now void the overdues "erased" by the back-dating
441 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
444 # If the circ was closed before and the balance owned !=0, re-open the transaction
445 $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
454 __PACKAGE__->register_method (
455 method => 'set_circ_due_date',
456 api_name => 'open-ils.circ.circulation.due_date.update',
458 Updates the due_date on the given circ
460 @param circid The id of the circ to update
461 @param date The timestamp of the new due date
465 sub set_circ_due_date {
466 my( $self, $conn, $auth, $circ_id, $date ) = @_;
468 my $e = new_editor(xact=>1, authtoken=>$auth);
469 return $e->die_event unless $e->checkauth;
470 my $circ = $e->retrieve_action_circulation($circ_id)
471 or return $e->die_event;
473 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
474 $date = clense_ISO8601($date);
475 $circ->due_date($date);
476 $e->update_action_circulation($circ) or return $e->die_event;
483 __PACKAGE__->register_method(
484 method => "create_in_house_use",
485 api_name => 'open-ils.circ.in_house_use.create',
487 Creates an in-house use action.
488 @param $authtoken The login session key
489 @param params A hash of params including
490 'location' The org unit id where the in-house use occurs
491 'copyid' The copy in question
492 'count' The number of in-house uses to apply to this copy
493 @return An array of id's representing the id's of the newly created
494 in-house use objects or an event on an error
497 __PACKAGE__->register_method(
498 method => "create_in_house_use",
499 api_name => 'open-ils.circ.non_cat_in_house_use.create',
503 sub create_in_house_use {
504 my( $self, $client, $auth, $params ) = @_;
507 my $org = $params->{location};
508 my $copyid = $params->{copyid};
509 my $count = $params->{count} || 1;
510 my $nc_type = $params->{non_cat_type};
511 my $use_time = $params->{use_time} || 'now';
513 my $e = new_editor(xact=>1,authtoken=>$auth);
514 return $e->event unless $e->checkauth;
515 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
517 my $non_cat = 1 if $self->api_name =~ /non_cat/;
521 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
523 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
529 if( $use_time ne 'now' ) {
530 $use_time = clense_ISO8601($use_time);
531 $logger->debug("in_house_use setting use time to $use_time");
542 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
543 $ihu->item_type($nc_type);
544 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
545 $cmeth = "create_action_non_cat_in_house_use";
548 $ihu = Fieldmapper::action::in_house_use->new;
550 $method = 'open-ils.storage.direct.action.in_house_use.create';
551 $cmeth = "create_action_in_house_use";
554 $ihu->staff($e->requestor->id);
555 $ihu->org_unit($org);
556 $ihu->use_time($use_time);
558 $ihu = $e->$cmeth($ihu) or return $e->event;
559 push( @ids, $ihu->id );
570 __PACKAGE__->register_method(
571 method => "view_circs",
572 api_name => "open-ils.circ.copy_checkout_history.retrieve",
574 Retrieves the last X circs for a given copy
575 @param authtoken The login session key
576 @param copyid The copy to check
577 @param count How far to go back in the item history
578 @return An array of circ ids
581 # ----------------------------------------------------------------------
582 # Returns $count most recent circs. If count exceeds the configured
583 # max, use the configured max instead
584 # ----------------------------------------------------------------------
586 my( $self, $client, $authtoken, $copyid, $count ) = @_;
588 my $e = new_editor(authtoken => $authtoken);
589 return $e->event unless $e->checkauth;
591 my $copy = $e->retrieve_asset_copy([
594 flesh_fields => {acp => ['call_number']}
596 ]) or return $e->event;
598 return $e->event unless $e->allowed(
599 'VIEW_COPY_CHECKOUT_HISTORY',
600 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
601 $copy->circ_lib : $copy->call_number->owning_lib);
603 my $max_history = $U->ou_ancestor_setting_value(
604 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
606 if(defined $max_history) {
607 $count = $max_history unless defined $count and $count < $max_history;
609 $count = 4 unless defined $count;
612 return $e->search_action_circulation([
613 {target_copy => $copyid},
614 {limit => $count, order_by => { circ => "xact_start DESC" }}
619 __PACKAGE__->register_method(
620 method => "circ_count",
621 api_name => "open-ils.circ.circulation.count",
623 Returns the number of times the item has circulated
624 @param copyid The copy to check
628 my( $self, $client, $copyid, $range ) = @_;
629 my $e = OpenILS::Utils::Editor->new;
630 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
635 __PACKAGE__->register_method(
636 method => 'fetch_notes',
637 api_name => 'open-ils.circ.copy_note.retrieve.all',
639 Returns an array of copy note objects.
640 @param args A named hash of parameters including:
641 authtoken : Required if viewing non-public notes
642 itemid : The id of the item whose notes we want to retrieve
643 pub : True if all the caller wants are public notes
644 @return An array of note objects
647 __PACKAGE__->register_method(
648 method => 'fetch_notes',
649 api_name => 'open-ils.circ.call_number_note.retrieve.all',
650 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
652 __PACKAGE__->register_method(
653 method => 'fetch_notes',
654 api_name => 'open-ils.circ.title_note.retrieve.all',
655 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
658 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
660 my( $self, $connection, $args ) = @_;
662 my $id = $$args{itemid};
663 my $authtoken = $$args{authtoken};
666 if( $self->api_name =~ /copy/ ) {
668 return $U->cstorereq(
669 'open-ils.cstore.direct.asset.copy_note.search.atomic',
670 { owning_copy => $id, pub => 't' } );
672 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
674 return $U->cstorereq(
675 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
678 } elsif( $self->api_name =~ /call_number/ ) {
680 return $U->cstorereq(
681 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
682 { call_number => $id, pub => 't' } );
684 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
686 return $U->cstorereq(
687 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
690 } elsif( $self->api_name =~ /title/ ) {
692 return $U->cstorereq(
693 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
694 { record => $id, pub => 't' } );
696 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
698 return $U->cstorereq(
699 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
706 __PACKAGE__->register_method(
707 method => 'has_notes',
708 api_name => 'open-ils.circ.copy.has_notes');
709 __PACKAGE__->register_method(
710 method => 'has_notes',
711 api_name => 'open-ils.circ.call_number.has_notes');
712 __PACKAGE__->register_method(
713 method => 'has_notes',
714 api_name => 'open-ils.circ.title.has_notes');
718 my( $self, $conn, $authtoken, $id ) = @_;
719 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
720 return $editor->event unless $editor->checkauth;
722 my $n = $editor->search_asset_copy_note(
723 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
725 $n = $editor->search_asset_call_number_note(
726 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
728 $n = $editor->search_biblio_record_note(
729 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
736 __PACKAGE__->register_method(
737 method => 'create_copy_note',
738 api_name => 'open-ils.circ.copy_note.create',
740 Creates a new copy note
741 @param authtoken The login session key
742 @param note The note object to create
743 @return The id of the new note object
746 sub create_copy_note {
747 my( $self, $connection, $authtoken, $note ) = @_;
749 my $e = new_editor(xact=>1, authtoken=>$authtoken);
750 return $e->event unless $e->checkauth;
751 my $copy = $e->retrieve_asset_copy(
755 flesh_fields => { 'acp' => ['call_number'] }
760 return $e->event unless
761 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
763 $note->create_date('now');
764 $note->creator($e->requestor->id);
765 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
768 $e->create_asset_copy_note($note) or return $e->event;
774 __PACKAGE__->register_method(
775 method => 'delete_copy_note',
776 api_name => 'open-ils.circ.copy_note.delete',
778 Deletes an existing copy note
779 @param authtoken The login session key
780 @param noteid The id of the note to delete
781 @return 1 on success - Event otherwise.
783 sub delete_copy_note {
784 my( $self, $conn, $authtoken, $noteid ) = @_;
786 my $e = new_editor(xact=>1, authtoken=>$authtoken);
787 return $e->die_event unless $e->checkauth;
789 my $note = $e->retrieve_asset_copy_note([
793 'acpn' => [ 'owning_copy' ],
794 'acp' => [ 'call_number' ],
797 ]) or return $e->die_event;
799 if( $note->creator ne $e->requestor->id ) {
800 return $e->die_event unless
801 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
804 $e->delete_asset_copy_note($note) or return $e->die_event;
810 __PACKAGE__->register_method(
811 method => 'age_hold_rules',
812 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
816 my( $self, $conn ) = @_;
817 return new_editor()->retrieve_all_config_rules_age_hold_protect();
822 __PACKAGE__->register_method(
823 method => 'copy_details_barcode',
825 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
826 sub copy_details_barcode {
827 my( $self, $conn, $auth, $barcode ) = @_;
828 my $e = new_editor();
829 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
830 return $e->event unless $cid;
831 return copy_details( $self, $conn, $auth, $cid );
835 __PACKAGE__->register_method(
836 method => 'copy_details',
837 api_name => 'open-ils.circ.copy_details.retrieve');
840 my( $self, $conn, $auth, $copy_id ) = @_;
841 my $e = new_editor(authtoken=>$auth);
842 return $e->event unless $e->checkauth;
844 my $flesh = { flesh => 1 };
846 my $copy = $e->retrieve_asset_copy(
852 acp => ['call_number'],
856 ]) or return $e->event;
859 # De-flesh the copy for backwards compatibility
861 my $vol = $copy->call_number;
863 $copy->call_number($vol->id);
864 my $record = $vol->record;
866 $vol->record($record->id);
867 $mvr = $U->record_to_mvr($record);
872 my $hold = $e->search_action_hold_request(
874 current_copy => $copy_id,
875 capture_time => { "!=" => undef },
876 fulfillment_time => undef,
877 cancel_time => undef,
881 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
883 my $transit = $e->search_action_transit_copy(
884 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
886 # find the latest circ, open or closed
887 my $circ = $e->search_action_circulation(
889 { target_copy => $copy_id },
895 'checkin_workstation',
901 order_by => { circ => 'xact_start desc' },
921 __PACKAGE__->register_method(
922 method => 'mark_item',
923 api_name => 'open-ils.circ.mark_item_damaged',
925 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
926 @param authtoken The login session key
927 @param copy_id The ID of the copy to mark as damaged
928 @return 1 on success - Event otherwise.
931 __PACKAGE__->register_method(
932 method => 'mark_item',
933 api_name => 'open-ils.circ.mark_item_missing',
935 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
936 @param authtoken The login session key
937 @param copy_id The ID of the copy to mark as missing
938 @return 1 on success - Event otherwise.
941 __PACKAGE__->register_method(
942 method => 'mark_item',
943 api_name => 'open-ils.circ.mark_item_bindery',
945 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
946 @param authtoken The login session key
947 @param copy_id The ID of the copy to mark as bindery
948 @return 1 on success - Event otherwise.
951 __PACKAGE__->register_method(
952 method => 'mark_item',
953 api_name => 'open-ils.circ.mark_item_on_order',
955 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
956 @param authtoken The login session key
957 @param copy_id The ID of the copy to mark as on order
958 @return 1 on success - Event otherwise.
961 __PACKAGE__->register_method(
962 method => 'mark_item',
963 api_name => 'open-ils.circ.mark_item_ill',
965 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
966 @param authtoken The login session key
967 @param copy_id The ID of the copy to mark as inter-library loan
968 @return 1 on success - Event otherwise.
971 __PACKAGE__->register_method(
972 method => 'mark_item',
973 api_name => 'open-ils.circ.mark_item_cataloging',
975 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
976 @param authtoken The login session key
977 @param copy_id The ID of the copy to mark as cataloging
978 @return 1 on success - Event otherwise.
981 __PACKAGE__->register_method(
982 method => 'mark_item',
983 api_name => 'open-ils.circ.mark_item_reserves',
985 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
986 @param authtoken The login session key
987 @param copy_id The ID of the copy to mark as reserves
988 @return 1 on success - Event otherwise.
991 __PACKAGE__->register_method(
992 method => 'mark_item',
993 api_name => 'open-ils.circ.mark_item_discard',
995 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
996 @param authtoken The login session key
997 @param copy_id The ID of the copy to mark as discard
998 @return 1 on success - Event otherwise.
1003 my( $self, $conn, $auth, $copy_id, $args ) = @_;
1004 my $e = new_editor(authtoken=>$auth, xact =>1);
1005 return $e->die_event unless $e->checkauth;
1008 my $copy = $e->retrieve_asset_copy([
1010 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1011 or return $e->die_event;
1014 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1015 $copy->circ_lib : $copy->call_number->owning_lib;
1017 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
1020 my $perm = 'MARK_ITEM_MISSING';
1021 my $stat = OILS_COPY_STATUS_MISSING;
1023 if( $self->api_name =~ /damaged/ ) {
1024 $perm = 'MARK_ITEM_DAMAGED';
1025 $stat = OILS_COPY_STATUS_DAMAGED;
1026 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1027 return $evt if $evt;
1029 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1030 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1032 } elsif ( $self->api_name =~ /bindery/ ) {
1033 $perm = 'MARK_ITEM_BINDERY';
1034 $stat = OILS_COPY_STATUS_BINDERY;
1035 } elsif ( $self->api_name =~ /on_order/ ) {
1036 $perm = 'MARK_ITEM_ON_ORDER';
1037 $stat = OILS_COPY_STATUS_ON_ORDER;
1038 } elsif ( $self->api_name =~ /ill/ ) {
1039 $perm = 'MARK_ITEM_ILL';
1040 $stat = OILS_COPY_STATUS_ILL;
1041 } elsif ( $self->api_name =~ /cataloging/ ) {
1042 $perm = 'MARK_ITEM_CATALOGING';
1043 $stat = OILS_COPY_STATUS_CATALOGING;
1044 } elsif ( $self->api_name =~ /reserves/ ) {
1045 $perm = 'MARK_ITEM_RESERVES';
1046 $stat = OILS_COPY_STATUS_RESERVES;
1047 } elsif ( $self->api_name =~ /discard/ ) {
1048 $perm = 'MARK_ITEM_DISCARD';
1049 $stat = OILS_COPY_STATUS_DISCARD;
1053 $copy->status($stat);
1054 $copy->edit_date('now');
1055 $copy->editor($e->requestor->id);
1057 $e->update_asset_copy($copy) or return $e->die_event;
1059 my $holds = $e->search_action_hold_request(
1061 current_copy => $copy->id,
1062 fulfillment_time => undef,
1063 cancel_time => undef,
1069 $logger->debug("resetting holds that target the marked copy");
1070 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1075 sub handle_mark_damaged {
1076 my($e, $copy, $owning_lib, $args) = @_;
1078 my $apply = $args->{apply_fines} || '';
1079 return undef if $apply eq 'noapply';
1081 my $new_amount = $args->{override_amount};
1082 my $new_btype = $args->{override_btype};
1083 my $new_note = $args->{override_note};
1085 # grab the last circulation
1086 my $circ = $e->search_action_circulation([
1087 { target_copy => $copy->id},
1089 order_by => {circ => "xact_start DESC"},
1091 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1095 return undef unless $circ;
1097 my $charge_price = $U->ou_ancestor_setting_value(
1098 $owning_lib, 'circ.charge_on_damaged', $e);
1100 my $proc_fee = $U->ou_ancestor_setting_value(
1101 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1103 my $void_overdue = $U->ou_ancestor_setting_value(
1104 $owning_lib, 'circ.damaged.void_ovedue', $e) || 0;
1106 return undef unless $charge_price or $proc_fee;
1108 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1109 my $total = $copy_price + $proc_fee;
1113 if($new_amount and $new_btype) {
1115 # Allow staff to override the amount to charge for a damaged item
1116 # Consider the case where the item is only partially damaged
1117 # This value is meant to take the place of the item price and
1118 # optional processing fee.
1120 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1121 $e, $new_amount, $new_btype, 'Damaged Item Override', $circ->id, $new_note);
1122 return $evt if $evt;
1126 if($charge_price and $copy_price) {
1127 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1128 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1129 return $evt if $evt;
1133 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1134 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1135 return $evt if $evt;
1139 # the assumption is that you would not void the overdues unless you
1140 # were also charging for the item and/or applying a processing fee
1142 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
1143 return $evt if $evt;
1146 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1147 return $evt if $evt;
1149 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1150 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1155 return OpenILS::Event->new('DAMAGE_CHARGE',
1169 # ----------------------------------------------------------------------
1170 __PACKAGE__->register_method(
1171 method => 'magic_fetch',
1172 api_name => 'open-ils.agent.fetch'
1175 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1178 my( $self, $conn, $auth, $args ) = @_;
1179 my $e = new_editor( authtoken => $auth );
1180 return $e->event unless $e->checkauth;
1182 my $hint = $$args{hint};
1183 my $id = $$args{id};
1185 # Is the call allowed to fetch this type of object?
1186 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1188 # Find the class the implements the given hint
1189 my ($class) = grep {
1190 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1192 $class =~ s/Fieldmapper:://og;
1193 $class =~ s/::/_/og;
1194 my $method = "retrieve_$class";
1196 my $obj = $e->$method($id) or return $e->event;
1199 # ----------------------------------------------------------------------
1202 __PACKAGE__->register_method(
1203 method => "fleshed_circ_retrieve",
1205 api_name => "open-ils.circ.fleshed.retrieve",);
1207 sub fleshed_circ_retrieve {
1208 my( $self, $client, $id ) = @_;
1209 my $e = new_editor();
1210 my $circ = $e->retrieve_action_circulation(
1216 circ => [ qw/ target_copy / ],
1217 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1218 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1219 acn => [ qw/ record / ],
1223 ) or return $e->event;
1225 my $copy = $circ->target_copy;
1226 my $vol = $copy->call_number;
1227 my $rec = $circ->target_copy->call_number->record;
1229 $vol->record($rec->id);
1230 $copy->call_number($vol->id);
1231 $circ->target_copy($copy->id);
1235 if( $rec->id == OILS_PRECAT_RECORD ) {
1239 $mvr = $U->record_to_mvr($rec);
1240 $rec->marc(''); # drop the bulky marc data
1254 __PACKAGE__->register_method(
1255 method => "test_batch_circ_events",
1256 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1259 # method for testing the behavior of a given event definition
1260 sub test_batch_circ_events {
1261 my($self, $conn, $auth, $event_def, $barcode) = @_;
1263 my $e = new_editor(authtoken => $auth);
1264 return $e->event unless $e->checkauth;
1265 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1267 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1268 or return $e->event;
1270 my $circ = $e->search_action_circulation(
1271 {target_copy => $copy->id, checkin_time => undef})->[0]
1272 or return $e->event;
1274 return undef unless $circ;
1276 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1281 __PACKAGE__->register_method(
1282 method => "user_payments_list",
1283 api_name => "open-ils.circ.user_payments.filtered.batch",
1286 desc => q/Returns a fleshed, date-limited set of all payments a user
1287 has made. By default, ordered by payment date. Optionally
1288 ordered by other columns in the top-level "mp" object/,
1290 {desc => 'Authentication token', type => 'string'},
1291 {desc => 'User ID', type => 'number'},
1292 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1294 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1295 and the related fully-realized payment object (e.g money.cash_payment)/}
1299 sub user_payments_list {
1300 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1302 my $e = new_editor(authtoken => $auth);
1303 return $e->event unless $e->checkauth;
1305 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1306 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1308 $order_by ||= ['payment_ts'];
1310 # all payments by user, between start_date and end_date
1311 my $payments = $e->json_query({
1312 select => {mp => ['id']},
1316 fkey => 'xact', field => 'id'}
1320 '+mbt' => {usr => $user_id},
1321 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1323 order_by => {mp => $order_by}
1326 for my $payment_id (@$payments) {
1327 my $payment = $e->retrieve_money_payment([
1335 'credit_card_payment',
1349 $conn->respond($payment);
1356 __PACKAGE__->register_method(
1357 method => "retrieve_circ_chain",
1358 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1361 desc => q/Given a circulation, this returns all circulation objects
1362 that are part of the same chain of renewals./,
1364 {desc => 'Authentication token', type => 'string'},
1365 {desc => 'Circ ID', type => 'number'},
1367 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1371 sub retrieve_circ_chain {
1372 my($self, $conn, $auth, $circ_id) = @_;
1374 my $e = new_editor(authtoken => $auth);
1375 return $e->event unless $e->checkauth;
1377 # grab the base circ and all parent (previous) circs by fleshing
1378 my $base_circ = $e->retrieve_action_circulation([
1382 flesh_fields => {circ => [qw/parent_circ workstation checkin_workstation/]}
1384 ]) or return $e->event;
1386 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $base_circ->circ_lib);
1388 # send each circ to the caller, starting with the oldest circulation
1390 my $circ = $base_circ;
1392 push(@chain, $circ);
1394 # unflesh for consistency
1395 my $parent = $circ->parent_circ;
1396 $circ->parent_circ($parent->id) if $parent;
1400 $conn->respond($_) for reverse(@chain);
1402 # base circ may not be the end of the chain. see if there are any subsequent circs
1404 $conn->respond($circ) while ($circ = $e->search_action_circulation([
1405 {parent_circ => $circ->id},
1406 {flesh => 1, flesh_fields => {circ => [qw/workstation checkin_workstation/]}}
1414 # {"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}}