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