]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
[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 OpenILS::Application::AppUtils;
15 my $apputils = "OpenILS::Application::AppUtils";
16 my $U = $apputils;
17 use OpenSRF::Utils;
18 use OpenILS::Utils::ModsParser;
19 use OpenILS::Event;
20 use OpenSRF::EX qw(:try);
21 use OpenSRF::Utils::Logger qw(:logger);
22 #my $logger = "OpenSRF::Utils::Logger";
23
24
25 # ------------------------------------------------------------------------
26 # Top level Circ package;
27 # ------------------------------------------------------------------------
28
29 sub initialize {
30         my $self = shift;
31         OpenILS::Application::Circ::Rules->initialize();
32         OpenILS::Application::Circ::Circulate->initialize();
33 }
34
35
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",
42         NOTES           => <<"  NOTES");
43         Returns a list of open circulations as a pile of objects.  each object
44         contains the relevant copy, circ, and record
45         NOTES
46
47 sub checkouts_by_user {
48         my( $self, $client, $user_session, $user_id ) = @_;
49
50         my( $requestor, $target, $copy, $record, $evt );
51
52         ( $requestor, $target, $evt ) = 
53                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
54         return $evt if $evt;
55
56         my $circs = $apputils->simplereq(
57                 'open-ils.storage',
58                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
59                 { usr => $target->id, checkin_time => undef } );
60 #               { usr => $target->id } );
61
62         my @results;
63         for my $circ (@$circs) {
64
65                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
66                 return $evt if $evt;
67
68                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
69
70                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
71                 return $evt if $evt;
72
73                 my $mods = $apputils->record_to_mvr($record);
74
75                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
76         }
77
78         return \@results;
79
80 }
81
82
83
84 __PACKAGE__->register_method(
85         method  => "checkouts_by_user_slim",
86         api_name        => "open-ils.circ.actor.user.checked_out.slim",
87         NOTES           => <<"  NOTES");
88         Returns a list of open circulation objects
89         NOTES
90
91 sub checkouts_by_user_slim {
92         my( $self, $client, $user_session, $user_id ) = @_;
93
94         my( $requestor, $target, $copy, $record, $evt );
95
96         ( $requestor, $target, $evt ) = 
97                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
98         return $evt if $evt;
99
100         $logger->debug( 'User ' . $requestor->id . 
101                 " retrieving checked out items for user " . $target->id );
102
103         # XXX Make the call correct..
104         return $apputils->simplereq(
105                 'open-ils.storage',
106                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
107                 { usr => $target->id, checkin_time => undef } );
108 #               { usr => $target->id } );
109 }
110
111
112
113
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
120         NOTES
121
122 sub title_from_transaction {
123         my( $self, $client, $login_session, $transactionid ) = @_;
124
125         my( $user, $circ, $title, $evt );
126
127         ( $user, $evt ) = $apputils->checkses( $login_session );
128         return $evt if $evt;
129
130         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
131         return $evt if $evt;
132         
133         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
134         return $evt if $evt;
135
136         return $apputils->record_to_mvr($title);
137 }
138
139
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
147         NOTES
148
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
156         NOTES
157
158 sub set_circ_lost {
159         my( $self, $client, $login, $circid ) = @_;
160         my( $user, $circ, $evt );
161
162         ( $user, $evt ) = $apputils->checkses($login);
163         return $evt if $evt;
164
165         ( $circ, $evt ) = $apputils->fetch_circulation( $circid );
166         return $evt if $evt;
167
168         if($self->api_name =~ /lost/) {
169                 if($evt = $apputils->checkperms(
170                         $user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
171                         return $evt;
172                 }
173                 $circ->stop_fines("LOST");              
174         }
175
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")) {
180                         return $evt;
181                 }
182                 $circ->stop_fines("CLAIMSRETURNED");
183         }
184
185         my $s = $apputils->simplereq(
186                 'open-ils.storage',
187                 "open-ils.storage.direct.action.circulation.update", $circ );
188
189         if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
190 }
191
192 __PACKAGE__->register_method(
193         method          => "create_in_house_use",
194         api_name                => 'open-ils.circ.in_house_use.create',
195         signature       =>      q/
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
204         /);
205
206 sub create_in_house_use {
207         my( $self, $client, $authtoken, $params ) = @_;
208
209         my( $staff, $evt, $copy );
210         my $org                 = $params->{location};
211         my $copyid              = $params->{copyid};
212         my $count               = $params->{count} || 1;
213         my $use_time    = $params->{use_time} || 'now';
214
215         ($staff, $evt) = $U->checkses($authtoken);
216         return $evt if $evt;
217
218         ($copy, $evt) = $U->fetch_copy($copyid);
219         return $evt if $evt;
220
221         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
222         return $evt if $evt;
223
224         $logger->activity("User " . $staff->id .
225                 " creating $count in-house use(s) for copy $copyid at location $org");
226
227         if( $use_time ne 'now' ) {
228                 my @dates               = split(/ /, $use_time);
229                 my ($y,$m,$d)   = split(/-/, $dates[0]);
230                 my ($h,$min,$s) = split(/:/, $dates[1]);
231                 $logger->debug("in_house_use setting use time to $use_time");
232                 $use_time = OpenILS::Application::Circ::Circulate::_create_date_stamp($s,$min,$h,$d,$m,$y);
233         }
234
235         my @ids;
236         for(1..$count) {
237                 my $ihu = Fieldmapper::action::in_house_use->new;
238
239                 $ihu->item($copyid);
240                 $ihu->staff($staff->id);
241                 $ihu->org_unit($org);
242                 $ihu->use_time($use_time);
243
244                 my $id = $U->simplereq(
245                         'open-ils.storage',
246                         'open-ils.storage.direct.action.in_house_use.create', $ihu );
247
248                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
249                 push @ids, $id;
250         }
251
252         return \@ids;
253 }
254
255
256
257 __PACKAGE__->register_method(
258         method  => "view_circ_patrons",
259         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
260         notes           => q/
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
266         /);
267
268 sub view_circ_patrons {
269         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
270
271         my( $requestor, $evt ) = $U->checksesperm(
272                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
273         return $evt if $evt;
274
275         return [] unless $count;
276
277         my $circs = $U->storagereq(
278                 'open-ils.storage.direct.action.circulation.search_where.atomic',
279                         { 
280                                 target_copy => $copyid, 
281                                 opac_renewal => 'f',   
282                                 desk_renewal => 'f',
283                                 phone_renewal => 'f',
284                         }, 
285                         { 
286                                 limit => $count, 
287                                 order_by => "xact_start DESC" 
288                         } );
289
290
291         my @users;
292         push(@users, $_->usr) for @$circs;
293         return \@users;
294 }
295
296
297
298 __PACKAGE__->register_method(
299         method          => 'fetch_notes',
300         api_name                => 'open-ils.circ.copy_note.retrieve.all',
301         signature       => q/
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
308         /);
309
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/);
314
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/);
319
320 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
321 sub fetch_notes {
322         my( $self, $connection, $args ) = @_;
323
324         my $id = $$args{itemid};
325         my $authtoken = $$args{authtoken};
326         my( $r, $evt);
327
328         if( $self->api_name =~ /copy/ ) {
329                 if( $$args{pub} ) {
330                         return $U->storagereq(
331                                 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
332                                 { owning_copy => $id, pub => 't' } );
333                 } else {
334                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_COPY_NOTES');
335                         return $evt if $evt;
336                         return $U->storagereq(
337                                 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
338                 }
339
340         } elsif( $self->api_name =~ /call_number/ ) {
341                 if( $$args{pub} ) {
342                         return $U->storagereq(
343                                 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
344                                 { call_number => $id, pub => 't' } );
345                 } else {
346                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_VOLUME_NOTES');
347                         return $evt if $evt;
348                         return $U->storagereq(
349                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
350                 }
351
352         } elsif( $self->api_name =~ /title/ ) {
353                 if( $$args{pub} ) {
354                         return $U->storagereq(
355                                 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
356                                 { record => $id, pub => 't' } );
357                 } else {
358                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_TITLE_NOTES');
359                         return $evt if $evt;
360                         return $U->storagereq(
361                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
362                 }
363         }
364
365         return undef;
366 }
367
368 __PACKAGE__->register_method(
369         method          => 'create_copy_note',
370         api_name                => 'open-ils.circ.copy_note.create',
371         signature       => q/
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
376         /);
377
378 sub create_copy_note {
379         my( $self, $connection, $authtoken, $note ) = @_;
380         my( $cnowner, $requestor, $evt );
381
382         ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
383         return $evt if $evt;
384         ($requestor, $evt) = $U->checkses($authtoken);
385         return $evt if $evt;
386         $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
387         return $evt if $evt;
388
389         $note->create_date('now');
390         $note->pub( ($note->pub) ? 't' : 'f' );
391
392         my $id = $U->storagereq(
393                 'open-ils.storage.direct.asset.copy_note.create', $note );
394         return $U->DB_UPDATE_FAILED($note) unless $id;
395
396         $logger->activity("User ".$requestor->id." created a new copy ".
397                 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
398
399         return $id;
400 }
401
402 __PACKAGE__->register_method(
403         method          => 'delete_copy_note',
404         api_name                =>      'open-ils.circ.copy_note.delete',
405         signature       => q/
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.
410                 /);
411
412 sub delete_copy_note {
413         my( $self, $conn, $authtoken, $noteid ) = @_;
414         my( $requestor, $note, $owner, $evt );
415
416         ($requestor, $evt) = $U->checkses($authtoken);
417         return $evt if $evt;
418
419         ($note, $evt) = $U->fetch_copy_note($noteid);
420         return $evt if $evt;
421
422         if( $note->creator ne $requestor->id ) {
423                 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
424                 return $evt if $evt;
425                 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
426                 return $evt if $evt;
427         }
428
429         my $stat = $U->storagereq(
430                 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
431         return $U->DB_UPDATE_FAILED($noteid) unless $stat;
432
433         $logger->activity("User ".$requestor->id." deleted copy note $noteid");
434         return 1;
435 }
436
437
438 __PACKAGE__->register_method(
439         method          => 'note_batch',
440         api_name                => 'open-ils.circ.biblio_notes.public.batch.retrieve',
441         signature       => q/
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:
445                         {
446                                 "titles"                : [ { id : $id, notes : [ n1, n2 ] },... ]
447                                 "volumes"       : [ { id : $id, notes : [ n1, n2 ] },... ]
448                                 "copies"                : [ { id : $id, notes : [ n1, n2 ] },... ]
449                         }
450         /
451 );
452
453 sub note_batch {
454         my( $self, $conn, $titleid ) = @_;
455
456         my @copies;
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 );
461
462         for my $c (@$cns) {
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);
467         }
468
469         return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
470 }
471
472
473 sub _note_batch {
474         my $args = shift;
475
476         my %resp;
477         $resp{titles}   = [];
478         $resp{volumes} = [];
479         $resp{copies}   = [];
480
481         my $titles      = (ref($$args{titles})) ? $$args{titles} : [];
482         my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
483         my $copies      = (ref($$args{copies})) ? $$args{copies} : [];
484
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;
490         }
491
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;
497         }
498
499
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;
506         }
507
508         return \%resp;
509 }
510
511
512
513
514
515
516
517 1;