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 __PACKAGE__->register_method(
379 method => "post_checkin_backdate_circ",
380 api_name => "open-ils.circ.post_checkin_backdate.batch",
383 desc => q/@see open-ils.circ.post_checkin_backdate. Batch mode/,
385 {desc => 'Authentication token', type => 'string'},
386 {desc => 'List of Circ ID', type => 'array'},
387 {desc => 'ISO8601 backdate', type => 'string'},
389 return => {desc => q/Set of: 1 on success, failure event on error/}
394 sub post_checkin_backdate_circ {
395 my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
396 my $e = new_editor(authtoken=>$auth);
397 return $e->die_event unless $e->checkauth;
398 if($self->api_name =~ /batch/) {
399 $conn->respond(post_checkin_backdate_circ_impl($e, $_, $backdate)) for $circ_id;
401 $conn->respond_complete(post_checkin_backdate_circ_impl($e, $circ_id, $backdate));
409 sub post_checkin_backdate_circ_impl {
410 my($e, $circ_id, $backdate) = @_;
414 my $circ = $e->retrieve_action_circulation($circ_id)
415 or return $e->die_event;
417 # anyone with checkin perms can backdate (more restrictive?)
418 return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
420 # don't allow back-dating an open circulation
421 return OpenILS::Event->new('BAD_PARAMS') unless
422 $backdate and $circ->checkin_time;
424 # update the checkin and stop_fines times to reflect the new backdate
425 $circ->stop_fines_time(clense_ISO8601($backdate));
426 $circ->checkin_time(clense_ISO8601($backdate));
427 $e->update_action_circulation($circ) or return $e->die_event;
429 # now void the overdues "erased" by the back-dating
430 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
433 # If the circ was closed before and the balance owned !=0, re-open the transaction
434 $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
443 __PACKAGE__->register_method (
444 method => 'set_circ_due_date',
445 api_name => 'open-ils.circ.circulation.due_date.update',
447 Updates the due_date on the given circ
449 @param circid The id of the circ to update
450 @param date The timestamp of the new due date
454 sub set_circ_due_date {
455 my( $self, $conn, $auth, $circ_id, $date ) = @_;
457 my $e = new_editor(xact=>1, authtoken=>$auth);
458 return $e->die_event unless $e->checkauth;
459 my $circ = $e->retrieve_action_circulation($circ_id)
460 or return $e->die_event;
462 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
463 $date = clense_ISO8601($date);
464 $circ->due_date($date);
465 $e->update_action_circulation($circ) or return $e->die_event;
472 __PACKAGE__->register_method(
473 method => "create_in_house_use",
474 api_name => 'open-ils.circ.in_house_use.create',
476 Creates an in-house use action.
477 @param $authtoken The login session key
478 @param params A hash of params including
479 'location' The org unit id where the in-house use occurs
480 'copyid' The copy in question
481 'count' The number of in-house uses to apply to this copy
482 @return An array of id's representing the id's of the newly created
483 in-house use objects or an event on an error
486 __PACKAGE__->register_method(
487 method => "create_in_house_use",
488 api_name => 'open-ils.circ.non_cat_in_house_use.create',
492 sub create_in_house_use {
493 my( $self, $client, $auth, $params ) = @_;
496 my $org = $params->{location};
497 my $copyid = $params->{copyid};
498 my $count = $params->{count} || 1;
499 my $nc_type = $params->{non_cat_type};
500 my $use_time = $params->{use_time} || 'now';
502 my $e = new_editor(xact=>1,authtoken=>$auth);
503 return $e->event unless $e->checkauth;
504 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
506 my $non_cat = 1 if $self->api_name =~ /non_cat/;
510 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
512 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
518 if( $use_time ne 'now' ) {
519 $use_time = clense_ISO8601($use_time);
520 $logger->debug("in_house_use setting use time to $use_time");
531 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
532 $ihu->item_type($nc_type);
533 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
534 $cmeth = "create_action_non_cat_in_house_use";
537 $ihu = Fieldmapper::action::in_house_use->new;
539 $method = 'open-ils.storage.direct.action.in_house_use.create';
540 $cmeth = "create_action_in_house_use";
543 $ihu->staff($e->requestor->id);
544 $ihu->org_unit($org);
545 $ihu->use_time($use_time);
547 $ihu = $e->$cmeth($ihu) or return $e->event;
548 push( @ids, $ihu->id );
559 __PACKAGE__->register_method(
560 method => "view_circs",
561 api_name => "open-ils.circ.copy_checkout_history.retrieve",
563 Retrieves the last X circs for a given copy
564 @param authtoken The login session key
565 @param copyid The copy to check
566 @param count How far to go back in the item history
567 @return An array of circ ids
570 # ----------------------------------------------------------------------
571 # Returns $count most recent circs. If count exceeds the configured
572 # max, use the configured max instead
573 # ----------------------------------------------------------------------
575 my( $self, $client, $authtoken, $copyid, $count ) = @_;
577 my $e = new_editor(authtoken => $authtoken);
578 return $e->event unless $e->checkauth;
580 my $copy = $e->retrieve_asset_copy([
583 flesh_fields => {acp => ['call_number']}
585 ]) or return $e->event;
587 return $e->event unless $e->allowed(
588 'VIEW_COPY_CHECKOUT_HISTORY',
589 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
590 $copy->circ_lib : $copy->call_number->owning_lib);
592 my $max_history = $U->ou_ancestor_setting_value(
593 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
595 if(defined $max_history) {
596 $count = $max_history unless defined $count and $count < $max_history;
598 $count = 4 unless defined $count;
601 return $e->search_action_circulation([
602 {target_copy => $copyid},
603 {limit => $count, order_by => { circ => "xact_start DESC" }}
608 __PACKAGE__->register_method(
609 method => "circ_count",
610 api_name => "open-ils.circ.circulation.count",
612 Returns the number of times the item has circulated
613 @param copyid The copy to check
617 my( $self, $client, $copyid, $range ) = @_;
618 my $e = OpenILS::Utils::Editor->new;
619 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
624 __PACKAGE__->register_method(
625 method => 'fetch_notes',
626 api_name => 'open-ils.circ.copy_note.retrieve.all',
628 Returns an array of copy note objects.
629 @param args A named hash of parameters including:
630 authtoken : Required if viewing non-public notes
631 itemid : The id of the item whose notes we want to retrieve
632 pub : True if all the caller wants are public notes
633 @return An array of note objects
636 __PACKAGE__->register_method(
637 method => 'fetch_notes',
638 api_name => 'open-ils.circ.call_number_note.retrieve.all',
639 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
641 __PACKAGE__->register_method(
642 method => 'fetch_notes',
643 api_name => 'open-ils.circ.title_note.retrieve.all',
644 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
647 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
649 my( $self, $connection, $args ) = @_;
651 my $id = $$args{itemid};
652 my $authtoken = $$args{authtoken};
655 if( $self->api_name =~ /copy/ ) {
657 return $U->cstorereq(
658 'open-ils.cstore.direct.asset.copy_note.search.atomic',
659 { owning_copy => $id, pub => 't' } );
661 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
663 return $U->cstorereq(
664 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
667 } elsif( $self->api_name =~ /call_number/ ) {
669 return $U->cstorereq(
670 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
671 { call_number => $id, pub => 't' } );
673 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
675 return $U->cstorereq(
676 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
679 } elsif( $self->api_name =~ /title/ ) {
681 return $U->cstorereq(
682 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
683 { record => $id, pub => 't' } );
685 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
687 return $U->cstorereq(
688 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
695 __PACKAGE__->register_method(
696 method => 'has_notes',
697 api_name => 'open-ils.circ.copy.has_notes');
698 __PACKAGE__->register_method(
699 method => 'has_notes',
700 api_name => 'open-ils.circ.call_number.has_notes');
701 __PACKAGE__->register_method(
702 method => 'has_notes',
703 api_name => 'open-ils.circ.title.has_notes');
707 my( $self, $conn, $authtoken, $id ) = @_;
708 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
709 return $editor->event unless $editor->checkauth;
711 my $n = $editor->search_asset_copy_note(
712 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
714 $n = $editor->search_asset_call_number_note(
715 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
717 $n = $editor->search_biblio_record_note(
718 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
725 __PACKAGE__->register_method(
726 method => 'create_copy_note',
727 api_name => 'open-ils.circ.copy_note.create',
729 Creates a new copy note
730 @param authtoken The login session key
731 @param note The note object to create
732 @return The id of the new note object
735 sub create_copy_note {
736 my( $self, $connection, $authtoken, $note ) = @_;
738 my $e = new_editor(xact=>1, authtoken=>$authtoken);
739 return $e->event unless $e->checkauth;
740 my $copy = $e->retrieve_asset_copy(
744 flesh_fields => { 'acp' => ['call_number'] }
749 return $e->event unless
750 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
752 $note->create_date('now');
753 $note->creator($e->requestor->id);
754 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
757 $e->create_asset_copy_note($note) or return $e->event;
763 __PACKAGE__->register_method(
764 method => 'delete_copy_note',
765 api_name => 'open-ils.circ.copy_note.delete',
767 Deletes an existing copy note
768 @param authtoken The login session key
769 @param noteid The id of the note to delete
770 @return 1 on success - Event otherwise.
772 sub delete_copy_note {
773 my( $self, $conn, $authtoken, $noteid ) = @_;
775 my $e = new_editor(xact=>1, authtoken=>$authtoken);
776 return $e->die_event unless $e->checkauth;
778 my $note = $e->retrieve_asset_copy_note([
782 'acpn' => [ 'owning_copy' ],
783 'acp' => [ 'call_number' ],
786 ]) or return $e->die_event;
788 if( $note->creator ne $e->requestor->id ) {
789 return $e->die_event unless
790 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
793 $e->delete_asset_copy_note($note) or return $e->die_event;
799 __PACKAGE__->register_method(
800 method => 'age_hold_rules',
801 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
805 my( $self, $conn ) = @_;
806 return new_editor()->retrieve_all_config_rules_age_hold_protect();
811 __PACKAGE__->register_method(
812 method => 'copy_details_barcode',
814 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
815 sub copy_details_barcode {
816 my( $self, $conn, $auth, $barcode ) = @_;
817 my $e = new_editor();
818 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
819 return $e->event unless $cid;
820 return copy_details( $self, $conn, $auth, $cid );
824 __PACKAGE__->register_method(
825 method => 'copy_details',
826 api_name => 'open-ils.circ.copy_details.retrieve');
829 my( $self, $conn, $auth, $copy_id ) = @_;
830 my $e = new_editor(authtoken=>$auth);
831 return $e->event unless $e->checkauth;
833 my $flesh = { flesh => 1 };
835 my $copy = $e->retrieve_asset_copy(
841 acp => ['call_number'],
845 ]) or return $e->event;
848 # De-flesh the copy for backwards compatibility
850 my $vol = $copy->call_number;
852 $copy->call_number($vol->id);
853 my $record = $vol->record;
855 $vol->record($record->id);
856 $mvr = $U->record_to_mvr($record);
861 my $hold = $e->search_action_hold_request(
863 current_copy => $copy_id,
864 capture_time => { "!=" => undef },
865 fulfillment_time => undef,
866 cancel_time => undef,
870 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
872 my $transit = $e->search_action_transit_copy(
873 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
875 # find the latest circ, open or closed
876 my $circ = $e->search_action_circulation(
878 { target_copy => $copy_id },
883 'checkin_workstation',
889 order_by => { circ => 'xact_start desc' },
909 __PACKAGE__->register_method(
910 method => 'mark_item',
911 api_name => 'open-ils.circ.mark_item_damaged',
913 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
914 @param authtoken The login session key
915 @param copy_id The ID of the copy to mark as damaged
916 @return 1 on success - Event otherwise.
919 __PACKAGE__->register_method(
920 method => 'mark_item',
921 api_name => 'open-ils.circ.mark_item_missing',
923 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
924 @param authtoken The login session key
925 @param copy_id The ID of the copy to mark as missing
926 @return 1 on success - Event otherwise.
929 __PACKAGE__->register_method(
930 method => 'mark_item',
931 api_name => 'open-ils.circ.mark_item_bindery',
933 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
934 @param authtoken The login session key
935 @param copy_id The ID of the copy to mark as bindery
936 @return 1 on success - Event otherwise.
939 __PACKAGE__->register_method(
940 method => 'mark_item',
941 api_name => 'open-ils.circ.mark_item_on_order',
943 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
944 @param authtoken The login session key
945 @param copy_id The ID of the copy to mark as on order
946 @return 1 on success - Event otherwise.
949 __PACKAGE__->register_method(
950 method => 'mark_item',
951 api_name => 'open-ils.circ.mark_item_ill',
953 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
954 @param authtoken The login session key
955 @param copy_id The ID of the copy to mark as inter-library loan
956 @return 1 on success - Event otherwise.
959 __PACKAGE__->register_method(
960 method => 'mark_item',
961 api_name => 'open-ils.circ.mark_item_cataloging',
963 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
964 @param authtoken The login session key
965 @param copy_id The ID of the copy to mark as cataloging
966 @return 1 on success - Event otherwise.
969 __PACKAGE__->register_method(
970 method => 'mark_item',
971 api_name => 'open-ils.circ.mark_item_reserves',
973 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
974 @param authtoken The login session key
975 @param copy_id The ID of the copy to mark as reserves
976 @return 1 on success - Event otherwise.
979 __PACKAGE__->register_method(
980 method => 'mark_item',
981 api_name => 'open-ils.circ.mark_item_discard',
983 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
984 @param authtoken The login session key
985 @param copy_id The ID of the copy to mark as discard
986 @return 1 on success - Event otherwise.
991 my( $self, $conn, $auth, $copy_id, $args ) = @_;
992 my $e = new_editor(authtoken=>$auth, xact =>1);
993 return $e->die_event unless $e->checkauth;
996 my $copy = $e->retrieve_asset_copy([
998 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
999 or return $e->die_event;
1002 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1003 $copy->circ_lib : $copy->call_number->owning_lib;
1005 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
1008 my $perm = 'MARK_ITEM_MISSING';
1009 my $stat = OILS_COPY_STATUS_MISSING;
1011 if( $self->api_name =~ /damaged/ ) {
1012 $perm = 'MARK_ITEM_DAMAGED';
1013 $stat = OILS_COPY_STATUS_DAMAGED;
1014 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1015 return $evt if $evt;
1017 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1018 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1020 } elsif ( $self->api_name =~ /bindery/ ) {
1021 $perm = 'MARK_ITEM_BINDERY';
1022 $stat = OILS_COPY_STATUS_BINDERY;
1023 } elsif ( $self->api_name =~ /on_order/ ) {
1024 $perm = 'MARK_ITEM_ON_ORDER';
1025 $stat = OILS_COPY_STATUS_ON_ORDER;
1026 } elsif ( $self->api_name =~ /ill/ ) {
1027 $perm = 'MARK_ITEM_ILL';
1028 $stat = OILS_COPY_STATUS_ILL;
1029 } elsif ( $self->api_name =~ /cataloging/ ) {
1030 $perm = 'MARK_ITEM_CATALOGING';
1031 $stat = OILS_COPY_STATUS_CATALOGING;
1032 } elsif ( $self->api_name =~ /reserves/ ) {
1033 $perm = 'MARK_ITEM_RESERVES';
1034 $stat = OILS_COPY_STATUS_RESERVES;
1035 } elsif ( $self->api_name =~ /discard/ ) {
1036 $perm = 'MARK_ITEM_DISCARD';
1037 $stat = OILS_COPY_STATUS_DISCARD;
1041 $copy->status($stat);
1042 $copy->edit_date('now');
1043 $copy->editor($e->requestor->id);
1045 $e->update_asset_copy($copy) or return $e->die_event;
1047 my $holds = $e->search_action_hold_request(
1049 current_copy => $copy->id,
1050 fulfillment_time => undef,
1051 cancel_time => undef,
1057 $logger->debug("resetting holds that target the marked copy");
1058 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1063 sub handle_mark_damaged {
1064 my($e, $copy, $owning_lib, $args) = @_;
1066 my $apply = $args->{apply_fines} || '';
1067 return undef if $apply eq 'noapply';
1069 # grab the last circulation
1070 my $circ = $e->search_action_circulation([
1071 { target_copy => $copy->id},
1073 order_by => {circ => "xact_start DESC"},
1075 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1079 return undef unless $circ;
1081 my $charge_price = $U->ou_ancestor_setting_value(
1082 $owning_lib, 'circ.charge_on_damaged', $e);
1084 my $proc_fee = $U->ou_ancestor_setting_value(
1085 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1087 return undef unless $charge_price or $proc_fee;
1089 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1090 my $total = $copy_price + $proc_fee;
1094 if($charge_price and $copy_price) {
1095 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1096 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1097 return $evt if $evt;
1101 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1102 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1103 return $evt if $evt;
1106 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1107 return $evt if $evt;
1109 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1110 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1115 return OpenILS::Event->new('DAMAGE_CHARGE',
1129 # ----------------------------------------------------------------------
1130 __PACKAGE__->register_method(
1131 method => 'magic_fetch',
1132 api_name => 'open-ils.agent.fetch'
1135 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1138 my( $self, $conn, $auth, $args ) = @_;
1139 my $e = new_editor( authtoken => $auth );
1140 return $e->event unless $e->checkauth;
1142 my $hint = $$args{hint};
1143 my $id = $$args{id};
1145 # Is the call allowed to fetch this type of object?
1146 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1148 # Find the class the implements the given hint
1149 my ($class) = grep {
1150 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1152 $class =~ s/Fieldmapper:://og;
1153 $class =~ s/::/_/og;
1154 my $method = "retrieve_$class";
1156 my $obj = $e->$method($id) or return $e->event;
1159 # ----------------------------------------------------------------------
1162 __PACKAGE__->register_method(
1163 method => "fleshed_circ_retrieve",
1165 api_name => "open-ils.circ.fleshed.retrieve",);
1167 sub fleshed_circ_retrieve {
1168 my( $self, $client, $id ) = @_;
1169 my $e = new_editor();
1170 my $circ = $e->retrieve_action_circulation(
1176 circ => [ qw/ target_copy / ],
1177 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1178 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1179 acn => [ qw/ record / ],
1183 ) or return $e->event;
1185 my $copy = $circ->target_copy;
1186 my $vol = $copy->call_number;
1187 my $rec = $circ->target_copy->call_number->record;
1189 $vol->record($rec->id);
1190 $copy->call_number($vol->id);
1191 $circ->target_copy($copy->id);
1195 if( $rec->id == OILS_PRECAT_RECORD ) {
1199 $mvr = $U->record_to_mvr($rec);
1200 $rec->marc(''); # drop the bulky marc data
1214 __PACKAGE__->register_method(
1215 method => "test_batch_circ_events",
1216 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1219 # method for testing the behavior of a given event definition
1220 sub test_batch_circ_events {
1221 my($self, $conn, $auth, $event_def, $barcode) = @_;
1223 my $e = new_editor(authtoken => $auth);
1224 return $e->event unless $e->checkauth;
1225 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1227 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1228 or return $e->event;
1230 my $circ = $e->search_action_circulation(
1231 {target_copy => $copy->id, checkin_time => undef})->[0]
1232 or return $e->event;
1234 return undef unless $circ;
1236 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1241 __PACKAGE__->register_method(
1242 method => "user_payments_list",
1243 api_name => "open-ils.circ.user_payments.filtered.batch",
1246 desc => q/Returns a fleshed, date-limited set of all payments a user
1247 has made. By default, ordered by payment date. Optionally
1248 ordered by other columns in the top-level "mp" object/,
1250 {desc => 'Authentication token', type => 'string'},
1251 {desc => 'User ID', type => 'number'},
1252 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1254 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1255 and the related fully-realized payment object (e.g money.cash_payment)/}
1259 sub user_payments_list {
1260 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1262 my $e = new_editor(authtoken => $auth);
1263 return $e->event unless $e->checkauth;
1265 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1266 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1268 $order_by ||= ['payment_ts'];
1270 # all payments by user, between start_date and end_date
1271 my $payments = $e->json_query({
1272 select => {mp => ['id']},
1276 fkey => 'xact', field => 'id'}
1280 '+mbt' => {usr => $user_id},
1281 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1283 order_by => {mp => $order_by}
1286 for my $payment_id (@$payments) {
1287 my $payment = $e->retrieve_money_payment([
1295 'credit_card_payment',
1309 $conn->respond($payment);
1316 __PACKAGE__->register_method(
1317 method => "retrieve_circ_chain",
1318 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1321 desc => q/Given a circulation, this returns all circulation objects
1322 that are part of the same chain of renewals./,
1324 {desc => 'Authentication token', type => 'string'},
1325 {desc => 'Circ ID', type => 'number'},
1327 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1331 sub retrieve_circ_chain {
1332 my($self, $conn, $auth, $circ_id) = @_;
1334 my $e = new_editor(authtoken => $auth);
1335 return $e->event unless $e->checkauth;
1337 # grab the base circ and all parent (previous) circs by fleshing
1338 my $base_circ = $e->retrieve_action_circulation([
1342 flesh_fields => {circ => ['parent_circ']}
1344 ]) or return $e->event;
1346 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $base_circ->circ_lib);
1348 # send each circ to the caller, starting with the oldest circulation
1350 my $circ = $base_circ;
1352 push(@chain, $circ);
1354 # unflesh for consistency
1355 my $parent = $circ->parent_circ;
1356 $circ->parent_circ($parent->id) if $parent;
1360 $conn->respond($_) for reverse(@chain);
1362 # base circ may not be the end of the chain. see if there are any subsequent circs
1364 $conn->respond($circ) while ($circ = $e->search_action_circulation({parent_circ => $circ->id})->[0]);
1371 # {"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}}