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;
364 __PACKAGE__->register_method(
365 method => "post_checkin_backdate_circ",
366 api_name => "open-ils.circ.post_checkin_backdate",
368 desc => q/Back-date an already checked in circulation/,
370 {desc => 'Authentication token', type => 'string'},
371 {desc => 'Circ ID', type => 'number'},
372 {desc => 'ISO8601 backdate', type => 'string'},
374 return => {desc => q/1 on success, failure event on error/}
378 sub post_checkin_backdate_circ {
379 my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
381 my $e = new_editor(authtoken=>$auth, xact=>1);
382 return $e->die_event unless $e->checkauth;
384 my $circ = $e->retrieve_action_circulation($circ_id)
385 or return $e->die_event;
387 # anyone with checkin perms can backdate (more restrictive?)
388 return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
390 # don't allow back-dating an open circulation
391 return OpenILS::Event->new('BAD_PARAMS') unless
392 $backdate and $circ->checkin_time;
394 # update the checkin and stop_fines times to reflect the new backdate
395 $circ->stop_fines_time(clense_ISO8601($backdate));
396 $circ->checkin_time(clense_ISO8601($backdate));
397 $e->update_action_circulation($circ) or return $e->die_event;
399 # now void the overdues "erased" by the back-dating
400 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
403 # If the circ was closed before and the balance owned !=0, re-open the transaction
404 $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
413 __PACKAGE__->register_method (
414 method => 'set_circ_due_date',
415 api_name => 'open-ils.circ.circulation.due_date.update',
417 Updates the due_date on the given circ
419 @param circid The id of the circ to update
420 @param date The timestamp of the new due date
424 sub set_circ_due_date {
425 my( $self, $conn, $auth, $circ_id, $date ) = @_;
427 my $e = new_editor(xact=>1, authtoken=>$auth);
428 return $e->die_event unless $e->checkauth;
429 my $circ = $e->retrieve_action_circulation($circ_id)
430 or return $e->die_event;
432 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
433 $date = clense_ISO8601($date);
434 $circ->due_date($date);
435 $e->update_action_circulation($circ) or return $e->die_event;
442 __PACKAGE__->register_method(
443 method => "create_in_house_use",
444 api_name => 'open-ils.circ.in_house_use.create',
446 Creates an in-house use action.
447 @param $authtoken The login session key
448 @param params A hash of params including
449 'location' The org unit id where the in-house use occurs
450 'copyid' The copy in question
451 'count' The number of in-house uses to apply to this copy
452 @return An array of id's representing the id's of the newly created
453 in-house use objects or an event on an error
456 __PACKAGE__->register_method(
457 method => "create_in_house_use",
458 api_name => 'open-ils.circ.non_cat_in_house_use.create',
462 sub create_in_house_use {
463 my( $self, $client, $auth, $params ) = @_;
466 my $org = $params->{location};
467 my $copyid = $params->{copyid};
468 my $count = $params->{count} || 1;
469 my $nc_type = $params->{non_cat_type};
470 my $use_time = $params->{use_time} || 'now';
472 my $e = new_editor(xact=>1,authtoken=>$auth);
473 return $e->event unless $e->checkauth;
474 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
476 my $non_cat = 1 if $self->api_name =~ /non_cat/;
480 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
482 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
488 if( $use_time ne 'now' ) {
489 $use_time = clense_ISO8601($use_time);
490 $logger->debug("in_house_use setting use time to $use_time");
501 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
502 $ihu->item_type($nc_type);
503 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
504 $cmeth = "create_action_non_cat_in_house_use";
507 $ihu = Fieldmapper::action::in_house_use->new;
509 $method = 'open-ils.storage.direct.action.in_house_use.create';
510 $cmeth = "create_action_in_house_use";
513 $ihu->staff($e->requestor->id);
514 $ihu->org_unit($org);
515 $ihu->use_time($use_time);
517 $ihu = $e->$cmeth($ihu) or return $e->event;
518 push( @ids, $ihu->id );
529 __PACKAGE__->register_method(
530 method => "view_circs",
531 api_name => "open-ils.circ.copy_checkout_history.retrieve",
533 Retrieves the last X circs for a given copy
534 @param authtoken The login session key
535 @param copyid The copy to check
536 @param count How far to go back in the item history
537 @return An array of circ ids
540 # ----------------------------------------------------------------------
541 # Returns $count most recent circs. If count exceeds the configured
542 # max, use the configured max instead
543 # ----------------------------------------------------------------------
545 my( $self, $client, $authtoken, $copyid, $count ) = @_;
547 my $e = new_editor(authtoken => $authtoken);
548 return $e->event unless $e->checkauth;
550 my $copy = $e->retrieve_asset_copy([
553 flesh_fields => {acp => ['call_number']}
555 ]) or return $e->event;
557 return $e->event unless $e->allowed(
558 'VIEW_COPY_CHECKOUT_HISTORY',
559 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
560 $copy->circ_lib : $copy->call_number->owning_lib);
562 my $max_history = $U->ou_ancestor_setting_value(
563 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
565 if(defined $max_history) {
566 $count = $max_history unless defined $count and $count < $max_history;
568 $count = 4 unless defined $count;
571 return $e->search_action_circulation([
572 {target_copy => $copyid},
573 {limit => $count, order_by => { circ => "xact_start DESC" }}
578 __PACKAGE__->register_method(
579 method => "circ_count",
580 api_name => "open-ils.circ.circulation.count",
582 Returns the number of times the item has circulated
583 @param copyid The copy to check
587 my( $self, $client, $copyid, $range ) = @_;
588 my $e = OpenILS::Utils::Editor->new;
589 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
594 __PACKAGE__->register_method(
595 method => 'fetch_notes',
596 api_name => 'open-ils.circ.copy_note.retrieve.all',
598 Returns an array of copy note objects.
599 @param args A named hash of parameters including:
600 authtoken : Required if viewing non-public notes
601 itemid : The id of the item whose notes we want to retrieve
602 pub : True if all the caller wants are public notes
603 @return An array of note objects
606 __PACKAGE__->register_method(
607 method => 'fetch_notes',
608 api_name => 'open-ils.circ.call_number_note.retrieve.all',
609 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
611 __PACKAGE__->register_method(
612 method => 'fetch_notes',
613 api_name => 'open-ils.circ.title_note.retrieve.all',
614 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
617 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
619 my( $self, $connection, $args ) = @_;
621 my $id = $$args{itemid};
622 my $authtoken = $$args{authtoken};
625 if( $self->api_name =~ /copy/ ) {
627 return $U->cstorereq(
628 'open-ils.cstore.direct.asset.copy_note.search.atomic',
629 { owning_copy => $id, pub => 't' } );
631 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
633 return $U->cstorereq(
634 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
637 } elsif( $self->api_name =~ /call_number/ ) {
639 return $U->cstorereq(
640 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
641 { call_number => $id, pub => 't' } );
643 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
645 return $U->cstorereq(
646 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
649 } elsif( $self->api_name =~ /title/ ) {
651 return $U->cstorereq(
652 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
653 { record => $id, pub => 't' } );
655 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
657 return $U->cstorereq(
658 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
665 __PACKAGE__->register_method(
666 method => 'has_notes',
667 api_name => 'open-ils.circ.copy.has_notes');
668 __PACKAGE__->register_method(
669 method => 'has_notes',
670 api_name => 'open-ils.circ.call_number.has_notes');
671 __PACKAGE__->register_method(
672 method => 'has_notes',
673 api_name => 'open-ils.circ.title.has_notes');
677 my( $self, $conn, $authtoken, $id ) = @_;
678 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
679 return $editor->event unless $editor->checkauth;
681 my $n = $editor->search_asset_copy_note(
682 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
684 $n = $editor->search_asset_call_number_note(
685 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
687 $n = $editor->search_biblio_record_note(
688 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
695 __PACKAGE__->register_method(
696 method => 'create_copy_note',
697 api_name => 'open-ils.circ.copy_note.create',
699 Creates a new copy note
700 @param authtoken The login session key
701 @param note The note object to create
702 @return The id of the new note object
705 sub create_copy_note {
706 my( $self, $connection, $authtoken, $note ) = @_;
708 my $e = new_editor(xact=>1, authtoken=>$authtoken);
709 return $e->event unless $e->checkauth;
710 my $copy = $e->retrieve_asset_copy(
714 flesh_fields => { 'acp' => ['call_number'] }
719 return $e->event unless
720 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
722 $note->create_date('now');
723 $note->creator($e->requestor->id);
724 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
727 $e->create_asset_copy_note($note) or return $e->event;
733 __PACKAGE__->register_method(
734 method => 'delete_copy_note',
735 api_name => 'open-ils.circ.copy_note.delete',
737 Deletes an existing copy note
738 @param authtoken The login session key
739 @param noteid The id of the note to delete
740 @return 1 on success - Event otherwise.
742 sub delete_copy_note {
743 my( $self, $conn, $authtoken, $noteid ) = @_;
745 my $e = new_editor(xact=>1, authtoken=>$authtoken);
746 return $e->die_event unless $e->checkauth;
748 my $note = $e->retrieve_asset_copy_note([
752 'acpn' => [ 'owning_copy' ],
753 'acp' => [ 'call_number' ],
756 ]) or return $e->die_event;
758 if( $note->creator ne $e->requestor->id ) {
759 return $e->die_event unless
760 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
763 $e->delete_asset_copy_note($note) or return $e->die_event;
769 __PACKAGE__->register_method(
770 method => 'age_hold_rules',
771 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
775 my( $self, $conn ) = @_;
776 return new_editor()->retrieve_all_config_rules_age_hold_protect();
781 __PACKAGE__->register_method(
782 method => 'copy_details_barcode',
784 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
785 sub copy_details_barcode {
786 my( $self, $conn, $auth, $barcode ) = @_;
787 my $e = new_editor();
788 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
789 return $e->event unless $cid;
790 return copy_details( $self, $conn, $auth, $cid );
794 __PACKAGE__->register_method(
795 method => 'copy_details',
796 api_name => 'open-ils.circ.copy_details.retrieve');
799 my( $self, $conn, $auth, $copy_id ) = @_;
800 my $e = new_editor(authtoken=>$auth);
801 return $e->event unless $e->checkauth;
803 my $flesh = { flesh => 1 };
805 my $copy = $e->retrieve_asset_copy(
811 acp => ['call_number'],
815 ]) or return $e->event;
818 # De-flesh the copy for backwards compatibility
820 my $vol = $copy->call_number;
822 $copy->call_number($vol->id);
823 my $record = $vol->record;
825 $vol->record($record->id);
826 $mvr = $U->record_to_mvr($record);
831 my $hold = $e->search_action_hold_request(
833 current_copy => $copy_id,
834 capture_time => { "!=" => undef },
835 fulfillment_time => undef,
836 cancel_time => undef,
840 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
842 my $transit = $e->search_action_transit_copy(
843 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
845 # find the latest circ, open or closed
846 my $circ = $e->search_action_circulation(
848 { target_copy => $copy_id },
853 'checkin_workstation',
859 order_by => { circ => 'xact_start desc' },
879 __PACKAGE__->register_method(
880 method => 'mark_item',
881 api_name => 'open-ils.circ.mark_item_damaged',
883 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
884 @param authtoken The login session key
885 @param copy_id The ID of the copy to mark as damaged
886 @return 1 on success - Event otherwise.
889 __PACKAGE__->register_method(
890 method => 'mark_item',
891 api_name => 'open-ils.circ.mark_item_missing',
893 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
894 @param authtoken The login session key
895 @param copy_id The ID of the copy to mark as missing
896 @return 1 on success - Event otherwise.
899 __PACKAGE__->register_method(
900 method => 'mark_item',
901 api_name => 'open-ils.circ.mark_item_bindery',
903 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
904 @param authtoken The login session key
905 @param copy_id The ID of the copy to mark as bindery
906 @return 1 on success - Event otherwise.
909 __PACKAGE__->register_method(
910 method => 'mark_item',
911 api_name => 'open-ils.circ.mark_item_on_order',
913 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
914 @param authtoken The login session key
915 @param copy_id The ID of the copy to mark as on order
916 @return 1 on success - Event otherwise.
919 __PACKAGE__->register_method(
920 method => 'mark_item',
921 api_name => 'open-ils.circ.mark_item_ill',
923 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
924 @param authtoken The login session key
925 @param copy_id The ID of the copy to mark as inter-library loan
926 @return 1 on success - Event otherwise.
929 __PACKAGE__->register_method(
930 method => 'mark_item',
931 api_name => 'open-ils.circ.mark_item_cataloging',
933 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
934 @param authtoken The login session key
935 @param copy_id The ID of the copy to mark as cataloging
936 @return 1 on success - Event otherwise.
939 __PACKAGE__->register_method(
940 method => 'mark_item',
941 api_name => 'open-ils.circ.mark_item_reserves',
943 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
944 @param authtoken The login session key
945 @param copy_id The ID of the copy to mark as reserves
946 @return 1 on success - Event otherwise.
949 __PACKAGE__->register_method(
950 method => 'mark_item',
951 api_name => 'open-ils.circ.mark_item_discard',
953 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
954 @param authtoken The login session key
955 @param copy_id The ID of the copy to mark as discard
956 @return 1 on success - Event otherwise.
961 my( $self, $conn, $auth, $copy_id, $args ) = @_;
962 my $e = new_editor(authtoken=>$auth, xact =>1);
963 return $e->die_event unless $e->checkauth;
966 my $copy = $e->retrieve_asset_copy([
968 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
969 or return $e->die_event;
972 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
973 $copy->circ_lib : $copy->call_number->owning_lib;
975 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
978 my $perm = 'MARK_ITEM_MISSING';
979 my $stat = OILS_COPY_STATUS_MISSING;
981 if( $self->api_name =~ /damaged/ ) {
982 $perm = 'MARK_ITEM_DAMAGED';
983 $stat = OILS_COPY_STATUS_DAMAGED;
984 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
987 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
988 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
990 } elsif ( $self->api_name =~ /bindery/ ) {
991 $perm = 'MARK_ITEM_BINDERY';
992 $stat = OILS_COPY_STATUS_BINDERY;
993 } elsif ( $self->api_name =~ /on_order/ ) {
994 $perm = 'MARK_ITEM_ON_ORDER';
995 $stat = OILS_COPY_STATUS_ON_ORDER;
996 } elsif ( $self->api_name =~ /ill/ ) {
997 $perm = 'MARK_ITEM_ILL';
998 $stat = OILS_COPY_STATUS_ILL;
999 } elsif ( $self->api_name =~ /cataloging/ ) {
1000 $perm = 'MARK_ITEM_CATALOGING';
1001 $stat = OILS_COPY_STATUS_CATALOGING;
1002 } elsif ( $self->api_name =~ /reserves/ ) {
1003 $perm = 'MARK_ITEM_RESERVES';
1004 $stat = OILS_COPY_STATUS_RESERVES;
1005 } elsif ( $self->api_name =~ /discard/ ) {
1006 $perm = 'MARK_ITEM_DISCARD';
1007 $stat = OILS_COPY_STATUS_DISCARD;
1011 $copy->status($stat);
1012 $copy->edit_date('now');
1013 $copy->editor($e->requestor->id);
1015 $e->update_asset_copy($copy) or return $e->die_event;
1017 my $holds = $e->search_action_hold_request(
1019 current_copy => $copy->id,
1020 fulfillment_time => undef,
1021 cancel_time => undef,
1027 $logger->debug("resetting holds that target the marked copy");
1028 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1033 sub handle_mark_damaged {
1034 my($e, $copy, $owning_lib, $args) = @_;
1036 my $apply = $args->{apply_fines} || '';
1037 return undef if $apply eq 'noapply';
1039 # grab the last circulation
1040 my $circ = $e->search_action_circulation([
1041 { target_copy => $copy->id},
1043 order_by => {circ => "xact_start DESC"},
1045 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1049 return undef unless $circ;
1051 my $charge_price = $U->ou_ancestor_setting_value(
1052 $owning_lib, 'circ.charge_on_damaged', $e);
1054 my $proc_fee = $U->ou_ancestor_setting_value(
1055 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1057 return undef unless $charge_price or $proc_fee;
1059 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1060 my $total = $copy_price + $proc_fee;
1064 if($charge_price and $copy_price) {
1065 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1066 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1067 return $evt if $evt;
1071 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1072 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1073 return $evt if $evt;
1076 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1077 return $evt if $evt;
1079 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1080 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1085 return OpenILS::Event->new('DAMAGE_CHARGE',
1099 # ----------------------------------------------------------------------
1100 __PACKAGE__->register_method(
1101 method => 'magic_fetch',
1102 api_name => 'open-ils.agent.fetch'
1105 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1108 my( $self, $conn, $auth, $args ) = @_;
1109 my $e = new_editor( authtoken => $auth );
1110 return $e->event unless $e->checkauth;
1112 my $hint = $$args{hint};
1113 my $id = $$args{id};
1115 # Is the call allowed to fetch this type of object?
1116 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1118 # Find the class the implements the given hint
1119 my ($class) = grep {
1120 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1122 $class =~ s/Fieldmapper:://og;
1123 $class =~ s/::/_/og;
1124 my $method = "retrieve_$class";
1126 my $obj = $e->$method($id) or return $e->event;
1129 # ----------------------------------------------------------------------
1132 __PACKAGE__->register_method(
1133 method => "fleshed_circ_retrieve",
1135 api_name => "open-ils.circ.fleshed.retrieve",);
1137 sub fleshed_circ_retrieve {
1138 my( $self, $client, $id ) = @_;
1139 my $e = new_editor();
1140 my $circ = $e->retrieve_action_circulation(
1146 circ => [ qw/ target_copy / ],
1147 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1148 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1149 acn => [ qw/ record / ],
1153 ) or return $e->event;
1155 my $copy = $circ->target_copy;
1156 my $vol = $copy->call_number;
1157 my $rec = $circ->target_copy->call_number->record;
1159 $vol->record($rec->id);
1160 $copy->call_number($vol->id);
1161 $circ->target_copy($copy->id);
1165 if( $rec->id == OILS_PRECAT_RECORD ) {
1169 $mvr = $U->record_to_mvr($rec);
1170 $rec->marc(''); # drop the bulky marc data
1184 __PACKAGE__->register_method(
1185 method => "test_batch_circ_events",
1186 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1189 # method for testing the behavior of a given event definition
1190 sub test_batch_circ_events {
1191 my($self, $conn, $auth, $event_def, $barcode) = @_;
1193 my $e = new_editor(authtoken => $auth);
1194 return $e->event unless $e->checkauth;
1195 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1197 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1198 or return $e->event;
1200 my $circ = $e->search_action_circulation(
1201 {target_copy => $copy->id, checkin_time => undef})->[0]
1202 or return $e->event;
1204 return undef unless $circ;
1206 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1211 __PACKAGE__->register_method(
1212 method => "user_payments_list",
1213 api_name => "open-ils.circ.user_payments.filtered.batch",
1216 desc => q/Returns a fleshed, date-limited set of all payments a user
1217 has made. By default, ordered by payment date. Optionally
1218 ordered by other columns in the top-level "mp" object/,
1220 {desc => 'Authentication token', type => 'string'},
1221 {desc => 'User ID', type => 'number'},
1222 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1224 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1225 and the related fully-realized payment object (e.g money.cash_payment)/}
1229 sub user_payments_list {
1230 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1232 my $e = new_editor(authtoken => $auth);
1233 return $e->event unless $e->checkauth;
1235 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1236 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1238 $order_by ||= ['payment_ts'];
1240 # all payments by user, between start_date and end_date
1241 my $payments = $e->json_query({
1242 select => {mp => ['id']},
1246 fkey => 'xact', field => 'id'}
1250 '+mbt' => {usr => $user_id},
1251 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1253 order_by => {mp => $order_by}
1256 for my $payment_id (@$payments) {
1257 my $payment = $e->retrieve_money_payment([
1265 'credit_card_payment',
1279 $conn->respond($payment);
1286 __PACKAGE__->register_method(
1287 method => "retrieve_circ_chain",
1288 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1291 desc => q/Given a circulation, this returns all circulation objects
1292 that are part of the same chain of renewals./,
1294 {desc => 'Authentication token', type => 'string'},
1295 {desc => 'Circ ID', type => 'number'},
1297 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1301 sub retrieve_circ_chain {
1302 my($self, $conn, $auth, $circ_id) = @_;
1304 my $e = new_editor(authtoken => $auth);
1305 return $e->event unless $e->checkauth;
1307 # grab the base circ and all parent (previous) circs by fleshing
1308 my $base_circ = $e->retrieve_action_circulation([
1312 flesh_fields => {circ => ['parent_circ']}
1314 ]) or return $e->event;
1316 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $base_circ->circ_lib);
1318 # send each circ to the caller, starting with the oldest circulation
1320 my $circ = $base_circ;
1322 push(@chain, $circ);
1324 # unflesh for consistency
1325 my $parent = $circ->parent_circ;
1326 $circ->parent_circ($parent->id) if $parent;
1330 $conn->respond($_) for reverse(@chain);
1332 # base circ may not be the end of the chain. see if there are any subsequent circs
1334 $conn->respond($circ) while ($circ = $e->search_action_circulation({parent_circ => $circ->id})->[0]);
1341 # {"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}}