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