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