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;
17 my $apputils = "OpenILS::Application::AppUtils";
19 use OpenSRF::Utils qw/:datetime/;
20 use OpenILS::Utils::ModsParser;
22 use OpenSRF::EX qw(:try);
23 use OpenSRF::Utils::Logger qw(:logger);
24 use OpenILS::Utils::Fieldmapper;
25 use OpenILS::Utils::Editor q/:funcs/;
26 use OpenILS::Utils::CStoreEditor q/:funcs/;
29 # ------------------------------------------------------------------------
30 # Top level Circ package;
31 # ------------------------------------------------------------------------
35 OpenILS::Application::Circ::Circulate->initialize();
39 __PACKAGE__->register_method(
40 method => 'retrieve_circ',
41 api_name => 'open-ils.circ.retrieve',
43 Retrieve a circ object by id
44 @param authtoken Login session key
45 @pararm circid The id of the circ object
49 my( $s, $c, $a, $i ) = @_;
50 my $e = new_editor(authtoken => $a);
51 return $e->event unless $e->checkauth;
52 my $circ = $e->retrieve_action_circulation($i) or return $e->event;
53 if( $e->requestor->id ne $circ->usr ) {
54 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
60 # ------------------------------------------------------------------------
61 # Returns an array of {circ, record} hashes checked out by the user.
62 # ------------------------------------------------------------------------
63 __PACKAGE__->register_method(
64 method => "checkouts_by_user",
65 api_name => "open-ils.circ.actor.user.checked_out",
67 Returns a list of open circulations as a pile of objects. each object
68 contains the relevant copy, circ, and record
71 sub checkouts_by_user {
72 my( $self, $client, $user_session, $user_id ) = @_;
74 my( $requestor, $target, $copy, $record, $evt );
76 ( $requestor, $target, $evt ) =
77 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
80 my $circs = $apputils->simplereq(
82 "open-ils.cstore.direct.action.open_circulation.search.atomic",
83 { usr => $target->id, checkin_time => undef } );
84 # { usr => $target->id } );
87 for my $circ (@$circs) {
89 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
92 $logger->debug("Retrieving record for copy " . $circ->target_copy);
94 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
97 my $mods = $apputils->record_to_mvr($record);
99 push( @results, { copy => $copy, circ => $circ, record => $mods } );
108 __PACKAGE__->register_method(
109 method => "checkouts_by_user_slim",
110 api_name => "open-ils.circ.actor.user.checked_out.slim",
111 NOTES => <<" NOTES");
112 Returns a list of open circulation objects
116 sub checkouts_by_user_slim {
117 my( $self, $client, $user_session, $user_id ) = @_;
119 my( $requestor, $target, $copy, $record, $evt );
121 ( $requestor, $target, $evt ) =
122 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
125 $logger->debug( 'User ' . $requestor->id .
126 " retrieving checked out items for user " . $target->id );
128 # XXX Make the call correct..
129 return $apputils->simplereq(
131 "open-ils.cstore.direct.action.open_circulation.search.atomic",
132 { usr => $target->id, checkin_time => undef } );
133 # { usr => $target->id } );
137 __PACKAGE__->register_method(
138 method => "checkouts_by_user_opac",
139 api_name => "open-ils.circ.actor.user.checked_out.opac",);
142 sub checkouts_by_user_opac {
143 my( $self, $client, $auth, $user_id ) = @_;
145 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
146 return $e->event unless $e->checkauth;
147 $user_id ||= $e->requestor->id;
148 return $e->event unless
149 my $patron = $e->retrieve_actor_user($user_id);
152 my $search = {usr => $user_id, stop_fines => undef};
154 if( $user_id ne $e->requestor->id ) {
155 $data = $e->search_action_circulation(
156 $search, {checkperm=>1, permorg=>$patron->home_ou})
160 $data = $e->search_action_circulation($search);
167 __PACKAGE__->register_method(
168 method => "title_from_transaction",
169 api_name => "open-ils.circ.circ_transaction.find_title",
170 NOTES => <<" NOTES");
171 Returns a mods object for the title that is linked to from the
172 copy from the hold that created the given transaction
175 sub title_from_transaction {
176 my( $self, $client, $login_session, $transactionid ) = @_;
178 my( $user, $circ, $title, $evt );
180 ( $user, $evt ) = $apputils->checkses( $login_session );
183 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
186 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
189 return $apputils->record_to_mvr($title);
193 __PACKAGE__->register_method(
194 method => "set_circ_lost",
195 api_name => "open-ils.circ.circulation.set_lost",
196 NOTES => <<" NOTES");
197 Params are login, barcode
198 login must have SET_CIRC_LOST perms
199 Sets a circulation to lost
202 __PACKAGE__->register_method(
203 method => "set_circ_lost",
204 api_name => "open-ils.circ.circulation.set_claims_returned",
205 NOTES => <<" NOTES");
206 Params are login, barcode
207 login must have SET_CIRC_MISSING perms
208 Sets a circulation to lost
212 my( $self, $client, $login, $args ) = @_;
213 my( $user, $circ, $copy, $evt );
215 my $barcode = $$args{barcode};
216 my $backdate = $$args{backdate};
218 ( $user, $evt ) = $U->checkses($login);
221 # Grab the related copy
222 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
225 my $isclaims = $self->api_name =~ /claims_returned/;
226 my $islost = $self->api_name =~ /lost/;
227 my $session = $U->start_db_session();
229 # grab the circulation
230 ( $circ ) = $U->fetch_open_circulation( $copy->id );
231 return 1 unless $circ;
234 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
239 $evt = _set_circ_claims_returned(
240 $user, $circ, $session, $backdate );
243 # $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
244 # return $evt if $evt;
245 # $circ->stop_fines("CLAIMSRETURNED");
247 # $logger->activity("user ".$user->id." marking circ". $circ->id. " as claims returned");
249 # # allow the caller to backdate the circulation and void any fines
250 # # that occurred after the backdate
252 # OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
253 # $backdate, $circ, $user, $session );
257 # ($patron, $evt) = $U->fetch_user($circ->usr);
258 # return $evt if $evt;
259 # $patron->claims_returned_count(
260 # $patron->claims_returned_count + 1 );
262 # my $stat = $U->storagereq(
263 # 'open-ils.storage.direct.actor.user.update', $patron );
264 # return $U->DB_UPDATE_FAILED($patron) unless $stat;
268 $circ->stop_fines_time('now') unless $circ->stop_fines_time('now');
269 my $s = $session->request(
270 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
272 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
273 $U->commit_db_session($session);
279 my( $copy, $circ, $reqr, $session ) = @_;
281 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
284 $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
285 " lost for circ ". $circ->id. " and checking for necessary charges");
287 my $newstat = $U->copy_status_from_name('lost');
288 if( $copy->status ne $newstat->id ) {
290 $copy->status($newstat);
294 session => $session);
297 # if the copy has a price defined and/or a processing fee, bill the patron
299 my $copy_price = $copy->price || 0;
301 # If the copy has a price configured, charge said price to the user
302 if($copy_price and $copy_price > 0) {
303 $evt = _make_bill($session, $copy_price, 'Lost Materials', $circ->id);
307 # if the location that owns the copy has a processing fee, charge the user
308 my $owner = $U->fetch_copy_owner($copy->id);
309 $logger->info("circ fetching org settings for $owner to determine processing fee");
311 my $settings = $U->simplereq(
312 'open-ils.actor', 'open-ils.actor.org_unit.settings.retrieve', $owner );
313 my $fee = $settings->{'circ.lost_materials_processing_fee'} || 0;
316 $evt = _make_bill($session, $fee, 'Lost Materials Processing Fee', $circ->id);
320 $circ->stop_fines("LOST");
325 my( $session, $amount, $type, $xactid ) = @_;
327 $logger->activity("The system is charging $amount ".
328 " [$type] for lost materials on circulation $xactid");
330 my $bill = Fieldmapper::money::billing->new;
332 $bill->xact($xactid);
333 $bill->amount($amount);
334 $bill->billing_type($type); # - XXX these strings should be configurable some day
335 $bill->note('SYSTEM GENERATED');
337 my $id = $session->request(
338 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
340 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
344 sub _set_circ_claims_returned {
345 my( $reqr, $circ, $session, $backdate ) = @_;
347 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
349 $circ->stop_fines("CLAIMSRETURNED");
351 $logger->activity("user ".$reqr->id.
352 " marking circ". $circ->id. " as claims returned");
354 # allow the caller to backdate the circulation and void any fines
355 # that occurred after the backdate
357 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
358 $backdate, $circ, $reqr, $session );
366 __PACKAGE__->register_method (
367 method => 'set_circ_due_date',
368 api_name => 'open-ils.circ.circulation.due_date.update',
370 Updates the due_date on the given circ
372 @param circid The id of the circ to update
373 @param date The timestamp of the new due date
377 sub set_circ_due_date {
378 my( $s, $c, $authtoken, $circid, $date ) = @_;
379 my ($circ, $evt) = $U->fetch_circulation($circid);
383 ($reqr, $evt) = $U->checkses($authtoken);
386 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
389 $date = clense_ISO8601($date);
390 $logger->activity("user ".$reqr->id.
391 " updating due_date on circ $circid: $date");
393 $circ->due_date($date);
394 my $stat = $U->storagereq(
395 'open-ils.storage.direct.action.circulation.update', $circ);
396 return $U->DB_UPDATE_FAILED unless defined $stat;
401 __PACKAGE__->register_method(
402 method => "create_in_house_use",
403 api_name => 'open-ils.circ.in_house_use.create',
405 Creates an in-house use action.
406 @param $authtoken The login session key
407 @param params A hash of params including
408 'location' The org unit id where the in-house use occurs
409 'copyid' The copy in question
410 'count' The number of in-house uses to apply to this copy
411 @return An array of id's representing the id's of the newly created
412 in-house use objects or an event on an error
415 sub create_in_house_use {
416 my( $self, $client, $authtoken, $params ) = @_;
418 my( $staff, $evt, $copy );
419 my $org = $params->{location};
420 my $copyid = $params->{copyid};
421 my $count = $params->{count} || 1;
422 my $use_time = $params->{use_time} || 'now';
425 my $barcode = $params->{barcode};
426 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
431 ($staff, $evt) = $U->checkses($authtoken);
434 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
437 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
440 $logger->activity("User " . $staff->id .
441 " creating $count in-house use(s) for copy $copyid at location $org");
443 if( $use_time ne 'now' ) {
444 $use_time = clense_ISO8601($use_time);
445 $logger->debug("in_house_use setting use time to $use_time");
450 my $ihu = Fieldmapper::action::in_house_use->new;
453 $ihu->staff($staff->id);
454 $ihu->org_unit($org);
455 $ihu->use_time($use_time);
457 my $id = $U->simplereq(
459 'open-ils.storage.direct.action.in_house_use.create', $ihu );
461 return $U->DB_UPDATE_FAILED($ihu) unless $id;
470 __PACKAGE__->register_method(
471 method => "view_circs",
472 api_name => "open-ils.circ.copy_checkout_history.retrieve",
474 Retrieves the last X circs for a given copy
475 @param authtoken The login session key
476 @param copyid The copy to check
477 @param count How far to go back in the item history
478 @return An array of circ ids
484 my( $self, $client, $authtoken, $copyid, $count ) = @_;
486 my( $requestor, $evt ) = $U->checksesperm(
487 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
490 return [] unless $count;
492 my $circs = $U->cstorereq(
493 'open-ils.cstore.direct.action.circulation.search.atomic',
495 target_copy => $copyid,
499 order_by => { ac => "xact_start DESC" }
507 __PACKAGE__->register_method(
508 method => "circ_count",
509 api_name => "open-ils.circ.circulation.count",
511 Returns the number of times the item has circulated
512 @param copyid The copy to check
516 my( $self, $client, $copyid, $range ) = @_;
517 my $e = OpenILS::Utils::Editor->new;
518 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
523 __PACKAGE__->register_method(
524 method => 'fetch_notes',
525 api_name => 'open-ils.circ.copy_note.retrieve.all',
527 Returns an array of copy note objects.
528 @param args A named hash of parameters including:
529 authtoken : Required if viewing non-public notes
530 itemid : The id of the item whose notes we want to retrieve
531 pub : True if all the caller wants are public notes
532 @return An array of note objects
535 __PACKAGE__->register_method(
536 method => 'fetch_notes',
537 api_name => 'open-ils.circ.call_number_note.retrieve.all',
538 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
540 __PACKAGE__->register_method(
541 method => 'fetch_notes',
542 api_name => 'open-ils.circ.title_note.retrieve.all',
543 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
546 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
548 my( $self, $connection, $args ) = @_;
550 my $id = $$args{itemid};
551 my $authtoken = $$args{authtoken};
554 if( $self->api_name =~ /copy/ ) {
556 return $U->cstorereq(
557 'open-ils.cstore.direct.asset.copy_note.search.atomic',
558 { owning_copy => $id, pub => 't' } );
560 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
562 return $U->cstorereq(
563 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
566 } elsif( $self->api_name =~ /call_number/ ) {
568 return $U->cstorereq(
569 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
570 { call_number => $id, pub => 't' } );
572 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
574 return $U->cstorereq(
575 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
578 } elsif( $self->api_name =~ /title/ ) {
580 return $U->cstorereq(
581 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
582 { record => $id, pub => 't' } );
584 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
586 return $U->cstorereq(
587 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
594 __PACKAGE__->register_method(
595 method => 'has_notes',
596 api_name => 'open-ils.circ.copy.has_notes');
597 __PACKAGE__->register_method(
598 method => 'has_notes',
599 api_name => 'open-ils.circ.call_number.has_notes');
600 __PACKAGE__->register_method(
601 method => 'has_notes',
602 api_name => 'open-ils.circ.title.has_notes');
606 my( $self, $conn, $authtoken, $id ) = @_;
607 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
608 return $editor->event unless $editor->checkauth;
610 my $n = $editor->search_asset_copy_note(
611 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
613 $n = $editor->search_asset_call_number_note(
614 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
616 $n = $editor->search_biblio_record_note(
617 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
622 __PACKAGE__->register_method(
623 method => 'create_copy_note',
624 api_name => 'open-ils.circ.copy_note.create',
626 Creates a new copy note
627 @param authtoken The login session key
628 @param note The note object to create
629 @return The id of the new note object
632 sub create_copy_note {
633 my( $self, $connection, $authtoken, $note ) = @_;
634 my( $cnowner, $requestor, $evt );
636 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
638 ($requestor, $evt) = $U->checkses($authtoken);
640 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
643 $note->create_date('now');
644 $note->creator($requestor->id);
645 $note->pub( ($note->pub) ? 't' : 'f' );
647 my $id = $U->storagereq(
648 'open-ils.storage.direct.asset.copy_note.create', $note );
649 return $U->DB_UPDATE_FAILED($note) unless $id;
651 $logger->activity("User ".$requestor->id." created a new copy ".
652 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
657 __PACKAGE__->register_method(
658 method => 'delete_copy_note',
659 api_name => 'open-ils.circ.copy_note.delete',
661 Deletes an existing copy note
662 @param authtoken The login session key
663 @param noteid The id of the note to delete
664 @return 1 on success - Event otherwise.
667 sub delete_copy_note {
668 my( $self, $conn, $authtoken, $noteid ) = @_;
669 my( $requestor, $note, $owner, $evt );
671 ($requestor, $evt) = $U->checkses($authtoken);
674 ($note, $evt) = $U->fetch_copy_note($noteid);
677 if( $note->creator ne $requestor->id ) {
678 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
680 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
684 my $stat = $U->storagereq(
685 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
686 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
688 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
693 __PACKAGE__->register_method(
694 method => 'age_hold_rules',
695 api_name => 'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
699 my( $self, $conn ) = @_;
700 return new_editor()->retrieve_all_config_rules_age_hold_protect();