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 Sets the circ for the given item as claims returned
275 If a backdate is provided, overdue fines will be voided
278 @param args : barcode, backdate
282 sub set_circ_claims_returned {
283 my( $self, $conn, $auth, $args ) = @_;
285 my $e = new_editor(authtoken=>$auth, xact=>1);
286 return $e->die_event unless $e->checkauth;
288 my $barcode = $$args{barcode};
289 my $backdate = $$args{backdate};
291 $logger->info("marking circ for item $barcode as claims returned".
292 (($backdate) ? " with backdate $backdate" : ''));
294 my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0]
295 or return $e->die_event;
297 my $circ = $e->search_action_circulation(
298 {checkin_time => undef, target_copy => $copy->id})->[0]
299 or return $e->die_event;
301 $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib)
302 or return $e->die_event;
304 $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
305 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
308 # make it look like the circ stopped at the cliams returned time
309 $circ->stop_fines_time(clense_ISO8601($backdate));
310 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
314 $e->update_action_circulation($circ) or return $e->die_event;
323 __PACKAGE__->register_method (
324 method => 'set_circ_due_date',
325 api_name => 'open-ils.circ.circulation.due_date.update',
327 Updates the due_date on the given circ
329 @param circid The id of the circ to update
330 @param date The timestamp of the new due date
334 sub set_circ_due_date {
335 my( $self, $conn, $auth, $circ_id, $date ) = @_;
337 my $e = new_editor(xact=>1, authtoken=>$auth);
338 return $e->die_event unless $e->checkauth;
339 my $circ = $e->retrieve_action_circulation($circ_id)
340 or return $e->die_event;
342 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
343 $date = clense_ISO8601($date);
344 $circ->due_date($date);
345 $e->update_action_circulation($circ) or return $e->die_event;
352 __PACKAGE__->register_method(
353 method => "create_in_house_use",
354 api_name => 'open-ils.circ.in_house_use.create',
356 Creates an in-house use action.
357 @param $authtoken The login session key
358 @param params A hash of params including
359 'location' The org unit id where the in-house use occurs
360 'copyid' The copy in question
361 'count' The number of in-house uses to apply to this copy
362 @return An array of id's representing the id's of the newly created
363 in-house use objects or an event on an error
366 __PACKAGE__->register_method(
367 method => "create_in_house_use",
368 api_name => 'open-ils.circ.non_cat_in_house_use.create',
372 sub create_in_house_use {
373 my( $self, $client, $auth, $params ) = @_;
376 my $org = $params->{location};
377 my $copyid = $params->{copyid};
378 my $count = $params->{count} || 1;
379 my $nc_type = $params->{non_cat_type};
380 my $use_time = $params->{use_time} || 'now';
382 my $e = new_editor(xact=>1,authtoken=>$auth);
383 return $e->event unless $e->checkauth;
384 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
386 my $non_cat = 1 if $self->api_name =~ /non_cat/;
390 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
392 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
398 if( $use_time ne 'now' ) {
399 $use_time = clense_ISO8601($use_time);
400 $logger->debug("in_house_use setting use time to $use_time");
411 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
412 $ihu->item_type($nc_type);
413 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
414 $cmeth = "create_action_non_cat_in_house_use";
417 $ihu = Fieldmapper::action::in_house_use->new;
419 $method = 'open-ils.storage.direct.action.in_house_use.create';
420 $cmeth = "create_action_in_house_use";
423 $ihu->staff($e->requestor->id);
424 $ihu->org_unit($org);
425 $ihu->use_time($use_time);
427 $ihu = $e->$cmeth($ihu) or return $e->event;
428 push( @ids, $ihu->id );
439 __PACKAGE__->register_method(
440 method => "view_circs",
441 api_name => "open-ils.circ.copy_checkout_history.retrieve",
443 Retrieves the last X circs for a given copy
444 @param authtoken The login session key
445 @param copyid The copy to check
446 @param count How far to go back in the item history
447 @return An array of circ ids
450 # ----------------------------------------------------------------------
451 # Returns $count most recent circs. If count exceeds the configured
452 # max, use the configured max instead
453 # ----------------------------------------------------------------------
455 my( $self, $client, $authtoken, $copyid, $count ) = @_;
457 my $e = new_editor(authtoken => $authtoken);
458 return $e->event unless $e->checkauth;
460 my $copy = $e->retrieve_asset_copy([
463 flesh_fields => {acp => ['call_number']}
465 ]) or return $e->event;
467 return $e->event unless $e->allowed(
468 'VIEW_COPY_CHECKOUT_HISTORY',
469 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
470 $copy->circ_lib : $copy->call_number->owning_lib);
472 my $max_history = $U->ou_ancestor_setting_value(
473 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
475 if(defined $max_history) {
476 $count = $max_history unless defined $count and $count < $max_history;
478 $count = 4 unless defined $count;
481 return $e->search_action_circulation([
482 {target_copy => $copyid},
483 {limit => $count, order_by => { circ => "xact_start DESC" }}
488 __PACKAGE__->register_method(
489 method => "circ_count",
490 api_name => "open-ils.circ.circulation.count",
492 Returns the number of times the item has circulated
493 @param copyid The copy to check
497 my( $self, $client, $copyid, $range ) = @_;
498 my $e = OpenILS::Utils::Editor->new;
499 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
504 __PACKAGE__->register_method(
505 method => 'fetch_notes',
506 api_name => 'open-ils.circ.copy_note.retrieve.all',
508 Returns an array of copy note objects.
509 @param args A named hash of parameters including:
510 authtoken : Required if viewing non-public notes
511 itemid : The id of the item whose notes we want to retrieve
512 pub : True if all the caller wants are public notes
513 @return An array of note objects
516 __PACKAGE__->register_method(
517 method => 'fetch_notes',
518 api_name => 'open-ils.circ.call_number_note.retrieve.all',
519 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
521 __PACKAGE__->register_method(
522 method => 'fetch_notes',
523 api_name => 'open-ils.circ.title_note.retrieve.all',
524 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
527 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
529 my( $self, $connection, $args ) = @_;
531 my $id = $$args{itemid};
532 my $authtoken = $$args{authtoken};
535 if( $self->api_name =~ /copy/ ) {
537 return $U->cstorereq(
538 'open-ils.cstore.direct.asset.copy_note.search.atomic',
539 { owning_copy => $id, pub => 't' } );
541 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
543 return $U->cstorereq(
544 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
547 } elsif( $self->api_name =~ /call_number/ ) {
549 return $U->cstorereq(
550 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
551 { call_number => $id, pub => 't' } );
553 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
555 return $U->cstorereq(
556 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
559 } elsif( $self->api_name =~ /title/ ) {
561 return $U->cstorereq(
562 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
563 { record => $id, pub => 't' } );
565 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
567 return $U->cstorereq(
568 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
575 __PACKAGE__->register_method(
576 method => 'has_notes',
577 api_name => 'open-ils.circ.copy.has_notes');
578 __PACKAGE__->register_method(
579 method => 'has_notes',
580 api_name => 'open-ils.circ.call_number.has_notes');
581 __PACKAGE__->register_method(
582 method => 'has_notes',
583 api_name => 'open-ils.circ.title.has_notes');
587 my( $self, $conn, $authtoken, $id ) = @_;
588 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
589 return $editor->event unless $editor->checkauth;
591 my $n = $editor->search_asset_copy_note(
592 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
594 $n = $editor->search_asset_call_number_note(
595 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
597 $n = $editor->search_biblio_record_note(
598 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
605 __PACKAGE__->register_method(
606 method => 'create_copy_note',
607 api_name => 'open-ils.circ.copy_note.create',
609 Creates a new copy note
610 @param authtoken The login session key
611 @param note The note object to create
612 @return The id of the new note object
615 sub create_copy_note {
616 my( $self, $connection, $authtoken, $note ) = @_;
618 my $e = new_editor(xact=>1, authtoken=>$authtoken);
619 return $e->event unless $e->checkauth;
620 my $copy = $e->retrieve_asset_copy(
624 flesh_fields => { 'acp' => ['call_number'] }
629 return $e->event unless
630 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
632 $note->create_date('now');
633 $note->creator($e->requestor->id);
634 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
637 $e->create_asset_copy_note($note) or return $e->event;
643 __PACKAGE__->register_method(
644 method => 'delete_copy_note',
645 api_name => 'open-ils.circ.copy_note.delete',
647 Deletes an existing copy note
648 @param authtoken The login session key
649 @param noteid The id of the note to delete
650 @return 1 on success - Event otherwise.
652 sub delete_copy_note {
653 my( $self, $conn, $authtoken, $noteid ) = @_;
655 my $e = new_editor(xact=>1, authtoken=>$authtoken);
656 return $e->die_event unless $e->checkauth;
658 my $note = $e->retrieve_asset_copy_note([
662 'acpn' => [ 'owning_copy' ],
663 'acp' => [ 'call_number' ],
666 ]) or return $e->die_event;
668 if( $note->creator ne $e->requestor->id ) {
669 return $e->die_event unless
670 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
673 $e->delete_asset_copy_note($note) or return $e->die_event;
679 __PACKAGE__->register_method(
680 method => 'age_hold_rules',
681 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
685 my( $self, $conn ) = @_;
686 return new_editor()->retrieve_all_config_rules_age_hold_protect();
691 __PACKAGE__->register_method(
692 method => 'copy_details_barcode',
694 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
695 sub copy_details_barcode {
696 my( $self, $conn, $auth, $barcode ) = @_;
697 my $e = new_editor();
698 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
699 return $e->event unless $cid;
700 return copy_details( $self, $conn, $auth, $cid );
704 __PACKAGE__->register_method(
705 method => 'copy_details',
706 api_name => 'open-ils.circ.copy_details.retrieve');
709 my( $self, $conn, $auth, $copy_id ) = @_;
710 my $e = new_editor(authtoken=>$auth);
711 return $e->event unless $e->checkauth;
713 my $flesh = { flesh => 1 };
715 my $copy = $e->retrieve_asset_copy(
721 acp => ['call_number'],
725 ]) or return $e->event;
728 # De-flesh the copy for backwards compatibility
730 my $vol = $copy->call_number;
732 $copy->call_number($vol->id);
733 my $record = $vol->record;
735 $vol->record($record->id);
736 $mvr = $U->record_to_mvr($record);
741 my $hold = $e->search_action_hold_request(
743 current_copy => $copy_id,
744 capture_time => { "!=" => undef },
745 fulfillment_time => undef,
746 cancel_time => undef,
750 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
752 my $transit = $e->search_action_transit_copy(
753 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
755 # find the latest circ, open or closed
756 my $circ = $e->search_action_circulation(
758 { target_copy => $copy_id },
759 { order_by => { circ => 'xact_start desc' }, limit => 1 }
777 __PACKAGE__->register_method(
778 method => 'mark_item',
779 api_name => 'open-ils.circ.mark_item_damaged',
781 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
782 @param authtoken The login session key
783 @param copy_id The ID of the copy to mark as damaged
784 @return 1 on success - Event otherwise.
787 __PACKAGE__->register_method(
788 method => 'mark_item',
789 api_name => 'open-ils.circ.mark_item_missing',
791 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
792 @param authtoken The login session key
793 @param copy_id The ID of the copy to mark as missing
794 @return 1 on success - Event otherwise.
797 __PACKAGE__->register_method(
798 method => 'mark_item',
799 api_name => 'open-ils.circ.mark_item_bindery',
801 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
802 @param authtoken The login session key
803 @param copy_id The ID of the copy to mark as bindery
804 @return 1 on success - Event otherwise.
807 __PACKAGE__->register_method(
808 method => 'mark_item',
809 api_name => 'open-ils.circ.mark_item_on_order',
811 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
812 @param authtoken The login session key
813 @param copy_id The ID of the copy to mark as on order
814 @return 1 on success - Event otherwise.
817 __PACKAGE__->register_method(
818 method => 'mark_item',
819 api_name => 'open-ils.circ.mark_item_ill',
821 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
822 @param authtoken The login session key
823 @param copy_id The ID of the copy to mark as inter-library loan
824 @return 1 on success - Event otherwise.
827 __PACKAGE__->register_method(
828 method => 'mark_item',
829 api_name => 'open-ils.circ.mark_item_cataloging',
831 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
832 @param authtoken The login session key
833 @param copy_id The ID of the copy to mark as cataloging
834 @return 1 on success - Event otherwise.
837 __PACKAGE__->register_method(
838 method => 'mark_item',
839 api_name => 'open-ils.circ.mark_item_reserves',
841 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
842 @param authtoken The login session key
843 @param copy_id The ID of the copy to mark as reserves
844 @return 1 on success - Event otherwise.
847 __PACKAGE__->register_method(
848 method => 'mark_item',
849 api_name => 'open-ils.circ.mark_item_discard',
851 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
852 @param authtoken The login session key
853 @param copy_id The ID of the copy to mark as discard
854 @return 1 on success - Event otherwise.
859 my( $self, $conn, $auth, $copy_id, $args ) = @_;
860 my $e = new_editor(authtoken=>$auth, xact =>1);
861 return $e->die_event unless $e->checkauth;
864 my $copy = $e->retrieve_asset_copy([
866 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
867 or return $e->die_event;
870 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
871 $copy->circ_lib : $copy->call_number->owning_lib;
873 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
876 my $perm = 'MARK_ITEM_MISSING';
877 my $stat = OILS_COPY_STATUS_MISSING;
879 if( $self->api_name =~ /damaged/ ) {
880 $perm = 'MARK_ITEM_DAMAGED';
881 $stat = OILS_COPY_STATUS_DAMAGED;
882 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
885 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
886 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
888 } elsif ( $self->api_name =~ /bindery/ ) {
889 $perm = 'MARK_ITEM_BINDERY';
890 $stat = OILS_COPY_STATUS_BINDERY;
891 } elsif ( $self->api_name =~ /on_order/ ) {
892 $perm = 'MARK_ITEM_ON_ORDER';
893 $stat = OILS_COPY_STATUS_ON_ORDER;
894 } elsif ( $self->api_name =~ /ill/ ) {
895 $perm = 'MARK_ITEM_ILL';
896 $stat = OILS_COPY_STATUS_ILL;
897 } elsif ( $self->api_name =~ /cataloging/ ) {
898 $perm = 'MARK_ITEM_CATALOGING';
899 $stat = OILS_COPY_STATUS_CATALOGING;
900 } elsif ( $self->api_name =~ /reserves/ ) {
901 $perm = 'MARK_ITEM_RESERVES';
902 $stat = OILS_COPY_STATUS_RESERVES;
903 } elsif ( $self->api_name =~ /discard/ ) {
904 $perm = 'MARK_ITEM_DISCARD';
905 $stat = OILS_COPY_STATUS_DISCARD;
909 $copy->status($stat);
910 $copy->edit_date('now');
911 $copy->editor($e->requestor->id);
913 $e->update_asset_copy($copy) or return $e->die_event;
915 my $holds = $e->search_action_hold_request(
917 current_copy => $copy->id,
918 fulfillment_time => undef,
919 cancel_time => undef,
925 $logger->debug("resetting holds that target the marked copy");
926 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
931 sub handle_mark_damaged {
932 my($e, $copy, $owning_lib, $args) = @_;
934 my $apply = $args->{apply_fines} || '';
935 return undef if $apply eq 'noapply';
937 # grab the last circulation
938 my $circ = $e->search_action_circulation([
939 { target_copy => $copy->id},
941 order_by => {circ => "xact_start DESC"},
943 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
947 return undef unless $circ;
949 my $charge_price = $U->ou_ancestor_setting_value(
950 $owning_lib, 'circ.charge_on_damaged', $e);
952 my $proc_fee = $U->ou_ancestor_setting_value(
953 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
955 return undef unless $charge_price or $proc_fee;
957 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
958 my $total = $copy_price + $proc_fee;
962 if($charge_price and $copy_price) {
963 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
964 $e, $copy_price, 7, 'Damaged Item', $circ->id);
969 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
970 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
974 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
977 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
978 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
983 return OpenILS::Event->new('DAMAGE_CHARGE',
997 # ----------------------------------------------------------------------
998 __PACKAGE__->register_method(
999 method => 'magic_fetch',
1000 api_name => 'open-ils.agent.fetch'
1003 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1006 my( $self, $conn, $auth, $args ) = @_;
1007 my $e = new_editor( authtoken => $auth );
1008 return $e->event unless $e->checkauth;
1010 my $hint = $$args{hint};
1011 my $id = $$args{id};
1013 # Is the call allowed to fetch this type of object?
1014 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1016 # Find the class the implements the given hint
1017 my ($class) = grep {
1018 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1020 $class =~ s/Fieldmapper:://og;
1021 $class =~ s/::/_/og;
1022 my $method = "retrieve_$class";
1024 my $obj = $e->$method($id) or return $e->event;
1027 # ----------------------------------------------------------------------
1030 __PACKAGE__->register_method(
1031 method => "fleshed_circ_retrieve",
1033 api_name => "open-ils.circ.fleshed.retrieve",);
1035 sub fleshed_circ_retrieve {
1036 my( $self, $client, $id ) = @_;
1037 my $e = new_editor();
1038 my $circ = $e->retrieve_action_circulation(
1044 circ => [ qw/ target_copy / ],
1045 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1046 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1047 acn => [ qw/ record / ],
1051 ) or return $e->event;
1053 my $copy = $circ->target_copy;
1054 my $vol = $copy->call_number;
1055 my $rec = $circ->target_copy->call_number->record;
1057 $vol->record($rec->id);
1058 $copy->call_number($vol->id);
1059 $circ->target_copy($copy->id);
1063 if( $rec->id == OILS_PRECAT_RECORD ) {
1067 $mvr = $U->record_to_mvr($rec);
1068 $rec->marc(''); # drop the bulky marc data
1082 __PACKAGE__->register_method(
1083 method => "test_batch_circ_events",
1084 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1087 # method for testing the behavior of a given event definition
1088 sub test_batch_circ_events {
1089 my($self, $conn, $auth, $event_def, $barcode) = @_;
1091 my $e = new_editor(authtoken => $auth);
1092 return $e->event unless $e->checkauth;
1093 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1095 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1096 or return $e->event;
1098 my $circ = $e->search_action_circulation(
1099 {target_copy => $copy->id, checkin_time => undef})->[0]
1100 or return $e->event;
1102 return undef unless $circ;
1104 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1110 # {"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}}