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 OpenILS::Utils::ModsParser;
24 use OpenSRF::EX qw(:try);
25 use OpenSRF::Utils::Logger qw(:logger);
26 use OpenILS::Utils::Fieldmapper;
27 use OpenILS::Utils::Editor;
28 use OpenILS::Utils::CStoreEditor q/:funcs/;
29 use OpenILS::Const qw/:const/;
30 use OpenSRF::Utils::SettingsClient;
31 use OpenILS::Application::Cat::AssetCommon;
33 my $apputils = "OpenILS::Application::AppUtils";
37 # ------------------------------------------------------------------------
38 # Top level Circ package;
39 # ------------------------------------------------------------------------
43 OpenILS::Application::Circ::Circulate->initialize();
47 __PACKAGE__->register_method(
48 method => 'retrieve_circ',
49 api_name => 'open-ils.circ.retrieve',
51 Retrieve a circ object by id
52 @param authtoken Login session key
53 @pararm circid The id of the circ object
57 my( $s, $c, $a, $i ) = @_;
58 my $e = new_editor(authtoken => $a);
59 return $e->event unless $e->checkauth;
60 my $circ = $e->retrieve_action_circulation($i) or return $e->event;
61 if( $e->requestor->id ne $circ->usr ) {
62 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
68 __PACKAGE__->register_method(
69 method => 'fetch_circ_mods',
70 api_name => 'open-ils.circ.circ_modifier.retrieve.all');
72 my($self, $conn, $args) = @_;
73 my $mods = new_editor()->retrieve_all_config_circ_modifier;
74 return [ map {$_->code} @$mods ] unless $$args{full};
78 __PACKAGE__->register_method(
79 method => 'fetch_bill_types',
80 api_name => 'open-ils.circ.billing_type.retrieve.all');
81 sub fetch_bill_types {
82 my $conf = OpenSRF::Utils::SettingsClient->new;
83 return $conf->config_value(
84 'apps', 'open-ils.circ', 'app_settings', 'billing_types', 'type' );
88 __PACKAGE__->register_method(
89 method => 'ranged_billing_types',
90 api_name => 'open-ils.circ.billing_type.ranged.retrieve.all');
92 sub ranged_billing_types {
93 my($self, $conn, $auth, $org_id, $depth) = @_;
94 my $e = new_editor(authtoken => $auth);
95 return $e->event unless $e->checkauth;
96 return $e->event unless $e->allowed('VIEW_BILLING_TYPE', $org_id);
97 return $e->search_config_billing_type(
98 {owner => $U->get_org_full_path($org_id, $depth)});
103 # ------------------------------------------------------------------------
104 # Returns an array of {circ, record} hashes checked out by the user.
105 # ------------------------------------------------------------------------
106 __PACKAGE__->register_method(
107 method => "checkouts_by_user",
108 api_name => "open-ils.circ.actor.user.checked_out",
109 NOTES => <<" NOTES");
110 Returns a list of open circulations as a pile of objects. Each object
111 contains the relevant copy, circ, and record
114 sub checkouts_by_user {
115 my( $self, $client, $user_session, $user_id ) = @_;
117 my( $requestor, $target, $copy, $record, $evt );
119 ( $requestor, $target, $evt ) =
120 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
123 my $circs = $apputils->simplereq(
125 "open-ils.cstore.direct.action.open_circulation.search.atomic",
126 { usr => $target->id, checkin_time => undef } );
127 # { usr => $target->id } );
130 for my $circ (@$circs) {
132 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
135 $logger->debug("Retrieving record for copy " . $circ->target_copy);
137 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
140 my $mods = $apputils->record_to_mvr($record);
142 push( @results, { copy => $copy, circ => $circ, record => $mods } );
151 __PACKAGE__->register_method(
152 method => "checkouts_by_user_slim",
153 api_name => "open-ils.circ.actor.user.checked_out.slim",
154 NOTES => <<" NOTES");
155 Returns a list of open circulation objects
159 sub checkouts_by_user_slim {
160 my( $self, $client, $user_session, $user_id ) = @_;
162 my( $requestor, $target, $copy, $record, $evt );
164 ( $requestor, $target, $evt ) =
165 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
168 $logger->debug( 'User ' . $requestor->id .
169 " retrieving checked out items for user " . $target->id );
171 # XXX Make the call correct..
172 return $apputils->simplereq(
174 "open-ils.cstore.direct.action.open_circulation.search.atomic",
175 { usr => $target->id, checkin_time => undef } );
176 # { usr => $target->id } );
180 __PACKAGE__->register_method(
181 method => "checkouts_by_user_opac",
182 api_name => "open-ils.circ.actor.user.checked_out.opac",);
185 sub checkouts_by_user_opac {
186 my( $self, $client, $auth, $user_id ) = @_;
188 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
189 return $e->event unless $e->checkauth;
190 $user_id ||= $e->requestor->id;
191 return $e->event unless
192 my $patron = $e->retrieve_actor_user($user_id);
195 my $search = {usr => $user_id, stop_fines => undef};
197 if( $user_id ne $e->requestor->id ) {
198 $data = $e->search_action_circulation(
199 $search, {checkperm=>1, permorg=>$patron->home_ou})
203 $data = $e->search_action_circulation($search);
210 __PACKAGE__->register_method(
211 method => "title_from_transaction",
212 api_name => "open-ils.circ.circ_transaction.find_title",
213 NOTES => <<" NOTES");
214 Returns a mods object for the title that is linked to from the
215 copy from the hold that created the given transaction
218 sub title_from_transaction {
219 my( $self, $client, $login_session, $transactionid ) = @_;
221 my( $user, $circ, $title, $evt );
223 ( $user, $evt ) = $apputils->checkses( $login_session );
226 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
229 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
232 return $apputils->record_to_mvr($title);
237 __PACKAGE__->register_method(
238 method => "new_set_circ_lost",
239 api_name => "open-ils.circ.circulation.set_lost",
241 Sets the copy and related open circulation to lost
243 @param args : barcode
248 # ---------------------------------------------------------------------
249 # Sets a circulation to lost. updates copy status to lost
250 # applies copy and/or prcoessing fees depending on org settings
251 # ---------------------------------------------------------------------
252 sub new_set_circ_lost {
253 my( $self, $conn, $auth, $args ) = @_;
255 my $e = new_editor(authtoken=>$auth, xact=>1);
256 return $e->die_event unless $e->checkauth;
258 my $barcode = $$args{barcode};
259 $logger->info("marking item lost $barcode");
261 # ---------------------------------------------------------------------
263 my $copy = $e->search_asset_copy([
264 {barcode=>$barcode, deleted=>'f'},
265 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])->[0]
266 or return $e->die_event;
269 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
270 $copy->circ_lib : $copy->call_number->owning_lib;
272 my $circ = $e->search_action_circulation(
273 {checkin_time => undef, target_copy => $copy->id} )->[0]
274 or return $e->die_event;
276 $e->allowed('SET_CIRC_LOST', $circ->circ_lib) 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 Sets the circ for the given item as claims returned
291 If a backdate is provided, overdue fines will be voided
294 @param args : barcode, backdate
298 sub set_circ_claims_returned {
299 my( $self, $conn, $auth, $args ) = @_;
301 my $e = new_editor(authtoken=>$auth, xact=>1);
302 return $e->die_event unless $e->checkauth;
304 my $barcode = $$args{barcode};
305 my $backdate = $$args{backdate};
307 $logger->info("marking circ for item $barcode as claims returned".
308 (($backdate) ? " with backdate $backdate" : ''));
310 my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0]
311 or return $e->die_event;
313 my $circ = $e->search_action_circulation(
314 {checkin_time => undef, target_copy => $copy->id})->[0]
315 or return $e->die_event;
317 $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib)
318 or return $e->die_event;
320 $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
321 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
324 # make it look like the circ stopped at the cliams returned time
325 $circ->stop_fines_time(clense_ISO8601($backdate));
326 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
330 $e->update_action_circulation($circ) or return $e->die_event;
339 __PACKAGE__->register_method (
340 method => 'set_circ_due_date',
341 api_name => 'open-ils.circ.circulation.due_date.update',
343 Updates the due_date on the given circ
345 @param circid The id of the circ to update
346 @param date The timestamp of the new due date
350 sub set_circ_due_date {
351 my( $self, $conn, $auth, $circ_id, $date ) = @_;
353 my $e = new_editor(xact=>1, authtoken=>$auth);
354 return $e->die_event unless $e->checkauth;
355 my $circ = $e->retrieve_action_circulation($circ_id)
356 or return $e->die_event;
358 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
359 $date = clense_ISO8601($date);
360 $circ->due_date($date);
361 $e->update_action_circulation($circ) or return $e->die_event;
368 __PACKAGE__->register_method(
369 method => "create_in_house_use",
370 api_name => 'open-ils.circ.in_house_use.create',
372 Creates an in-house use action.
373 @param $authtoken The login session key
374 @param params A hash of params including
375 'location' The org unit id where the in-house use occurs
376 'copyid' The copy in question
377 'count' The number of in-house uses to apply to this copy
378 @return An array of id's representing the id's of the newly created
379 in-house use objects or an event on an error
382 __PACKAGE__->register_method(
383 method => "create_in_house_use",
384 api_name => 'open-ils.circ.non_cat_in_house_use.create',
388 sub create_in_house_use {
389 my( $self, $client, $auth, $params ) = @_;
392 my $org = $params->{location};
393 my $copyid = $params->{copyid};
394 my $count = $params->{count} || 1;
395 my $nc_type = $params->{non_cat_type};
396 my $use_time = $params->{use_time} || 'now';
398 my $e = new_editor(xact=>1,authtoken=>$auth);
399 return $e->event unless $e->checkauth;
400 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
402 my $non_cat = 1 if $self->api_name =~ /non_cat/;
406 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
408 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
414 if( $use_time ne 'now' ) {
415 $use_time = clense_ISO8601($use_time);
416 $logger->debug("in_house_use setting use time to $use_time");
427 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
428 $ihu->item_type($nc_type);
429 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
430 $cmeth = "create_action_non_cat_in_house_use";
433 $ihu = Fieldmapper::action::in_house_use->new;
435 $method = 'open-ils.storage.direct.action.in_house_use.create';
436 $cmeth = "create_action_in_house_use";
439 $ihu->staff($e->requestor->id);
440 $ihu->org_unit($org);
441 $ihu->use_time($use_time);
443 $ihu = $e->$cmeth($ihu) or return $e->event;
444 push( @ids, $ihu->id );
455 __PACKAGE__->register_method(
456 method => "view_circs",
457 api_name => "open-ils.circ.copy_checkout_history.retrieve",
459 Retrieves the last X circs for a given copy
460 @param authtoken The login session key
461 @param copyid The copy to check
462 @param count How far to go back in the item history
463 @return An array of circ ids
466 # ----------------------------------------------------------------------
467 # Returns $count most recent circs. If count exceeds the configured
468 # max, use the configured max instead
469 # ----------------------------------------------------------------------
471 my( $self, $client, $authtoken, $copyid, $count ) = @_;
473 my $e = new_editor(authtoken => $authtoken);
474 return $e->event unless $e->checkauth;
476 my $copy = $e->retrieve_asset_copy([
479 flesh_fields => {acp => ['call_number']}
481 ]) or return $e->event;
483 return $e->event unless $e->allowed(
484 'VIEW_COPY_CHECKOUT_HISTORY',
485 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
486 $copy->circ_lib : $copy->call_number->owning_lib);
488 my $max_history = $U->ou_ancestor_setting_value(
489 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
491 if(defined $max_history) {
492 $count = $max_history unless defined $count and $count < $max_history;
494 $count = 4 unless defined $count;
497 return $e->search_action_circulation([
498 {target_copy => $copyid},
499 {limit => $count, order_by => { circ => "xact_start DESC" }}
504 __PACKAGE__->register_method(
505 method => "circ_count",
506 api_name => "open-ils.circ.circulation.count",
508 Returns the number of times the item has circulated
509 @param copyid The copy to check
513 my( $self, $client, $copyid, $range ) = @_;
514 my $e = OpenILS::Utils::Editor->new;
515 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
520 __PACKAGE__->register_method(
521 method => 'fetch_notes',
522 api_name => 'open-ils.circ.copy_note.retrieve.all',
524 Returns an array of copy note objects.
525 @param args A named hash of parameters including:
526 authtoken : Required if viewing non-public notes
527 itemid : The id of the item whose notes we want to retrieve
528 pub : True if all the caller wants are public notes
529 @return An array of note objects
532 __PACKAGE__->register_method(
533 method => 'fetch_notes',
534 api_name => 'open-ils.circ.call_number_note.retrieve.all',
535 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
537 __PACKAGE__->register_method(
538 method => 'fetch_notes',
539 api_name => 'open-ils.circ.title_note.retrieve.all',
540 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
543 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
545 my( $self, $connection, $args ) = @_;
547 my $id = $$args{itemid};
548 my $authtoken = $$args{authtoken};
551 if( $self->api_name =~ /copy/ ) {
553 return $U->cstorereq(
554 'open-ils.cstore.direct.asset.copy_note.search.atomic',
555 { owning_copy => $id, pub => 't' } );
557 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
559 return $U->cstorereq(
560 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
563 } elsif( $self->api_name =~ /call_number/ ) {
565 return $U->cstorereq(
566 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
567 { call_number => $id, pub => 't' } );
569 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
571 return $U->cstorereq(
572 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
575 } elsif( $self->api_name =~ /title/ ) {
577 return $U->cstorereq(
578 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
579 { record => $id, pub => 't' } );
581 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
583 return $U->cstorereq(
584 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
591 __PACKAGE__->register_method(
592 method => 'has_notes',
593 api_name => 'open-ils.circ.copy.has_notes');
594 __PACKAGE__->register_method(
595 method => 'has_notes',
596 api_name => 'open-ils.circ.call_number.has_notes');
597 __PACKAGE__->register_method(
598 method => 'has_notes',
599 api_name => 'open-ils.circ.title.has_notes');
603 my( $self, $conn, $authtoken, $id ) = @_;
604 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
605 return $editor->event unless $editor->checkauth;
607 my $n = $editor->search_asset_copy_note(
608 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
610 $n = $editor->search_asset_call_number_note(
611 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
613 $n = $editor->search_biblio_record_note(
614 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
621 __PACKAGE__->register_method(
622 method => 'create_copy_note',
623 api_name => 'open-ils.circ.copy_note.create',
625 Creates a new copy note
626 @param authtoken The login session key
627 @param note The note object to create
628 @return The id of the new note object
631 sub create_copy_note {
632 my( $self, $connection, $authtoken, $note ) = @_;
634 my $e = new_editor(xact=>1, authtoken=>$authtoken);
635 return $e->event unless $e->checkauth;
636 my $copy = $e->retrieve_asset_copy(
640 flesh_fields => { 'acp' => ['call_number'] }
645 return $e->event unless
646 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
648 $note->create_date('now');
649 $note->creator($e->requestor->id);
650 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
653 $e->create_asset_copy_note($note) or return $e->event;
659 __PACKAGE__->register_method(
660 method => 'delete_copy_note',
661 api_name => 'open-ils.circ.copy_note.delete',
663 Deletes an existing copy note
664 @param authtoken The login session key
665 @param noteid The id of the note to delete
666 @return 1 on success - Event otherwise.
668 sub delete_copy_note {
669 my( $self, $conn, $authtoken, $noteid ) = @_;
671 my $e = new_editor(xact=>1, authtoken=>$authtoken);
672 return $e->die_event unless $e->checkauth;
674 my $note = $e->retrieve_asset_copy_note([
678 'acpn' => [ 'owning_copy' ],
679 'acp' => [ 'call_number' ],
682 ]) or return $e->die_event;
684 if( $note->creator ne $e->requestor->id ) {
685 return $e->die_event unless
686 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
689 $e->delete_asset_copy_note($note) or return $e->die_event;
695 __PACKAGE__->register_method(
696 method => 'age_hold_rules',
697 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
701 my( $self, $conn ) = @_;
702 return new_editor()->retrieve_all_config_rules_age_hold_protect();
707 __PACKAGE__->register_method(
708 method => 'copy_details_barcode',
710 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
711 sub copy_details_barcode {
712 my( $self, $conn, $auth, $barcode ) = @_;
713 my $e = new_editor();
714 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
715 return $e->event unless $cid;
716 return copy_details( $self, $conn, $auth, $cid );
720 __PACKAGE__->register_method(
721 method => 'copy_details',
722 api_name => 'open-ils.circ.copy_details.retrieve');
725 my( $self, $conn, $auth, $copy_id ) = @_;
726 my $e = new_editor(authtoken=>$auth);
727 return $e->event unless $e->checkauth;
729 my $flesh = { flesh => 1 };
731 my $copy = $e->retrieve_asset_copy(
737 acp => ['call_number'],
741 ]) or return $e->event;
744 # De-flesh the copy for backwards compatibility
746 my $vol = $copy->call_number;
748 $copy->call_number($vol->id);
749 my $record = $vol->record;
751 $vol->record($record->id);
752 $mvr = $U->record_to_mvr($record);
757 my $hold = $e->search_action_hold_request(
759 current_copy => $copy_id,
760 capture_time => { "!=" => undef },
761 fulfillment_time => undef,
762 cancel_time => undef,
766 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
768 my $transit = $e->search_action_transit_copy(
769 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
771 # find the latest circ, open or closed
772 my $circ = $e->search_action_circulation(
774 { target_copy => $copy_id },
775 { order_by => { circ => 'xact_start desc' }, limit => 1 }
793 __PACKAGE__->register_method(
794 method => 'mark_item',
795 api_name => 'open-ils.circ.mark_item_damaged',
797 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
798 @param authtoken The login session key
799 @param copy_id The ID of the copy to mark as damaged
800 @return 1 on success - Event otherwise.
803 __PACKAGE__->register_method(
804 method => 'mark_item',
805 api_name => 'open-ils.circ.mark_item_missing',
807 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
808 @param authtoken The login session key
809 @param copy_id The ID of the copy to mark as missing
810 @return 1 on success - Event otherwise.
813 __PACKAGE__->register_method(
814 method => 'mark_item',
815 api_name => 'open-ils.circ.mark_item_bindery',
817 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
818 @param authtoken The login session key
819 @param copy_id The ID of the copy to mark as bindery
820 @return 1 on success - Event otherwise.
823 __PACKAGE__->register_method(
824 method => 'mark_item',
825 api_name => 'open-ils.circ.mark_item_on_order',
827 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
828 @param authtoken The login session key
829 @param copy_id The ID of the copy to mark as on order
830 @return 1 on success - Event otherwise.
833 __PACKAGE__->register_method(
834 method => 'mark_item',
835 api_name => 'open-ils.circ.mark_item_ill',
837 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
838 @param authtoken The login session key
839 @param copy_id The ID of the copy to mark as inter-library loan
840 @return 1 on success - Event otherwise.
843 __PACKAGE__->register_method(
844 method => 'mark_item',
845 api_name => 'open-ils.circ.mark_item_cataloging',
847 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
848 @param authtoken The login session key
849 @param copy_id The ID of the copy to mark as cataloging
850 @return 1 on success - Event otherwise.
853 __PACKAGE__->register_method(
854 method => 'mark_item',
855 api_name => 'open-ils.circ.mark_item_reserves',
857 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
858 @param authtoken The login session key
859 @param copy_id The ID of the copy to mark as reserves
860 @return 1 on success - Event otherwise.
863 __PACKAGE__->register_method(
864 method => 'mark_item',
865 api_name => 'open-ils.circ.mark_item_discard',
867 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
868 @param authtoken The login session key
869 @param copy_id The ID of the copy to mark as discard
870 @return 1 on success - Event otherwise.
875 my( $self, $conn, $auth, $copy_id, $args ) = @_;
876 my $e = new_editor(authtoken=>$auth, xact =>1);
877 return $e->die_event unless $e->checkauth;
880 my $copy = $e->retrieve_asset_copy([
882 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])->[0]
883 or return $e->die_event;
886 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
887 $copy->circ_lib : $copy->call_number->owning_lib;
889 return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
892 my $perm = 'MARK_ITEM_MISSING';
893 my $stat = OILS_COPY_STATUS_MISSING;
895 if( $self->api_name =~ /damaged/ ) {
896 $perm = 'MARK_ITEM_DAMAGED';
897 $stat = OILS_COPY_STATUS_DAMAGED;
898 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
901 } elsif ( $self->api_name =~ /bindery/ ) {
902 $perm = 'MARK_ITEM_BINDERY';
903 $stat = OILS_COPY_STATUS_BINDERY;
904 } elsif ( $self->api_name =~ /on_order/ ) {
905 $perm = 'MARK_ITEM_ON_ORDER';
906 $stat = OILS_COPY_STATUS_ON_ORDER;
907 } elsif ( $self->api_name =~ /ill/ ) {
908 $perm = 'MARK_ITEM_ILL';
909 $stat = OILS_COPY_STATUS_ILL;
910 } elsif ( $self->api_name =~ /cataloging/ ) {
911 $perm = 'MARK_ITEM_CATALOGING';
912 $stat = OILS_COPY_STATUS_CATALOGING;
913 } elsif ( $self->api_name =~ /reserves/ ) {
914 $perm = 'MARK_ITEM_RESERVES';
915 $stat = OILS_COPY_STATUS_RESERVES;
916 } elsif ( $self->api_name =~ /discard/ ) {
917 $perm = 'MARK_ITEM_DISCARD';
918 $stat = OILS_COPY_STATUS_DISCARD;
922 $copy->status($stat);
923 $copy->edit_date('now');
924 $copy->editor($e->requestor->id);
926 $e->update_asset_copy($copy) or return $e->die_event;
928 my $holds = $e->search_action_hold_request(
930 current_copy => $copy->id,
931 fulfillment_time => undef,
932 cancel_time => undef,
938 $logger->debug("resetting holds that target the marked copy");
939 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
944 sub handle_mark_damaged {
945 my($e, $copy, $owning_lib, $args) = @_;
947 my $apply = $args->{apply_fines} || '';
948 return undef if $apply eq 'noapply';
950 # grab the last circulation
951 my $circ = $e->search_action_circulation([
952 { target_copy => $copy->id},
954 order_by => {circ => "xact_start DESC"},
956 flesh_fields => {circ => ['target_copy', 'usr']}
960 return undef unless $circ;
962 my $charge_price = $U->ou_ancestor_setting_value(
963 $owning_lib, 'circ.charge_on_damaged', $e);
965 my $proc_fee = $U->ou_ancestor_setting_value(
966 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
968 return undef unless $charge_price or $proc_fee;
970 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
971 my $total = $copy_price + $proc_fee;
972 my $apply = $args->{apply_fines};
976 if($charge_price and $copy_price) {
977 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
978 $e, $copy_price, 7, 'Damaged Item', $circ->id);
983 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
984 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
988 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
993 return new OpenILS::Event->('DAMAGE_CHARGE',
994 {usr => $circ->usr->id, charge => $total});
1003 # ----------------------------------------------------------------------
1004 __PACKAGE__->register_method(
1005 method => 'magic_fetch',
1006 api_name => 'open-ils.agent.fetch'
1009 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1012 my( $self, $conn, $auth, $args ) = @_;
1013 my $e = new_editor( authtoken => $auth );
1014 return $e->event unless $e->checkauth;
1016 my $hint = $$args{hint};
1017 my $id = $$args{id};
1019 # Is the call allowed to fetch this type of object?
1020 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1022 # Find the class the implements the given hint
1023 my ($class) = grep {
1024 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1026 $class =~ s/Fieldmapper:://og;
1027 $class =~ s/::/_/og;
1028 my $method = "retrieve_$class";
1030 my $obj = $e->$method($id) or return $e->event;
1033 # ----------------------------------------------------------------------
1036 __PACKAGE__->register_method(
1037 method => "fleshed_circ_retrieve",
1039 api_name => "open-ils.circ.fleshed.retrieve",);
1041 sub fleshed_circ_retrieve {
1042 my( $self, $client, $id ) = @_;
1043 my $e = new_editor();
1044 my $circ = $e->retrieve_action_circulation(
1050 circ => [ qw/ target_copy / ],
1051 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1052 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1053 acn => [ qw/ record / ],
1057 ) or return $e->event;
1059 my $copy = $circ->target_copy;
1060 my $vol = $copy->call_number;
1061 my $rec = $circ->target_copy->call_number->record;
1063 $vol->record($rec->id);
1064 $copy->call_number($vol->id);
1065 $circ->target_copy($copy->id);
1069 if( $rec->id == OILS_PRECAT_RECORD ) {
1073 $mvr = $U->record_to_mvr($rec);
1074 $rec->marc(''); # drop the bulky marc data
1086 # {"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}}