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;
14 use OpenILS::Application::AppUtils;
15 my $apputils = "OpenILS::Application::AppUtils";
18 use OpenILS::Utils::ModsParser;
20 use OpenSRF::EX qw(:try);
21 use OpenSRF::Utils::Logger qw(:logger);
22 #my $logger = "OpenSRF::Utils::Logger";
25 # ------------------------------------------------------------------------
26 # Top level Circ package;
27 # ------------------------------------------------------------------------
31 OpenILS::Application::Circ::Rules->initialize();
32 OpenILS::Application::Circ::Circulate->initialize();
36 # ------------------------------------------------------------------------
37 # Returns an array of {circ, record} hashes checked out by the user.
38 # ------------------------------------------------------------------------
39 __PACKAGE__->register_method(
40 method => "checkouts_by_user",
41 api_name => "open-ils.circ.actor.user.checked_out",
43 Returns a list of open circulations as a pile of objects. each object
44 contains the relevant copy, circ, and record
47 sub checkouts_by_user {
48 my( $self, $client, $user_session, $user_id ) = @_;
50 my( $requestor, $target, $copy, $record, $evt );
52 ( $requestor, $target, $evt ) =
53 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
56 my $circs = $apputils->simplereq(
58 "open-ils.storage.direct.action.open_circulation.search.atomic",
59 { usr => $target->id, checkin_time => undef } );
60 # { usr => $target->id } );
63 for my $circ (@$circs) {
65 ( $copy, $evt ) = $apputils->fetch_copy($circ->target_copy);
68 $logger->debug("Retrieving record for copy " . $circ->target_copy);
70 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
73 my $mods = $apputils->record_to_mvr($record);
75 push( @results, { copy => $copy, circ => $circ, record => $mods } );
84 __PACKAGE__->register_method(
85 method => "checkouts_by_user_slim",
86 api_name => "open-ils.circ.actor.user.checked_out.slim",
88 Returns a list of open circulation objects
91 sub checkouts_by_user_slim {
92 my( $self, $client, $user_session, $user_id ) = @_;
94 my( $requestor, $target, $copy, $record, $evt );
96 ( $requestor, $target, $evt ) =
97 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
100 $logger->debug( 'User ' . $requestor->id .
101 " retrieving checked out items for user " . $target->id );
103 # XXX Make the call correct..
104 return $apputils->simplereq(
106 "open-ils.storage.direct.action.open_circulation.search.atomic",
107 { usr => $target->id, checkin_time => undef } );
108 # { usr => $target->id } );
114 __PACKAGE__->register_method(
115 method => "title_from_transaction",
116 api_name => "open-ils.circ.circ_transaction.find_title",
117 NOTES => <<" NOTES");
118 Returns a mods object for the title that is linked to from the
119 copy from the hold that created the given transaction
122 sub title_from_transaction {
123 my( $self, $client, $login_session, $transactionid ) = @_;
125 my( $user, $circ, $title, $evt );
127 ( $user, $evt ) = $apputils->checkses( $login_session );
130 ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
133 ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
136 return $apputils->record_to_mvr($title);
140 __PACKAGE__->register_method(
141 method => "set_circ_lost",
142 api_name => "open-ils.circ.circulation.set_lost",
143 NOTES => <<" NOTES");
144 Params are login, circid
145 login must have SET_CIRC_LOST perms
146 Sets a circulation to lost
149 __PACKAGE__->register_method(
150 method => "set_circ_lost",
151 api_name => "open-ils.circ.circulation.set_claims_returned",
152 NOTES => <<" NOTES");
153 Params are login, circid
154 login must have SET_CIRC_MISSING perms
155 Sets a circulation to lost
159 my( $self, $client, $login, $circid ) = @_;
160 my( $user, $circ, $evt );
162 ( $user, $evt ) = $apputils->checkses($login);
165 ( $circ, $evt ) = $apputils->fetch_circulation( $circid );
168 if($self->api_name =~ /lost/) {
169 if($evt = $apputils->checkperms(
170 $user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
173 $circ->stop_fines("LOST");
176 # XXX Back date the checkin time so the patron has no fines
177 if($self->api_name =~ /claims_returned/) {
178 if($evt = $apputils->checkperms(
179 $user->id, $circ->circ_lib, "SET_CIRC_CLAIMS_RETURNED")) {
182 $circ->stop_fines("CLAIMSRETURNED");
185 my $s = $apputils->simplereq(
187 "open-ils.storage.direct.action.circulation.update", $circ );
189 if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
192 __PACKAGE__->register_method(
193 method => "create_in_house_use",
194 api_name => 'open-ils.circ.in_house_use.create',
196 Creates an in-house use action.
197 @param $authtoken The login session key
198 @param params A hash of params including
199 'location' The org unit id where the in-house use occurs
200 'copyid' The copy in question
201 'count' The number of in-house uses to apply to this copy
202 @return An array of id's representing the id's of the newly created
203 in-house use objects or an event on an error
206 sub create_in_house_use {
207 my( $self, $client, $authtoken, $params ) = @_;
209 my( $staff, $evt, $copy );
210 my $org = $params->{location};
211 my $copyid = $params->{copyid};
212 my $count = $params->{count} || 1;
214 ($staff, $evt) = $U->checkses($authtoken);
217 ($copy, $evt) = $U->fetch_copy($copyid);
220 $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
223 $logger->activity("User " . $staff->id .
224 " creating $count in-house use(s) for copy $copyid at location $org");
228 my $ihu = Fieldmapper::action::in_house_use->new;
231 $ihu->staff($staff->id);
232 $ihu->org_unit($org);
233 $ihu->use_time('now');
235 my $id = $U->simplereq(
237 'open-ils.storage.direct.action.in_house_use.create', $ihu );
239 return $U->DB_UPDATE_FAILED($ihu) unless $id;
248 __PACKAGE__->register_method(
249 method => "view_circ_patrons",
250 api_name => "open-ils.circ.copy_checkout_history.retrieve",
252 Retrieves the last X users who checked out a given copy
253 @param authtoken The login session key
254 @param copyid The copy to check
255 @param count How far to go back in the item history
256 @return An array of patron ids
259 sub view_circ_patrons {
260 my( $self, $client, $authtoken, $copyid, $count ) = @_;
262 my( $requestor, $evt ) = $U->checksesperm(
263 $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
266 return [] unless $count;
268 my $circs = $U->storagereq(
269 'open-ils.storage.direct.action.circulation.search_where.atomic',
271 target_copy => $copyid,
274 phone_renewal => 'f',
278 order_by => "xact_start DESC"
283 push(@users, $_->usr) for @$circs;
289 __PACKAGE__->register_method(
290 method => 'fetch_notes',
291 api_name => 'open-ils.circ.copy_note.retrieve.all',
293 Returns an array of copy note objects.
294 @param args A named hash of parameters including:
295 authtoken : Required if viewing non-public notes
296 itemid : The id of the item whose notes we want to retrieve
297 pub : True if all the caller wants are public notes
298 @return An array of note objects
301 __PACKAGE__->register_method(
302 method => 'fetch_notes',
303 api_name => 'open-ils.circ.call_number_note.retrieve.all',
304 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
306 __PACKAGE__->register_method(
307 method => 'fetch_notes',
308 api_name => 'open-ils.circ.title_note.retrieve.all',
309 signature => q/@see open-ils.circ.copy_note.retrieve.all/);
311 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
313 my( $self, $connection, $args ) = @_;
315 my $id = $$args{itemid};
316 my $authtoken = $$args{authtoken};
319 if( $self->api_name =~ /copy/ ) {
321 return $U->storagereq(
322 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
323 { owning_copy => $id, pub => 't' } );
325 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_COPY_NOTES');
327 return $U->storagereq(
328 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
331 } elsif( $self->api_name =~ /call_number/ ) {
333 return $U->storagereq(
334 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
335 { call_number => $id, pub => 't' } );
337 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_VOLUME_NOTES');
339 return $U->storagereq(
340 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
343 } elsif( $self->api_name =~ /title/ ) {
345 return $U->storagereq(
346 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
347 { record => $id, pub => 't' } );
349 ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_TITLE_NOTES');
351 return $U->storagereq(
352 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
359 __PACKAGE__->register_method(
360 method => 'create_copy_note',
361 api_name => 'open-ils.circ.copy_note.create',
363 Creates a new copy note
364 @param authtoken The login session key
365 @param note The note object to create
366 @return The id of the new note object
369 sub create_copy_note {
370 my( $self, $connection, $authtoken, $note ) = @_;
371 my( $cnowner, $requestor, $evt );
373 ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
375 ($requestor, $evt) = $U->checkses($authtoken);
377 $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
380 $note->create_date('now');
381 $note->pub( ($note->pub) ? 't' : 'f' );
383 my $id = $U->storagereq(
384 'open-ils.storage.direct.asset.copy_note.create', $note );
385 return $U->DB_UPDATE_FAILED($note) unless $id;
387 $logger->activity("User ".$requestor->id." created a new copy ".
388 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
393 __PACKAGE__->register_method(
394 method => 'delete_copy_note',
395 api_name => 'open-ils.circ.copy_note.delete',
397 Deletes an existing copy note
398 @param authtoken The login session key
399 @param noteid The id of the note to delete
400 @return 1 on success - Event otherwise.
403 sub delete_copy_note {
404 my( $self, $conn, $authtoken, $noteid ) = @_;
405 my( $requestor, $note, $owner, $evt );
407 ($requestor, $evt) = $U->checkses($authtoken);
410 ($note, $evt) = $U->fetch_copy_note($noteid);
413 if( $note->creator ne $requestor->id ) {
414 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
416 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
420 my $stat = $U->storagereq(
421 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
422 return $U->DB_UPDATE_FAILED($noteid) unless $stat;
424 $logger->activity("User ".$requestor->id." deleted copy note $noteid");
429 __PACKAGE__->register_method(
430 method => 'note_batch',
431 api_name => 'open-ils.circ.biblio_notes.public.batch.retrieve',
433 Returns a set of notes for a given set of titles, volumes, and copies.
434 @param titleid The id of the title who's notes are retrieving
435 @return A list like so:
437 "titles" : [ { id : $id, notes : [ n1, n2 ] },... ]
438 "volumes" : [ { id : $id, notes : [ n1, n2 ] },... ]
439 "copies" : [ { id : $id, notes : [ n1, n2 ] },... ]
445 my( $self, $conn, $titleid ) = @_;
448 my $cns = $U->storagereq(
449 'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
452 my $copyids = $U->storagereq(
453 'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
454 push(@copies, @$copyids);
457 return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
469 my $titles = (ref($$args{titles})) ? $$args{titles} : [];
470 my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
471 my $copies = (ref($$args{copies})) ? $$args{copies} : [];
473 for my $title (@$titles) {
474 my $notes = $U->storagereq(
475 'open-ils.storage.direct.biblio.record_note.search_where.atomic',
476 { record => $title, pub => 't' });
477 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
480 for my $volume (@$volumes) {
481 my $notes = $U->storagereq(
482 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
483 { call_number => $volume, pub => 't' });
484 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
488 for my $copy (@$copies) {
489 $logger->debug("Fetching copy notes for copy $copy");
490 my $notes = $U->storagereq(
491 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
492 { owning_copy => $copy, pub => 't' });
493 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;