]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
added backdate support for claims_returned
[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, $args ) = @_;
163         my( $user, $circ, $copy, $evt );
164
165         my $barcode             = $$args{barcode};
166         my $backdate    = $$args{backdate};
167
168         ( $user, $evt ) = $U->checkses($login);
169         return $evt if $evt;
170
171         # Grab the related copy
172         ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
173         return $evt if $evt;
174
175         my $isclaims    = $self->api_name =~ /claims_returned/;
176         my $islost              = $self->api_name =~ /lost/;
177         my $session             = $U->start_db_session(); 
178
179
180         # if setting to list, update the copy's statua
181         if( $islost ) {
182                 my $newstat = $U->copy_status_from_name('lost') if $islost;
183                 if( $copy->status ne $newstat->id ) {
184                         $copy->status($newstat);
185                         $U->update_copy(copy => $copy, editor => $user->id, session => $session);
186                 }
187         }
188
189         # grab the circulation
190         ( $circ ) = $U->fetch_open_circulation( $copy->id );
191         return 1 unless $circ;
192
193
194         if($islost) {
195                 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_LOST');
196                 return $evt if $evt;
197                 $circ->stop_fines("LOST");              
198         }
199
200         if($isclaims) {
201
202                 $evt = $U->check_perms($user->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
203                 return $evt if $evt;
204                 $circ->stop_fines("CLAIMSRETURNED");
205
206                 # allow the caller to backdate the circulation and void any fines
207                 # that occurred after the backdate
208                 if($backdate) {
209                         OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
210                                 $backdate, $circ, $user, $session );
211                 }
212         }
213
214         my $s = $session->request(
215                 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
216
217         return $U->DB_UPDATE_FAILED($circ) unless defined($s);
218         $U->commit_db_session($session);
219
220         return 1;
221 }
222
223 __PACKAGE__->register_method(
224         method          => "create_in_house_use",
225         api_name                => 'open-ils.circ.in_house_use.create',
226         signature       =>      q/
227                 Creates an in-house use action.
228                 @param $authtoken The login session key
229                 @param params A hash of params including
230                         'location' The org unit id where the in-house use occurs
231                         'copyid' The copy in question
232                         'count' The number of in-house uses to apply to this copy
233                 @return An array of id's representing the id's of the newly created
234                 in-house use objects or an event on an error
235         /);
236
237 sub create_in_house_use {
238         my( $self, $client, $authtoken, $params ) = @_;
239
240         my( $staff, $evt, $copy );
241         my $org                 = $params->{location};
242         my $copyid              = $params->{copyid};
243         my $count               = $params->{count} || 1;
244         my $use_time    = $params->{use_time} || 'now';
245
246         if(!$copyid) {
247                 my $barcode = $params->{barcode};
248                 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
249                 return $evt if $evt;
250                 $copyid = $copy->id;
251         }
252
253         ($staff, $evt) = $U->checkses($authtoken);
254         return $evt if $evt;
255
256         ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
257         return $evt if $evt;
258
259         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
260         return $evt if $evt;
261
262         $logger->activity("User " . $staff->id .
263                 " creating $count in-house use(s) for copy $copyid at location $org");
264
265         if( $use_time ne 'now' ) {
266                 $use_time = clense_ISO8601($use_time);
267                 $logger->debug("in_house_use setting use time to $use_time");
268         }
269
270         my @ids;
271         for(1..$count) {
272                 my $ihu = Fieldmapper::action::in_house_use->new;
273
274                 $ihu->item($copyid);
275                 $ihu->staff($staff->id);
276                 $ihu->org_unit($org);
277                 $ihu->use_time($use_time);
278
279                 my $id = $U->simplereq(
280                         'open-ils.storage',
281                         'open-ils.storage.direct.action.in_house_use.create', $ihu );
282
283                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
284                 push @ids, $id;
285         }
286
287         return \@ids;
288 }
289
290
291
292 __PACKAGE__->register_method(
293         method  => "view_circ_patrons",
294         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
295         notes           => q/
296                 Retrieves the last X users who checked out a given copy
297                 @param authtoken The login session key
298                 @param copyid The copy to check
299                 @param count How far to go back in the item history
300                 @return An array of patron ids
301         /);
302
303 sub view_circ_patrons {
304         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
305
306         my( $requestor, $evt ) = $U->checksesperm(
307                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
308         return $evt if $evt;
309
310         return [] unless $count;
311
312         my $circs = $U->storagereq(
313                 'open-ils.storage.direct.action.circulation.search_where.atomic',
314                         { 
315                                 target_copy => $copyid, 
316                                 opac_renewal => 'f',   
317                                 desk_renewal => 'f',
318                                 phone_renewal => 'f',
319                         }, 
320                         { 
321                                 limit => $count, 
322                                 order_by => "xact_start DESC" 
323                         } );
324
325
326         my @users;
327         push(@users, $_->usr) for @$circs;
328         return \@users;
329 }
330
331
332
333 __PACKAGE__->register_method(
334         method          => 'fetch_notes',
335         api_name                => 'open-ils.circ.copy_note.retrieve.all',
336         signature       => q/
337                 Returns an array of copy note objects.  
338                 @param args A named hash of parameters including:
339                         authtoken       : Required if viewing non-public notes
340                         itemid          : The id of the item whose notes we want to retrieve
341                         pub                     : True if all the caller wants are public notes
342                 @return An array of note objects
343         /);
344
345 __PACKAGE__->register_method(
346         method          => 'fetch_notes',
347         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
348         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
349
350 __PACKAGE__->register_method(
351         method          => 'fetch_notes',
352         api_name                => 'open-ils.circ.title_note.retrieve.all',
353         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
354
355 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
356 sub fetch_notes {
357         my( $self, $connection, $args ) = @_;
358
359         my $id = $$args{itemid};
360         my $authtoken = $$args{authtoken};
361         my( $r, $evt);
362
363         if( $self->api_name =~ /copy/ ) {
364                 if( $$args{pub} ) {
365                         return $U->storagereq(
366                                 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
367                                 { owning_copy => $id, pub => 't' } );
368                 } else {
369                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_COPY_NOTES');
370                         return $evt if $evt;
371                         return $U->storagereq(
372                                 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
373                 }
374
375         } elsif( $self->api_name =~ /call_number/ ) {
376                 if( $$args{pub} ) {
377                         return $U->storagereq(
378                                 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
379                                 { call_number => $id, pub => 't' } );
380                 } else {
381                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_VOLUME_NOTES');
382                         return $evt if $evt;
383                         return $U->storagereq(
384                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
385                 }
386
387         } elsif( $self->api_name =~ /title/ ) {
388                 if( $$args{pub} ) {
389                         return $U->storagereq(
390                                 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
391                                 { record => $id, pub => 't' } );
392                 } else {
393                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_TITLE_NOTES');
394                         return $evt if $evt;
395                         return $U->storagereq(
396                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
397                 }
398         }
399
400         return undef;
401 }
402
403 __PACKAGE__->register_method(
404         method          => 'create_copy_note',
405         api_name                => 'open-ils.circ.copy_note.create',
406         signature       => q/
407                 Creates a new copy note
408                 @param authtoken The login session key
409                 @param note     The note object to create
410                 @return The id of the new note object
411         /);
412
413 sub create_copy_note {
414         my( $self, $connection, $authtoken, $note ) = @_;
415         my( $cnowner, $requestor, $evt );
416
417         ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
418         return $evt if $evt;
419         ($requestor, $evt) = $U->checkses($authtoken);
420         return $evt if $evt;
421         $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
422         return $evt if $evt;
423
424         $note->create_date('now');
425         $note->pub( ($note->pub) ? 't' : 'f' );
426
427         my $id = $U->storagereq(
428                 'open-ils.storage.direct.asset.copy_note.create', $note );
429         return $U->DB_UPDATE_FAILED($note) unless $id;
430
431         $logger->activity("User ".$requestor->id." created a new copy ".
432                 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
433
434         return $id;
435 }
436
437 __PACKAGE__->register_method(
438         method          => 'delete_copy_note',
439         api_name                =>      'open-ils.circ.copy_note.delete',
440         signature       => q/
441                 Deletes an existing copy note
442                 @param authtoken The login session key
443                 @param noteid The id of the note to delete
444                 @return 1 on success - Event otherwise.
445                 /);
446
447 sub delete_copy_note {
448         my( $self, $conn, $authtoken, $noteid ) = @_;
449         my( $requestor, $note, $owner, $evt );
450
451         ($requestor, $evt) = $U->checkses($authtoken);
452         return $evt if $evt;
453
454         ($note, $evt) = $U->fetch_copy_note($noteid);
455         return $evt if $evt;
456
457         if( $note->creator ne $requestor->id ) {
458                 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
459                 return $evt if $evt;
460                 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
461                 return $evt if $evt;
462         }
463
464         my $stat = $U->storagereq(
465                 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
466         return $U->DB_UPDATE_FAILED($noteid) unless $stat;
467
468         $logger->activity("User ".$requestor->id." deleted copy note $noteid");
469         return 1;
470 }
471
472
473 __PACKAGE__->register_method(
474         method          => 'note_batch',
475         api_name                => 'open-ils.circ.biblio_notes.public.batch.retrieve',
476         signature       => q/
477                 Returns a set of notes for a given set of titles, volumes, and copies.
478                 @param titleid The id of the title who's notes are retrieving
479                 @return A list like so:
480                         {
481                                 "titles"                : [ { id : $id, notes : [ n1, n2 ] },... ]
482                                 "volumes"       : [ { id : $id, notes : [ n1, n2 ] },... ]
483                                 "copies"                : [ { id : $id, notes : [ n1, n2 ] },... ]
484                         }
485         /
486 );
487
488 sub note_batch {
489         my( $self, $conn, $titleid ) = @_;
490
491         my @copies;
492         my $cns = $U->storagereq(
493                 'open-ils.storage.id_list.asset.call_number.search_where.atomic', 
494                 { record => $titleid, deleted => 'f' } );
495                 #'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
496
497         for my $c (@$cns) {
498                 my $copyids = $U->storagereq(
499                         #'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
500                         'open-ils.storage.id_list.asset.copy.search_where.atomic', { call_number => $c, deleted => 'f' });
501                 push(@copies, @$copyids);
502         }
503
504         return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
505 }
506
507
508 sub _note_batch {
509         my $args = shift;
510
511         my %resp;
512         $resp{titles}   = [];
513         $resp{volumes} = [];
514         $resp{copies}   = [];
515
516         my $titles      = (ref($$args{titles})) ? $$args{titles} : [];
517         my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
518         my $copies      = (ref($$args{copies})) ? $$args{copies} : [];
519
520         for my $title (@$titles) {
521                 my $notes = $U->storagereq(
522                         'open-ils.storage.direct.biblio.record_note.search_where.atomic', 
523                         { record => $title, pub => 't' });
524                 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
525         }
526
527         for my $volume (@$volumes) {
528                 my $notes = $U->storagereq(
529                         'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
530                         { call_number => $volume, pub => 't' });
531                 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
532         }
533
534
535         for my $copy (@$copies) {
536                 $logger->debug("Fetching copy notes for copy $copy");
537                 my $notes = $U->storagereq(
538                         'open-ils.storage.direct.asset.copy_note.search_where.atomic',
539                         { owning_copy => $copy, pub => 't' });
540                 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;
541         }
542
543         return \%resp;
544 }
545
546
547
548
549
550
551
552 1;