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 #my $logger = "OpenSRF::Utils::Logger";
28 # ------------------------------------------------------------------------
29 # Top level Circ package;
30 # ------------------------------------------------------------------------
34 OpenILS::Application::Circ::Rules->initialize();
35 OpenILS::Application::Circ::Circulate->initialize();
39 # ------------------------------------------------------------------------
40 # Returns an array of {circ, record} hashes checked out by the user.
41 # ------------------------------------------------------------------------
42 __PACKAGE__->register_method(
43 method => "checkouts_by_user",
44 api_name => "open-ils.circ.actor.user.checked_out",
46 Returns a list of open circulations as a pile of objects. each object
47 contains the relevant copy, circ, and record
50 sub checkouts_by_user {
51 my( $self, $client, $user_session, $user_id ) = @_;
53 my( $requestor, $target, $copy, $record, $evt );
55 ( $requestor, $target, $evt ) =
56 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
59 my $circs = $apputils->simplereq(
61 "open-ils.storage.direct.action.open_circulation.search.atomic",
62 { usr => $target->id, checkin_time => undef } );
63 # { usr => $target->id } );
66 for my $circ (@$circs) {
68 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
71 $logger->debug("Retrieving record for copy " . $circ->target_copy);
73 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
76 my $mods = $apputils->record_to_mvr($record);
78 push( @results, { copy => $copy, circ => $circ, record => $mods } );
87 __PACKAGE__->register_method(
88 method => "checkouts_by_user_slim",
89 api_name => "open-ils.circ.actor.user.checked_out.slim",
91 Returns a list of open circulation objects
94 sub checkouts_by_user_slim {
95 my( $self, $client, $user_session, $user_id ) = @_;
97 my( $requestor, $target, $copy, $record, $evt );
99 ( $requestor, $target, $evt ) =
100 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
103 $logger->debug( 'User ' . $requestor->id .
104 " retrieving checked out items for user " . $target->id );
106 # XXX Make the call correct..
107 return $apputils->simplereq(
109 "open-ils.storage.direct.action.open_circulation.search.atomic",
110 { usr => $target->id, checkin_time => undef } );
111 # { usr => $target->id } );
117 __PACKAGE__->register_method(
118 method => "title_from_transaction",
119 api_name => "open-ils.circ.circ_transaction.find_title",
120 NOTES => <<" NOTES");
121 Returns a mods object for the title that is linked to from the
122 copy from the hold that created the given transaction
125 sub title_from_transaction {
126 my( $self, $client, $login_session, $transactionid ) = @_;
128 my( $user, $circ, $title, $evt );
130 ( $user, $evt ) = $apputils->checkses( $login_session );
133 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
136 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
139 return $apputils->record_to_mvr($title);
143 __PACKAGE__->register_method(
144 method => "set_circ_lost",
145 api_name => "open-ils.circ.circulation.set_lost",
146 NOTES => <<" NOTES");
147 Params are login, circid
148 login must have SET_CIRC_LOST perms
149 Sets a circulation to lost
152 __PACKAGE__->register_method(
153 method => "set_circ_lost",
154 api_name => "open-ils.circ.circulation.set_claims_returned",
155 NOTES => <<" NOTES");
156 Params are login, circid
157 login must have SET_CIRC_MISSING perms
158 Sets a circulation to lost
162 my( $self, $client, $login, $circid ) = @_;
163 my( $user, $circ, $evt );
165 ( $user, $evt ) = $apputils->checkses($login);
168 ( $circ, $evt ) = $apputils->fetch_circulation( $circid );
171 if($self->api_name =~ /lost/) {
172 if($evt = $apputils->checkperms(
173 $user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
176 $circ->stop_fines("LOST");
179 # XXX Back date the checkin time so the patron has no fines
180 if($self->api_name =~ /claims_returned/) {
181 if($evt = $apputils->checkperms(
182 $user->id, $circ->circ_lib, "SET_CIRC_CLAIMS_RETURNED")) {
185 $circ->stop_fines("CLAIMSRETURNED");
188 my $s = $apputils->simplereq(
190 "open-ils.storage.direct.action.circulation.update", $circ );
192 if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
195 __PACKAGE__->register_method(
196 method => "create_in_house_use",
197 api_name => 'open-ils.circ.in_house_use.create',
199 Creates an in-house use action.
200 @param $authtoken The login session key
201 @param params A hash of params including
202 'location' The org unit id where the in-house use occurs
203 'copyid' The copy in question
204 'count' The number of in-house uses to apply to this copy
205 @return An array of id's representing the id's of the newly created
206 in-house use objects or an event on an error
209 sub create_in_house_use {
210 my( $self, $client, $authtoken, $params ) = @_;
212 my( $staff, $evt, $copy );
213 my $org = $params->{location};
214 my $copyid = $params->{copyid};
215 my $count = $params->{count} || 1;
216 my $use_time = $params->{use_time} || 'now';
219 my $barcode = $params->{barcode};
220 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
224 ($staff, $evt) = $U->checkses($authtoken);
227 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
230 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
233 $logger->activity("User " . $staff->id .
234 " creating $count in-house use(s) for copy $copyid at location $org");
236 if( $use_time ne 'now' ) {
237 $use_time = clense_ISO8601($use_time);
238 $logger->debug("in_house_use setting use time to $use_time");
243 my $ihu = Fieldmapper::action::in_house_use->new;
246 $ihu->staff($staff->id);
247 $ihu->org_unit($org);
248 $ihu->use_time($use_time);
250 my $id = $U->simplereq(
252 'open-ils.storage.direct.action.in_house_use.create', $ihu );
254 return $U->DB_UPDATE_FAILED($ihu) unless $id;
263 __PACKAGE__->register_method(
264 method => "view_circ_patrons",
265 api_name => "open-ils.circ.copy_checkout_history.retrieve",
267 Retrieves the last X users who checked out a given copy
268 @param authtoken The login session key
269 @param copyid The copy to check
270 @param count How far to go back in the item history
271 @return An array of patron ids
274 sub view_circ_patrons {
275 my( $self, $client, $authtoken, $copyid, $count ) = @_;
277 my( $requestor, $evt ) = $U->checksesperm(
278 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
281 return [] unless $count;
283 my $circs = $U->storagereq(
284 'open-ils.storage.direct.action.circulation.search_where.atomic',
286 target_copy => $copyid,
289 phone_renewal => 'f',
293 order_by => "xact_start DESC"
298 push(@users, $_->usr) for @$circs;
304 __PACKAGE__->register_method(
305 method => 'fetch_notes',
306 api_name => 'open-ils.circ.copy_note.retrieve.all',
308 Returns an array of copy note objects.
309 @param args A named hash of parameters including:
310 authtoken : Required if viewing non-public notes
311 itemid : The id of the item whose notes we want to retrieve
312 pub : True if all the caller wants are public notes
313 @return An array of note objects
316 __PACKAGE__->register_method(
317 method => 'fetch_notes',
318 api_name => 'open-ils.circ.call_number_note.retrieve.all',
319 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
321 __PACKAGE__->register_method(
322 method => 'fetch_notes',
323 api_name => 'open-ils.circ.title_note.retrieve.all',
324 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
326 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
328 my( $self, $connection, $args ) = @_;
330 my $id = $$args{itemid};
331 my $authtoken = $$args{authtoken};
334 if( $self->api_name =~ /copy/ ) {
336 return $U->storagereq(
337 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
338 { owning_copy => $id, pub => 't' } );
340 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_COPY_NOTES');
342 return $U->storagereq(
343 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
346 } elsif( $self->api_name =~ /call_number/ ) {
348 return $U->storagereq(
349 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
350 { call_number => $id, pub => 't' } );
352 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_VOLUME_NOTES');
354 return $U->storagereq(
355 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
358 } elsif( $self->api_name =~ /title/ ) {
360 return $U->storagereq(
361 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
362 { record => $id, pub => 't' } );
364 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_TITLE_NOTES');
366 return $U->storagereq(
367 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
374 __PACKAGE__->register_method(
375 method => 'create_copy_note',
376 api_name => 'open-ils.circ.copy_note.create',
378 Creates a new copy note
379 @param authtoken The login session key
380 @param note The note object to create
381 @return The id of the new note object
384 sub create_copy_note {
385 my( $self, $connection, $authtoken, $note ) = @_;
386 my( $cnowner, $requestor, $evt );
388 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
390 ($requestor, $evt) = $U->checkses($authtoken);
392 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
395 $note->create_date('now');
396 $note->pub( ($note->pub) ? 't' : 'f' );
398 my $id = $U->storagereq(
399 'open-ils.storage.direct.asset.copy_note.create', $note );
400 return $U->DB_UPDATE_FAILED($note) unless $id;
402 $logger->activity("User ".$requestor->id." created a new copy ".
403 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
408 __PACKAGE__->register_method(
409 method => 'delete_copy_note',
410 api_name => 'open-ils.circ.copy_note.delete',
412 Deletes an existing copy note
413 @param authtoken The login session key
414 @param noteid The id of the note to delete
415 @return 1 on success - Event otherwise.
418 sub delete_copy_note {
419 my( $self, $conn, $authtoken, $noteid ) = @_;
420 my( $requestor, $note, $owner, $evt );
422 ($requestor, $evt) = $U->checkses($authtoken);
425 ($note, $evt) = $U->fetch_copy_note($noteid);
428 if( $note->creator ne $requestor->id ) {
429 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
431 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
435 my $stat = $U->storagereq(
436 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
437 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
439 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
444 __PACKAGE__->register_method(
445 method => 'note_batch',
446 api_name => 'open-ils.circ.biblio_notes.public.batch.retrieve',
448 Returns a set of notes for a given set of titles, volumes, and copies.
449 @param titleid The id of the title who's notes are retrieving
450 @return A list like so:
452 "titles" : [ { id : $id, notes : [ n1, n2 ] },... ]
453 "volumes" : [ { id : $id, notes : [ n1, n2 ] },... ]
454 "copies" : [ { id : $id, notes : [ n1, n2 ] },... ]
460 my( $self, $conn, $titleid ) = @_;
463 my $cns = $U->storagereq(
464 'open-ils.storage.id_list.asset.call_number.search_where.atomic',
465 { record => $titleid, deleted => 'f' } );
466 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
469 my $copyids = $U->storagereq(
470 #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
471 'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
472 push(@copies, @$copyids);
475 return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
487 my $titles = (ref($$args{titles})) ? $$args{titles} : [];
488 my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
489 my $copies = (ref($$args{copies})) ? $$args{copies} : [];
491 for my $title (@$titles) {
492 my $notes = $U->storagereq(
493 'open-ils.storage.direct.biblio.record_note.search_where.atomic',
494 { record => $title, pub => 't' });
495 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
498 for my $volume (@$volumes) {
499 my $notes = $U->storagereq(
500 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
501 { call_number => $volume, pub => 't' });
502 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
506 for my $copy (@$copies) {
507 $logger->debug("Fetching copy notes for copy $copy");
508 my $notes = $U->storagereq(
509 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
510 { owning_copy => $copy, pub => 't' });
511 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;