]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
mended the perm calls
[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->checkses($authtoken);
261         return $evt if $evt;
262
263         $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
264         return $evt if $evt;
265
266         $date = clense_ISO8601($date);
267         $logger->activity("user ".$reqr->id.
268                 " updating due_date on circ $circid: $date");
269
270         $circ->due_date($date);
271         my $stat = $U->storagereq(
272                 'open-ils.storage.direct.action.circulation.update', $circ);
273         return $U->DB_UPDATE_FAILED unless defined $stat;
274         return $stat;
275 }
276
277
278 __PACKAGE__->register_method(
279         method          => "create_in_house_use",
280         api_name                => 'open-ils.circ.in_house_use.create',
281         signature       =>      q/
282                 Creates an in-house use action.
283                 @param $authtoken The login session key
284                 @param params A hash of params including
285                         'location' The org unit id where the in-house use occurs
286                         'copyid' The copy in question
287                         'count' The number of in-house uses to apply to this copy
288                 @return An array of id's representing the id's of the newly created
289                 in-house use objects or an event on an error
290         /);
291
292 sub create_in_house_use {
293         my( $self, $client, $authtoken, $params ) = @_;
294
295         my( $staff, $evt, $copy );
296         my $org                 = $params->{location};
297         my $copyid              = $params->{copyid};
298         my $count               = $params->{count} || 1;
299         my $use_time    = $params->{use_time} || 'now';
300
301         if(!$copyid) {
302                 my $barcode = $params->{barcode};
303                 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
304                 return $evt if $evt;
305                 $copyid = $copy->id;
306         }
307
308         ($staff, $evt) = $U->checkses($authtoken);
309         return $evt if $evt;
310
311         ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
312         return $evt if $evt;
313
314         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
315         return $evt if $evt;
316
317         $logger->activity("User " . $staff->id .
318                 " creating $count in-house use(s) for copy $copyid at location $org");
319
320         if( $use_time ne 'now' ) {
321                 $use_time = clense_ISO8601($use_time);
322                 $logger->debug("in_house_use setting use time to $use_time");
323         }
324
325         my @ids;
326         for(1..$count) {
327                 my $ihu = Fieldmapper::action::in_house_use->new;
328
329                 $ihu->item($copyid);
330                 $ihu->staff($staff->id);
331                 $ihu->org_unit($org);
332                 $ihu->use_time($use_time);
333
334                 my $id = $U->simplereq(
335                         'open-ils.storage',
336                         'open-ils.storage.direct.action.in_house_use.create', $ihu );
337
338                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
339                 push @ids, $id;
340         }
341
342         return \@ids;
343 }
344
345
346
347 __PACKAGE__->register_method(
348         method  => "view_circ_patrons",
349         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
350         notes           => q/
351                 Retrieves the last X users who checked out a given copy
352                 @param authtoken The login session key
353                 @param copyid The copy to check
354                 @param count How far to go back in the item history
355                 @return An array of patron ids
356         /);
357
358 sub view_circ_patrons {
359         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
360
361         my( $requestor, $evt ) = $U->checksesperm(
362                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
363         return $evt if $evt;
364
365         return [] unless $count;
366
367         my $circs = $U->storagereq(
368                 'open-ils.storage.direct.action.circulation.search_where.atomic',
369                         { 
370                                 target_copy => $copyid, 
371                                 opac_renewal => 'f',   
372                                 desk_renewal => 'f',
373                                 phone_renewal => 'f',
374                         }, 
375                         { 
376                                 limit => $count, 
377                                 order_by => "xact_start DESC" 
378                         } );
379
380
381         my @users;
382         push(@users, $_->usr) for @$circs;
383         return \@users;
384 }
385
386
387
388 __PACKAGE__->register_method(
389         method          => 'fetch_notes',
390         api_name                => 'open-ils.circ.copy_note.retrieve.all',
391         signature       => q/
392                 Returns an array of copy note objects.  
393                 @param args A named hash of parameters including:
394                         authtoken       : Required if viewing non-public notes
395                         itemid          : The id of the item whose notes we want to retrieve
396                         pub                     : True if all the caller wants are public notes
397                 @return An array of note objects
398         /);
399
400 __PACKAGE__->register_method(
401         method          => 'fetch_notes',
402         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
403         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
404
405 __PACKAGE__->register_method(
406         method          => 'fetch_notes',
407         api_name                => 'open-ils.circ.title_note.retrieve.all',
408         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
409
410
411 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
412 sub fetch_notes {
413         my( $self, $connection, $args ) = @_;
414
415         my $id = $$args{itemid};
416         my $authtoken = $$args{authtoken};
417         my( $r, $evt);
418
419         if( $self->api_name =~ /copy/ ) {
420                 if( $$args{pub} ) {
421                         return $U->storagereq(
422                                 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
423                                 { owning_copy => $id, pub => 't' } );
424                 } else {
425                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
426                         return $evt if $evt;
427                         return $U->storagereq(
428                                 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
429                 }
430
431         } elsif( $self->api_name =~ /call_number/ ) {
432                 if( $$args{pub} ) {
433                         return $U->storagereq(
434                                 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
435                                 { call_number => $id, pub => 't' } );
436                 } else {
437                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
438                         return $evt if $evt;
439                         return $U->storagereq(
440                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
441                 }
442
443         } elsif( $self->api_name =~ /title/ ) {
444                 if( $$args{pub} ) {
445                         return $U->storagereq(
446                                 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
447                                 { record => $id, pub => 't' } );
448                 } else {
449                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
450                         return $evt if $evt;
451                         return $U->storagereq(
452                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
453                 }
454         }
455
456         return undef;
457 }
458
459 __PACKAGE__->register_method(
460         method          => 'create_copy_note',
461         api_name                => 'open-ils.circ.copy_note.create',
462         signature       => q/
463                 Creates a new copy note
464                 @param authtoken The login session key
465                 @param note     The note object to create
466                 @return The id of the new note object
467         /);
468
469 sub create_copy_note {
470         my( $self, $connection, $authtoken, $note ) = @_;
471         my( $cnowner, $requestor, $evt );
472
473         ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
474         return $evt if $evt;
475         ($requestor, $evt) = $U->checkses($authtoken);
476         return $evt if $evt;
477         $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
478         return $evt if $evt;
479
480         $note->create_date('now');
481         $note->pub( ($note->pub) ? 't' : 'f' );
482
483         my $id = $U->storagereq(
484                 'open-ils.storage.direct.asset.copy_note.create', $note );
485         return $U->DB_UPDATE_FAILED($note) unless $id;
486
487         $logger->activity("User ".$requestor->id." created a new copy ".
488                 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
489
490         return $id;
491 }
492
493 __PACKAGE__->register_method(
494         method          => 'delete_copy_note',
495         api_name                =>      'open-ils.circ.copy_note.delete',
496         signature       => q/
497                 Deletes an existing copy note
498                 @param authtoken The login session key
499                 @param noteid The id of the note to delete
500                 @return 1 on success - Event otherwise.
501                 /);
502
503 sub delete_copy_note {
504         my( $self, $conn, $authtoken, $noteid ) = @_;
505         my( $requestor, $note, $owner, $evt );
506
507         ($requestor, $evt) = $U->checkses($authtoken);
508         return $evt if $evt;
509
510         ($note, $evt) = $U->fetch_copy_note($noteid);
511         return $evt if $evt;
512
513         if( $note->creator ne $requestor->id ) {
514                 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
515                 return $evt if $evt;
516                 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
517                 return $evt if $evt;
518         }
519
520         my $stat = $U->storagereq(
521                 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
522         return $U->DB_UPDATE_FAILED($noteid) unless $stat;
523
524         $logger->activity("User ".$requestor->id." deleted copy note $noteid");
525         return 1;
526 }
527
528 =head this method is really inefficient - get rid of me
529
530 __PACKAGE__->register_method(
531         method          => 'note_batch',
532         api_name                => 'open-ils.circ.biblio_notes.public.batch.retrieve',
533         signature       => q/
534                 Returns a set of notes for a given set of titles, volumes, and copies.
535                 @param titleid The id of the title who's notes are retrieving
536                 @return A list like so:
537                         {
538                                 "titles"                : [ { id : $id, notes : [ n1, n2 ] },... ]
539                                 "volumes"       : [ { id : $id, notes : [ n1, n2 ] },... ]
540                                 "copies"                : [ { id : $id, notes : [ n1, n2 ] },... ]
541                         }
542         /
543 );
544
545 sub note_batch {
546         my( $self, $conn, $titleid ) = @_;
547
548         my @copies;
549         my $cns = $U->storagereq(
550                 'open-ils.storage.id_list.asset.call_number.search_where.atomic', 
551                 { record => $titleid, deleted => 'f' } );
552                 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
553
554         for my $c (@$cns) {
555                 my $copyids = $U->storagereq(
556                         #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
557                         'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
558                 push(@copies, @$copyids);
559         }
560
561         return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
562 }
563
564
565 sub _note_batch {
566         my $args = shift;
567
568         my %resp;
569         $resp{titles}   = [];
570         $resp{volumes} = [];
571         $resp{copies}   = [];
572
573         my $titles      = (ref($$args{titles})) ? $$args{titles} : [];
574         my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
575         my $copies      = (ref($$args{copies})) ? $$args{copies} : [];
576
577         for my $title (@$titles) {
578                 my $notes = $U->storagereq(
579                         'open-ils.storage.direct.biblio.record_note.search_where.atomic', 
580                         { record => $title, pub => 't' });
581                 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
582         }
583
584         for my $volume (@$volumes) {
585                 my $notes = $U->storagereq(
586                         'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
587                         { call_number => $volume, pub => 't' });
588                 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
589         }
590
591
592         for my $copy (@$copies) {
593                 $logger->debug("Fetching copy notes for copy $copy");
594                 my $notes = $U->storagereq(
595                         'open-ils.storage.direct.asset.copy_note.search_where.atomic',
596                         { owning_copy => $copy, pub => 't' });
597                 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;
598         }
599
600         return \%resp;
601 }
602
603 =cut
604
605
606
607
608
609
610
611 1;