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';
218 ($staff, $evt) = $U->checkses($authtoken);
221 ($copy, $evt) = $U->fetch_copy($copyid);
224 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
227 $logger->activity("User " . $staff->id .
228 " creating $count in-house use(s) for copy $copyid at location $org");
230 if( $use_time ne 'now' ) {
231 $use_time = clense_ISO8601($use_time);
232 $logger->debug("in_house_use setting use time to $use_time");
237 my $ihu = Fieldmapper::action::in_house_use->new;
240 $ihu->staff($staff->id);
241 $ihu->org_unit($org);
242 $ihu->use_time($use_time);
244 my $id = $U->simplereq(
246 'open-ils.storage.direct.action.in_house_use.create', $ihu );
248 return $U->DB_UPDATE_FAILED($ihu) unless $id;
257 __PACKAGE__->register_method(
258 method => "view_circ_patrons",
259 api_name => "open-ils.circ.copy_checkout_history.retrieve",
261 Retrieves the last X users who checked out a given copy
262 @param authtoken The login session key
263 @param copyid The copy to check
264 @param count How far to go back in the item history
265 @return An array of patron ids
268 sub view_circ_patrons {
269 my( $self, $client, $authtoken, $copyid, $count ) = @_;
271 my( $requestor, $evt ) = $U->checksesperm(
272 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
275 return [] unless $count;
277 my $circs = $U->storagereq(
278 'open-ils.storage.direct.action.circulation.search_where.atomic',
280 target_copy => $copyid,
283 phone_renewal => 'f',
287 order_by => "xact_start DESC"
292 push(@users, $_->usr) for @$circs;
298 __PACKAGE__->register_method(
299 method => 'fetch_notes',
300 api_name => 'open-ils.circ.copy_note.retrieve.all',
302 Returns an array of copy note objects.
303 @param args A named hash of parameters including:
304 authtoken : Required if viewing non-public notes
305 itemid : The id of the item whose notes we want to retrieve
306 pub : True if all the caller wants are public notes
307 @return An array of note objects
310 __PACKAGE__->register_method(
311 method => 'fetch_notes',
312 api_name => 'open-ils.circ.call_number_note.retrieve.all',
313 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
315 __PACKAGE__->register_method(
316 method => 'fetch_notes',
317 api_name => 'open-ils.circ.title_note.retrieve.all',
318 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
320 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
322 my( $self, $connection, $args ) = @_;
324 my $id = $$args{itemid};
325 my $authtoken = $$args{authtoken};
328 if( $self->api_name =~ /copy/ ) {
330 return $U->storagereq(
331 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
332 { owning_copy => $id, pub => 't' } );
334 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_COPY_NOTES');
336 return $U->storagereq(
337 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
340 } elsif( $self->api_name =~ /call_number/ ) {
342 return $U->storagereq(
343 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
344 { call_number => $id, pub => 't' } );
346 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_VOLUME_NOTES');
348 return $U->storagereq(
349 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
352 } elsif( $self->api_name =~ /title/ ) {
354 return $U->storagereq(
355 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
356 { record => $id, pub => 't' } );
358 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_TITLE_NOTES');
360 return $U->storagereq(
361 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
368 __PACKAGE__->register_method(
369 method => 'create_copy_note',
370 api_name => 'open-ils.circ.copy_note.create',
372 Creates a new copy note
373 @param authtoken The login session key
374 @param note The note object to create
375 @return The id of the new note object
378 sub create_copy_note {
379 my( $self, $connection, $authtoken, $note ) = @_;
380 my( $cnowner, $requestor, $evt );
382 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
384 ($requestor, $evt) = $U->checkses($authtoken);
386 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
389 $note->create_date('now');
390 $note->pub( ($note->pub) ? 't' : 'f' );
392 my $id = $U->storagereq(
393 'open-ils.storage.direct.asset.copy_note.create', $note );
394 return $U->DB_UPDATE_FAILED($note) unless $id;
396 $logger->activity("User ".$requestor->id." created a new copy ".
397 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
402 __PACKAGE__->register_method(
403 method => 'delete_copy_note',
404 api_name => 'open-ils.circ.copy_note.delete',
406 Deletes an existing copy note
407 @param authtoken The login session key
408 @param noteid The id of the note to delete
409 @return 1 on success - Event otherwise.
412 sub delete_copy_note {
413 my( $self, $conn, $authtoken, $noteid ) = @_;
414 my( $requestor, $note, $owner, $evt );
416 ($requestor, $evt) = $U->checkses($authtoken);
419 ($note, $evt) = $U->fetch_copy_note($noteid);
422 if( $note->creator ne $requestor->id ) {
423 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
425 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
429 my $stat = $U->storagereq(
430 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
431 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
433 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
438 __PACKAGE__->register_method(
439 method => 'note_batch',
440 api_name => 'open-ils.circ.biblio_notes.public.batch.retrieve',
442 Returns a set of notes for a given set of titles, volumes, and copies.
443 @param titleid The id of the title who's notes are retrieving
444 @return A list like so:
446 "titles" : [ { id : $id, notes : [ n1, n2 ] },... ]
447 "volumes" : [ { id : $id, notes : [ n1, n2 ] },... ]
448 "copies" : [ { id : $id, notes : [ n1, n2 ] },... ]
454 my( $self, $conn, $titleid ) = @_;
457 my $cns = $U->storagereq(
458 'open-ils.storage.id_list.asset.call_number.search_where.atomic',
459 { record => $titleid, deleted => 'f' } );
460 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
463 my $copyids = $U->storagereq(
464 #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
465 'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
466 push(@copies, @$copyids);
469 return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
481 my $titles = (ref($$args{titles})) ? $$args{titles} : [];
482 my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
483 my $copies = (ref($$args{copies})) ? $$args{copies} : [];
485 for my $title (@$titles) {
486 my $notes = $U->storagereq(
487 'open-ils.storage.direct.biblio.record_note.search_where.atomic',
488 { record => $title, pub => 't' });
489 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
492 for my $volume (@$volumes) {
493 my $notes = $U->storagereq(
494 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
495 { call_number => $volume, pub => 't' });
496 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
500 for my $copy (@$copies) {
501 $logger->debug("Fetching copy notes for copy $copy");
502 my $notes = $U->storagereq(
503 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
504 { owning_copy => $copy, pub => 't' });
505 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;