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