]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
moved mark-lost logic into cat/assetcommon and some transaction mgmt into circ/circco...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ.pm
1 package OpenILS::Application::Circ;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
4 use strict; use warnings;
5
6 use OpenILS::Application::Circ::Circulate;
7 use OpenILS::Application::Circ::Survey;
8 use OpenILS::Application::Circ::StatCat;
9 use OpenILS::Application::Circ::Holds;
10 use OpenILS::Application::Circ::HoldNotify;
11 use OpenILS::Application::Circ::Money;
12 use OpenILS::Application::Circ::NonCat;
13 use OpenILS::Application::Circ::CopyLocations;
14 use OpenILS::Application::Circ::CircCommon;
15
16 use DateTime;
17 use DateTime::Format::ISO8601;
18
19 use OpenILS::Application::AppUtils;
20
21 use OpenSRF::Utils qw/:datetime/;
22 use OpenILS::Utils::ModsParser;
23 use OpenILS::Event;
24 use OpenSRF::EX qw(:try);
25 use OpenSRF::Utils::Logger qw(:logger);
26 use OpenILS::Utils::Fieldmapper;
27 use OpenILS::Utils::Editor;
28 use OpenILS::Utils::CStoreEditor q/:funcs/;
29 use OpenILS::Const qw/:const/;
30 use OpenSRF::Utils::SettingsClient;
31 use OpenILS::Application::Cat::AssetCommon;
32
33 my $apputils = "OpenILS::Application::AppUtils";
34 my $U = $apputils;
35
36
37 # ------------------------------------------------------------------------
38 # Top level Circ package;
39 # ------------------------------------------------------------------------
40
41 sub initialize {
42         my $self = shift;
43         OpenILS::Application::Circ::Circulate->initialize();
44 }
45
46
47 __PACKAGE__->register_method(
48         method => 'retrieve_circ',
49         api_name        => 'open-ils.circ.retrieve',
50         signature => q/
51                 Retrieve a circ object by id
52                 @param authtoken Login session key
53                 @pararm circid The id of the circ object
54         /
55 );
56 sub retrieve_circ {
57         my( $s, $c, $a, $i ) = @_;
58         my $e = new_editor(authtoken => $a);
59         return $e->event unless $e->checkauth;
60         my $circ = $e->retrieve_action_circulation($i) or return $e->event;
61         if( $e->requestor->id ne $circ->usr ) {
62                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
63         }
64         return $circ;
65 }
66
67
68 __PACKAGE__->register_method(
69         method => 'fetch_circ_mods',
70         api_name => 'open-ils.circ.circ_modifier.retrieve.all');
71 sub fetch_circ_mods {
72     my($self, $conn, $args) = @_;
73     my $mods = new_editor()->retrieve_all_config_circ_modifier;
74     return [ map {$_->code} @$mods ] unless $$args{full};
75     return $mods;
76 }
77
78 __PACKAGE__->register_method(
79         method => 'fetch_bill_types',
80         api_name => 'open-ils.circ.billing_type.retrieve.all');
81 sub fetch_bill_types {
82         my $conf = OpenSRF::Utils::SettingsClient->new;
83         return $conf->config_value(
84                 'apps', 'open-ils.circ', 'app_settings', 'billing_types', 'type' );
85 }
86
87
88 __PACKAGE__->register_method(
89     method => 'ranged_billing_types',
90     api_name => 'open-ils.circ.billing_type.ranged.retrieve.all');
91
92 sub ranged_billing_types {
93     my($self, $conn, $auth, $org_id, $depth) = @_;
94     my $e = new_editor(authtoken => $auth);
95     return $e->event unless $e->checkauth;
96     return $e->event unless $e->allowed('VIEW_BILLING_TYPE', $org_id);
97     return $e->search_config_billing_type(
98         {owner => $U->get_org_full_path($org_id, $depth)});
99 }
100
101
102
103 # ------------------------------------------------------------------------
104 # Returns an array of {circ, record} hashes checked out by the user.
105 # ------------------------------------------------------------------------
106 __PACKAGE__->register_method(
107         method  => "checkouts_by_user",
108         api_name        => "open-ils.circ.actor.user.checked_out",
109         NOTES           => <<"  NOTES");
110         Returns a list of open circulations as a pile of objects.  Each object
111         contains the relevant copy, circ, and record
112         NOTES
113
114 sub checkouts_by_user {
115         my( $self, $client, $user_session, $user_id ) = @_;
116
117         my( $requestor, $target, $copy, $record, $evt );
118
119         ( $requestor, $target, $evt ) = 
120                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
121         return $evt if $evt;
122
123         my $circs = $apputils->simplereq(
124                 'open-ils.cstore',
125                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
126                 { usr => $target->id, checkin_time => undef } );
127 #               { usr => $target->id } );
128
129         my @results;
130         for my $circ (@$circs) {
131
132                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
133                 return $evt if $evt;
134
135                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
136
137                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
138                 return $evt if $evt;
139
140                 my $mods = $apputils->record_to_mvr($record);
141
142                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
143         }
144
145         return \@results;
146
147 }
148
149
150
151 __PACKAGE__->register_method(
152         method  => "checkouts_by_user_slim",
153         api_name        => "open-ils.circ.actor.user.checked_out.slim",
154         NOTES           => <<"  NOTES");
155         Returns a list of open circulation objects
156         NOTES
157
158 # DEPRECAT ME?? XXX
159 sub checkouts_by_user_slim {
160         my( $self, $client, $user_session, $user_id ) = @_;
161
162         my( $requestor, $target, $copy, $record, $evt );
163
164         ( $requestor, $target, $evt ) = 
165                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
166         return $evt if $evt;
167
168         $logger->debug( 'User ' . $requestor->id . 
169                 " retrieving checked out items for user " . $target->id );
170
171         # XXX Make the call correct..
172         return $apputils->simplereq(
173                 'open-ils.cstore',
174                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
175                 { usr => $target->id, checkin_time => undef } );
176 #               { usr => $target->id } );
177 }
178
179
180 __PACKAGE__->register_method(
181         method  => "checkouts_by_user_opac",
182         api_name        => "open-ils.circ.actor.user.checked_out.opac",);
183
184 # XXX Deprecate Me
185 sub checkouts_by_user_opac {
186         my( $self, $client, $auth, $user_id ) = @_;
187
188         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
189         return $e->event unless $e->checkauth;
190         $user_id ||= $e->requestor->id;
191         return $e->event unless 
192                 my $patron = $e->retrieve_actor_user($user_id);
193
194         my $data;
195         my $search = {usr => $user_id, stop_fines => undef};
196
197         if( $user_id ne $e->requestor->id ) {
198                 $data = $e->search_action_circulation(
199                         $search, {checkperm=>1, permorg=>$patron->home_ou})
200                         or return $e->event;
201
202         } else {
203                 $data = $e->search_action_circulation($search);
204         }
205
206         return $data;
207 }
208
209
210 __PACKAGE__->register_method(
211         method  => "title_from_transaction",
212         api_name        => "open-ils.circ.circ_transaction.find_title",
213         NOTES           => <<"  NOTES");
214         Returns a mods object for the title that is linked to from the 
215         copy from the hold that created the given transaction
216         NOTES
217
218 sub title_from_transaction {
219         my( $self, $client, $login_session, $transactionid ) = @_;
220
221         my( $user, $circ, $title, $evt );
222
223         ( $user, $evt ) = $apputils->checkses( $login_session );
224         return $evt if $evt;
225
226         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
227         return $evt if $evt;
228         
229         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
230         return $evt if $evt;
231
232         return $apputils->record_to_mvr($title);
233 }
234
235
236
237 __PACKAGE__->register_method(
238         method  => "new_set_circ_lost",
239         api_name        => "open-ils.circ.circulation.set_lost",
240         signature       => q/
241         Sets the copy and related open circulation to lost
242                 @param auth
243                 @param args : barcode
244         /
245 );
246
247
248 # ---------------------------------------------------------------------
249 # Sets a circulation to lost.  updates copy status to lost
250 # applies copy and/or prcoessing fees depending on org settings
251 # ---------------------------------------------------------------------
252 sub new_set_circ_lost {
253     my( $self, $conn, $auth, $args ) = @_;
254
255     my $e = new_editor(authtoken=>$auth, xact=>1);
256     return $e->die_event unless $e->checkauth;
257
258     my $barcode = $$args{barcode};
259     $logger->info("marking item lost $barcode");
260
261     # ---------------------------------------------------------------------
262     # gather the pieces
263     my $copy = $e->search_asset_copy([
264         {barcode=>$barcode, deleted=>'f'},
265         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])->[0] 
266             or return $e->die_event;
267
268     my $owning_lib = 
269         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
270             $copy->circ_lib : $copy->call_number->owning_lib;
271
272     my $circ = $e->search_action_circulation(
273         {checkin_time => undef, target_copy => $copy->id} )->[0]
274             or return $e->die_event;
275
276     $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
277
278     my $evt = OpenILS::Application::Cat::AssetCommon->set_item_lost($e, $copy->id);
279     return $evt if $evt;
280
281     $e->commit;
282     return 1;
283 }
284
285
286 __PACKAGE__->register_method(
287         method  => "set_circ_claims_returned",
288         api_name        => "open-ils.circ.circulation.set_claims_returned",
289         signature       => q/
290         Sets the circ for the given item as claims returned
291         If a backdate is provided, overdue fines will be voided
292         back to the backdate
293                 @param auth
294                 @param args : barcode, backdate
295         /
296 );
297
298 sub set_circ_claims_returned {
299     my( $self, $conn, $auth, $args ) = @_;
300
301     my $e = new_editor(authtoken=>$auth, xact=>1);
302     return $e->die_event unless $e->checkauth;
303
304     my $barcode = $$args{barcode};
305     my $backdate = $$args{backdate};
306
307     $logger->info("marking circ for item $barcode as claims returned".
308         (($backdate) ? " with backdate $backdate" : ''));
309
310     my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0] 
311         or return $e->die_event;
312
313     my $circ = $e->search_action_circulation(
314         {checkin_time => undef, target_copy => $copy->id})->[0]
315             or return $e->die_event;
316
317     $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib) 
318         or return $e->die_event;
319
320     $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
321         $circ->stop_fines_time('now') unless $circ->stop_fines_time;
322
323     if( $backdate ) {
324         # make it look like the circ stopped at the cliams returned time
325         $circ->stop_fines_time(clense_ISO8601($backdate));
326         my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
327         return $evt if $evt;
328     }
329
330     $e->update_action_circulation($circ) or return $e->die_event;
331     $e->commit;
332     return 1;
333 }
334
335
336
337
338
339 __PACKAGE__->register_method (
340         method          => 'set_circ_due_date',
341         api_name                => 'open-ils.circ.circulation.due_date.update',
342         signature       => q/
343                 Updates the due_date on the given circ
344                 @param authtoken
345                 @param circid The id of the circ to update
346                 @param date The timestamp of the new due date
347         /
348 );
349
350 sub set_circ_due_date {
351         my( $self, $conn, $auth, $circ_id, $date ) = @_;
352
353     my $e = new_editor(xact=>1, authtoken=>$auth);
354     return $e->die_event unless $e->checkauth;
355     my $circ = $e->retrieve_action_circulation($circ_id)
356         or return $e->die_event;
357
358     return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
359         $date = clense_ISO8601($date);
360         $circ->due_date($date);
361     $e->update_action_circulation($circ) or return $e->die_event;
362     $e->commit;
363
364     return $circ->id;
365 }
366
367
368 __PACKAGE__->register_method(
369         method          => "create_in_house_use",
370         api_name                => 'open-ils.circ.in_house_use.create',
371         signature       =>      q/
372                 Creates an in-house use action.
373                 @param $authtoken The login session key
374                 @param params A hash of params including
375                         'location' The org unit id where the in-house use occurs
376                         'copyid' The copy in question
377                         'count' The number of in-house uses to apply to this copy
378                 @return An array of id's representing the id's of the newly created
379                 in-house use objects or an event on an error
380         /);
381
382 __PACKAGE__->register_method(
383         method          => "create_in_house_use",
384         api_name                => 'open-ils.circ.non_cat_in_house_use.create',
385 );
386
387
388 sub create_in_house_use {
389         my( $self, $client, $auth, $params ) = @_;
390
391         my( $evt, $copy );
392         my $org                 = $params->{location};
393         my $copyid              = $params->{copyid};
394         my $count               = $params->{count} || 1;
395         my $nc_type             = $params->{non_cat_type};
396         my $use_time    = $params->{use_time} || 'now';
397
398         my $e = new_editor(xact=>1,authtoken=>$auth);
399         return $e->event unless $e->checkauth;
400         return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
401
402         my $non_cat = 1 if $self->api_name =~ /non_cat/;
403
404         unless( $non_cat ) {
405                 if( $copyid ) {
406                         $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
407                 } else {
408                         $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
409                                 or return $e->event;
410                         $copyid = $copy->id;
411                 }
412         }
413
414         if( $use_time ne 'now' ) {
415                 $use_time = clense_ISO8601($use_time);
416                 $logger->debug("in_house_use setting use time to $use_time");
417         }
418
419         my @ids;
420         for(1..$count) {
421
422                 my $ihu;
423                 my $method;
424                 my $cmeth;
425
426                 if($non_cat) {
427                         $ihu = Fieldmapper::action::non_cat_in_house_use->new;
428                         $ihu->item_type($nc_type);
429                         $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
430                         $cmeth = "create_action_non_cat_in_house_use";
431
432                 } else {
433                         $ihu = Fieldmapper::action::in_house_use->new;
434                         $ihu->item($copyid);
435                         $method = 'open-ils.storage.direct.action.in_house_use.create';
436                         $cmeth = "create_action_in_house_use";
437                 }
438
439                 $ihu->staff($e->requestor->id);
440                 $ihu->org_unit($org);
441                 $ihu->use_time($use_time);
442
443                 $ihu = $e->$cmeth($ihu) or return $e->event;
444                 push( @ids, $ihu->id );
445         }
446
447         $e->commit;
448         return \@ids;
449 }
450
451
452
453
454
455 __PACKAGE__->register_method(
456         method  => "view_circs",
457         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
458         notes           => q/
459                 Retrieves the last X circs for a given copy
460                 @param authtoken The login session key
461                 @param copyid The copy to check
462                 @param count How far to go back in the item history
463                 @return An array of circ ids
464         /);
465
466 # ----------------------------------------------------------------------
467 # Returns $count most recent circs.  If count exceeds the configured 
468 # max, use the configured max instead
469 # ----------------------------------------------------------------------
470 sub view_circs {
471         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
472
473     my $e = new_editor(authtoken => $authtoken);
474     return $e->event unless $e->checkauth;
475     
476     my $copy = $e->retrieve_asset_copy([
477         $copyid,
478         {   flesh => 1,
479             flesh_fields => {acp => ['call_number']}
480         }
481     ]) or return $e->event;
482
483     return $e->event unless $e->allowed(
484         'VIEW_COPY_CHECKOUT_HISTORY', 
485         ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ? 
486             $copy->circ_lib : $copy->call_number->owning_lib);
487         
488     my $max_history = $U->ou_ancestor_setting_value(
489         $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
490
491     if(defined $max_history) {
492         $count = $max_history unless defined $count and $count < $max_history;
493     } else {
494         $count = 4 unless defined $count;
495     }
496
497     return $e->search_action_circulation([
498         {target_copy => $copyid}, 
499         {limit => $count, order_by => { circ => "xact_start DESC" }} 
500     ]);
501 }
502
503
504 __PACKAGE__->register_method(
505         method  => "circ_count",
506         api_name        => "open-ils.circ.circulation.count",
507         notes           => q/
508                 Returns the number of times the item has circulated
509                 @param copyid The copy to check
510         /);
511
512 sub circ_count {
513         my( $self, $client, $copyid, $range ) = @_; 
514         my $e = OpenILS::Utils::Editor->new;
515         return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
516 }
517
518
519
520 __PACKAGE__->register_method(
521         method          => 'fetch_notes',
522         api_name                => 'open-ils.circ.copy_note.retrieve.all',
523         signature       => q/
524                 Returns an array of copy note objects.  
525                 @param args A named hash of parameters including:
526                         authtoken       : Required if viewing non-public notes
527                         itemid          : The id of the item whose notes we want to retrieve
528                         pub                     : True if all the caller wants are public notes
529                 @return An array of note objects
530         /);
531
532 __PACKAGE__->register_method(
533         method          => 'fetch_notes',
534         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
535         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
536
537 __PACKAGE__->register_method(
538         method          => 'fetch_notes',
539         api_name                => 'open-ils.circ.title_note.retrieve.all',
540         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
541
542
543 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
544 sub fetch_notes {
545         my( $self, $connection, $args ) = @_;
546
547         my $id = $$args{itemid};
548         my $authtoken = $$args{authtoken};
549         my( $r, $evt);
550
551         if( $self->api_name =~ /copy/ ) {
552                 if( $$args{pub} ) {
553                         return $U->cstorereq(
554                                 'open-ils.cstore.direct.asset.copy_note.search.atomic',
555                                 { owning_copy => $id, pub => 't' } );
556                 } else {
557                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
558                         return $evt if $evt;
559                         return $U->cstorereq(
560                                 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
561                 }
562
563         } elsif( $self->api_name =~ /call_number/ ) {
564                 if( $$args{pub} ) {
565                         return $U->cstorereq(
566                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
567                                 { call_number => $id, pub => 't' } );
568                 } else {
569                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
570                         return $evt if $evt;
571                         return $U->cstorereq(
572                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
573                 }
574
575         } elsif( $self->api_name =~ /title/ ) {
576                 if( $$args{pub} ) {
577                         return $U->cstorereq(
578                                 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
579                                 { record => $id, pub => 't' } );
580                 } else {
581                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
582                         return $evt if $evt;
583                         return $U->cstorereq(
584                                 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
585                 }
586         }
587
588         return undef;
589 }
590
591 __PACKAGE__->register_method(
592         method  => 'has_notes',
593         api_name        => 'open-ils.circ.copy.has_notes');
594 __PACKAGE__->register_method(
595         method  => 'has_notes',
596         api_name        => 'open-ils.circ.call_number.has_notes');
597 __PACKAGE__->register_method(
598         method  => 'has_notes',
599         api_name        => 'open-ils.circ.title.has_notes');
600
601
602 sub has_notes {
603         my( $self, $conn, $authtoken, $id ) = @_;
604         my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
605         return $editor->event unless $editor->checkauth;
606
607         my $n = $editor->search_asset_copy_note(
608                 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
609
610         $n = $editor->search_asset_call_number_note(
611                 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
612
613         $n = $editor->search_biblio_record_note(
614                 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
615
616         return scalar @$n;
617 }
618
619
620
621 __PACKAGE__->register_method(
622         method          => 'create_copy_note',
623         api_name                => 'open-ils.circ.copy_note.create',
624         signature       => q/
625                 Creates a new copy note
626                 @param authtoken The login session key
627                 @param note     The note object to create
628                 @return The id of the new note object
629         /);
630
631 sub create_copy_note {
632         my( $self, $connection, $authtoken, $note ) = @_;
633
634         my $e = new_editor(xact=>1, authtoken=>$authtoken);
635         return $e->event unless $e->checkauth;
636         my $copy = $e->retrieve_asset_copy(
637                 [
638                         $note->owning_copy,
639                         {       flesh => 1,
640                                 flesh_fields => { 'acp' => ['call_number'] }
641                         }
642                 ]
643         );
644
645         return $e->event unless 
646                 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
647
648         $note->create_date('now');
649         $note->creator($e->requestor->id);
650         $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
651         $note->clear_id;
652
653         $e->create_asset_copy_note($note) or return $e->event;
654         $e->commit;
655         return $note->id;
656 }
657
658
659 __PACKAGE__->register_method(
660         method          => 'delete_copy_note',
661         api_name                =>      'open-ils.circ.copy_note.delete',
662         signature       => q/
663                 Deletes an existing copy note
664                 @param authtoken The login session key
665                 @param noteid The id of the note to delete
666                 @return 1 on success - Event otherwise.
667                 /);
668 sub delete_copy_note {
669         my( $self, $conn, $authtoken, $noteid ) = @_;
670
671         my $e = new_editor(xact=>1, authtoken=>$authtoken);
672         return $e->die_event unless $e->checkauth;
673
674         my $note = $e->retrieve_asset_copy_note([
675                 $noteid,
676                 { flesh => 2,
677                         flesh_fields => {
678                                 'acpn' => [ 'owning_copy' ],
679                                 'acp' => [ 'call_number' ],
680                         }
681                 }
682         ]) or return $e->die_event;
683
684         if( $note->creator ne $e->requestor->id ) {
685                 return $e->die_event unless 
686                         $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
687         }
688
689         $e->delete_asset_copy_note($note) or return $e->die_event;
690         $e->commit;
691         return 1;
692 }
693
694
695 __PACKAGE__->register_method(
696         method => 'age_hold_rules',
697         api_name        =>  'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
698 );
699
700 sub age_hold_rules {
701         my( $self, $conn ) = @_;
702         return new_editor()->retrieve_all_config_rules_age_hold_protect();
703 }
704
705
706
707 __PACKAGE__->register_method(
708         method => 'copy_details_barcode',
709     authoritative => 1,
710         api_name => 'open-ils.circ.copy_details.retrieve.barcode');
711 sub copy_details_barcode {
712         my( $self, $conn, $auth, $barcode ) = @_;
713     my $e = new_editor();
714     my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
715     return $e->event unless $cid;
716         return copy_details( $self, $conn, $auth, $cid );
717 }
718
719
720 __PACKAGE__->register_method(
721         method => 'copy_details',
722         api_name => 'open-ils.circ.copy_details.retrieve');
723
724 sub copy_details {
725         my( $self, $conn, $auth, $copy_id ) = @_;
726         my $e = new_editor(authtoken=>$auth);
727         return $e->event unless $e->checkauth;
728
729         my $flesh = { flesh => 1 };
730
731         my $copy = $e->retrieve_asset_copy(
732                 [
733                         $copy_id,
734                         {
735                                 flesh => 2,
736                                 flesh_fields => {
737                                         acp => ['call_number'],
738                                         acn => ['record']
739                                 }
740                         }
741                 ]) or return $e->event;
742
743
744         # De-flesh the copy for backwards compatibility
745         my $mvr;
746         my $vol = $copy->call_number;
747         if( ref $vol ) {
748                 $copy->call_number($vol->id);
749                 my $record = $vol->record;
750                 if( ref $record ) {
751                         $vol->record($record->id);
752                         $mvr = $U->record_to_mvr($record);
753                 }
754         }
755
756
757         my $hold = $e->search_action_hold_request(
758                 { 
759                         current_copy            => $copy_id, 
760                         capture_time            => { "!=" => undef },
761                         fulfillment_time        => undef,
762                         cancel_time                     => undef,
763                 }
764         )->[0];
765
766         OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
767
768         my $transit = $e->search_action_transit_copy(
769                 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
770
771         # find the latest circ, open or closed
772         my $circ = $e->search_action_circulation(
773                 [
774                         { target_copy => $copy_id },
775                         { order_by => { circ => 'xact_start desc' }, limit => 1 }
776                 ]
777         )->[0];
778
779
780         return {
781                 copy            => $copy,
782                 hold            => $hold,
783                 transit => $transit,
784                 circ            => $circ,
785                 volume  => $vol,
786                 mvr             => $mvr,
787         };
788 }
789
790
791
792
793 __PACKAGE__->register_method(
794         method => 'mark_item',
795         api_name => 'open-ils.circ.mark_item_damaged',
796         signature       => q/
797                 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
798                 @param authtoken The login session key
799                 @param copy_id The ID of the copy to mark as damaged
800                 @return 1 on success - Event otherwise.
801                 /
802 );
803 __PACKAGE__->register_method(
804         method => 'mark_item',
805         api_name => 'open-ils.circ.mark_item_missing',
806         signature       => q/
807                 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
808                 @param authtoken The login session key
809                 @param copy_id The ID of the copy to mark as missing 
810                 @return 1 on success - Event otherwise.
811                 /
812 );
813 __PACKAGE__->register_method(
814         method => 'mark_item',
815         api_name => 'open-ils.circ.mark_item_bindery',
816         signature       => q/
817                 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
818                 @param authtoken The login session key
819                 @param copy_id The ID of the copy to mark as bindery
820                 @return 1 on success - Event otherwise.
821                 /
822 );
823 __PACKAGE__->register_method(
824         method => 'mark_item',
825         api_name => 'open-ils.circ.mark_item_on_order',
826         signature       => q/
827                 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
828                 @param authtoken The login session key
829                 @param copy_id The ID of the copy to mark as on order 
830                 @return 1 on success - Event otherwise.
831                 /
832 );
833 __PACKAGE__->register_method(
834         method => 'mark_item',
835         api_name => 'open-ils.circ.mark_item_ill',
836         signature       => q/
837                 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
838                 @param authtoken The login session key
839                 @param copy_id The ID of the copy to mark as inter-library loan
840                 @return 1 on success - Event otherwise.
841                 /
842 );
843 __PACKAGE__->register_method(
844         method => 'mark_item',
845         api_name => 'open-ils.circ.mark_item_cataloging',
846         signature       => q/
847                 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
848                 @param authtoken The login session key
849                 @param copy_id The ID of the copy to mark as cataloging 
850                 @return 1 on success - Event otherwise.
851                 /
852 );
853 __PACKAGE__->register_method(
854         method => 'mark_item',
855         api_name => 'open-ils.circ.mark_item_reserves',
856         signature       => q/
857                 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
858                 @param authtoken The login session key
859                 @param copy_id The ID of the copy to mark as reserves
860                 @return 1 on success - Event otherwise.
861                 /
862 );
863 __PACKAGE__->register_method(
864         method => 'mark_item',
865         api_name => 'open-ils.circ.mark_item_discard',
866         signature       => q/
867                 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
868                 @param authtoken The login session key
869                 @param copy_id The ID of the copy to mark as discard
870                 @return 1 on success - Event otherwise.
871                 /
872 );
873
874 sub mark_item {
875         my( $self, $conn, $auth, $copy_id ) = @_;
876         my $e = new_editor(authtoken=>$auth, xact =>1);
877         return $e->event unless $e->checkauth;
878
879         my $perm = 'MARK_ITEM_MISSING';
880         my $stat = OILS_COPY_STATUS_MISSING;
881
882         if( $self->api_name =~ /damaged/ ) {
883                 $perm = 'MARK_ITEM_DAMAGED';
884                 $stat = OILS_COPY_STATUS_DAMAGED;
885         } elsif ( $self->api_name =~ /bindery/ ) {
886                 $perm = 'MARK_ITEM_BINDERY';
887                 $stat = OILS_COPY_STATUS_BINDERY;
888         } elsif ( $self->api_name =~ /on_order/ ) {
889                 $perm = 'MARK_ITEM_ON_ORDER';
890                 $stat = OILS_COPY_STATUS_ON_ORDER;
891         } elsif ( $self->api_name =~ /ill/ ) {
892                 $perm = 'MARK_ITEM_ILL';
893                 $stat = OILS_COPY_STATUS_ILL;
894         } elsif ( $self->api_name =~ /cataloging/ ) {
895                 $perm = 'MARK_ITEM_CATALOGING';
896                 $stat = OILS_COPY_STATUS_CATALOGING;
897         } elsif ( $self->api_name =~ /reserves/ ) {
898                 $perm = 'MARK_ITEM_RESERVES';
899                 $stat = OILS_COPY_STATUS_RESERVES;
900         } elsif ( $self->api_name =~ /discard/ ) {
901                 $perm = 'MARK_ITEM_DISCARD';
902                 $stat = OILS_COPY_STATUS_DISCARD;
903         }
904
905         my $copy = $e->retrieve_asset_copy($copy_id)
906                 or return $e->event;
907         $copy->status($stat);
908         $copy->edit_date('now');
909         $copy->editor($e->requestor->id);
910
911         $e->update_asset_copy($copy) or return $e->event;
912
913
914         my $holds = $e->search_action_hold_request(
915                 { 
916                         current_copy => $copy->id,
917                         fulfillment_time => undef,
918                         cancel_time => undef,
919                 }
920         );
921
922         $e->commit;
923
924         $logger->debug("resetting holds that target the marked copy");
925         OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
926
927         return 1;
928 }
929
930
931
932
933
934
935 # ----------------------------------------------------------------------
936 __PACKAGE__->register_method(
937         method => 'magic_fetch',
938         api_name => 'open-ils.agent.fetch'
939 );
940
941 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
942
943 sub magic_fetch {
944         my( $self, $conn, $auth, $args ) = @_;
945         my $e = new_editor( authtoken => $auth );
946         return $e->event unless $e->checkauth;
947
948         my $hint = $$args{hint};
949         my $id  = $$args{id};
950
951         # Is the call allowed to fetch this type of object?
952         return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
953
954         # Find the class the implements the given hint
955         my ($class) = grep { 
956                 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
957
958         $class =~ s/Fieldmapper:://og;
959         $class =~ s/::/_/og;
960         my $method = "retrieve_$class";
961
962         my $obj = $e->$method($id) or return $e->event;
963         return $obj;
964 }
965 # ----------------------------------------------------------------------
966
967
968 __PACKAGE__->register_method(
969         method  => "fleshed_circ_retrieve",
970     authoritative => 1,
971         api_name        => "open-ils.circ.fleshed.retrieve",);
972
973 sub fleshed_circ_retrieve {
974         my( $self, $client, $id ) = @_;
975         my $e = new_editor();
976         my $circ = $e->retrieve_action_circulation(
977                 [
978                         $id,
979                         { 
980                                 flesh                           => 4,
981                                 flesh_fields    => { 
982                                         circ => [ qw/ target_copy / ],
983                                         acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
984                                         ascecm => [ qw/ stat_cat stat_cat_entry / ],
985                                         acn => [ qw/ record / ],
986                                 }
987                         }
988                 ]
989         ) or return $e->event;
990         
991         my $copy = $circ->target_copy;
992         my $vol = $copy->call_number;
993         my $rec = $circ->target_copy->call_number->record;
994
995         $vol->record($rec->id);
996         $copy->call_number($vol->id);
997         $circ->target_copy($copy->id);
998
999         my $mvr;
1000
1001         if( $rec->id == OILS_PRECAT_RECORD ) {
1002                 $rec = undef;
1003                 $vol = undef;
1004         } else { 
1005                 $mvr = $U->record_to_mvr($rec);
1006                 $rec->marc(''); # drop the bulky marc data
1007         }
1008
1009         return {
1010                 circ => $circ,
1011                 copy => $copy,
1012                 volume => $vol,
1013                 record => $rec,
1014                 mvr => $mvr,
1015         };
1016 }
1017
1018 # {"select":{"acp":["id"],"circ":[{"aggregate":true,"transform":"count","alias":"count","column":"id"}]},"from":{"acp":{"circ":{"field":"target_copy","fkey":"id","type":"left"},"acn"{"field":"id","fkey":"call_number"}}},"where":{"+acn":{"record":200057}}
1019
1020
1021 1;