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::CircNotify;
12 use OpenILS::Application::Circ::CreditCard;
13 use OpenILS::Application::Circ::Money;
14 use OpenILS::Application::Circ::NonCat;
15 use OpenILS::Application::Circ::CopyLocations;
16 use OpenILS::Application::Circ::CircCommon;
19 use DateTime::Format::ISO8601;
21 use OpenILS::Application::AppUtils;
23 use OpenSRF::Utils qw/:datetime/;
24 use OpenSRF::AppSession;
25 use OpenILS::Utils::ModsParser;
27 use OpenSRF::EX qw(:try);
28 use OpenSRF::Utils::Logger qw(:logger);
29 use OpenILS::Utils::Fieldmapper;
30 use OpenILS::Utils::CStoreEditor q/:funcs/;
31 use OpenILS::Const qw/:const/;
32 use OpenSRF::Utils::SettingsClient;
33 use OpenILS::Application::Cat::AssetCommon;
35 my $apputils = "OpenILS::Application::AppUtils";
38 my $holdcode = "OpenILS::Application::Circ::Holds";
40 # ------------------------------------------------------------------------
41 # Top level Circ package;
42 # ------------------------------------------------------------------------
46 OpenILS::Application::Circ::Circulate->initialize();
50 __PACKAGE__->register_method(
51 method => 'retrieve_circ',
53 api_name => 'open-ils.circ.retrieve',
55 Retrieve a circ object by id
56 @param authtoken Login session key
57 @pararm circid The id of the circ object
58 @param all_circ Returns an action.all_circulation object instead
59 of an action.circulation object to pick up aged circs.
64 my( $s, $c, $a, $i, $all_circ ) = @_;
65 my $e = new_editor(authtoken => $a);
66 return $e->event unless $e->checkauth;
67 my $method = $all_circ ?
68 'retrieve_action_all_circulation' :
69 'retrieve_action_circulation';
70 my $circ = $e->$method($i) or return $e->event;
71 if( $e->requestor->id ne ($circ->usr || '') ) {
72 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
78 __PACKAGE__->register_method(
79 method => 'fetch_circ_mods',
80 api_name => 'open-ils.circ.circ_modifier.retrieve.all');
82 my($self, $conn, $args) = @_;
83 my $mods = new_editor()->retrieve_all_config_circ_modifier;
84 return [ map {$_->code} @$mods ] unless $$args{full};
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",
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, $auth, $user_id) = @_;
118 my $e = new_editor(authtoken=>$auth);
119 return $e->event unless $e->checkauth;
121 my $circ_ids = $e->search_action_circulation(
123 checkin_time => undef,
125 {stop_fines => undef},
126 {stop_fines => ['MAXFINES','LONGOVERDUE']}
132 for my $id (@$circ_ids) {
133 my $circ = $e->retrieve_action_circulation([
137 circ => ['target_copy'],
138 acp => ['call_number'],
144 # un-flesh for consistency
145 my $c = $circ->target_copy;
146 $circ->target_copy($c->id);
148 my $cn = $c->call_number;
149 $c->call_number($cn->id);
157 record => $U->record_to_mvr($t)
167 __PACKAGE__->register_method(
168 method => "checkouts_by_user_slim",
169 api_name => "open-ils.circ.actor.user.checked_out.slim",
170 NOTES => <<" NOTES");
171 Returns a list of open circulation objects
175 sub checkouts_by_user_slim {
176 my( $self, $client, $user_session, $user_id ) = @_;
178 my( $requestor, $target, $copy, $record, $evt );
180 ( $requestor, $target, $evt ) =
181 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
184 $logger->debug( 'User ' . $requestor->id .
185 " retrieving checked out items for user " . $target->id );
187 # XXX Make the call correct..
188 return $apputils->simplereq(
190 "open-ils.cstore.direct.action.open_circulation.search.atomic",
191 { usr => $target->id, checkin_time => undef } );
192 # { usr => $target->id } );
196 __PACKAGE__->register_method(
197 method => "checkouts_by_user_opac",
198 api_name => "open-ils.circ.actor.user.checked_out.opac",);
201 sub checkouts_by_user_opac {
202 my( $self, $client, $auth, $user_id ) = @_;
204 my $e = new_editor( authtoken => $auth );
205 return $e->event unless $e->checkauth;
206 $user_id ||= $e->requestor->id;
207 return $e->event unless
208 my $patron = $e->retrieve_actor_user($user_id);
211 my $search = {usr => $user_id, stop_fines => undef};
213 if( $user_id ne $e->requestor->id ) {
214 $data = $e->search_action_circulation(
215 $search, {checkperm=>1, permorg=>$patron->home_ou})
219 $data = $e->search_action_circulation($search);
226 __PACKAGE__->register_method(
227 method => "title_from_transaction",
228 api_name => "open-ils.circ.circ_transaction.find_title",
229 NOTES => <<" NOTES");
230 Returns a mods object for the title that is linked to from the
231 copy from the hold that created the given transaction
234 sub title_from_transaction {
235 my( $self, $client, $login_session, $transactionid ) = @_;
237 my( $user, $circ, $title, $evt );
239 ( $user, $evt ) = $apputils->checkses( $login_session );
242 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
245 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
248 return $apputils->record_to_mvr($title);
251 __PACKAGE__->register_method(
252 method => "staff_age_to_lost",
253 api_name => "open-ils.circ.circulation.age_to_lost",
256 This fires a circ.staff_age_to_lost Action-Trigger event against all
257 overdue circulations in scope of the specified context library and
258 user profile, which effectively marks the associated items as Lost.
259 This is likely to be done at the end of a semester in an academic
262 @param args : circ_lib, user_profile
266 sub staff_age_to_lost {
267 my( $self, $conn, $auth, $args ) = @_;
268 my $e = new_editor(authtoken=>$auth);
269 return $e->event unless $e->checkauth;
270 return $e->event unless $e->allowed('SET_CIRC_LOST', $args->{'circ_lib'});
272 my $orgs = $U->get_org_descendants($args->{'circ_lib'});
273 my $profiles = $U->fetch_permission_group_descendants($args->{'user_profile'});
275 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
277 my $method = 'open-ils.trigger.passive.event.autocreate.batch';
278 my $hook = 'circ.staff_age_to_lost';
279 my $context_org = 'circ_lib';
280 my $opt_granularity = undef;
282 "checkin_time" => undef,
283 "due_date" => { "<" => "now" },
285 { "stop_fines" => ["MAXFINES", "LONGOVERDUE"] }, # FIXME: CLAIMSRETURNED also?
286 { "stop_fines" => undef }
290 "select" => {"au" => ["id"]},
293 "profile" => $profiles,
294 "id" => { "=" => {"+circ" => "usr"} }
298 "select" => {"aou" => ["id"]},
302 {"id" => { "=" => {"+circ" => "circ_lib"} }},
309 my $req_timeout = 10800;
310 my $chunk_size = 100;
313 my $req = $ses->request($method, $hook, $context_org, $filter, $opt_granularity);
314 my @event_ids; my @chunked_ids;
315 while (my $resp = $req->recv(timeout => $req_timeout)) {
316 push(@event_ids, $resp->content);
317 push(@chunked_ids, $resp->content);
318 if (scalar(@chunked_ids) > $chunk_size) {
319 $conn->respond({'progress'=>$progress++}); # 'event_ids'=>@chunked_ids
323 if (scalar(@chunked_ids) > 0) {
324 $conn->respond({'progress'=>$progress++}); # 'event_ids'=>@chunked_ids
328 $logger->info("staff_age_to_lost: created ".scalar(@event_ids)." events for circ.staff_age_to_lost");
329 $conn->respond_complete({'total_progress'=>$progress-1,'created'=>scalar(@event_ids)});
330 } elsif($req->complete) {
331 $logger->info("staff_age_to_lost: no events to create for circ.staff_age_to_lost");
332 $conn->respond_complete({'total_progress'=>$progress-1,'created'=>0});
334 $logger->warn("staff_age_to_lost: timeout occurred during event creation for circ.staff_age_to_lost");
335 $conn->respond_complete({'total_progress'=>$progress-1,'error'=>'timeout'});
342 __PACKAGE__->register_method(
343 method => "new_set_circ_lost",
344 api_name => "open-ils.circ.circulation.set_lost",
346 Sets the copy and related open circulation to lost
348 @param args : barcode
353 # ---------------------------------------------------------------------
354 # Sets a circulation to lost. updates copy status to lost
355 # applies copy and/or prcoessing fees depending on org settings
356 # ---------------------------------------------------------------------
357 sub new_set_circ_lost {
358 my( $self, $conn, $auth, $args ) = @_;
360 my $e = new_editor(authtoken=>$auth, xact=>1);
361 return $e->die_event unless $e->checkauth;
363 my $copy = $e->search_asset_copy({barcode=>$$args{barcode}, deleted=>'f'})->[0]
364 or return $e->die_event;
366 my $evt = OpenILS::Application::Cat::AssetCommon->set_item_lost($e, $copy->id);
374 __PACKAGE__->register_method(
375 method => "set_circ_claims_returned",
376 api_name => "open-ils.circ.circulation.set_claims_returned",
378 desc => q/Sets the circ for a given item as claims returned
379 If a backdate is provided, overdue fines will be voided
380 back to the backdate/,
382 {desc => 'Authentication token', type => 'string'},
383 {desc => 'Arguments, including "barcode" and optional "backdate"', type => 'object'}
385 return => {desc => q/1 on success, failure event on error, and
386 PATRON_EXCEEDS_CLAIMS_RETURN_COUNT if the patron exceeds the
387 configured claims return maximum/}
391 __PACKAGE__->register_method(
392 method => "set_circ_claims_returned",
393 api_name => "open-ils.circ.circulation.set_claims_returned.override",
395 desc => q/This adds support for overrideing the configured max
396 claims returned amount.
397 @see open-ils.circ.circulation.set_claims_returned./,
401 sub set_circ_claims_returned {
402 my( $self, $conn, $auth, $args, $oargs ) = @_;
404 my $e = new_editor(authtoken=>$auth, xact=>1);
405 return $e->die_event unless $e->checkauth;
407 $oargs = { all => 1 } unless defined $oargs;
409 my $barcode = $$args{barcode};
410 my $backdate = $$args{backdate};
412 my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0]
413 or return $e->die_event;
415 my $circ = $e->search_action_circulation(
416 {checkin_time => undef, target_copy => $copy->id})->[0]
417 or return $e->die_event;
419 $backdate = $circ->due_date if $$args{use_due_date};
421 $logger->info("marking circ for item $barcode as claims returned".
422 (($backdate) ? " with backdate $backdate" : ''));
424 my $patron = $e->retrieve_actor_user($circ->usr);
425 my $max_count = $U->ou_ancestor_setting_value(
426 $circ->circ_lib, 'circ.max_patron_claim_return_count', $e);
428 # If the patron has too instances of many claims returned,
429 # require an override to continue. A configured max of
430 # 0 means all attempts require an override
431 if(defined $max_count and $patron->claims_returned_count >= $max_count) {
433 if($self->api_name =~ /override/ && ($oargs->{all} || grep { $_ eq 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT' } @{$oargs->{events}})) {
435 # see if we're allowed to override
436 return $e->die_event unless
437 $e->allowed('SET_CIRC_CLAIMS_RETURNED.override', $circ->circ_lib);
441 # exit early and return the max claims return event
443 return OpenILS::Event->new(
444 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT',
446 patron_count => $patron->claims_returned_count,
447 max_count => $max_count
453 $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib)
454 or return $e->die_event;
456 $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
457 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
460 $backdate = cleanse_ISO8601($backdate);
462 my $original_date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($circ->due_date));
463 my $new_date = DateTime::Format::ISO8601->new->parse_datetime($backdate);
464 $backdate = $new_date->ymd . 'T' . $original_date->strftime('%T%z');
466 # clean it up once again; need a : in the timezone offset. E.g. -06:00 not -0600
467 $backdate = cleanse_ISO8601($backdate);
469 # make it look like the circ stopped at the cliams returned time
470 $circ->stop_fines_time($backdate);
471 my $evt = OpenILS::Application::Circ::CircCommon->void_or_zero_overdues($e, $circ, {backdate => $backdate, note => 'System: OVERDUE REVERSED FOR CLAIMS-RETURNED', force_zero => 1});
475 $e->update_action_circulation($circ) or return $e->die_event;
477 # see if there is a configured post-claims-return copy status
478 if(my $stat = $U->ou_ancestor_setting_value($circ->circ_lib, 'circ.claim_return.copy_status')) {
479 $copy->status($stat);
480 $copy->edit_date('now');
481 $copy->editor($e->requestor->id);
482 $e->update_asset_copy($copy) or return $e->die_event;
485 # Check if the copy circ lib wants lost fees voided on claims
487 if ($U->is_true($U->ou_ancestor_setting_value($copy->circ_lib, 'circ.void_lost_on_claimsreturned', $e))) {
488 my $result = OpenILS::Application::Circ::CircCommon->void_lost(
499 # Check if the copy circ lib wants lost processing fees voided on
501 if ($U->is_true($U->ou_ancestor_setting_value($copy->circ_lib, 'circ.void_lost_proc_fee_on_claimsreturned', $e))) {
502 my $result = OpenILS::Application::Circ::CircCommon->void_lost(
513 # Check if the copy circ lib wants longoverdue fees voided on claims
515 if ($U->is_true($U->ou_ancestor_setting_value($copy->circ_lib, 'circ.void_longoverdue_on_claimsreturned', $e))) {
516 my $result = OpenILS::Application::Circ::CircCommon->void_lost(
527 # Check if the copy circ lib wants longoverdue processing fees voided on
529 if ($U->is_true($U->ou_ancestor_setting_value($copy->circ_lib, 'circ.void_longoverdue_proc_fee_on_claimsreturned', $e))) {
530 my $result = OpenILS::Application::Circ::CircCommon->void_lost(
546 __PACKAGE__->register_method(
547 method => "post_checkin_backdate_circ",
548 api_name => "open-ils.circ.post_checkin_backdate",
550 desc => q/Back-date an already checked in circulation/,
552 {desc => 'Authentication token', type => 'string'},
553 {desc => 'Circ ID', type => 'number'},
554 {desc => 'ISO8601 backdate', type => 'string'},
556 return => {desc => q/1 on success, failure event on error/}
560 __PACKAGE__->register_method(
561 method => "post_checkin_backdate_circ",
562 api_name => "open-ils.circ.post_checkin_backdate.batch",
565 desc => q/@see open-ils.circ.post_checkin_backdate. Batch mode/,
567 {desc => 'Authentication token', type => 'string'},
568 {desc => 'List of Circ ID', type => 'array'},
569 {desc => 'ISO8601 backdate', type => 'string'},
571 return => {desc => q/Set of: 1 on success, failure event on error/}
576 sub post_checkin_backdate_circ {
577 my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
578 my $e = new_editor(authtoken=>$auth);
579 return $e->die_event unless $e->checkauth;
580 if($self->api_name =~ /batch/) {
581 foreach my $c (@$circ_id) {
582 $conn->respond(post_checkin_backdate_circ_impl($e, $c, $backdate));
585 $conn->respond_complete(post_checkin_backdate_circ_impl($e, $circ_id, $backdate));
593 sub post_checkin_backdate_circ_impl {
594 my($e, $circ_id, $backdate) = @_;
598 my $circ = $e->retrieve_action_circulation($circ_id)
599 or return $e->die_event;
601 # anyone with checkin perms can backdate (more restrictive?)
602 return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
604 # don't allow back-dating an open circulation
605 return OpenILS::Event->new('BAD_PARAMS') unless
606 $backdate and $circ->checkin_time;
608 # update the checkin and stop_fines times to reflect the new backdate
609 $circ->stop_fines_time(cleanse_ISO8601($backdate));
610 $circ->checkin_time(cleanse_ISO8601($backdate));
611 $e->update_action_circulation($circ) or return $e->die_event;
613 # now void the overdues "erased" by the back-dating
614 my $evt = OpenILS::Application::Circ::CircCommon->void_or_zero_overdues($e, $circ, {backdate => $backdate});
617 # If the circ was closed before and the balance owned !=0, re-open the transaction
618 $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
627 __PACKAGE__->register_method (
628 method => 'set_circ_due_date',
629 api_name => 'open-ils.circ.circulation.due_date.update',
631 Updates the due_date on the given circ
633 @param circid The id of the circ to update
634 @param date The timestamp of the new due date
638 sub set_circ_due_date {
639 my( $self, $conn, $auth, $circ_id, $date ) = @_;
641 my $e = new_editor(xact=>1, authtoken=>$auth);
642 return $e->die_event unless $e->checkauth;
643 my $circ = $e->retrieve_action_circulation($circ_id)
644 or return $e->die_event;
646 return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
647 $date = cleanse_ISO8601($date);
649 if (!(interval_to_seconds($circ->duration) % 86400)) { # duration is divisible by days
650 my $original_date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($circ->due_date));
651 my $new_date = DateTime::Format::ISO8601->new->parse_datetime($date);
652 $date = cleanse_ISO8601( $new_date->ymd . 'T' . $original_date->strftime('%T%z') );
655 $circ->due_date($date);
656 $e->update_action_circulation($circ) or return $e->die_event;
663 __PACKAGE__->register_method(
664 method => "create_in_house_use",
665 api_name => 'open-ils.circ.in_house_use.create',
667 Creates an in-house use action.
668 @param $authtoken The login session key
669 @param params A hash of params including
670 'location' The org unit id where the in-house use occurs
671 'copyid' The copy in question
672 'count' The number of in-house uses to apply to this copy
673 @return An array of id's representing the id's of the newly created
674 in-house use objects or an event on an error
677 __PACKAGE__->register_method(
678 method => "create_in_house_use",
679 api_name => 'open-ils.circ.non_cat_in_house_use.create',
683 sub create_in_house_use {
684 my( $self, $client, $auth, $params ) = @_;
687 my $org = $params->{location};
688 my $copyid = $params->{copyid};
689 my $count = $params->{count} || 1;
690 my $nc_type = $params->{non_cat_type};
691 my $use_time = $params->{use_time} || 'now';
693 my $e = new_editor(xact=>1,authtoken=>$auth);
694 return $e->event unless $e->checkauth;
695 return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
697 my $non_cat = 1 if $self->api_name =~ /non_cat/;
701 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
703 $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
709 if( $use_time ne 'now' ) {
710 $use_time = cleanse_ISO8601($use_time);
711 $logger->debug("in_house_use setting use time to $use_time");
722 $ihu = Fieldmapper::action::non_cat_in_house_use->new;
723 $ihu->item_type($nc_type);
724 $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
725 $cmeth = "create_action_non_cat_in_house_use";
728 $ihu = Fieldmapper::action::in_house_use->new;
730 $method = 'open-ils.storage.direct.action.in_house_use.create';
731 $cmeth = "create_action_in_house_use";
734 $ihu->staff($e->requestor->id);
735 $ihu->org_unit($org);
736 $ihu->use_time($use_time);
738 $ihu = $e->$cmeth($ihu) or return $e->event;
739 push( @ids, $ihu->id );
750 __PACKAGE__->register_method(
751 method => "view_circs",
752 api_name => "open-ils.circ.copy_checkout_history.retrieve",
754 Retrieves the last X circs for a given copy
755 @param authtoken The login session key
756 @param copyid The copy to check
757 @param count How far to go back in the item history
758 @return An array of circ ids
761 # ----------------------------------------------------------------------
762 # Returns $count most recent circs. If count exceeds the configured
763 # max, use the configured max instead
764 # ----------------------------------------------------------------------
766 my( $self, $client, $authtoken, $copyid, $count ) = @_;
768 my $e = new_editor(authtoken => $authtoken);
769 return $e->event unless $e->checkauth;
771 my $copy = $e->retrieve_asset_copy([
774 flesh_fields => {acp => ['call_number']}
776 ]) or return $e->event;
778 return $e->event unless $e->allowed(
779 'VIEW_COPY_CHECKOUT_HISTORY',
780 ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ?
781 $copy->circ_lib : $copy->call_number->owning_lib);
783 my $max_history = $U->ou_ancestor_setting_value(
784 $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
786 if(defined $max_history) {
787 $count = $max_history unless defined $count and $count < $max_history;
789 $count = 4 unless defined $count;
792 return $e->search_action_all_circulation([
793 {target_copy => $copyid},
794 {limit => $count, order_by => { combcirc => "xact_start DESC" }}
799 __PACKAGE__->register_method(
800 method => "circ_count",
801 api_name => "open-ils.circ.circulation.count",
803 Returns the number of times the item has circulated
804 @param copyid The copy to check
808 my( $self, $client, $copyid ) = @_;
810 my $count = new_editor()->json_query({
819 where => {'+circbyyr' => {copy => $copyid}}
831 __PACKAGE__->register_method(
832 method => 'fetch_notes',
834 api_name => 'open-ils.circ.copy_note.retrieve.all',
836 Returns an array of copy note objects.
837 @param args A named hash of parameters including:
838 authtoken : Required if viewing non-public notes
839 itemid : The id of the item whose notes we want to retrieve
840 pub : True if all the caller wants are public notes
841 @return An array of note objects
844 __PACKAGE__->register_method(
845 method => 'fetch_notes',
846 api_name => 'open-ils.circ.call_number_note.retrieve.all',
847 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
849 __PACKAGE__->register_method(
850 method => 'fetch_notes',
851 api_name => 'open-ils.circ.title_note.retrieve.all',
852 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
855 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
857 my( $self, $connection, $args ) = @_;
859 my $id = $$args{itemid};
860 my $authtoken = $$args{authtoken};
863 if( $self->api_name =~ /copy/ ) {
865 return $U->cstorereq(
866 'open-ils.cstore.direct.asset.copy_note.search.atomic',
867 { owning_copy => $id, pub => 't' } );
869 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
871 return $U->cstorereq(
872 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
875 } elsif( $self->api_name =~ /call_number/ ) {
877 return $U->cstorereq(
878 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
879 { call_number => $id, pub => 't' } );
881 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
883 return $U->cstorereq(
884 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
887 } elsif( $self->api_name =~ /title/ ) {
889 return $U->cstorereq(
890 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
891 { record => $id, pub => 't' } );
893 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
895 return $U->cstorereq(
896 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
903 __PACKAGE__->register_method(
904 method => 'has_notes',
905 api_name => 'open-ils.circ.copy.has_notes');
906 __PACKAGE__->register_method(
907 method => 'has_notes',
908 api_name => 'open-ils.circ.call_number.has_notes');
909 __PACKAGE__->register_method(
910 method => 'has_notes',
911 api_name => 'open-ils.circ.title.has_notes');
915 my( $self, $conn, $authtoken, $id ) = @_;
916 my $editor = new_editor(authtoken => $authtoken);
917 return $editor->event unless $editor->checkauth;
919 my $n = $editor->search_asset_copy_note(
920 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
922 $n = $editor->search_asset_call_number_note(
923 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
925 $n = $editor->search_biblio_record_note(
926 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
933 __PACKAGE__->register_method(
934 method => 'create_copy_note',
935 api_name => 'open-ils.circ.copy_note.create',
937 Creates a new copy note
938 @param authtoken The login session key
939 @param note The note object to create
940 @return The id of the new note object
943 sub create_copy_note {
944 my( $self, $connection, $authtoken, $note ) = @_;
946 my $e = new_editor(xact=>1, authtoken=>$authtoken);
947 return $e->event unless $e->checkauth;
948 my $copy = $e->retrieve_asset_copy(
952 flesh_fields => { 'acp' => ['call_number'] }
957 return $e->event unless
958 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
960 $note->create_date('now');
961 $note->creator($e->requestor->id);
962 $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
965 $e->create_asset_copy_note($note) or return $e->event;
971 __PACKAGE__->register_method(
972 method => 'delete_copy_note',
973 api_name => 'open-ils.circ.copy_note.delete',
975 Deletes an existing copy note
976 @param authtoken The login session key
977 @param noteid The id of the note to delete
978 @return 1 on success - Event otherwise.
980 sub delete_copy_note {
981 my( $self, $conn, $authtoken, $noteid ) = @_;
983 my $e = new_editor(xact=>1, authtoken=>$authtoken);
984 return $e->die_event unless $e->checkauth;
986 my $note = $e->retrieve_asset_copy_note([
990 'acpn' => [ 'owning_copy' ],
991 'acp' => [ 'call_number' ],
994 ]) or return $e->die_event;
996 if( $note->creator ne $e->requestor->id ) {
997 return $e->die_event unless
998 $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
1001 $e->delete_asset_copy_note($note) or return $e->die_event;
1007 __PACKAGE__->register_method(
1008 method => 'age_hold_rules',
1009 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
1012 sub age_hold_rules {
1013 my( $self, $conn ) = @_;
1014 return new_editor()->retrieve_all_config_rules_age_hold_protect();
1019 __PACKAGE__->register_method(
1020 method => 'copy_details_barcode',
1022 api_name => 'open-ils.circ.copy_details.retrieve.barcode');
1023 sub copy_details_barcode {
1024 my( $self, $conn, $auth, $barcode ) = @_;
1025 my $e = new_editor();
1026 my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
1027 return $e->event unless $cid;
1028 return copy_details( $self, $conn, $auth, $cid );
1032 __PACKAGE__->register_method(
1033 method => 'copy_details',
1034 api_name => 'open-ils.circ.copy_details.retrieve');
1037 my( $self, $conn, $auth, $copy_id ) = @_;
1038 my $e = new_editor(authtoken=>$auth);
1039 return $e->event unless $e->checkauth;
1041 my $flesh = { flesh => 1 };
1043 my $copy = $e->retrieve_asset_copy(
1049 acp => ['call_number','parts','peer_record_maps','floating'],
1050 acn => ['record','prefix','suffix','label_class']
1053 ]) or return $e->event;
1056 # De-flesh the copy for backwards compatibility
1058 my $vol = $copy->call_number;
1060 $copy->call_number($vol->id);
1061 my $record = $vol->record;
1063 $vol->record($record->id);
1064 $mvr = $U->record_to_mvr($record);
1069 my $hold = $e->search_action_hold_request(
1071 current_copy => $copy_id,
1072 capture_time => { "!=" => undef },
1073 fulfillment_time => undef,
1074 cancel_time => undef,
1078 OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
1080 my $transit = $e->search_action_transit_copy(
1081 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
1083 # find the most recent circulation for the requested copy,
1084 # be it active, completed, or aged.
1085 my $circ = $e->search_action_all_circulation([
1086 { target_copy => $copy_id },
1092 'checkin_workstation',
1095 'recurring_fine_rule'
1098 order_by => { combcirc => 'xact_start desc' },
1106 transit => $transit,
1116 __PACKAGE__->register_method(
1117 method => 'mark_item',
1118 api_name => 'open-ils.circ.mark_item_damaged',
1120 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
1121 @param authtoken The login session key
1122 @param copy_id The ID of the copy to mark as damaged
1123 @return 1 on success - Event otherwise.
1126 __PACKAGE__->register_method(
1127 method => 'mark_item',
1128 api_name => 'open-ils.circ.mark_item_missing',
1130 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
1131 @param authtoken The login session key
1132 @param copy_id The ID of the copy to mark as missing
1133 @return 1 on success - Event otherwise.
1136 __PACKAGE__->register_method(
1137 method => 'mark_item',
1138 api_name => 'open-ils.circ.mark_item_bindery',
1140 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
1141 @param authtoken The login session key
1142 @param copy_id The ID of the copy to mark as bindery
1143 @return 1 on success - Event otherwise.
1146 __PACKAGE__->register_method(
1147 method => 'mark_item',
1148 api_name => 'open-ils.circ.mark_item_on_order',
1150 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
1151 @param authtoken The login session key
1152 @param copy_id The ID of the copy to mark as on order
1153 @return 1 on success - Event otherwise.
1156 __PACKAGE__->register_method(
1157 method => 'mark_item',
1158 api_name => 'open-ils.circ.mark_item_ill',
1160 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
1161 @param authtoken The login session key
1162 @param copy_id The ID of the copy to mark as inter-library loan
1163 @return 1 on success - Event otherwise.
1166 __PACKAGE__->register_method(
1167 method => 'mark_item',
1168 api_name => 'open-ils.circ.mark_item_cataloging',
1170 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
1171 @param authtoken The login session key
1172 @param copy_id The ID of the copy to mark as cataloging
1173 @return 1 on success - Event otherwise.
1176 __PACKAGE__->register_method(
1177 method => 'mark_item',
1178 api_name => 'open-ils.circ.mark_item_reserves',
1180 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
1181 @param authtoken The login session key
1182 @param copy_id The ID of the copy to mark as reserves
1183 @return 1 on success - Event otherwise.
1186 __PACKAGE__->register_method(
1187 method => 'mark_item',
1188 api_name => 'open-ils.circ.mark_item_discard',
1190 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
1191 @param authtoken The login session key
1192 @param copy_id The ID of the copy to mark as discard
1193 @return 1 on success - Event otherwise.
1198 my( $self, $conn, $auth, $copy_id, $args ) = @_;
1199 my $e = new_editor(authtoken=>$auth, xact =>1);
1200 return $e->die_event unless $e->checkauth;
1203 my $copy = $e->retrieve_asset_copy([
1205 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1206 or return $e->die_event;
1209 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1210 $copy->circ_lib : $copy->call_number->owning_lib;
1212 my $perm = 'MARK_ITEM_MISSING';
1213 my $stat = OILS_COPY_STATUS_MISSING;
1215 if( $self->api_name =~ /damaged/ ) {
1216 $perm = 'MARK_ITEM_DAMAGED';
1217 $stat = OILS_COPY_STATUS_DAMAGED;
1218 my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1219 return $evt if $evt;
1221 } elsif ( $self->api_name =~ /bindery/ ) {
1222 $perm = 'MARK_ITEM_BINDERY';
1223 $stat = OILS_COPY_STATUS_BINDERY;
1224 } elsif ( $self->api_name =~ /on_order/ ) {
1225 $perm = 'MARK_ITEM_ON_ORDER';
1226 $stat = OILS_COPY_STATUS_ON_ORDER;
1227 } elsif ( $self->api_name =~ /ill/ ) {
1228 $perm = 'MARK_ITEM_ILL';
1229 $stat = OILS_COPY_STATUS_ILL;
1230 } elsif ( $self->api_name =~ /cataloging/ ) {
1231 $perm = 'MARK_ITEM_CATALOGING';
1232 $stat = OILS_COPY_STATUS_CATALOGING;
1233 } elsif ( $self->api_name =~ /reserves/ ) {
1234 $perm = 'MARK_ITEM_RESERVES';
1235 $stat = OILS_COPY_STATUS_RESERVES;
1236 } elsif ( $self->api_name =~ /discard/ ) {
1237 $perm = 'MARK_ITEM_DISCARD';
1238 $stat = OILS_COPY_STATUS_DISCARD;
1241 # caller may proceed if either perm is allowed
1242 return $e->die_event unless $e->allowed([$perm, 'UPDATE_COPY'], $owning_lib);
1244 $copy->status($stat);
1245 $copy->edit_date('now');
1246 $copy->editor($e->requestor->id);
1248 $e->update_asset_copy($copy) or return $e->die_event;
1250 my $holds = $e->search_action_hold_request(
1252 current_copy => $copy->id,
1253 fulfillment_time => undef,
1254 cancel_time => undef,
1260 if( $self->api_name =~ /damaged/ ) {
1261 # now that we've committed the changes, create related A/T events
1262 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1263 $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1266 $logger->debug("resetting holds that target the marked copy");
1267 OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1272 sub handle_mark_damaged {
1273 my($e, $copy, $owning_lib, $args) = @_;
1275 my $apply = $args->{apply_fines} || '';
1276 return undef if $apply eq 'noapply';
1278 my $new_amount = $args->{override_amount};
1279 my $new_btype = $args->{override_btype};
1280 my $new_note = $args->{override_note};
1282 # grab the last circulation
1283 my $circ = $e->search_action_circulation([
1284 { target_copy => $copy->id},
1286 order_by => {circ => "xact_start DESC"},
1288 flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1292 return undef unless $circ;
1294 my $charge_price = $U->ou_ancestor_setting_value(
1295 $owning_lib, 'circ.charge_on_damaged', $e);
1297 my $proc_fee = $U->ou_ancestor_setting_value(
1298 $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1300 my $void_overdue = $U->ou_ancestor_setting_value(
1301 $owning_lib, 'circ.damaged.void_ovedue', $e) || 0;
1303 return undef unless $charge_price or $proc_fee;
1305 my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1306 my $total = $copy_price + $proc_fee;
1310 if($new_amount and $new_btype) {
1312 # Allow staff to override the amount to charge for a damaged item
1313 # Consider the case where the item is only partially damaged
1314 # This value is meant to take the place of the item price and
1315 # optional processing fee.
1317 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1318 $e, $new_amount, $new_btype, 'Damaged Item Override', $circ->id, $new_note);
1319 return $evt if $evt;
1323 if($charge_price and $copy_price) {
1324 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1325 $e, $copy_price, 7, 'Damaged Item', $circ->id);
1326 return $evt if $evt;
1330 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1331 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1332 return $evt if $evt;
1336 # the assumption is that you would not void the overdues unless you
1337 # were also charging for the item and/or applying a processing fee
1339 my $evt = OpenILS::Application::Circ::CircCommon->void_or_zero_overdues($e, $circ, {note => 'System: OVERDUE REVERSED FOR DAMAGE CHARGE'});
1340 return $evt if $evt;
1343 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1344 return $evt if $evt;
1346 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1347 $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1349 my $evt2 = OpenILS::Utils::Penalty->calculate_penalties($e, $circ->usr->id, $e->requestor->ws_ou);
1350 return $evt2 if $evt2;
1355 return OpenILS::Event->new('DAMAGE_CHARGE',
1366 # ----------------------------------------------------------------------
1367 __PACKAGE__->register_method(
1368 method => 'mark_item_missing_pieces',
1369 api_name => 'open-ils.circ.mark_item_missing_pieces',
1371 Changes the status of a copy to "damaged" or to a custom status based on the
1372 circ.missing_pieces.copy_status org unit setting. Requires MARK_ITEM_MISSING_PIECES
1374 @param authtoken The login session key
1375 @param copy_id The ID of the copy to mark as damaged
1376 @return Success event with circ and copy objects in the payload, or error Event otherwise.
1380 sub mark_item_missing_pieces {
1381 my( $self, $conn, $auth, $copy_id, $args ) = @_;
1382 ### FIXME: We're starting a transaction here, but we're doing a lot of things outside of the transaction
1383 ### FIXME: Even better, we're going to use two transactions, the first to affect pertinent holds before checkout can
1385 my $e2 = new_editor(authtoken=>$auth, xact =>1);
1386 return $e2->die_event unless $e2->checkauth;
1389 my $copy = $e2->retrieve_asset_copy([
1391 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1392 or return $e2->die_event;
1395 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
1396 $copy->circ_lib : $copy->call_number->owning_lib;
1398 return $e2->die_event unless $e2->allowed('MARK_ITEM_MISSING_PIECES', $owning_lib);
1400 #### grab the last circulation
1401 my $circ = $e2->search_action_circulation([
1402 { target_copy => $copy->id},
1404 order_by => {circ => "xact_start DESC"}
1409 $logger->info('open-ils.circ.mark_item_missing_pieces: no previous checkout');
1411 return OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND',{'copy'=>$copy});
1414 my $holds = $e2->search_action_hold_request(
1416 current_copy => $copy->id,
1417 fulfillment_time => undef,
1418 cancel_time => undef,
1422 $logger->debug("resetting holds that target the marked copy");
1423 OpenILS::Application::Circ::Holds->_reset_hold($e2->requestor, $_) for @$holds;
1426 if (! $e2->commit) {
1427 return $e2->die_event;
1430 my $e = new_editor(authtoken=>$auth, xact =>1);
1431 return $e->die_event unless $e->checkauth;
1433 if (! $circ->checkin_time) { # if circ active, attempt renew
1434 my ($res) = $self->method_lookup('open-ils.circ.renew')->run($e->authtoken,{'copy_id'=>$circ->target_copy});
1435 if (ref $res ne 'ARRAY') { $res = [ $res ]; }
1436 if ( $res->[0]->{textcode} eq 'SUCCESS' ) {
1437 $circ = $res->[0]->{payload}{'circ'};
1438 $circ->target_copy( $copy->id );
1439 $logger->info('open-ils.circ.mark_item_missing_pieces: successful renewal');
1441 $logger->info('open-ils.circ.mark_item_missing_pieces: non-successful renewal');
1446 'copy_id'=>$circ->target_copy,
1447 'patron_id'=>$circ->usr,
1448 'skip_deposit_fee'=>1,
1449 'skip_rental_fee'=>1
1452 if ($U->ou_ancestor_setting_value($e->requestor->ws_ou, 'circ.block_renews_for_holds')) {
1454 my ($hold, undef, $retarget) = $holdcode->find_nearest_permitted_hold(
1455 $e, $copy, $e->requestor, 1 );
1457 if ($hold) { # needed for hold? then due now
1459 $logger->info('open-ils.circ.mark_item_missing_pieces: item needed for hold, shortening due date');
1460 my $due_date = DateTime->now(time_zone => 'local');
1461 $co_params->{'due_date'} = cleanse_ISO8601( $due_date->strftime('%FT%T%z') );
1463 $logger->info('open-ils.circ.mark_item_missing_pieces: item not needed for hold');
1467 my ($res) = $self->method_lookup('open-ils.circ.checkout.full.override')->run($e->authtoken,$co_params,{ all => 1 });
1468 if (ref $res ne 'ARRAY') { $res = [ $res ]; }
1469 if ( $res->[0]->{textcode} eq 'SUCCESS' ) {
1470 $logger->info('open-ils.circ.mark_item_missing_pieces: successful checkout');
1471 $circ = $res->[0]->{payload}{'circ'};
1473 $logger->info('open-ils.circ.mark_item_missing_pieces: non-successful checkout');
1479 ### Update the item status
1481 my $custom_stat = $U->ou_ancestor_setting_value(
1482 $owning_lib, 'circ.missing_pieces.copy_status', $e);
1483 my $stat = $custom_stat || OILS_COPY_STATUS_DAMAGED;
1485 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1486 $ses->request('open-ils.trigger.event.autocreate', 'missing_pieces', $copy, $owning_lib);
1488 $copy->status($stat);
1489 $copy->edit_date('now');
1490 $copy->editor($e->requestor->id);
1492 $e->update_asset_copy($copy) or return $e->die_event;
1496 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1497 $ses->request('open-ils.trigger.event.autocreate', 'circ.missing_pieces', $circ, $circ->circ_lib);
1499 return OpenILS::Event->new('SUCCESS',
1503 slip => $U->fire_object_event(undef, 'circ.format.missing_pieces.slip.print', $circ, $circ->circ_lib),
1504 letter => $U->fire_object_event(undef, 'circ.format.missing_pieces.letter.print', $circ, $circ->circ_lib)
1509 return $e->die_event;
1517 # ----------------------------------------------------------------------
1518 __PACKAGE__->register_method(
1519 method => 'magic_fetch',
1520 api_name => 'open-ils.agent.fetch'
1523 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1526 my( $self, $conn, $auth, $args ) = @_;
1527 my $e = new_editor( authtoken => $auth );
1528 return $e->event unless $e->checkauth;
1530 my $hint = $$args{hint};
1531 my $id = $$args{id};
1533 # Is the call allowed to fetch this type of object?
1534 return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1536 # Find the class the implements the given hint
1537 my ($class) = grep {
1538 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1540 $class =~ s/Fieldmapper:://og;
1541 $class =~ s/::/_/og;
1542 my $method = "retrieve_$class";
1544 my $obj = $e->$method($id) or return $e->event;
1547 # ----------------------------------------------------------------------
1550 __PACKAGE__->register_method(
1551 method => "fleshed_circ_retrieve",
1553 api_name => "open-ils.circ.fleshed.retrieve",);
1555 sub fleshed_circ_retrieve {
1556 my( $self, $client, $id ) = @_;
1557 my $e = new_editor();
1558 my $circ = $e->retrieve_action_circulation(
1564 circ => [ qw/ target_copy / ],
1565 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number parts / ],
1566 ascecm => [ qw/ stat_cat stat_cat_entry / ],
1567 acn => [ qw/ record / ],
1571 ) or return $e->event;
1573 my $copy = $circ->target_copy;
1574 my $vol = $copy->call_number;
1575 my $rec = $circ->target_copy->call_number->record;
1577 $vol->record($rec->id);
1578 $copy->call_number($vol->id);
1579 $circ->target_copy($copy->id);
1583 if( $rec->id == OILS_PRECAT_RECORD ) {
1587 $mvr = $U->record_to_mvr($rec);
1588 $rec->marc(''); # drop the bulky marc data
1602 __PACKAGE__->register_method(
1603 method => "test_batch_circ_events",
1604 api_name => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1607 # method for testing the behavior of a given event definition
1608 sub test_batch_circ_events {
1609 my($self, $conn, $auth, $event_def, $barcode) = @_;
1611 my $e = new_editor(authtoken => $auth);
1612 return $e->event unless $e->checkauth;
1613 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1615 my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1616 or return $e->event;
1618 my $circ = $e->search_action_circulation(
1619 {target_copy => $copy->id, checkin_time => undef})->[0]
1620 or return $e->event;
1622 return undef unless $circ;
1624 return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1628 __PACKAGE__->register_method(
1629 method => "fire_circ_events",
1630 api_name => "open-ils.circ.fire_circ_trigger_events",
1632 General event def runner for circ objects. If no event def ID
1633 is provided, the hook will be used to find the best event_def
1634 match based on the context org unit
1638 __PACKAGE__->register_method(
1639 method => "fire_circ_events",
1640 api_name => "open-ils.circ.fire_hold_trigger_events",
1642 General event def runner for hold objects. If no event def ID
1643 is provided, the hook will be used to find the best event_def
1644 match based on the context org unit
1648 __PACKAGE__->register_method(
1649 method => "fire_circ_events",
1650 api_name => "open-ils.circ.fire_user_trigger_events",
1652 General event def runner for user objects. If no event def ID
1653 is provided, the hook will be used to find the best event_def
1654 match based on the context org unit
1659 sub fire_circ_events {
1660 my($self, $conn, $auth, $org_id, $event_def, $hook, $granularity, $target_ids, $user_data) = @_;
1662 my $e = new_editor(authtoken => $auth, xact => 1);
1663 return $e->event unless $e->checkauth;
1667 if($self->api_name =~ /hold/) {
1668 return $e->event unless $e->allowed('VIEW_HOLD', $org_id);
1669 $targets = $e->batch_retrieve_action_hold_request($target_ids);
1670 } elsif($self->api_name =~ /user/) {
1671 return $e->event unless $e->allowed('VIEW_USER', $org_id);
1672 $targets = $e->batch_retrieve_actor_user($target_ids);
1674 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $org_id);
1675 $targets = $e->batch_retrieve_action_circulation($target_ids);
1677 $e->rollback; # FIXME using transaction because of pgpool/slony setups, but not
1678 # simply making this method authoritative because of weirdness
1679 # with transaction handling in A/T code that causes rollback
1680 # failure down the line if handling many targets
1682 return undef unless @$targets;
1683 return $U->fire_object_event($event_def, $hook, $targets, $org_id, $granularity, $user_data);
1686 __PACKAGE__->register_method(
1687 method => "user_payments_list",
1688 api_name => "open-ils.circ.user_payments.filtered.batch",
1691 desc => q/Returns a fleshed, date-limited set of all payments a user
1692 has made. By default, ordered by payment date. Optionally
1693 ordered by other columns in the top-level "mp" object/,
1695 {desc => 'Authentication token', type => 'string'},
1696 {desc => 'User ID', type => 'number'},
1697 {desc => 'Order by column(s), optional. Array of "mp" class columns', type => 'array'}
1699 return => {desc => q/List of "mp" objects, fleshed with the billable transaction
1700 and the related fully-realized payment object (e.g money.cash_payment)/}
1704 sub user_payments_list {
1705 my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1707 my $e = new_editor(authtoken => $auth);
1708 return $e->event unless $e->checkauth;
1710 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1711 return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1713 $order_by ||= ['payment_ts'];
1715 # all payments by user, between start_date and end_date
1716 my $payments = $e->json_query({
1717 select => {mp => ['id']},
1721 fkey => 'xact', field => 'id'}
1725 '+mbt' => {usr => $user_id},
1726 '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1728 order_by => {mp => $order_by}
1731 for my $payment_id (@$payments) {
1732 my $payment = $e->retrieve_money_payment([
1740 'credit_card_payment',
1755 $conn->respond($payment);
1762 __PACKAGE__->register_method(
1763 method => "retrieve_circ_chain",
1764 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ",
1767 desc => q/Given a circulation, this returns all circulation objects
1768 that are part of the same chain of renewals./,
1770 {desc => 'Authentication token', type => 'string'},
1771 {desc => 'Circ ID', type => 'number'},
1773 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1777 __PACKAGE__->register_method(
1778 method => "retrieve_circ_chain",
1779 api_name => "open-ils.circ.renewal_chain.retrieve_by_circ.summary",
1781 desc => q/Given a circulation, this returns a summary of the circulation objects
1782 that are part of the same chain of renewals./,
1784 {desc => 'Authentication token', type => 'string'},
1785 {desc => 'Circ ID', type => 'number'},
1787 return => {desc => q/Circulation Chain Summary/}
1791 sub retrieve_circ_chain {
1792 my($self, $conn, $auth, $circ_id) = @_;
1794 my $e = new_editor(authtoken => $auth);
1795 return $e->event unless $e->checkauth;
1796 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1798 if($self->api_name =~ /summary/) {
1799 return $U->create_circ_chain_summary($e, $circ_id);
1803 my $chain = $e->json_query({from => ['action.all_circ_chain', $circ_id]});
1805 for my $circ_info (@$chain) {
1806 my $circ = Fieldmapper::action::all_circulation->new;
1807 $circ->$_($circ_info->{$_}) for keys %$circ_info;
1808 $conn->respond($circ);
1815 __PACKAGE__->register_method(
1816 method => "retrieve_prev_circ_chain",
1817 api_name => "open-ils.circ.prev_renewal_chain.retrieve_by_circ",
1820 desc => q/Given a circulation, this returns all circulation objects
1821 that are part of the previous chain of renewals./,
1823 {desc => 'Authentication token', type => 'string'},
1824 {desc => 'Circ ID', type => 'number'},
1826 return => {desc => q/List of circ objects, orderd by oldest circ first/}
1830 __PACKAGE__->register_method(
1831 method => "retrieve_prev_circ_chain",
1832 api_name => "open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary",
1834 desc => q/Given a circulation, this returns a summary of the circulation objects
1835 that are part of the previous chain of renewals./,
1837 {desc => 'Authentication token', type => 'string'},
1838 {desc => 'Circ ID', type => 'number'},
1840 return => {desc => q/Object containing Circulation Chain Summary and User Id/}
1844 sub retrieve_prev_circ_chain {
1845 my($self, $conn, $auth, $circ_id) = @_;
1847 my $e = new_editor(authtoken => $auth);
1848 return $e->event unless $e->checkauth;
1849 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1852 $e->json_query({from => ['action.all_circ_chain', $circ_id]})->[0];
1854 my $prev_circ = $e->search_action_all_circulation([
1855 { target_copy => $first_circ->{target_copy},
1856 xact_start => {'<' => $first_circ->{xact_start}}
1865 order_by => { combcirc => 'xact_start desc' },
1870 return undef unless $prev_circ;
1872 my $chain_usr = $prev_circ->usr; # note: may be undef
1874 if ($self->api_name =~ /summary/) {
1875 my $sum = $e->json_query({
1877 'action.summarize_all_circ_chain',
1882 my $summary = Fieldmapper::action::circ_chain_summary->new;
1883 $summary->$_($sum->{$_}) for keys %$sum;
1885 return {summary => $summary, usr => $chain_usr};
1889 my $chain = $e->json_query(
1890 {from => ['action.all_circ_chain', $prev_circ->id]});
1892 for my $circ_info (@$chain) {
1893 my $circ = Fieldmapper::action::all_circulation->new;
1894 $circ->$_($circ_info->{$_}) for keys %$circ_info;
1895 $conn->respond($circ);
1902 __PACKAGE__->register_method(
1903 method => "get_copy_due_date",
1904 api_name => "open-ils.circ.copy.due_date.retrieve",
1907 Given a copy ID, returns the due date for the copy if it's
1908 currently circulating. Otherwise, returns null. Note, this is a public
1909 method requiring no authentication. Only the due date is exposed.
1912 {desc => 'Copy ID', type => 'number'}
1914 return => {desc => q/
1915 Due date (ISO date stamp) if the copy is circulating, null otherwise.
1920 sub get_copy_due_date {
1921 my($self, $conn, $copy_id) = @_;
1922 my $e = new_editor();
1924 my $circ = $e->json_query({
1925 select => {circ => ['due_date']},
1928 target_copy => $copy_id,
1929 checkin_time => undef,
1931 {stop_fines => ["MAXFINES","LONGOVERDUE"]},
1932 {stop_fines => undef}
1936 })->[0] or return undef;
1938 return $circ->{due_date};
1945 # {"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}}