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",
111 NOTES => <<" NOTES");
112 Returns a list of open circulations as a pile of objects. Each object
113 contains the relevant copy, circ, and record
116 sub checkouts_by_user {
117 my($self, $client, $auth, $user_id) = @_;
119 my $e = new_editor(authtoken=>$auth);
120 return $e->event unless $e->checkauth;
122 my $circ_ids = $e->search_action_circulation(
124 checkin_time => undef,
126 {stop_fines => undef},
127 {stop_fines => ['MAXFINES','LONGOVERDUE']}
133 for my $id (@$circ_ids) {
134 my $circ = $e->retrieve_action_circulation([
138 circ => ['target_copy'],
139 acp => ['call_number'],
145 # un-flesh for consistency
146 my $c = $circ->target_copy;
147 $circ->target_copy($c->id);
149 my $cn = $c->call_number;
150 $c->call_number($cn->id);
158 record => $U->record_to_mvr($t)
168 __PACKAGE__->register_method(
169 method => "checkouts_by_user_slim",
170 api_name => "open-ils.circ.actor.user.checked_out.slim",
171 NOTES => <<" NOTES");
172 Returns a list of open circulation objects
176 sub checkouts_by_user_slim {
177 my( $self, $client, $user_session, $user_id ) = @_;
179 my( $requestor, $target, $copy, $record, $evt );
181 ( $requestor, $target, $evt ) =
182 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
185 $logger->debug( 'User ' . $requestor->id .
186 " retrieving checked out items for user " . $target->id );
188 # XXX Make the call correct..
189 return $apputils->simplereq(
191 "open-ils.cstore.direct.action.open_circulation.search.atomic",
192 { usr => $target->id, checkin_time => undef } );
193 # { usr => $target->id } );
197 __PACKAGE__->register_method(
198 method => "checkouts_by_user_opac",
199 api_name => "open-ils.circ.actor.user.checked_out.opac",);
202 sub checkouts_by_user_opac {
203 my( $self, $client, $auth, $user_id ) = @_;
205 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
206 return $e->event unless $e->checkauth;
207 $user_id ||= $e->requestor->id;
208 return $e->event unless
209 my $patron = $e->retrieve_actor_user($user_id);
212 my $search = {usr => $user_id, stop_fines => undef};
214 if( $user_id ne $e->requestor->id ) {
215 $data = $e->search_action_circulation(
216 $search, {checkperm=>1, permorg=>$patron->home_ou})
220 $data = $e->search_action_circulation($search);
227 __PACKAGE__->register_method(
228 method => "title_from_transaction",
229 api_name => "open-ils.circ.circ_transaction.find_title",
230 NOTES => <<" NOTES");
231 Returns a mods object for the title that is linked to from the
232 copy from the hold that created the given transaction
235 sub title_from_transaction {
236 my( $self, $client, $login_session, $transactionid ) = @_;
238 my( $user, $circ, $title, $evt );
240 ( $user, $evt ) = $apputils->checkses( $login_session );
243 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
246 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
249 return $apputils->record_to_mvr($title);
254 __PACKAGE__->register_method(
255 method => "new_set_circ_lost",
256 api_name => "open-ils.circ.circulation.set_lost",
258 Sets the copy and related open circulation to lost
260 @param args : barcode
265 # ---------------------------------------------------------------------
266 # Sets a circulation to lost. updates copy status to lost
267 # applies copy and/or prcoessing fees depending on org settings
268 # ---------------------------------------------------------------------
269 sub new_set_circ_lost {
270 my( $self, $conn, $auth, $args ) = @_;
272 my $e = new_editor(authtoken=>$auth, xact=>1);
273 return $e->die_event unless $e->checkauth;
275 my $copy = $e->search_asset_copy({barcode=>$$args{barcode}, deleted=>'f'})->[0]
276 or return $e->die_event;
278 my $evt = OpenILS::Application::Cat::AssetCommon->set_item_lost($e, $copy->id);
286 __PACKAGE__->register_method(
287 method => "set_circ_claims_returned",
288 api_name => "open-ils.circ.circulation.set_claims_returned",
290 desc => q/Sets the circ for a given item as claims returned
291 If a backdate is provided, overdue fines will be voided
292 back to the backdate/,
294 {desc => 'Authentication token', type => 'string'},
295 {desc => 'Arguments, including "barcode" and optional "backdate"', type => 'object'}
297 return => {desc => q/1 on success, failure event on error, and
298 PATRON_EXCEEDS_CLAIMS_RETURN_COUNT if the patron exceeds the
299 configured claims return maximum/}
303 __PACKAGE__->register_method(
304 method => "set_circ_claims_returned",
305 api_name => "open-ils.circ.circulation.set_claims_returned.override",
307 desc => q/This adds support for overrideing the configured max
308 claims returned amount.
309 @see open-ils.circ.circulation.set_claims_returned./,
313 sub set_circ_claims_returned {
314 my( $self, $conn, $auth, $args ) = @_;
316 my $e = new_editor(authtoken=>$auth, xact=>1);
317 return $e->die_event unless $e->checkauth;
319 my $barcode = $$args{barcode};
320 my $backdate = $$args{backdate};
322 my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0]
323 or return $e->die_event;
325 my $circ = $e->search_action_circulation(
326 {checkin_time => undef, target_copy => $copy->id})->[0]
327 or return $e->die_event;
329 $backdate = $circ->due_date if $$args{use_due_date};
331 $logger->info("marking circ for item $barcode as claims returned".
332 (($backdate) ? " with backdate $backdate" : ''));
334 my $patron = $e->retrieve_actor_user($circ->usr);
335 my $max_count = $U->ou_ancestor_setting_value(
336 $circ->circ_lib, 'circ.max_patron_claim_return_count', $e);
338 # If the patron has too instances of many claims returned,
339 # require an override to continue. A configured max of
340 # 0 means all attempts require an override
341 if(defined $max_count and $patron->claims_returned_count >= $max_count) {
343 if($self->api_name =~ /override/) {
345 # see if we're allowed to override
346 return $e->die_event unless
347 $e->allowed('SET_CIRC_CLAIMS_RETURNED.override', $circ->circ_lib);
351 # exit early and return the max claims return event
353 return OpenILS::Event->new(
354 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT',
356 patron_count => $patron->claims_returned_count,
357 max_count => $max_count
363 $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib)
364 or return $e->die_event;
366 $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
367 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
370 # make it look like the circ stopped at the cliams returned time
371 $circ->stop_fines_time(clense_ISO8601($backdate));
372 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
376 $e->update_action_circulation($circ) or return $e->die_event;
378 # see if there is a configured post-claims-return copy status
379 if(my $stat = $U->ou_ancestor_setting_value($circ->circ_lib, 'circ.claim_return.copy_status')) {
380 $copy->status($stat);
381 $copy->edit_date('now');
382 $copy->editor($e->requestor->id);
383 $e->update_asset_copy($copy) or return $e->die_event;
391 __PACKAGE__->register_method(
392 method => "post_checkin_backdate_circ",
393 api_name => "open-ils.circ.post_checkin_backdate",
395 desc => q/Back-date an already checked in circulation/,
397 {desc => 'Authentication token', type => 'string'},
398 {desc => 'Circ ID', type => 'number'},
399 {desc => 'ISO8601 backdate', type => 'string'},
401 return => {desc => q/1 on success, failure event on error/}
405 __PACKAGE__->register_method(
406 method => "post_checkin_backdate_circ",
407 api_name => "open-ils.circ.post_checkin_backdate.batch",
410 desc => q/@see open-ils.circ.post_checkin_backdate. Batch mode/,
412 {desc => 'Authentication token', type => 'string'},
413 {desc => 'List of Circ ID', type => 'array'},
414 {desc => 'ISO8601 backdate', type => 'string'},
416 return => {desc => q/Set of: 1 on success, failure event on error/}
421 sub post_checkin_backdate_circ {
422 my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
423 my $e = new_editor(authtoken=>$auth);
424 return $e->die_event unless $e->checkauth;
425 if($self->api_name =~ /batch/) {
426 foreach my $c (@$circ_id) {
427 $conn->respond(post_checkin_backdate_circ_impl($e, $c, $backdate));
430 $conn->respond_complete(post_checkin_backdate_circ_impl($e, $circ_id, $backdate));
438 sub post_checkin_backdate_circ_impl {
439 my($e, $circ_id, $backdate) = @_;
443 my $circ = $e->retrieve_action_circulation($circ_id)
444 or return $e->die_event;
446 # anyone with checkin perms can backdate (more restrictive?)
447 return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
449 # don't allow back-dating an open circulation
450 return OpenILS::Event->new('BAD_PARAMS') unless
451 $backdate and $circ->checkin_time;
453 # update the checkin and stop_fines times to reflect the new backdate
454 $circ->stop_fines_time(clense_ISO8601($backdate));
455 $circ->checkin_time(clense_ISO8601($backdate));
456 $e->update_action_circulation($circ) or return $e->die_event;
458 # now void the overdues "erased" by the back-dating
459 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
462 # If the circ was closed before and the balance owned !=0, re-open the transaction
463 $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
472 __PACKAGE__->register_method (
473 method => 'set_circ_due_date',
474 api_name => 'open-ils.circ.circulation.due_date.update',
476 Updates the due_date on the given circ
478 @param circid The id of the circ to update
479 @param date The timestamp of the new due date
483 sub set_circ_due_date {
484 my( $self, $conn, $auth, $circ_id, $date ) = @_;
486 my $e = new_editor(xact=>1, authtoken=>$auth);
487 return $e->die_event unless $e->checkauth;
488 my $circ = $e->retrieve_action_circulation($circ_id)
489 or return $e->die_event;
491 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
492 $date = clense_ISO8601($date);
493 $circ->due_date($date);
494 $e->update_action_circulation($circ) or return $e->die_event;
501 __PACKAGE__->register_method(
502 method => "create_in_house_use",
503 api_name => 'open-ils.circ.in_house_use.create',
505 Creates an in-house use action.
506 @param $authtoken The login session key
507 @param params A hash of params including
508 'location' The org unit id where the in-house use occurs
509 'copyid' The copy in question
510 'count' The number of in-house uses to apply to this copy
511 @return An array of id's representing the id's of the newly created
512 in-house use objects or an event on an error
515 __PACKAGE__->register_method(
516 method => "create_in_house_use",
517 api_name => 'open-ils.circ.non_cat_in_house_use.create',
521 sub create_in_house_use {
522 my( $self, $client, $auth, $params ) = @_;
525 my $org = $params->{location};
526 my $copyid = $params->{copyid};
527 my $count = $params->{count} || 1;
528 my $nc_type = $params->{non_cat_type};
529 my $use_time = $params->{use_time} || 'now';
531 my $e = new_editor(xact=>1,authtoken=>$auth);
532 return $e->event unless $e->checkauth;
533 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
535 my $non_cat = 1 if $self->api_name =~ /non_cat/;
539 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
541 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
547 if( $use_time ne 'now' ) {
548 $use_time = clense_ISO8601($use_time);
549 $logger->debug("in_house_use setting use time to $use_time");
560 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
561 $ihu->item_type($nc_type);
562 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
563 $cmeth = "create_action_non_cat_in_house_use";
566 $ihu = Fieldmapper::action::in_house_use->new;
568 $method = 'open-ils.storage.direct.action.in_house_use.create';
569 $cmeth = "create_action_in_house_use";
572 $ihu->staff($e->requestor->id);
573 $ihu->org_unit($org);
574 $ihu->use_time($use_time);
576 $ihu = $e->$cmeth($ihu) or return $e->event;
577 push( @ids, $ihu->id );
588 __PACKAGE__->register_method(
589 method => "view_circs",
590 api_name => "open-ils.circ.copy_checkout_history.retrieve",
592 Retrieves the last X circs for a given copy
593 @param authtoken The login session key
594 @param copyid The copy to check
595 @param count How far to go back in the item history
596 @return An array of circ ids
599 # ----------------------------------------------------------------------
600 # Returns $count most recent circs. If count exceeds the configured
601 # max, use the configured max instead
602 # ----------------------------------------------------------------------
604 my( $self, $client, $authtoken, $copyid, $count ) = @_;
606 my $e = new_editor(authtoken => $authtoken);
607 return $e->event unless $e->checkauth;
609 my $copy = $e->retrieve_asset_copy([
612 flesh_fields => {acp => ['call_number']}
614 ]) or return $e->event;
616 return $e->event unless $e->allowed(
617 'VIEW_COPY_CHECKOUT_HISTORY',
618 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
619 $copy->circ_lib : $copy->call_number->owning_lib);
621 my $max_history = $U->ou_ancestor_setting_value(
622 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
624 if(defined $max_history) {
625 $count = $max_history unless defined $count and $count < $max_history;
627 $count = 4 unless defined $count;
630 return $e->search_action_circulation([
631 {target_copy => $copyid},
632 {limit => $count, order_by => { circ => "xact_start DESC" }}
637 __PACKAGE__->register_method(
638 method => "circ_count",
639 api_name => "open-ils.circ.circulation.count",
641 Returns the number of times the item has circulated
642 @param copyid The copy to check
646 my( $self, $client, $copyid, $range ) = @_;
647 my $e = OpenILS::Utils::Editor->new;
648 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
653 __PACKAGE__->register_method(
654 method => 'fetch_notes',
655 api_name => 'open-ils.circ.copy_note.retrieve.all',
657 Returns an array of copy note objects.
658 @param args A named hash of parameters including:
659 authtoken : Required if viewing non-public notes
660 itemid : The id of the item whose notes we want to retrieve
661 pub : True if all the caller wants are public notes
662 @return An array of note objects
665 __PACKAGE__->register_method(
666 method => 'fetch_notes',
667 api_name => 'open-ils.circ.call_number_note.retrieve.all',
668 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
670 __PACKAGE__->register_method(
671 method => 'fetch_notes',
672 api_name => 'open-ils.circ.title_note.retrieve.all',
673 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
676 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
678 my( $self, $connection, $args ) = @_;
680 my $id = $$args{itemid};
681 my $authtoken = $$args{authtoken};
684 if( $self->api_name =~ /copy/ ) {
686 return $U->cstorereq(
687 'open-ils.cstore.direct.asset.copy_note.search.atomic',
688 { owning_copy => $id, pub => 't' } );
690 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
692 return $U->cstorereq(
693 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
696 } elsif( $self->api_name =~ /call_number/ ) {
698 return $U->cstorereq(
699 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
700 { call_number => $id, pub => 't' } );
702 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
704 return $U->cstorereq(
705 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
708 } elsif( $self->api_name =~ /title/ ) {
710 return $U->cstorereq(
711 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
712 { record => $id, pub => 't' } );
714 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
716 return $U->cstorereq(
717 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
724 __PACKAGE__->register_method(
725 method => 'has_notes',
726 api_name => 'open-ils.circ.copy.has_notes');
727 __PACKAGE__->register_method(
728 method => 'has_notes',
729 api_name => 'open-ils.circ.call_number.has_notes');
730 __PACKAGE__->register_method(
731 method => 'has_notes',
732 api_name => 'open-ils.circ.title.has_notes');
736 my( $self, $conn, $authtoken, $id ) = @_;
737 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
738 return $editor->event unless $editor->checkauth;
740 my $n = $editor->search_asset_copy_note(
741 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
743 $n = $editor->search_asset_call_number_note(
744 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
746 $n = $editor->search_biblio_record_note(
747 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
754 __PACKAGE__->register_method(
755 method => 'create_copy_note',
756 api_name => 'open-ils.circ.copy_note.create',
758 Creates a new copy note
759 @param authtoken The login session key
760 @param note The note object to create
761 @return The id of the new note object
764 sub create_copy_note {
765 my( $self, $connection, $authtoken, $note ) = @_;
767 my $e = new_editor(xact=>1, authtoken=>$authtoken);
768 return $e->event unless $e->checkauth;
769 my $copy = $e->retrieve_asset_copy(
773 flesh_fields => { 'acp' => ['call_number'] }
778 return $e->event unless
779 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
781 $note->create_date('now');
782 $note->creator($e->requestor->id);
783 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
786 $e->create_asset_copy_note($note) or return $e->event;
792 __PACKAGE__->register_method(
793 method => 'delete_copy_note',
794 api_name => 'open-ils.circ.copy_note.delete',
796 Deletes an existing copy note
797 @param authtoken The login session key
798 @param noteid The id of the note to delete
799 @return 1 on success - Event otherwise.
801 sub delete_copy_note {
802 my( $self, $conn, $authtoken, $noteid ) = @_;
804 my $e = new_editor(xact=>1, authtoken=>$authtoken);
805 return $e->die_event unless $e->checkauth;
807 my $note = $e->retrieve_asset_copy_note([
811 'acpn' => [ 'owning_copy' ],
812 'acp' => [ 'call_number' ],
815 ]) or return $e->die_event;
817 if( $note->creator ne $e->requestor->id ) {
818 return $e->die_event unless
819 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
822 $e->delete_asset_copy_note($note) or return $e->die_event;
828 __PACKAGE__->register_method(
829 method => 'age_hold_rules',
830 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
834 my( $self, $conn ) = @_;
835 return new_editor()->retrieve_all_config_rules_age_hold_protect();
840 __PACKAGE__->register_method(
841 method => 'copy_details_barcode',
843 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
844 sub copy_details_barcode {
845 my( $self, $conn, $auth, $barcode ) = @_;
846 my $e = new_editor();
847 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
848 return $e->event unless $cid;
849 return copy_details( $self, $conn, $auth, $cid );
853 __PACKAGE__->register_method(
854 method => 'copy_details',
855 api_name => 'open-ils.circ.copy_details.retrieve');
858 my( $self, $conn, $auth, $copy_id ) = @_;
859 my $e = new_editor(authtoken=>$auth);
860 return $e->event unless $e->checkauth;
862 my $flesh = { flesh => 1 };
864 my $copy = $e->retrieve_asset_copy(
870 acp => ['call_number'],
874 ]) or return $e->event;
877 # De-flesh the copy for backwards compatibility
879 my $vol = $copy->call_number;
881 $copy->call_number($vol->id);
882 my $record = $vol->record;
884 $vol->record($record->id);
885 $mvr = $U->record_to_mvr($record);
890 my $hold = $e->search_action_hold_request(
892 current_copy => $copy_id,
893 capture_time => { "!=" => undef },
894 fulfillment_time => undef,
895 cancel_time => undef,
899 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
901 my $transit = $e->search_action_transit_copy(
902 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
904 # find the latest circ, open or closed
905 my $circ = $e->search_action_circulation(
907 { target_copy => $copy_id },
913 'checkin_workstation',
916 'recurring_fine_rule'
919 order_by => { circ => 'xact_start desc' },
939 __PACKAGE__->register_method(
940 method => 'mark_item',
941 api_name => 'open-ils.circ.mark_item_damaged',
943 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
944 @param authtoken The login session key
945 @param copy_id The ID of the copy to mark as damaged
946 @return 1 on success - Event otherwise.
949 __PACKAGE__->register_method(
950 method => 'mark_item',
951 api_name => 'open-ils.circ.mark_item_missing',
953 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
954 @param authtoken The login session key
955 @param copy_id The ID of the copy to mark as missing
956 @return 1 on success - Event otherwise.
959 __PACKAGE__->register_method(
960 method => 'mark_item',
961 api_name => 'open-ils.circ.mark_item_bindery',
963 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
964 @param authtoken The login session key
965 @param copy_id The ID of the copy to mark as bindery
966 @return 1 on success - Event otherwise.
969 __PACKAGE__->register_method(
970 method => 'mark_item',
971 api_name => 'open-ils.circ.mark_item_on_order',
973 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
974 @param authtoken The login session key
975 @param copy_id The ID of the copy to mark as on order
976 @return 1 on success - Event otherwise.
979 __PACKAGE__->register_method(
980 method => 'mark_item',
981 api_name => 'open-ils.circ.mark_item_ill',
983 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
984 @param authtoken The login session key
985 @param copy_id The ID of the copy to mark as inter-library loan
986 @return 1 on success - Event otherwise.
989 __PACKAGE__->register_method(
990 method => 'mark_item',
991 api_name => 'open-ils.circ.mark_item_cataloging',
993 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
994 @param authtoken The login session key
995 @param copy_id The ID of the copy to mark as cataloging
996 @return 1 on success - Event otherwise.
999 __PACKAGE__->register_method(
1000 method => 'mark_item',
1001 api_name => 'open-ils.circ.mark_item_reserves',
1003 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
1004 @param authtoken The login session key
1005 @param copy_id The ID of the copy to mark as reserves
1006 @return 1 on success - Event otherwise.
1009 __PACKAGE__->register_method(
1010 method => 'mark_item',
1011 api_name => 'open-ils.circ.mark_item_discard',
1013 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
1014 @param authtoken The login session key
1015 @param copy_id The ID of the copy to mark as discard
1016 @return 1 on success - Event otherwise.
1021 my( $self, $conn, $auth, $copy_id, $args ) = @_;
1022 my $e = new_editor(authtoken=>$auth, xact =>1);
1023 return $e->die_event unless $e->checkauth;
1026 my $copy = $e->retrieve_asset_copy([
1028 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1029 or return $e->die_event;
1032 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1033 $copy->circ_lib : $copy->call_number->owning_lib;
1035 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
1038 my $perm = 'MARK_ITEM_MISSING';
1039 my $stat = OILS_COPY_STATUS_MISSING;
1041 if( $self->api_name =~ /damaged/ ) {
1042 $perm = 'MARK_ITEM_DAMAGED';
1043 $stat = OILS_COPY_STATUS_DAMAGED;
1044 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1045 return $evt if $evt;
1047 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1048 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1050 } elsif ( $self->api_name =~ /bindery/ ) {
1051 $perm = 'MARK_ITEM_BINDERY';
1052 $stat = OILS_COPY_STATUS_BINDERY;
1053 } elsif ( $self->api_name =~ /on_order/ ) {
1054 $perm = 'MARK_ITEM_ON_ORDER';
1055 $stat = OILS_COPY_STATUS_ON_ORDER;
1056 } elsif ( $self->api_name =~ /ill/ ) {
1057 $perm = 'MARK_ITEM_ILL';
1058 $stat = OILS_COPY_STATUS_ILL;
1059 } elsif ( $self->api_name =~ /cataloging/ ) {
1060 $perm = 'MARK_ITEM_CATALOGING';
1061 $stat = OILS_COPY_STATUS_CATALOGING;
1062 } elsif ( $self->api_name =~ /reserves/ ) {
1063 $perm = 'MARK_ITEM_RESERVES';
1064 $stat = OILS_COPY_STATUS_RESERVES;
1065 } elsif ( $self->api_name =~ /discard/ ) {
1066 $perm = 'MARK_ITEM_DISCARD';
1067 $stat = OILS_COPY_STATUS_DISCARD;
1071 $copy->status($stat);
1072 $copy->edit_date('now');
1073 $copy->editor($e->requestor->id);
1075 $e->update_asset_copy($copy) or return $e->die_event;
1077 my $holds = $e->search_action_hold_request(
1079 current_copy => $copy->id,
1080 fulfillment_time => undef,
1081 cancel_time => undef,
1087 $logger->debug("resetting holds that target the marked copy");
1088 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1093 sub handle_mark_damaged {
1094 my($e, $copy, $owning_lib, $args) = @_;
1096 my $apply = $args->{apply_fines} || '';
1097 return undef if $apply eq 'noapply';
1099 my $new_amount = $args->{override_amount};
1100 my $new_btype = $args->{override_btype};
1101 my $new_note = $args->{override_note};
1103 # grab the last circulation
1104 my $circ = $e->search_action_circulation([
1105 { target_copy => $copy->id},
1107 order_by => {circ => "xact_start DESC"},
1109 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1113 return undef unless $circ;
1115 my $charge_price = $U->ou_ancestor_setting_value(
1116 $owning_lib, 'circ.charge_on_damaged', $e);
1118 my $proc_fee = $U->ou_ancestor_setting_value(
1119 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1121 my $void_overdue = $U->ou_ancestor_setting_value(
1122 $owning_lib, 'circ.damaged.void_ovedue', $e) || 0;
1124 return undef unless $charge_price or $proc_fee;
1126 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1127 my $total = $copy_price + $proc_fee;
1131 if($new_amount and $new_btype) {
1133 # Allow staff to override the amount to charge for a damaged item
1134 # Consider the case where the item is only partially damaged
1135 # This value is meant to take the place of the item price and
1136 # optional processing fee.
1138 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1139 $e, $new_amount, $new_btype, 'Damaged Item Override', $circ->id, $new_note);
1140 return $evt if $evt;
1144 if($charge_price and $copy_price) {
1145 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1146 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1147 return $evt if $evt;
1151 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1152 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1153 return $evt if $evt;
1157 # the assumption is that you would not void the overdues unless you
1158 # were also charging for the item and/or applying a processing fee
1160 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
1161 return $evt if $evt;
1164 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1165 return $evt if $evt;
1167 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1168 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1173 return OpenILS::Event->new('DAMAGE_CHARGE',
1187 # ----------------------------------------------------------------------
1188 __PACKAGE__->register_method(
1189 method => 'magic_fetch',
1190 api_name => 'open-ils.agent.fetch'
1193 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1196 my( $self, $conn, $auth, $args ) = @_;
1197 my $e = new_editor( authtoken => $auth );
1198 return $e->event unless $e->checkauth;
1200 my $hint = $$args{hint};
1201 my $id = $$args{id};
1203 # Is the call allowed to fetch this type of object?
1204 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1206 # Find the class the implements the given hint
1207 my ($class) = grep {
1208 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1210 $class =~ s/Fieldmapper:://og;
1211 $class =~ s/::/_/og;
1212 my $method = "retrieve_$class";
1214 my $obj = $e->$method($id) or return $e->event;
1217 # ----------------------------------------------------------------------
1220 __PACKAGE__->register_method(
1221 method => "fleshed_circ_retrieve",
1223 api_name => "open-ils.circ.fleshed.retrieve",);
1225 sub fleshed_circ_retrieve {
1226 my( $self, $client, $id ) = @_;
1227 my $e = new_editor();
1228 my $circ = $e->retrieve_action_circulation(
1234 circ => [ qw/ target_copy / ],
1235 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1236 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1237 acn => [ qw/ record / ],
1241 ) or return $e->event;
1243 my $copy = $circ->target_copy;
1244 my $vol = $copy->call_number;
1245 my $rec = $circ->target_copy->call_number->record;
1247 $vol->record($rec->id);
1248 $copy->call_number($vol->id);
1249 $circ->target_copy($copy->id);
1253 if( $rec->id == OILS_PRECAT_RECORD ) {
1257 $mvr = $U->record_to_mvr($rec);
1258 $rec->marc(''); # drop the bulky marc data
1272 __PACKAGE__->register_method(
1273 method => "test_batch_circ_events",
1274 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1277 # method for testing the behavior of a given event definition
1278 sub test_batch_circ_events {
1279 my($self, $conn, $auth, $event_def, $barcode) = @_;
1281 my $e = new_editor(authtoken => $auth);
1282 return $e->event unless $e->checkauth;
1283 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1285 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1286 or return $e->event;
1288 my $circ = $e->search_action_circulation(
1289 {target_copy => $copy->id, checkin_time => undef})->[0]
1290 or return $e->event;
1292 return undef unless $circ;
1294 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1298 __PACKAGE__->register_method(
1299 method => "fire_circ_events",
1300 api_name => "open-ils.circ.fire_circ_trigger_events",
1302 General event def runner for circ objects. If no event def ID
1303 is provided, the hook will be used to find the best event_def
1304 match based on the context org unit
1308 __PACKAGE__->register_method(
1309 method => "fire_circ_events",
1310 api_name => "open-ils.circ.fire_hold_trigger_events",
1312 General event def runner for hold objects. If no event def ID
1313 is provided, the hook will be used to find the best event_def
1314 match based on the context org unit
1318 __PACKAGE__->register_method(
1319 method => "fire_circ_events",
1320 api_name => "open-ils.circ.fire_user_trigger_events",
1322 General event def runner for user objects. If no event def ID
1323 is provided, the hook will be used to find the best event_def
1324 match based on the context org unit
1329 sub fire_circ_events {
1330 my($self, $conn, $auth, $org_id, $event_def, $hook, $granularity, $target_ids, $user_data) = @_;
1332 my $e = new_editor(authtoken => $auth);
1333 return $e->event unless $e->checkauth;
1337 if($self->api_name =~ /hold/) {
1338 return $e->event unless $e->allowed('VIEW_HOLD', $org_id);
1339 $targets = $e->batch_retrieve_action_hold_request($target_ids);
1340 } elsif($self->api_name =~ /user/) {
1341 return $e->event unless $e->allowed('VIEW_USER', $org_id);
1342 $targets = $e->batch_retrieve_actor_user($target_ids);
1344 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $org_id);
1345 $targets = $e->batch_retrieve_action_circulation($target_ids);
1348 return undef unless @$targets;
1349 return $U->fire_object_event($event_def, $hook, $targets, $org_id, $granularity, $user_data);
1352 __PACKAGE__->register_method(
1353 method => "user_payments_list",
1354 api_name => "open-ils.circ.user_payments.filtered.batch",
1357 desc => q/Returns a fleshed, date-limited set of all payments a user
1358 has made. By default, ordered by payment date. Optionally
1359 ordered by other columns in the top-level "mp" object/,
1361 {desc => 'Authentication token', type => 'string'},
1362 {desc => 'User ID', type => 'number'},
1363 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1365 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1366 and the related fully-realized payment object (e.g money.cash_payment)/}
1370 sub user_payments_list {
1371 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1373 my $e = new_editor(authtoken => $auth);
1374 return $e->event unless $e->checkauth;
1376 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1377 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1379 $order_by ||= ['payment_ts'];
1381 # all payments by user, between start_date and end_date
1382 my $payments = $e->json_query({
1383 select => {mp => ['id']},
1387 fkey => 'xact', field => 'id'}
1391 '+mbt' => {usr => $user_id},
1392 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1394 order_by => {mp => $order_by}
1397 for my $payment_id (@$payments) {
1398 my $payment = $e->retrieve_money_payment([
1406 'credit_card_payment',
1421 $conn->respond($payment);
1428 __PACKAGE__->register_method(
1429 method => "retrieve_circ_chain",
1430 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1433 desc => q/Given a circulation, this returns all circulation objects
1434 that are part of the same chain of renewals./,
1436 {desc => 'Authentication token', type => 'string'},
1437 {desc => 'Circ ID', type => 'number'},
1439 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1443 __PACKAGE__->register_method(
1444 method => "retrieve_circ_chain",
1445 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ.summary",
1447 desc => q/Given a circulation, this returns all circulation objects
1448 that are part of the same chain of renewals./,
1450 {desc => 'Authentication token', type => 'string'},
1451 {desc => 'Circ ID', type => 'number'},
1453 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1457 sub retrieve_circ_chain {
1458 my($self, $conn, $auth, $circ_id) = @_;
1460 my $e = new_editor(authtoken => $auth);
1461 return $e->event unless $e->checkauth;
1462 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1464 if($self->api_name =~ /summary/) {
1465 my $sum = $e->json_query({from => ['action.summarize_circ_chain', $circ_id]})->[0];
1466 return undef unless $sum;
1467 my $obj = Fieldmapper::action::circ_chain_summary->new;
1468 $obj->$_($sum->{$_}) for keys %$sum;
1473 my $chain = $e->json_query({from => ['action.circ_chain', $circ_id]});
1475 for my $circ_info (@$chain) {
1476 my $circ = Fieldmapper::action::circulation->new;
1477 $circ->$_($circ_info->{$_}) for keys %$circ_info;
1478 $conn->respond($circ);
1487 # {"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}}