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