]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
started work on the notes interface in the opac.
[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 OpenILS::Application::AppUtils;
15 my $apputils = "OpenILS::Application::AppUtils";
16 my $U = $apputils;
17 use OpenSRF::Utils;
18 use OpenILS::Utils::ModsParser;
19 use OpenILS::Event;
20 use OpenSRF::EX qw(:try);
21 use OpenSRF::Utils::Logger qw(:logger);
22 #my $logger = "OpenSRF::Utils::Logger";
23
24
25 # ------------------------------------------------------------------------
26 # Top level Circ package;
27 # ------------------------------------------------------------------------
28
29 sub initialize {
30         my $self = shift;
31         OpenILS::Application::Circ::Rules->initialize();
32         OpenILS::Application::Circ::Circulate->initialize();
33 }
34
35
36 # ------------------------------------------------------------------------
37 # Returns an array of {circ, record} hashes checked out by the user.
38 # ------------------------------------------------------------------------
39 __PACKAGE__->register_method(
40         method  => "checkouts_by_user",
41         api_name        => "open-ils.circ.actor.user.checked_out",
42         NOTES           => <<"  NOTES");
43         Returns a list of open circulations as a pile of objects.  each object
44         contains the relevant copy, circ, and record
45         NOTES
46
47 sub checkouts_by_user {
48         my( $self, $client, $user_session, $user_id ) = @_;
49
50         my( $requestor, $target, $copy, $record, $evt );
51
52         ( $requestor, $target, $evt ) = 
53                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
54         return $evt if $evt;
55
56         my $circs = $apputils->simplereq(
57                 'open-ils.storage',
58                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
59                 { usr => $target->id, checkin_time => undef } );
60 #               { usr => $target->id } );
61
62         my @results;
63         for my $circ (@$circs) {
64
65                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
66                 return $evt if $evt;
67
68                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
69
70                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
71                 return $evt if $evt;
72
73                 my $mods = $apputils->record_to_mvr($record);
74
75                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
76         }
77
78         return \@results;
79
80 }
81
82
83
84 __PACKAGE__->register_method(
85         method  => "checkouts_by_user_slim",
86         api_name        => "open-ils.circ.actor.user.checked_out.slim",
87         NOTES           => <<"  NOTES");
88         Returns a list of open circulation objects
89         NOTES
90
91 sub checkouts_by_user_slim {
92         my( $self, $client, $user_session, $user_id ) = @_;
93
94         my( $requestor, $target, $copy, $record, $evt );
95
96         ( $requestor, $target, $evt ) = 
97                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
98         return $evt if $evt;
99
100         $logger->debug( 'User ' . $requestor->id . 
101                 " retrieving checked out items for user " . $target->id );
102
103         # XXX Make the call correct..
104         return $apputils->simplereq(
105                 'open-ils.storage',
106                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
107                 { usr => $target->id, checkin_time => undef } );
108 #               { usr => $target->id } );
109 }
110
111
112
113
114 __PACKAGE__->register_method(
115         method  => "title_from_transaction",
116         api_name        => "open-ils.circ.circ_transaction.find_title",
117         NOTES           => <<"  NOTES");
118         Returns a mods object for the title that is linked to from the 
119         copy from the hold that created the given transaction
120         NOTES
121
122 sub title_from_transaction {
123         my( $self, $client, $login_session, $transactionid ) = @_;
124
125         my( $user, $circ, $title, $evt );
126
127         ( $user, $evt ) = $apputils->checkses( $login_session );
128         return $evt if $evt;
129
130         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
131         return $evt if $evt;
132         
133         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
134         return $evt if $evt;
135
136         return $apputils->record_to_mvr($title);
137 }
138
139
140 __PACKAGE__->register_method(
141         method  => "set_circ_lost",
142         api_name        => "open-ils.circ.circulation.set_lost",
143         NOTES           => <<"  NOTES");
144         Params are login, circid
145         login must have SET_CIRC_LOST perms
146         Sets a circulation to lost
147         NOTES
148
149 __PACKAGE__->register_method(
150         method  => "set_circ_lost",
151         api_name        => "open-ils.circ.circulation.set_claims_returned",
152         NOTES           => <<"  NOTES");
153         Params are login, circid
154         login must have SET_CIRC_MISSING perms
155         Sets a circulation to lost
156         NOTES
157
158 sub set_circ_lost {
159         my( $self, $client, $login, $circid ) = @_;
160         my( $user, $circ, $evt );
161
162         ( $user, $evt ) = $apputils->checkses($login);
163         return $evt if $evt;
164
165         ( $circ, $evt ) = $apputils->fetch_circulation( $circid );
166         return $evt if $evt;
167
168         if($self->api_name =~ /lost/) {
169                 if($evt = $apputils->checkperms(
170                         $user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
171                         return $evt;
172                 }
173                 $circ->stop_fines("LOST");              
174         }
175
176         # XXX Back date the checkin time so the patron has no fines
177         if($self->api_name =~ /claims_returned/) {
178                 if($evt = $apputils->checkperms(
179                         $user->id, $circ->circ_lib, "SET_CIRC_CLAIMS_RETURNED")) {
180                         return $evt;
181                 }
182                 $circ->stop_fines("CLAIMSRETURNED");
183         }
184
185         my $s = $apputils->simplereq(
186                 'open-ils.storage',
187                 "open-ils.storage.direct.action.circulation.update", $circ );
188
189         if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
190 }
191
192 __PACKAGE__->register_method(
193         method          => "create_in_house_use",
194         api_name                => 'open-ils.circ.in_house_use.create',
195         signature       =>      q/
196                 Creates an in-house use action.
197                 @param $authtoken The login session key
198                 @param params A hash of params including
199                         'location' The org unit id where the in-house use occurs
200                         'copyid' The copy in question
201                         'count' The number of in-house uses to apply to this copy
202                 @return An array of id's representing the id's of the newly created
203                 in-house use objects or an event on an error
204         /);
205
206 sub create_in_house_use {
207         my( $self, $client, $authtoken, $params ) = @_;
208
209         my( $staff, $evt, $copy );
210         my $org         = $params->{location};
211         my $copyid      = $params->{copyid};
212         my $count       = $params->{count} || 1;
213
214         ($staff, $evt) = $U->checkses($authtoken);
215         return $evt if $evt;
216
217         ($copy, $evt) = $U->fetch_copy($copyid);
218         return $evt if $evt;
219
220         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
221         return $evt if $evt;
222
223         $logger->activity("User " . $staff->id .
224                 " creating $count in-house use(s) for copy $copyid at location $org");
225
226         my @ids;
227         for(1..$count) {
228                 my $ihu = Fieldmapper::action::in_house_use->new;
229
230                 $ihu->item($copyid);
231                 $ihu->staff($staff->id);
232                 $ihu->org_unit($org);
233                 $ihu->use_time('now');
234
235                 my $id = $U->simplereq(
236                         'open-ils.storage',
237                         'open-ils.storage.direct.action.in_house_use.create', $ihu );
238
239                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
240                 push @ids, $id;
241         }
242
243         return \@ids;
244 }
245
246
247
248 __PACKAGE__->register_method(
249         method  => "view_circ_patrons",
250         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
251         notes           => q/
252                 Retrieves the last X users who checked out a given copy
253                 @param authtoken The login session key
254                 @param copyid The copy to check
255                 @param count How far to go back in the item history
256                 @return An array of patron ids
257         /);
258
259 sub view_circ_patrons {
260         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
261
262         my( $requestor, $evt ) = $U->checksesperm(
263                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
264         return $evt if $evt;
265
266         return [] unless $count;
267
268         my $circs = $U->storagereq(
269                 'open-ils.storage.direct.action.circulation.search_where.atomic',
270                         { 
271                                 target_copy => $copyid, 
272                                 opac_renewal => 'f',   
273                                 desk_renewal => 'f',
274                                 phone_renewal => 'f',
275                         }, 
276                         { 
277                                 limit => $count, 
278                                 order_by => "xact_start DESC" 
279                         } );
280
281
282         my @users;
283         push(@users, $_->usr) for @$circs;
284         return \@users;
285 }
286
287
288
289 __PACKAGE__->register_method(
290         method          => 'fetch_notes',
291         api_name                => 'open-ils.circ.copy_note.retrieve.all',
292         signature       => q/
293                 Returns an array of copy note objects.  
294                 @param args A named hash of parameters including:
295                         authtoken       : Required if viewing non-public notes
296                         itemid          : The id of the item whose notes we want to retrieve
297                         pub                     : True if all the caller wants are public notes
298                 @return An array of note objects
299         /);
300
301 __PACKAGE__->register_method(
302         method          => 'fetch_notes',
303         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
304         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
305
306 __PACKAGE__->register_method(
307         method          => 'fetch_notes',
308         api_name                => 'open-ils.circ.title_note.retrieve.all',
309         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
310
311 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
312 sub fetch_notes {
313         my( $self, $connection, $args ) = @_;
314
315         my $id = $$args{itemid};
316         my $authtoken = $$args{authtoken};
317         my( $r, $evt);
318
319         if( $self->api_name =~ /copy/ ) {
320                 if( $$args{pub} ) {
321                         return $U->storagereq(
322                                 'open-ils.storage.direct.asset.copy_note.search_where.atomic',
323                                 { owning_copy => $id, pub => 't' } );
324                 } else {
325                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_COPY_NOTES');
326                         return $evt if $evt;
327                         return $U->storagereq(
328                                 'open-ils.storage.direct.asset.copy_note.search.owning_copy.atomic', $id );
329                 }
330
331         } elsif( $self->api_name =~ /call_number/ ) {
332                 if( $$args{pub} ) {
333                         return $U->storagereq(
334                                 'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
335                                 { call_number => $id, pub => 't' } );
336                 } else {
337                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_VOLUME_NOTES');
338                         return $evt if $evt;
339                         return $U->storagereq(
340                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
341                 }
342
343         } elsif( $self->api_name =~ /title/ ) {
344                 if( $$args{pub} ) {
345                         return $U->storagereq(
346                                 'open-ils.storage.direct.bilbio.record_note.search_where.atomic',
347                                 { record => $id, pub => 't' } );
348                 } else {
349                         ( $r, $evt ) = $U->checksesperms($authtoken, 'VIEW_TITLE_NOTES');
350                         return $evt if $evt;
351                         return $U->storagereq(
352                                 'open-ils.storage.direct.asset.call_number_note.search.call_number.atomic', $id );
353                 }
354         }
355
356         return undef;
357 }
358
359 __PACKAGE__->register_method(
360         method          => 'create_copy_note',
361         api_name                => 'open-ils.circ.copy_note.create',
362         signature       => q/
363                 Creates a new copy note
364                 @param authtoken The login session key
365                 @param note     The note object to create
366                 @return The id of the new note object
367         /);
368
369 sub create_copy_note {
370         my( $self, $connection, $authtoken, $note ) = @_;
371         my( $cnowner, $requestor, $evt );
372
373         ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
374         return $evt if $evt;
375         ($requestor, $evt) = $U->checkses($authtoken);
376         return $evt if $evt;
377         $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
378         return $evt if $evt;
379
380         $note->create_date('now');
381         $note->pub( ($note->pub) ? 't' : 'f' );
382
383         my $id = $U->storagereq(
384                 'open-ils.storage.direct.asset.copy_note.create', $note );
385         return $U->DB_UPDATE_FAILED($note) unless $id;
386
387         $logger->activity("User ".$requestor->id." created a new copy ".
388                 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
389
390         return $id;
391 }
392
393 __PACKAGE__->register_method(
394         method          => 'delete_copy_note',
395         api_name                =>      'open-ils.circ.copy_note.delete',
396         signature       => q/
397                 Deletes an existing copy note
398                 @param authtoken The login session key
399                 @param noteid The id of the note to delete
400                 @return 1 on success - Event otherwise.
401                 /);
402
403 sub delete_copy_note {
404         my( $self, $conn, $authtoken, $noteid ) = @_;
405         my( $requestor, $note, $owner, $evt );
406
407         ($requestor, $evt) = $U->checkses($authtoken);
408         return $evt if $evt;
409
410         ($note, $evt) = $U->fetch_copy_note($noteid);
411         return $evt if $evt;
412
413         if( $note->creator ne $requestor->id ) {
414                 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
415                 return $evt if $evt;
416                 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
417                 return $evt if $evt;
418         }
419
420         my $stat = $U->storagereq(
421                 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
422         return $U->DB_UPDATE_FAILED($noteid) unless $stat;
423
424         $logger->activity("User ".$requestor->id." deleted copy note $noteid");
425         return 1;
426 }
427
428
429 __PACKAGE__->register_method(
430         method          => 'note_batch',
431         api_name                => 'open-ils.circ.biblio_notes.public.batch.retrieve',
432         signature       => q/
433                 Returns a set of notes for a given set of titles, volumes, and copies.
434                 @param titleid The id of the title who's notes are retrieving
435                 @return A list like so:
436                         {
437                                 "titles"                : [ { id : $id, notes : [ n1, n2 ] },... ]
438                                 "volumes"       : [ { id : $id, notes : [ n1, n2 ] },... ]
439                                 "copies"                : [ { id : $id, notes : [ n1, n2 ] },... ]
440                         }
441         /
442 );
443
444 sub note_batch {
445         my( $self, $conn, $titleid ) = @_;
446
447         my @copies;
448         my $cns = $U->storagereq(
449                 'open-ils.storage.id_list.asset.call_number.search.record.atomic', $titleid );
450
451         for my $c (@$cns) {
452                 my $copyids = $U->storagereq(
453                         'open-ils.storage.id_list.asset.copy.search.call_number.atomic', $c);
454                 push(@copies, @$copyids);
455         }
456
457         return _note_batch( { titles => [$titleid], volumes => $cns, copies => \@copies} );
458 }
459
460
461 sub _note_batch {
462         my $args = shift;
463
464         my %resp;
465         $resp{titles}   = [];
466         $resp{volumes} = [];
467         $resp{copies}   = [];
468
469         my $titles      = (ref($$args{titles})) ? $$args{titles} : [];
470         my $volumes = (ref($$args{volumes})) ? $$args{volumes} : [];
471         my $copies      = (ref($$args{copies})) ? $$args{copies} : [];
472
473         for my $title (@$titles) {
474                 my $notes = $U->storagereq(
475                         'open-ils.storage.direct.biblio.record_note.search_where.atomic', 
476                         { record => $title, pub => 't' });
477                 push(@{$resp{titles}}, {id => $title, notes => $notes}) if @$notes;
478         }
479
480         for my $volume (@$volumes) {
481                 my $notes = $U->storagereq(
482                         'open-ils.storage.direct.asset.call_number_note.search_where.atomic',
483                         { call_number => $volume, pub => 't' });
484                 push( @{$resp{volumes}}, {id => $volume, notes => $notes} ) if @$notes;
485         }
486
487
488         for my $copy (@$copies) {
489                 $logger->debug("Fetching copy notes for copy $copy");
490                 my $notes = $U->storagereq(
491                         'open-ils.storage.direct.asset.copy_note.search_where.atomic',
492                         { owning_copy => $copy, pub => 't' });
493                 push( @{$resp{copies}}, { id => $copy, notes => $notes }) if @$notes;
494         }
495
496         return \%resp;
497 }
498
499
500
501
502
503
504
505 1;