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/;
28 # ------------------------------------------------------------------------
29 # Top level Circ package;
30 # ------------------------------------------------------------------------
34 OpenILS::Application::Circ::Circulate->initialize();
38 __PACKAGE__->register_method(
39 method => 'retrieve_circ',
40 api_name => 'open-ils.circ.retrieve',
42 Retrieve a circ object by id
43 @param authtoken Login session key
44 @pararm circid The id of the circ object
48 my( $s, $c, $a, $i ) = @_;
49 my $e = new_editor(authtoken => $a);
50 return $e->event unless $e->checkauth;
51 my $circ = $e->retrieve_action_circulation($i) or return $e->event;
52 if( $e->requestor->id ne $circ->usr ) {
53 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
59 # ------------------------------------------------------------------------
60 # Returns an array of {circ, record} hashes checked out by the user.
61 # ------------------------------------------------------------------------
62 __PACKAGE__->register_method(
63 method => "checkouts_by_user",
64 api_name => "open-ils.circ.actor.user.checked_out",
66 Returns a list of open circulations as a pile of objects. each object
67 contains the relevant copy, circ, and record
70 sub checkouts_by_user {
71 my( $self, $client, $user_session, $user_id ) = @_;
73 my( $requestor, $target, $copy, $record, $evt );
75 ( $requestor, $target, $evt ) =
76 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
79 my $circs = $apputils->simplereq(
81 "open-ils.cstore.direct.action.open_circulation.search.atomic",
82 { usr => $target->id, checkin_time => undef } );
83 # { usr => $target->id } );
86 for my $circ (@$circs) {
88 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
91 $logger->debug("Retrieving record for copy " . $circ->target_copy);
93 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
96 my $mods = $apputils->record_to_mvr($record);
98 push( @results, { copy => $copy, circ => $circ, record => $mods } );
107 __PACKAGE__->register_method(
108 method => "checkouts_by_user_slim",
109 api_name => "open-ils.circ.actor.user.checked_out.slim",
110 NOTES => <<" NOTES");
111 Returns a list of open circulation objects
115 sub checkouts_by_user_slim {
116 my( $self, $client, $user_session, $user_id ) = @_;
118 my( $requestor, $target, $copy, $record, $evt );
120 ( $requestor, $target, $evt ) =
121 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
124 $logger->debug( 'User ' . $requestor->id .
125 " retrieving checked out items for user " . $target->id );
127 # XXX Make the call correct..
128 return $apputils->simplereq(
130 "open-ils.cstore.direct.action.open_circulation.search.atomic",
131 { usr => $target->id, checkin_time => undef } );
132 # { usr => $target->id } );
136 __PACKAGE__->register_method(
137 method => "checkouts_by_user_opac",
138 api_name => "open-ils.circ.actor.user.checked_out.opac",);
141 sub checkouts_by_user_opac {
142 my( $self, $client, $auth, $user_id ) = @_;
144 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
145 return $e->event unless $e->checkauth;
146 $user_id ||= $e->requestor->id;
147 return $e->event unless
148 my $patron = $e->retrieve_actor_user($user_id);
151 my $search = {usr => $user_id, stop_fines => undef};
153 if( $user_id ne $e->requestor->id ) {
154 $data = $e->search_action_circulation(
155 $search, {checkperm=>1, permorg=>$patron->home_ou})
159 $data = $e->search_action_circulation($search);
166 __PACKAGE__->register_method(
167 method => "title_from_transaction",
168 api_name => "open-ils.circ.circ_transaction.find_title",
169 NOTES => <<" NOTES");
170 Returns a mods object for the title that is linked to from the
171 copy from the hold that created the given transaction
174 sub title_from_transaction {
175 my( $self, $client, $login_session, $transactionid ) = @_;
177 my( $user, $circ, $title, $evt );
179 ( $user, $evt ) = $apputils->checkses( $login_session );
182 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
185 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
188 return $apputils->record_to_mvr($title);
192 __PACKAGE__->register_method(
193 method => "set_circ_lost",
194 api_name => "open-ils.circ.circulation.set_lost",
195 NOTES => <<" NOTES");
196 Params are login, barcode
197 login must have SET_CIRC_LOST perms
198 Sets a circulation to lost
201 __PACKAGE__->register_method(
202 method => "set_circ_lost",
203 api_name => "open-ils.circ.circulation.set_claims_returned",
204 NOTES => <<" NOTES");
205 Params are login, barcode
206 login must have SET_CIRC_MISSING perms
207 Sets a circulation to lost
211 my( $self, $client, $login, $args ) = @_;
212 my( $user, $circ, $copy, $evt );
214 my $barcode = $$args{barcode};
215 my $backdate = $$args{backdate};
217 ( $user, $evt ) = $U->checkses($login);
220 # Grab the related copy
221 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
224 my $isclaims = $self->api_name =~ /claims_returned/;
225 my $islost = $self->api_name =~ /lost/;
226 my $session = $U->start_db_session();
228 # grab the circulation
229 ( $circ ) = $U->fetch_open_circulation( $copy->id );
230 return 1 unless $circ;
233 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
238 $evt = _set_circ_claims_returned(
239 $user, $circ, $session, $backdate );
242 # $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
243 # return $evt if $evt;
244 # $circ->stop_fines("CLAIMSRETURNED");
246 # $logger->activity("user ".$user->id." marking circ". $circ->id. " as claims returned");
248 # # allow the caller to backdate the circulation and void any fines
249 # # that occurred after the backdate
251 # OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
252 # $backdate, $circ, $user, $session );
256 # ($patron, $evt) = $U->fetch_user($circ->usr);
257 # return $evt if $evt;
258 # $patron->claims_returned_count(
259 # $patron->claims_returned_count + 1 );
261 # my $stat = $U->storagereq(
262 # 'open-ils.storage.direct.actor.user.update', $patron );
263 # return $U->DB_UPDATE_FAILED($patron) unless $stat;
267 $circ->stop_fines_time('now') unless $circ->stop_fines_time('now');
268 my $s = $session->request(
269 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
271 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
272 $U->commit_db_session($session);
278 my( $copy, $circ, $reqr, $session ) = @_;
280 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
283 $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
284 " lost for circ ". $circ->id. " and checking for necessary charges");
286 my $newstat = $U->copy_status_from_name('lost');
287 if( $copy->status ne $newstat->id ) {
289 $copy->status($newstat);
293 session => $session);
296 # if the copy has a price defined and/or a processing fee, bill the patron
298 my $copy_price = $copy->price || 0;
300 # If the copy has a price configured, charge said price to the user
302 $evt = _make_bill($session, $copy_price, 'Lost Materials', $circ->id);
306 # if the location that owns the copy has a processing fee, charge the user
307 my $owner = $U->fetch_copy_owner($copy->id);
308 $logger->info("circ fetching org settings for $owner to determine processing fee");
310 my $settings = $U->simplereq(
311 'open-ils.actor', 'open-ils.actor.org_unit.settings.retrieve', $owner );
312 my $fee = $settings->{'circ.lost_materials_processing_fee'} || 0;
315 $evt = _make_bill($session, $fee, 'Lost Materials Processing Fee', $circ->id);
319 $circ->stop_fines("LOST");
324 my( $session, $amount, $type, $xactid ) = @_;
326 $logger->activity("The system is charging $amount ".
327 " [$type] for lost materials on circulation $xactid");
329 my $bill = Fieldmapper::money::billing->new;
331 $bill->xact($xactid);
332 $bill->amount($amount);
333 $bill->billing_type($type); # - XXX these strings should be configurable some day
334 $bill->note('SYSTEM GENERATED');
336 my $id = $session->request(
337 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
339 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
343 sub _set_circ_claims_returned {
344 my( $reqr, $circ, $session, $backdate ) = @_;
346 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
348 $circ->stop_fines("CLAIMSRETURNED");
350 $logger->activity("user ".$reqr->id.
351 " marking circ". $circ->id. " as claims returned");
353 # allow the caller to backdate the circulation and void any fines
354 # that occurred after the backdate
356 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
357 $backdate, $circ, $reqr, $session );
365 __PACKAGE__->register_method (
366 method => 'set_circ_due_date',
367 api_name => 'open-ils.circ.circulation.due_date.update',
369 Updates the due_date on the given circ
371 @param circid The id of the circ to update
372 @param date The timestamp of the new due date
376 sub set_circ_due_date {
377 my( $s, $c, $authtoken, $circid, $date ) = @_;
378 my ($circ, $evt) = $U->fetch_circulation($circid);
382 ($reqr, $evt) = $U->checkses($authtoken);
385 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
388 $date = clense_ISO8601($date);
389 $logger->activity("user ".$reqr->id.
390 " updating due_date on circ $circid: $date");
392 $circ->due_date($date);
393 my $stat = $U->storagereq(
394 'open-ils.storage.direct.action.circulation.update', $circ);
395 return $U->DB_UPDATE_FAILED unless defined $stat;
400 __PACKAGE__->register_method(
401 method => "create_in_house_use",
402 api_name => 'open-ils.circ.in_house_use.create',
404 Creates an in-house use action.
405 @param $authtoken The login session key
406 @param params A hash of params including
407 'location' The org unit id where the in-house use occurs
408 'copyid' The copy in question
409 'count' The number of in-house uses to apply to this copy
410 @return An array of id's representing the id's of the newly created
411 in-house use objects or an event on an error
414 sub create_in_house_use {
415 my( $self, $client, $authtoken, $params ) = @_;
417 my( $staff, $evt, $copy );
418 my $org = $params->{location};
419 my $copyid = $params->{copyid};
420 my $count = $params->{count} || 1;
421 my $use_time = $params->{use_time} || 'now';
424 my $barcode = $params->{barcode};
425 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
430 ($staff, $evt) = $U->checkses($authtoken);
433 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
436 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
439 $logger->activity("User " . $staff->id .
440 " creating $count in-house use(s) for copy $copyid at location $org");
442 if( $use_time ne 'now' ) {
443 $use_time = clense_ISO8601($use_time);
444 $logger->debug("in_house_use setting use time to $use_time");
449 my $ihu = Fieldmapper::action::in_house_use->new;
452 $ihu->staff($staff->id);
453 $ihu->org_unit($org);
454 $ihu->use_time($use_time);
456 my $id = $U->simplereq(
458 'open-ils.storage.direct.action.in_house_use.create', $ihu );
460 return $U->DB_UPDATE_FAILED($ihu) unless $id;
469 __PACKAGE__->register_method(
470 method => "view_circs",
471 api_name => "open-ils.circ.copy_checkout_history.retrieve",
473 Retrieves the last X circs for a given copy
474 @param authtoken The login session key
475 @param copyid The copy to check
476 @param count How far to go back in the item history
477 @return An array of circ ids
483 my( $self, $client, $authtoken, $copyid, $count ) = @_;
485 my( $requestor, $evt ) = $U->checksesperm(
486 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
489 return [] unless $count;
491 my $circs = $U->cstorereq(
492 'open-ils.cstore.direct.action.circulation.search.atomic',
494 target_copy => $copyid,
498 order_by => { ac => "xact_start DESC" }
506 __PACKAGE__->register_method(
507 method => "circ_count",
508 api_name => "open-ils.circ.circulation.count",
510 Returns the number of times the item has circulated
511 @param copyid The copy to check
515 my( $self, $client, $copyid, $range ) = @_;
516 my $e = OpenILS::Utils::Editor->new;
517 return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
522 __PACKAGE__->register_method(
523 method => 'fetch_notes',
524 api_name => 'open-ils.circ.copy_note.retrieve.all',
526 Returns an array of copy note objects.
527 @param args A named hash of parameters including:
528 authtoken : Required if viewing non-public notes
529 itemid : The id of the item whose notes we want to retrieve
530 pub : True if all the caller wants are public notes
531 @return An array of note objects
534 __PACKAGE__->register_method(
535 method => 'fetch_notes',
536 api_name => 'open-ils.circ.call_number_note.retrieve.all',
537 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
539 __PACKAGE__->register_method(
540 method => 'fetch_notes',
541 api_name => 'open-ils.circ.title_note.retrieve.all',
542 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
545 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
547 my( $self, $connection, $args ) = @_;
549 my $id = $$args{itemid};
550 my $authtoken = $$args{authtoken};
553 if( $self->api_name =~ /copy/ ) {
555 return $U->cstorereq(
556 'open-ils.cstore.direct.asset.copy_note.search.atomic',
557 { owning_copy => $id, pub => 't' } );
559 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
561 return $U->cstorereq(
562 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
565 } elsif( $self->api_name =~ /call_number/ ) {
567 return $U->cstorereq(
568 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
569 { call_number => $id, pub => 't' } );
571 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
573 return $U->cstorereq(
574 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
577 } elsif( $self->api_name =~ /title/ ) {
579 return $U->cstorereq(
580 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
581 { record => $id, pub => 't' } );
583 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
585 return $U->cstorereq(
586 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
593 __PACKAGE__->register_method(
594 method => 'has_notes',
595 api_name => 'open-ils.circ.copy.has_notes');
596 __PACKAGE__->register_method(
597 method => 'has_notes',
598 api_name => 'open-ils.circ.call_number.has_notes');
599 __PACKAGE__->register_method(
600 method => 'has_notes',
601 api_name => 'open-ils.circ.title.has_notes');
605 my( $self, $conn, $authtoken, $id ) = @_;
606 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
607 return $editor->event unless $editor->checkauth;
609 my $n = $editor->search_asset_copy_note(
610 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
612 $n = $editor->search_asset_call_number_note(
613 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
615 $n = $editor->search_biblio_record_note(
616 {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 ) = @_;
633 my( $cnowner, $requestor, $evt );
635 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
637 ($requestor, $evt) = $U->checkses($authtoken);
639 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
642 $note->create_date('now');
643 $note->creator($requestor->id);
644 $note->pub( ($note->pub) ? 't' : 'f' );
646 my $id = $U->storagereq(
647 'open-ils.storage.direct.asset.copy_note.create', $note );
648 return $U->DB_UPDATE_FAILED($note) unless $id;
650 $logger->activity("User ".$requestor->id." created a new copy ".
651 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
656 __PACKAGE__->register_method(
657 method => 'delete_copy_note',
658 api_name => 'open-ils.circ.copy_note.delete',
660 Deletes an existing copy note
661 @param authtoken The login session key
662 @param noteid The id of the note to delete
663 @return 1 on success - Event otherwise.
666 sub delete_copy_note {
667 my( $self, $conn, $authtoken, $noteid ) = @_;
668 my( $requestor, $note, $owner, $evt );
670 ($requestor, $evt) = $U->checkses($authtoken);
673 ($note, $evt) = $U->fetch_copy_note($noteid);
676 if( $note->creator ne $requestor->id ) {
677 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
679 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
683 my $stat = $U->storagereq(
684 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
685 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
687 $logger->activity("User ".$requestor->id." deleted copy note $noteid");