1 package OpenILS::Application::Circ;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
5 use OpenILS::Application::Circ::Circulate;
6 use OpenILS::Application::Circ::Survey;
7 use OpenILS::Application::Circ::StatCat;
8 use OpenILS::Application::Circ::Holds;
9 use OpenILS::Application::Circ::Money;
10 use OpenILS::Application::Circ::NonCat;
11 use OpenILS::Application::Circ::CopyLocations;
14 use DateTime::Format::ISO8601;
16 use OpenILS::Application::AppUtils;
18 use OpenSRF::Utils qw/:datetime/;
19 use OpenILS::Utils::ModsParser;
21 use OpenSRF::EX qw(:try);
22 use OpenSRF::Utils::Logger qw(:logger);
23 use OpenILS::Utils::Fieldmapper;
24 use OpenILS::Utils::Editor q/:funcs/;
25 use OpenILS::Utils::CStoreEditor q/:funcs/;
26 use OpenILS::Const qw/:const/;
28 my $apputils = "OpenILS::Application::AppUtils";
32 # ------------------------------------------------------------------------
33 # Top level Circ package;
34 # ------------------------------------------------------------------------
38 OpenILS::Application::Circ::Circulate->initialize();
42 __PACKAGE__->register_method(
43 method => 'retrieve_circ',
44 api_name => 'open-ils.circ.retrieve',
46 Retrieve a circ object by id
47 @param authtoken Login session key
48 @pararm circid The id of the circ object
52 my( $s, $c, $a, $i ) = @_;
53 my $e = new_editor(authtoken => $a);
54 return $e->event unless $e->checkauth;
55 my $circ = $e->retrieve_action_circulation($i) or return $e->event;
56 if( $e->requestor->id ne $circ->usr ) {
57 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
63 # ------------------------------------------------------------------------
64 # Returns an array of {circ, record} hashes checked out by the user.
65 # ------------------------------------------------------------------------
66 __PACKAGE__->register_method(
67 method => "checkouts_by_user",
68 api_name => "open-ils.circ.actor.user.checked_out",
70 Returns a list of open circulations as a pile of objects. each object
71 contains the relevant copy, circ, and record
74 sub checkouts_by_user {
75 my( $self, $client, $user_session, $user_id ) = @_;
77 my( $requestor, $target, $copy, $record, $evt );
79 ( $requestor, $target, $evt ) =
80 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
83 my $circs = $apputils->simplereq(
85 "open-ils.cstore.direct.action.open_circulation.search.atomic",
86 { usr => $target->id, checkin_time => undef } );
87 # { usr => $target->id } );
90 for my $circ (@$circs) {
92 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
95 $logger->debug("Retrieving record for copy " . $circ->target_copy);
97 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
100 my $mods = $apputils->record_to_mvr($record);
102 push( @results, { copy => $copy, circ => $circ, record => $mods } );
111 __PACKAGE__->register_method(
112 method => "checkouts_by_user_slim",
113 api_name => "open-ils.circ.actor.user.checked_out.slim",
114 NOTES => <<" NOTES");
115 Returns a list of open circulation objects
119 sub checkouts_by_user_slim {
120 my( $self, $client, $user_session, $user_id ) = @_;
122 my( $requestor, $target, $copy, $record, $evt );
124 ( $requestor, $target, $evt ) =
125 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
128 $logger->debug( 'User ' . $requestor->id .
129 " retrieving checked out items for user " . $target->id );
131 # XXX Make the call correct..
132 return $apputils->simplereq(
134 "open-ils.cstore.direct.action.open_circulation.search.atomic",
135 { usr => $target->id, checkin_time => undef } );
136 # { usr => $target->id } );
140 __PACKAGE__->register_method(
141 method => "checkouts_by_user_opac",
142 api_name => "open-ils.circ.actor.user.checked_out.opac",);
145 sub checkouts_by_user_opac {
146 my( $self, $client, $auth, $user_id ) = @_;
148 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
149 return $e->event unless $e->checkauth;
150 $user_id ||= $e->requestor->id;
151 return $e->event unless
152 my $patron = $e->retrieve_actor_user($user_id);
155 my $search = {usr => $user_id, stop_fines => undef};
157 if( $user_id ne $e->requestor->id ) {
158 $data = $e->search_action_circulation(
159 $search, {checkperm=>1, permorg=>$patron->home_ou})
163 $data = $e->search_action_circulation($search);
170 __PACKAGE__->register_method(
171 method => "title_from_transaction",
172 api_name => "open-ils.circ.circ_transaction.find_title",
173 NOTES => <<" NOTES");
174 Returns a mods object for the title that is linked to from the
175 copy from the hold that created the given transaction
178 sub title_from_transaction {
179 my( $self, $client, $login_session, $transactionid ) = @_;
181 my( $user, $circ, $title, $evt );
183 ( $user, $evt ) = $apputils->checkses( $login_session );
186 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
189 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
192 return $apputils->record_to_mvr($title);
196 __PACKAGE__->register_method(
197 method => "set_circ_lost",
198 api_name => "open-ils.circ.circulation.set_lost",
199 NOTES => <<" NOTES");
200 Params are login, barcode
201 login must have SET_CIRC_LOST perms
202 Sets a circulation to lost
205 __PACKAGE__->register_method(
206 method => "set_circ_lost",
207 api_name => "open-ils.circ.circulation.set_claims_returned",
208 NOTES => <<" NOTES");
209 Params are login, barcode
210 login must have SET_CIRC_MISSING perms
211 Sets a circulation to lost
215 my( $self, $client, $login, $args ) = @_;
216 my( $user, $circ, $copy, $evt );
218 my $barcode = $$args{barcode};
219 my $backdate = $$args{backdate};
221 ( $user, $evt ) = $U->checkses($login);
224 # Grab the related copy
225 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
228 my $isclaims = $self->api_name =~ /claims_returned/;
229 my $islost = $self->api_name =~ /lost/;
230 my $session = $U->start_db_session();
232 # grab the circulation
233 ( $circ ) = $U->fetch_open_circulation( $copy->id );
234 return 1 unless $circ;
237 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
242 $evt = _set_circ_claims_returned(
243 $user, $circ, $session, $backdate );
248 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
249 my $s = $session->request(
250 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
252 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
253 $U->commit_db_session($session);
259 my( $copy, $circ, $reqr, $session ) = @_;
261 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
264 $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
265 " lost for circ ". $circ->id. " and checking for necessary charges");
267 if( $copy->status ne OILS_COPY_STATUS_LOST ) {
268 $copy->status(OILS_COPY_STATUS_LOST);
272 session => $session);
275 # if the copy has a price defined and/or a processing fee, bill the patron
277 my $copy_price = $copy->price || 0;
279 $logger->debug("lost copy has a price of $copy_price");
281 # If the copy has a price configured, charge said price to the user
282 if($copy_price and $copy_price > 0) {
283 $evt = _make_bill($session, $copy_price, 'Lost Materials', $circ->id);
287 # if the location that owns the copy has a processing fee, charge the user
288 my $owner = $U->fetch_copy_owner($copy->id);
289 $logger->info("circ fetching org settings for $owner to determine processing fee");
291 my $settings = $U->simplereq(
292 'open-ils.actor', 'open-ils.actor.org_unit.settings.retrieve', $owner );
293 my $fee = $settings->{'circ.lost_materials_processing_fee'} || 0;
296 $evt = _make_bill($session, $fee, 'Lost Materials Processing Fee', $circ->id);
300 $circ->stop_fines(OILS_STOP_FINES_LOST);
305 my( $session, $amount, $type, $xactid ) = @_;
307 $logger->activity("The system is charging $amount ".
308 " [$type] for lost materials on circulation $xactid");
310 my $bill = Fieldmapper::money::billing->new;
312 $bill->xact($xactid);
313 $bill->amount($amount);
314 $bill->billing_type($type); # - XXX these strings should be configurable some day
315 $bill->note('SYSTEM GENERATED');
317 my $id = $session->request(
318 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
320 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
324 sub _set_circ_claims_returned {
325 my( $reqr, $circ, $session, $backdate ) = @_;
327 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
329 $circ->stop_fines("CLAIMSRETURNED");
331 $logger->activity("user ".$reqr->id.
332 " marking circ". $circ->id. " as claims returned");
334 # allow the caller to backdate the circulation and void any fines
335 # that occurred after the backdate
337 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
338 $backdate, $circ, $reqr, $session );
346 __PACKAGE__->register_method (
347 method => 'set_circ_due_date',
348 api_name => 'open-ils.circ.circulation.due_date.update',
350 Updates the due_date on the given circ
352 @param circid The id of the circ to update
353 @param date The timestamp of the new due date
357 sub set_circ_due_date {
358 my( $s, $c, $authtoken, $circid, $date ) = @_;
359 my ($circ, $evt) = $U->fetch_circulation($circid);
363 ($reqr, $evt) = $U->checkses($authtoken);
366 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
369 $date = clense_ISO8601($date);
370 $logger->activity("user ".$reqr->id.
371 " updating due_date on circ $circid: $date");
373 $circ->due_date($date);
374 my $stat = $U->storagereq(
375 'open-ils.storage.direct.action.circulation.update', $circ);
376 return $U->DB_UPDATE_FAILED unless defined $stat;
381 __PACKAGE__->register_method(
382 method => "create_in_house_use",
383 api_name => 'open-ils.circ.in_house_use.create',
385 Creates an in-house use action.
386 @param $authtoken The login session key
387 @param params A hash of params including
388 'location' The org unit id where the in-house use occurs
389 'copyid' The copy in question
390 'count' The number of in-house uses to apply to this copy
391 @return An array of id's representing the id's of the newly created
392 in-house use objects or an event on an error
395 sub create_in_house_use {
396 my( $self, $client, $authtoken, $params ) = @_;
398 my( $staff, $evt, $copy );
399 my $org = $params->{location};
400 my $copyid = $params->{copyid};
401 my $count = $params->{count} || 1;
402 my $use_time = $params->{use_time} || 'now';
405 my $barcode = $params->{barcode};
406 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
411 ($staff, $evt) = $U->checkses($authtoken);
414 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
417 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
420 $logger->activity("User " . $staff->id .
421 " creating $count in-house use(s) for copy $copyid at location $org");
423 if( $use_time ne 'now' ) {
424 $use_time = clense_ISO8601($use_time);
425 $logger->debug("in_house_use setting use time to $use_time");
430 my $ihu = Fieldmapper::action::in_house_use->new;
433 $ihu->staff($staff->id);
434 $ihu->org_unit($org);
435 $ihu->use_time($use_time);
437 my $id = $U->simplereq(
439 'open-ils.storage.direct.action.in_house_use.create', $ihu );
441 return $U->DB_UPDATE_FAILED($ihu) unless $id;
450 __PACKAGE__->register_method(
451 method => "view_circs",
452 api_name => "open-ils.circ.copy_checkout_history.retrieve",
454 Retrieves the last X circs for a given copy
455 @param authtoken The login session key
456 @param copyid The copy to check
457 @param count How far to go back in the item history
458 @return An array of circ ids
464 my( $self, $client, $authtoken, $copyid, $count ) = @_;
466 my( $requestor, $evt ) = $U->checksesperm(
467 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
470 return [] unless $count;
472 my $circs = $U->cstorereq(
473 'open-ils.cstore.direct.action.circulation.search.atomic',
475 target_copy => $copyid,
479 order_by => { ac => "xact_start DESC" }
487 __PACKAGE__->register_method(
488 method => "circ_count",
489 api_name => "open-ils.circ.circulation.count",
491 Returns the number of times the item has circulated
492 @param copyid The copy to check
496 my( $self, $client, $copyid, $range ) = @_;
497 my $e = OpenILS::Utils::Editor->new;
498 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
503 __PACKAGE__->register_method(
504 method => 'fetch_notes',
505 api_name => 'open-ils.circ.copy_note.retrieve.all',
507 Returns an array of copy note objects.
508 @param args A named hash of parameters including:
509 authtoken : Required if viewing non-public notes
510 itemid : The id of the item whose notes we want to retrieve
511 pub : True if all the caller wants are public notes
512 @return An array of note objects
515 __PACKAGE__->register_method(
516 method => 'fetch_notes',
517 api_name => 'open-ils.circ.call_number_note.retrieve.all',
518 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
520 __PACKAGE__->register_method(
521 method => 'fetch_notes',
522 api_name => 'open-ils.circ.title_note.retrieve.all',
523 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
526 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
528 my( $self, $connection, $args ) = @_;
530 my $id = $$args{itemid};
531 my $authtoken = $$args{authtoken};
534 if( $self->api_name =~ /copy/ ) {
536 return $U->cstorereq(
537 'open-ils.cstore.direct.asset.copy_note.search.atomic',
538 { owning_copy => $id, pub => 't' } );
540 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
542 return $U->cstorereq(
543 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
546 } elsif( $self->api_name =~ /call_number/ ) {
548 return $U->cstorereq(
549 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
550 { call_number => $id, pub => 't' } );
552 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
554 return $U->cstorereq(
555 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
558 } elsif( $self->api_name =~ /title/ ) {
560 return $U->cstorereq(
561 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
562 { record => $id, pub => 't' } );
564 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
566 return $U->cstorereq(
567 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
574 __PACKAGE__->register_method(
575 method => 'has_notes',
576 api_name => 'open-ils.circ.copy.has_notes');
577 __PACKAGE__->register_method(
578 method => 'has_notes',
579 api_name => 'open-ils.circ.call_number.has_notes');
580 __PACKAGE__->register_method(
581 method => 'has_notes',
582 api_name => 'open-ils.circ.title.has_notes');
586 my( $self, $conn, $authtoken, $id ) = @_;
587 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
588 return $editor->event unless $editor->checkauth;
590 my $n = $editor->search_asset_copy_note(
591 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
593 $n = $editor->search_asset_call_number_note(
594 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
596 $n = $editor->search_biblio_record_note(
597 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
602 __PACKAGE__->register_method(
603 method => 'create_copy_note',
604 api_name => 'open-ils.circ.copy_note.create',
606 Creates a new copy note
607 @param authtoken The login session key
608 @param note The note object to create
609 @return The id of the new note object
612 sub create_copy_note {
613 my( $self, $connection, $authtoken, $note ) = @_;
614 my( $cnowner, $requestor, $evt );
616 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
618 ($requestor, $evt) = $U->checkses($authtoken);
620 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
623 $note->create_date('now');
624 $note->creator($requestor->id);
625 $note->pub( ($note->pub) ? 't' : 'f' );
627 my $id = $U->storagereq(
628 'open-ils.storage.direct.asset.copy_note.create', $note );
629 return $U->DB_UPDATE_FAILED($note) unless $id;
631 $logger->activity("User ".$requestor->id." created a new copy ".
632 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
637 __PACKAGE__->register_method(
638 method => 'delete_copy_note',
639 api_name => 'open-ils.circ.copy_note.delete',
641 Deletes an existing copy note
642 @param authtoken The login session key
643 @param noteid The id of the note to delete
644 @return 1 on success - Event otherwise.
647 sub delete_copy_note {
648 my( $self, $conn, $authtoken, $noteid ) = @_;
649 my( $requestor, $note, $owner, $evt );
651 ($requestor, $evt) = $U->checkses($authtoken);
654 ($note, $evt) = $U->fetch_copy_note($noteid);
657 if( $note->creator ne $requestor->id ) {
658 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
660 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
664 my $stat = $U->storagereq(
665 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
666 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
668 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
673 __PACKAGE__->register_method(
674 method => 'age_hold_rules',
675 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
679 my( $self, $conn ) = @_;
680 return new_editor()->retrieve_all_config_rules_age_hold_protect();