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::Rules;
7 use OpenILS::Application::Circ::Survey;
8 use OpenILS::Application::Circ::StatCat;
9 use OpenILS::Application::Circ::Holds;
10 use OpenILS::Application::Circ::Money;
11 use OpenILS::Application::Circ::NonCat;
12 use OpenILS::Application::Circ::CopyLocations;
15 use DateTime::Format::ISO8601;
17 use OpenILS::Application::AppUtils;
18 my $apputils = "OpenILS::Application::AppUtils";
20 use OpenSRF::Utils qw/:datetime/;
21 use OpenILS::Utils::ModsParser;
23 use OpenSRF::EX qw(:try);
24 use OpenSRF::Utils::Logger qw(:logger);
25 use OpenILS::Utils::Fieldmapper;
26 use OpenILS::Utils::Editor;
27 #my $logger = "OpenSRF::Utils::Logger";
30 # ------------------------------------------------------------------------
31 # Top level Circ package;
32 # ------------------------------------------------------------------------
36 OpenILS::Application::Circ::Circulate->initialize();
40 __PACKAGE__->register_method(
41 method => 'retrieve_circ',
42 api_name => 'open-ils.circ.retrieve',
44 Retrieve a circ object by id
45 @param authtoken Login session key
46 @pararm circid The id of the circ object
50 my( $s, $c, $a, $i ) = @_;
51 my($reqr, $evt) = $U->checksesperm($a, 'VIEW_CIRCULATIONS');
54 ($circ, $evt) = $U->fetch_circulation($i);
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.storage.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
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.storage.direct.action.open_circulation.search.atomic",
131 { usr => $target->id, checkin_time => undef } );
132 # { usr => $target->id } );
138 __PACKAGE__->register_method(
139 method => "title_from_transaction",
140 api_name => "open-ils.circ.circ_transaction.find_title",
141 NOTES => <<" NOTES");
142 Returns a mods object for the title that is linked to from the
143 copy from the hold that created the given transaction
146 sub title_from_transaction {
147 my( $self, $client, $login_session, $transactionid ) = @_;
149 my( $user, $circ, $title, $evt );
151 ( $user, $evt ) = $apputils->checkses( $login_session );
154 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
157 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
160 return $apputils->record_to_mvr($title);
164 __PACKAGE__->register_method(
165 method => "set_circ_lost",
166 api_name => "open-ils.circ.circulation.set_lost",
167 NOTES => <<" NOTES");
168 Params are login, barcode
169 login must have SET_CIRC_LOST perms
170 Sets a circulation to lost
173 __PACKAGE__->register_method(
174 method => "set_circ_lost",
175 api_name => "open-ils.circ.circulation.set_claims_returned",
176 NOTES => <<" NOTES");
177 Params are login, barcode
178 login must have SET_CIRC_MISSING perms
179 Sets a circulation to lost
183 my( $self, $client, $login, $args ) = @_;
184 my( $user, $circ, $copy, $evt );
186 my $barcode = $$args{barcode};
187 my $backdate = $$args{backdate};
189 ( $user, $evt ) = $U->checkses($login);
192 # Grab the related copy
193 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
196 my $isclaims = $self->api_name =~ /claims_returned/;
197 my $islost = $self->api_name =~ /lost/;
198 my $session = $U->start_db_session();
200 # grab the circulation
201 ( $circ ) = $U->fetch_open_circulation( $copy->id );
202 return 1 unless $circ;
205 $evt = _set_circ_lost($copy, $circ, $user, $session) if $islost;
210 $evt = _set_circ_claims_returned(
211 $user, $circ, $session, $backdate );
214 # $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
215 # return $evt if $evt;
216 # $circ->stop_fines("CLAIMSRETURNED");
218 # $logger->activity("user ".$user->id." marking circ". $circ->id. " as claims returned");
220 # # allow the caller to backdate the circulation and void any fines
221 # # that occurred after the backdate
223 # OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
224 # $backdate, $circ, $user, $session );
228 # ($patron, $evt) = $U->fetch_user($circ->usr);
229 # return $evt if $evt;
230 # $patron->claims_returned_count(
231 # $patron->claims_returned_count + 1 );
233 # my $stat = $U->storagereq(
234 # 'open-ils.storage.direct.actor.user.update', $patron );
235 # return $U->DB_UPDATE_FAILED($patron) unless $stat;
239 $circ->stop_fines_time('now') unless $circ->stop_fines_time('now');
240 my $s = $session->request(
241 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
243 return $U->DB_UPDATE_FAILED($circ) unless defined($s);
244 $U->commit_db_session($session);
250 my( $copy, $circ, $reqr, $session ) = @_;
252 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
255 $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
256 " lost for circ ". $circ->id. " and checking for necessary charges");
258 my $newstat = $U->copy_status_from_name('lost');
259 if( $copy->status ne $newstat->id ) {
261 $copy->status($newstat);
265 session => $session);
268 # if the copy has a price defined and/or a processing fee, bill the patron
269 my $amount = $copy->price || 0;
270 my $owner = $U->fetch_copy_owner($copy->id);
271 $logger->info("circ fetching org settings for $owner to determine processing fee");
272 my $settings = $U->simplereq(
274 'open-ils.actor.org_unit.settings.retrieve', $owner );
275 my $f = $settings->{'circ.processing_fee'} || 0;
280 $logger->activity("The system is charging $amount ".
281 "for lost materials on circulation ".$circ->id);
283 my $bill = Fieldmapper::money::billing->new;
285 $bill->xact( $circ->id );
286 $bill->amount( $amount );
287 $bill->billing_type('Lost materials'); # - these strings should be configurable some day
288 $bill->note('SYSTEM GENERATED');
290 my $id = $session->request(
291 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
293 return $U->DB_UPDATE_FAILED($bill) unless defined $id;
296 $circ->stop_fines("LOST");
300 sub _set_circ_claims_returned {
301 my( $reqr, $circ, $session, $backdate ) = @_;
303 my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
305 $circ->stop_fines("CLAIMSRETURNED");
307 $logger->activity("user ".$reqr->id.
308 " marking circ". $circ->id. " as claims returned");
310 # allow the caller to backdate the circulation and void any fines
311 # that occurred after the backdate
313 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
314 $backdate, $circ, $reqr, $session );
322 __PACKAGE__->register_method (
323 method => 'set_circ_due_date',
324 api_name => 'open-ils.circ.circulation.due_date.update',
326 Updates the due_date on the given circ
328 @param circid The id of the circ to update
329 @param date The timestamp of the new due date
333 sub set_circ_due_date {
334 my( $s, $c, $authtoken, $circid, $date ) = @_;
335 my ($circ, $evt) = $U->fetch_circulation($circid);
339 ($reqr, $evt) = $U->checkses($authtoken);
342 $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
345 $date = clense_ISO8601($date);
346 $logger->activity("user ".$reqr->id.
347 " updating due_date on circ $circid: $date");
349 $circ->due_date($date);
350 my $stat = $U->storagereq(
351 'open-ils.storage.direct.action.circulation.update', $circ);
352 return $U->DB_UPDATE_FAILED unless defined $stat;
357 __PACKAGE__->register_method(
358 method => "create_in_house_use",
359 api_name => 'open-ils.circ.in_house_use.create',
361 Creates an in-house use action.
362 @param $authtoken The login session key
363 @param params A hash of params including
364 'location' The org unit id where the in-house use occurs
365 'copyid' The copy in question
366 'count' The number of in-house uses to apply to this copy
367 @return An array of id's representing the id's of the newly created
368 in-house use objects or an event on an error
371 sub create_in_house_use {
372 my( $self, $client, $authtoken, $params ) = @_;
374 my( $staff, $evt, $copy );
375 my $org = $params->{location};
376 my $copyid = $params->{copyid};
377 my $count = $params->{count} || 1;
378 my $use_time = $params->{use_time} || 'now';
381 my $barcode = $params->{barcode};
382 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
387 ($staff, $evt) = $U->checkses($authtoken);
390 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
393 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
396 $logger->activity("User " . $staff->id .
397 " creating $count in-house use(s) for copy $copyid at location $org");
399 if( $use_time ne 'now' ) {
400 $use_time = clense_ISO8601($use_time);
401 $logger->debug("in_house_use setting use time to $use_time");
406 my $ihu = Fieldmapper::action::in_house_use->new;
409 $ihu->staff($staff->id);
410 $ihu->org_unit($org);
411 $ihu->use_time($use_time);
413 my $id = $U->simplereq(
415 'open-ils.storage.direct.action.in_house_use.create', $ihu );
417 return $U->DB_UPDATE_FAILED($ihu) unless $id;
426 __PACKAGE__->register_method(
427 method => "view_circs",
428 api_name => "open-ils.circ.copy_checkout_history.retrieve",
430 Retrieves the last X circs for a given copy
431 @param authtoken The login session key
432 @param copyid The copy to check
433 @param count How far to go back in the item history
434 @return An array of circ ids
438 my( $self, $client, $authtoken, $copyid, $count ) = @_;
440 my( $requestor, $evt ) = $U->checksesperm(
441 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
444 return [] unless $count;
446 my $circs = $U->storagereq(
447 'open-ils.storage.direct.action.circulation.search_where.atomic',
449 target_copy => $copyid,
450 # opac_renewal => 'f',
451 # desk_renewal => 'f',
452 # phone_renewal => 'f',
456 order_by => "xact_start DESC"
460 # push(@users, $_->usr) for @$circs;
467 __PACKAGE__->register_method(
468 method => "circ_count",
469 api_name => "open-ils.circ.circulation.count",
471 Returns the number of times the item has circulated
472 @param copyid The copy to check
476 my( $self, $client, $copyid ) = @_;
477 my $circs = $U->storagereq(
478 'open-ils.storage.id_list.action.circulation.search.target_copy.atomic', $copyid );
479 return scalar @$circs;
484 __PACKAGE__->register_method(
485 method => 'fetch_notes',
486 api_name => 'open-ils.circ.copy_note.retrieve.all',
488 Returns an array of copy note objects.
489 @param args A named hash of parameters including:
490 authtoken : Required if viewing non-public notes
491 itemid : The id of the item whose notes we want to retrieve
492 pub : True if all the caller wants are public notes
493 @return An array of note objects
496 __PACKAGE__->register_method(
497 method => 'fetch_notes',
498 api_name => 'open-ils.circ.call_number_note.retrieve.all',
499 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
501 __PACKAGE__->register_method(
502 method => 'fetch_notes',
503 api_name => 'open-ils.circ.title_note.retrieve.all',
504 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
507 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
509 my( $self, $connection, $args ) = @_;
511 my $id = $$args{itemid};
512 my $authtoken = $$args{authtoken};
515 if( $self->api_name =~ /copy/ ) {
517 return $U->storagereq(
518 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
519 { owning_copy => $id, pub => 't' } );
521 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
523 return $U->storagereq(
524 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
527 } elsif( $self->api_name =~ /call_number/ ) {
529 return $U->storagereq(
530 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
531 { call_number => $id, pub => 't' } );
533 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
535 return $U->storagereq(
536 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
539 } elsif( $self->api_name =~ /title/ ) {
541 return $U->storagereq(
542 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
543 { record => $id, pub => 't' } );
545 ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
547 return $U->storagereq(
548 'open-ils.storage.direct.biblio.record_note.search.record.atomic', $id );
555 __PACKAGE__->register_method(
556 method => 'has_notes',
557 api_name => 'open-ils.circ.copy.has_notes');
558 __PACKAGE__->register_method(
559 method => 'has_notes',
560 api_name => 'open-ils.circ.call_number.has_notes');
561 __PACKAGE__->register_method(
562 method => 'has_notes',
563 api_name => 'open-ils.circ.title.has_notes');
567 my( $self, $conn, $authtoken, $id ) = @_;
568 my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
569 return $editor->event unless $editor->checkauth;
571 my $n = $editor->search_asset_copy_note(
572 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
574 $n = $editor->search_asset_call_number_note(
575 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
577 $n = $editor->search_biblio_record_note(
578 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
583 __PACKAGE__->register_method(
584 method => 'create_copy_note',
585 api_name => 'open-ils.circ.copy_note.create',
587 Creates a new copy note
588 @param authtoken The login session key
589 @param note The note object to create
590 @return The id of the new note object
593 sub create_copy_note {
594 my( $self, $connection, $authtoken, $note ) = @_;
595 my( $cnowner, $requestor, $evt );
597 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
599 ($requestor, $evt) = $U->checkses($authtoken);
601 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
604 $note->create_date('now');
605 $note->creator($requestor->id);
606 $note->pub( ($note->pub) ? 't' : 'f' );
608 my $id = $U->storagereq(
609 'open-ils.storage.direct.asset.copy_note.create', $note );
610 return $U->DB_UPDATE_FAILED($note) unless $id;
612 $logger->activity("User ".$requestor->id." created a new copy ".
613 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
618 __PACKAGE__->register_method(
619 method => 'delete_copy_note',
620 api_name => 'open-ils.circ.copy_note.delete',
622 Deletes an existing copy note
623 @param authtoken The login session key
624 @param noteid The id of the note to delete
625 @return 1 on success - Event otherwise.
628 sub delete_copy_note {
629 my( $self, $conn, $authtoken, $noteid ) = @_;
630 my( $requestor, $note, $owner, $evt );
632 ($requestor, $evt) = $U->checkses($authtoken);
635 ($note, $evt) = $U->fetch_copy_note($noteid);
638 if( $note->creator ne $requestor->id ) {
639 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
641 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
645 my $stat = $U->storagereq(
646 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
647 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
649 $logger->activity("User ".$requestor->id." deleted copy note $noteid");