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