]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
typos
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ.pm
1 package OpenILS::Application::Circ;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
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;
13
14 use DateTime;
15 use DateTime::Format::ISO8601;
16
17 use OpenILS::Application::AppUtils;
18 my $apputils = "OpenILS::Application::AppUtils";
19 my $U = $apputils;
20 use OpenSRF::Utils qw/:datetime/;
21 use OpenILS::Utils::ModsParser;
22 use OpenILS::Event;
23 use OpenSRF::EX qw(:try);
24 use OpenSRF::Utils::Logger qw(:logger);
25 #my $logger = "OpenSRF::Utils::Logger";
26
27
28 # ------------------------------------------------------------------------
29 # Top level Circ package;
30 # ------------------------------------------------------------------------
31
32 sub initialize {
33         my $self = shift;
34         OpenILS::Application::Circ::Circulate->initialize();
35 }
36
37
38 __PACKAGE__->register_method(
39         method => 'retrieve_circ',
40         api_name        => 'open-ils.circ.retrieve',
41         signature => q/
42                 Retrieve a circ object by id
43                 @param authtoken Login session key
44                 @pararm circid The id of the circ object
45         /
46 );
47 sub retrieve_circ {
48         my( $s, $c, $a, $i ) = @_;
49         my($reqr, $evt) = $U->checksesperm($a, 'VIEW_CIRCULATIONS');
50         return $evt if $evt;
51         my $circ;
52         ($circ, $evt) = $U->fetch_circulation($i);
53         return $evt if $evt;
54         return $circ;
55 }
56
57
58 # ------------------------------------------------------------------------
59 # Returns an array of {circ, record} hashes checked out by the user.
60 # ------------------------------------------------------------------------
61 __PACKAGE__->register_method(
62         method  => "checkouts_by_user",
63         api_name        => "open-ils.circ.actor.user.checked_out",
64         NOTES           => <<"  NOTES");
65         Returns a list of open circulations as a pile of objects.  each object
66         contains the relevant copy, circ, and record
67         NOTES
68
69 sub checkouts_by_user {
70         my( $self, $client, $user_session, $user_id ) = @_;
71
72         my( $requestor, $target, $copy, $record, $evt );
73
74         ( $requestor, $target, $evt ) = 
75                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
76         return $evt if $evt;
77
78         my $circs = $apputils->simplereq(
79                 'open-ils.storage',
80                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
81                 { usr => $target->id, checkin_time => undef } );
82 #               { usr => $target->id } );
83
84         my @results;
85         for my $circ (@$circs) {
86
87                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
88                 return $evt if $evt;
89
90                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
91
92                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
93                 return $evt if $evt;
94
95                 my $mods = $apputils->record_to_mvr($record);
96
97                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
98         }
99
100         return \@results;
101
102 }
103
104
105
106 __PACKAGE__->register_method(
107         method  => "checkouts_by_user_slim",
108         api_name        => "open-ils.circ.actor.user.checked_out.slim",
109         NOTES           => <<"  NOTES");
110         Returns a list of open circulation objects
111         NOTES
112
113 sub checkouts_by_user_slim {
114         my( $self, $client, $user_session, $user_id ) = @_;
115
116         my( $requestor, $target, $copy, $record, $evt );
117
118         ( $requestor, $target, $evt ) = 
119                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
120         return $evt if $evt;
121
122         $logger->debug( 'User ' . $requestor->id . 
123                 " retrieving checked out items for user " . $target->id );
124
125         # XXX Make the call correct..
126         return $apputils->simplereq(
127                 'open-ils.storage',
128                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
129                 { usr => $target->id, checkin_time => undef } );
130 #               { usr => $target->id } );
131 }
132
133
134
135
136 __PACKAGE__->register_method(
137         method  => "title_from_transaction",
138         api_name        => "open-ils.circ.circ_transaction.find_title",
139         NOTES           => <<"  NOTES");
140         Returns a mods object for the title that is linked to from the 
141         copy from the hold that created the given transaction
142         NOTES
143
144 sub title_from_transaction {
145         my( $self, $client, $login_session, $transactionid ) = @_;
146
147         my( $user, $circ, $title, $evt );
148
149         ( $user, $evt ) = $apputils->checkses( $login_session );
150         return $evt if $evt;
151
152         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
153         return $evt if $evt;
154         
155         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
156         return $evt if $evt;
157
158         return $apputils->record_to_mvr($title);
159 }
160
161
162 __PACKAGE__->register_method(
163         method  => "set_circ_lost",
164         api_name        => "open-ils.circ.circulation.set_lost",
165         NOTES           => <<"  NOTES");
166         Params are login, barcode
167         login must have SET_CIRC_LOST perms
168         Sets a circulation to lost
169         NOTES
170
171 __PACKAGE__->register_method(
172         method  => "set_circ_lost",
173         api_name        => "open-ils.circ.circulation.set_claims_returned",
174         NOTES           => <<"  NOTES");
175         Params are login, barcode
176         login must have SET_CIRC_MISSING perms
177         Sets a circulation to lost
178         NOTES
179
180 sub set_circ_lost {
181         my( $self, $client, $login, $args ) = @_;
182         my( $user, $circ, $copy, $evt );
183
184         my $barcode             = $$args{barcode};
185         my $backdate    = $$args{backdate};
186
187         ( $user, $evt ) = $U->checkses($login);
188         return $evt if $evt;
189
190         # Grab the related copy
191         ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
192         return $evt if $evt;
193
194         my $isclaims    = $self->api_name =~ /claims_returned/;
195         my $islost              = $self->api_name =~ /lost/;
196         my $session             = $U->start_db_session(); 
197
198
199         # if setting to list, update the copy's statua
200         if( $islost ) {
201                 my $newstat = $U->copy_status_from_name('lost') if $islost;
202                 if( $copy->status ne $newstat->id ) {
203                         $copy->status($newstat);
204                         $U->update_copy(copy => $copy, editor => $user->id, session => $session);
205                 }
206         }
207
208         # grab the circulation
209         ( $circ ) = $U->fetch_open_circulation( $copy->id );
210         return 1 unless $circ;
211
212
213         if($islost) {
214                 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_LOST');
215                 return $evt if $evt;
216                 $circ->stop_fines("LOST");              
217         }
218
219         if($isclaims) {
220
221                 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
222                 return $evt if $evt;
223                 $circ->stop_fines("CLAIMSRETURNED");
224
225                 # allow the caller to backdate the circulation and void any fines
226                 # that occurred after the backdate
227                 if($backdate) {
228                         OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
229                                 $backdate, $circ, $user, $session );
230                 }
231         }
232
233         my $s = $session->request(
234                 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
235
236         return $U->DB_UPDATE_FAILED($circ) unless defined($s);
237         $U->commit_db_session($session);
238
239         return 1;
240 }
241
242
243 __PACKAGE__->register_method (
244         method          => 'set_circ_due_date',
245         api_name                => 'open-ils.circ.circulation.due_date.update',
246         signature       => q/
247                 Updates the due_date on the given circ
248                 @param authtoken
249                 @param circid The id of the circ to update
250                 @param date The timestamp of the new due date
251         /
252 );
253
254 sub set_circ_due_date {
255         my( $s, $c, $authtoken, $circid, $date ) = @_;
256         my ($circ, $evt) = $U->fetch_circulation($circid);
257         return $evt if $evt;
258
259         my $reqr;
260         ($reqr, $evt) = $U->checksesperm(
261                 $authtoken, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
262         return $evt if $evt;
263
264         $date = clense_ISO8601($date);
265         $logger->activity("user ".$reqr->id." updating due_date on circ $circid: $date");
266
267         $circ->due_date($date);
268         my $stat = $U->storagereq(
269                 'open-ils.storage.action.circulation.update', $circ);
270         return $U->DB_UPDATE_FAILED unless defined $stat;
271         return $stat;
272 }
273
274
275 __PACKAGE__->register_method(
276         method          => "create_in_house_use",
277         api_name                => 'open-ils.circ.in_house_use.create',
278         signature       =>      q/
279                 Creates an in-house use action.
280                 @param $authtoken The login session key
281                 @param params A hash of params including
282                         'location' The org unit id where the in-house use occurs
283                         'copyid' The copy in question
284                         'count' The number of in-house uses to apply to this copy
285                 @return An array of id's representing the id's of the newly created
286                 in-house use objects or an event on an error
287         /);
288
289 sub create_in_house_use {
290         my( $self, $client, $authtoken, $params ) = @_;
291
292         my( $staff, $evt, $copy );
293         my $org                 = $params->{location};
294         my $copyid              = $params->{copyid};
295         my $count               = $params->{count} || 1;
296         my $use_time    = $params->{use_time} || 'now';
297
298         if(!$copyid) {
299                 my $barcode = $params->{barcode};
300                 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
301                 return $evt if $evt;
302                 $copyid = $copy->id;
303         }
304
305         ($staff, $evt) = $U->checkses($authtoken);
306         return $evt if $evt;
307
308         ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
309         return $evt if $evt;
310
311         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
312         return $evt if $evt;
313
314         $logger->activity("User " . $staff->id .
315                 " creating $count in-house use(s) for copy $copyid at location $org");
316
317         if( $use_time ne 'now' ) {
318                 $use_time = clense_ISO8601($use_time);
319                 $logger->debug("in_house_use setting use time to $use_time");
320         }
321
322         my @ids;
323         for(1..$count) {
324                 my $ihu = Fieldmapper::action::in_house_use->new;
325
326                 $ihu->item($copyid);
327                 $ihu->staff($staff->id);
328                 $ihu->org_unit($org);
329                 $ihu->use_time($use_time);
330
331                 my $id = $U->simplereq(
332                         'open-ils.storage',
333                         'open-ils.storage.direct.action.in_house_use.create', $ihu );
334
335                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
336                 push @ids, $id;
337         }
338
339         return \@ids;
340 }
341
342
343
344 __PACKAGE__->register_method(
345         method  => "view_circ_patrons",
346         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
347         notes           => q/
348                 Retrieves the last X users who checked out a given copy
349                 @param authtoken The login session key
350                 @param copyid The copy to check
351                 @param count How far to go back in the item history
352                 @return An array of patron ids
353         /);
354
355 sub view_circ_patrons {
356         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
357
358         my( $requestor, $evt ) = $U->checksesperm(
359                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
360         return $evt if $evt;
361
362         return [] unless $count;
363
364         my $circs = $U->storagereq(
365                 'open-ils.storage.direct.action.circulation.search_where.atomic',
366                         { 
367                                 target_copy => $copyid, 
368                                 opac_renewal => 'f',   
369                                 desk_renewal => 'f',
370                                 phone_renewal => 'f',
371                         }, 
372                         { 
373                                 limit => $count, 
374                                 order_by => "xact_start DESC" 
375                         } );
376
377
378         my @users;
379         push(@users, $_->usr) for @$circs;
380         return \@users;
381 }
382
383
384
385 __PACKAGE__->register_method(
386         method          => 'fetch_notes',
387         api_name                => 'open-ils.circ.copy_note.retrieve.all',
388         signature       => q/
389                 Returns an array of copy note objects.  
390                 @param args A named hash of parameters including:
391                         authtoken       : Required if viewing non-public notes
392                         itemid          : The id of the item whose notes we want to retrieve
393                         pub                     : True if all the caller wants are public notes
394                 @return An array of note objects
395         /);
396
397 __PACKAGE__->register_method(
398         method          => 'fetch_notes',
399         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
400         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
401
402 __PACKAGE__->register_method(
403         method          => 'fetch_notes',
404         api_name                => 'open-ils.circ.title_note.retrieve.all',
405         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
406
407
408 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
409 sub fetch_notes {
410         my( $self, $connection, $args ) = @_;
411
412         my $id = $$args{itemid};
413         my $authtoken = $$args{authtoken};
414         my( $r, $evt);
415
416         if( $self->api_name =~ /copy/ ) {
417                 if( $$args{pub} ) {
418                         return $U->storagereq(
419                                 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
420                                 { owning_copy => $id, pub => 't' } );
421                 } else {
422                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
423                         return $evt if $evt;
424                         return $U->storagereq(
425                                 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
426                 }
427
428         } elsif( $self->api_name =~ /call_number/ ) {
429                 if( $$args{pub} ) {
430                         return $U->storagereq(
431                                 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
432                                 { call_number => $id, pub => 't' } );
433                 } else {
434                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
435                         return $evt if $evt;
436                         return $U->storagereq(
437                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
438                 }
439
440         } elsif( $self->api_name =~ /title/ ) {
441                 if( $$args{pub} ) {
442                         return $U->storagereq(
443                                 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
444                                 { record => $id, pub => 't' } );
445                 } else {
446                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
447                         return $evt if $evt;
448                         return $U->storagereq(
449                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
450                 }
451         }
452
453         return undef;
454 }
455
456 __PACKAGE__->register_method(
457         method          => 'create_copy_note',
458         api_name                => 'open-ils.circ.copy_note.create',
459         signature       => q/
460                 Creates a new copy note
461                 @param authtoken The login session key
462                 @param note     The note object to create
463                 @return The id of the new note object
464         /);
465
466 sub create_copy_note {
467         my( $self, $connection, $authtoken, $note ) = @_;
468         my( $cnowner, $requestor, $evt );
469
470         ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
471         return $evt if $evt;
472         ($requestor, $evt) = $U->checkses($authtoken);
473         return $evt if $evt;
474         $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
475         return $evt if $evt;
476
477         $note->create_date('now');
478         $note->pub( ($note->pub) ? 't' : 'f' );
479
480         my $id = $U->storagereq(
481                 'open-ils.storage.direct.asset.copy_note.create', $note );
482         return $U->DB_UPDATE_FAILED($note) unless $id;
483
484         $logger->activity("User ".$requestor->id." created a new copy ".
485                 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
486
487         return $id;
488 }
489
490 __PACKAGE__->register_method(
491         method          => 'delete_copy_note',
492         api_name                =>      'open-ils.circ.copy_note.delete',
493         signature       => q/
494                 Deletes an existing copy note
495                 @param authtoken The login session key
496                 @param noteid The id of the note to delete
497                 @return 1 on success - Event otherwise.
498                 /);
499
500 sub delete_copy_note {
501         my( $self, $conn, $authtoken, $noteid ) = @_;
502         my( $requestor, $note, $owner, $evt );
503
504         ($requestor, $evt) = $U->checkses($authtoken);
505         return $evt if $evt;
506
507         ($note, $evt) = $U->fetch_copy_note($noteid);
508         return $evt if $evt;
509
510         if( $note->creator ne $requestor->id ) {
511                 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
512                 return $evt if $evt;
513                 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
514                 return $evt if $evt;
515         }
516
517         my $stat = $U->storagereq(
518                 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
519         return $U->DB_UPDATE_FAILED($noteid) unless $stat;
520
521         $logger->activity("User ".$requestor->id." deleted copy note $noteid");
522         return 1;
523 }
524
525 =head this method is really inefficient - get rid of me
526
527 __PACKAGE__->register_method(
528         method          => 'note_batch',
529         api_name                => 'open-ils.circ.biblio_notes.public.batch.retrieve',
530         signature       => q/
531                 Returns a set of notes for a given set of titles, volumes, and copies.
532                 @param titleid The id of the title who's notes are retrieving
533                 @return A list like so:
534                         {
535                                 "titles"                : [ { id : $id, notes : [ n1, n2 ] },... ]
536                                 "volumes"       : [ { id : $id, notes : [ n1, n2 ] },... ]
537                                 "copies"                : [ { id : $id, notes : [ n1, n2 ] },... ]
538                         }
539         /
540 );
541
542 sub note_batch {
543         my( $self, $conn, $titleid ) = @_;
544
545         my @copies;
546         my $cns = $U->storagereq(
547                 'open-ils.storage.id_list.asset.call_number.search_where.atomic', 
548                 { record => $titleid, deleted => 'f' } );
549                 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
550
551         for my $c (@$cns) {
552                 my $copyids = $U->storagereq(
553                         #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
554                         'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
555                 push(@copies, @$copyids);
556         }
557
558         return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
559 }
560
561
562 sub _note_batch {
563         my $args = shift;
564
565         my %resp;
566         $resp{titles}   = [];
567         $resp{volumes} = [];
568         $resp{copies}   = [];
569
570         my $titles      = (ref($$args{titles})) ? $$args{titles} : [];
571         my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
572         my $copies      = (ref($$args{copies})) ? $$args{copies} : [];
573
574         for my $title (@$titles) {
575                 my $notes = $U->storagereq(
576                         'open-ils.storage.direct.biblio.record_note.search_where.atomic', 
577                         { record => $title, pub => 't' });
578                 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
579         }
580
581         for my $volume (@$volumes) {
582                 my $notes = $U->storagereq(
583                         'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
584                         { call_number => $volume, pub => 't' });
585                 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
586         }
587
588
589         for my $copy (@$copies) {
590                 $logger->debug("Fetching copy notes for copy $copy");
591                 my $notes = $U->storagereq(
592                         'open-ils.storage.direct.asset.copy_note.search_where.atomic',
593                         { owning_copy => $copy, pub => 't' });
594                 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;
595         }
596
597         return \%resp;
598 }
599
600 =cut
601
602
603
604
605
606
607
608 1;