]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
instead of an explicit put-copy-into-missing status operation when an item is marked...
[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 OpenSRF::AppSession;
23 use OpenILS::Utils::ModsParser;
24 use OpenILS::Event;
25 use OpenSRF::EX qw(:try);
26 use OpenSRF::Utils::Logger qw(:logger);
27 use OpenILS::Utils::Fieldmapper;
28 use OpenILS::Utils::Editor;
29 use OpenILS::Utils::CStoreEditor q/:funcs/;
30 use OpenILS::Const qw/:const/;
31 use OpenSRF::Utils::SettingsClient;
32 use OpenILS::Application::Cat::AssetCommon;
33
34 my $apputils = "OpenILS::Application::AppUtils";
35 my $U = $apputils;
36
37
38 # ------------------------------------------------------------------------
39 # Top level Circ package;
40 # ------------------------------------------------------------------------
41
42 sub initialize {
43         my $self = shift;
44         OpenILS::Application::Circ::Circulate->initialize();
45 }
46
47
48 __PACKAGE__->register_method(
49         method => 'retrieve_circ',
50         api_name        => 'open-ils.circ.retrieve',
51         signature => q/
52                 Retrieve a circ object by id
53                 @param authtoken Login session key
54                 @pararm circid The id of the circ object
55         /
56 );
57 sub retrieve_circ {
58         my( $s, $c, $a, $i ) = @_;
59         my $e = new_editor(authtoken => $a);
60         return $e->event unless $e->checkauth;
61         my $circ = $e->retrieve_action_circulation($i) or return $e->event;
62         if( $e->requestor->id ne $circ->usr ) {
63                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
64         }
65         return $circ;
66 }
67
68
69 __PACKAGE__->register_method(
70         method => 'fetch_circ_mods',
71         api_name => 'open-ils.circ.circ_modifier.retrieve.all');
72 sub fetch_circ_mods {
73     my($self, $conn, $args) = @_;
74     my $mods = new_editor()->retrieve_all_config_circ_modifier;
75     return [ map {$_->code} @$mods ] unless $$args{full};
76     return $mods;
77 }
78
79 __PACKAGE__->register_method(
80         method => 'fetch_bill_types',
81         api_name => 'open-ils.circ.billing_type.retrieve.all');
82 sub fetch_bill_types {
83         my $conf = OpenSRF::Utils::SettingsClient->new;
84         return $conf->config_value(
85                 'apps', 'open-ils.circ', 'app_settings', 'billing_types', 'type' );
86 }
87
88
89 __PACKAGE__->register_method(
90     method => 'ranged_billing_types',
91     api_name => 'open-ils.circ.billing_type.ranged.retrieve.all');
92
93 sub ranged_billing_types {
94     my($self, $conn, $auth, $org_id, $depth) = @_;
95     my $e = new_editor(authtoken => $auth);
96     return $e->event unless $e->checkauth;
97     return $e->event unless $e->allowed('VIEW_BILLING_TYPE', $org_id);
98     return $e->search_config_billing_type(
99         {owner => $U->get_org_full_path($org_id, $depth)});
100 }
101
102
103
104 # ------------------------------------------------------------------------
105 # Returns an array of {circ, record} hashes checked out by the user.
106 # ------------------------------------------------------------------------
107 __PACKAGE__->register_method(
108         method  => "checkouts_by_user",
109         api_name        => "open-ils.circ.actor.user.checked_out",
110         NOTES           => <<"  NOTES");
111         Returns a list of open circulations as a pile of objects.  Each object
112         contains the relevant copy, circ, and record
113         NOTES
114
115 sub checkouts_by_user {
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         my $circs = $apputils->simplereq(
125                 'open-ils.cstore',
126                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
127                 { usr => $target->id, checkin_time => undef } );
128 #               { usr => $target->id } );
129
130         my @results;
131         for my $circ (@$circs) {
132
133                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
134                 return $evt if $evt;
135
136                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
137
138                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
139                 return $evt if $evt;
140
141                 my $mods = $apputils->record_to_mvr($record);
142
143                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
144         }
145
146         return \@results;
147
148 }
149
150
151
152 __PACKAGE__->register_method(
153         method  => "checkouts_by_user_slim",
154         api_name        => "open-ils.circ.actor.user.checked_out.slim",
155         NOTES           => <<"  NOTES");
156         Returns a list of open circulation objects
157         NOTES
158
159 # DEPRECAT ME?? XXX
160 sub checkouts_by_user_slim {
161         my( $self, $client, $user_session, $user_id ) = @_;
162
163         my( $requestor, $target, $copy, $record, $evt );
164
165         ( $requestor, $target, $evt ) = 
166                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
167         return $evt if $evt;
168
169         $logger->debug( 'User ' . $requestor->id . 
170                 " retrieving checked out items for user " . $target->id );
171
172         # XXX Make the call correct..
173         return $apputils->simplereq(
174                 'open-ils.cstore',
175                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
176                 { usr => $target->id, checkin_time => undef } );
177 #               { usr => $target->id } );
178 }
179
180
181 __PACKAGE__->register_method(
182         method  => "checkouts_by_user_opac",
183         api_name        => "open-ils.circ.actor.user.checked_out.opac",);
184
185 # XXX Deprecate Me
186 sub checkouts_by_user_opac {
187         my( $self, $client, $auth, $user_id ) = @_;
188
189         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
190         return $e->event unless $e->checkauth;
191         $user_id ||= $e->requestor->id;
192         return $e->event unless 
193                 my $patron = $e->retrieve_actor_user($user_id);
194
195         my $data;
196         my $search = {usr => $user_id, stop_fines => undef};
197
198         if( $user_id ne $e->requestor->id ) {
199                 $data = $e->search_action_circulation(
200                         $search, {checkperm=>1, permorg=>$patron->home_ou})
201                         or return $e->event;
202
203         } else {
204                 $data = $e->search_action_circulation($search);
205         }
206
207         return $data;
208 }
209
210
211 __PACKAGE__->register_method(
212         method  => "title_from_transaction",
213         api_name        => "open-ils.circ.circ_transaction.find_title",
214         NOTES           => <<"  NOTES");
215         Returns a mods object for the title that is linked to from the 
216         copy from the hold that created the given transaction
217         NOTES
218
219 sub title_from_transaction {
220         my( $self, $client, $login_session, $transactionid ) = @_;
221
222         my( $user, $circ, $title, $evt );
223
224         ( $user, $evt ) = $apputils->checkses( $login_session );
225         return $evt if $evt;
226
227         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
228         return $evt if $evt;
229         
230         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
231         return $evt if $evt;
232
233         return $apputils->record_to_mvr($title);
234 }
235
236
237
238 __PACKAGE__->register_method(
239         method  => "new_set_circ_lost",
240         api_name        => "open-ils.circ.circulation.set_lost",
241         signature       => q/
242         Sets the copy and related open circulation to lost
243                 @param auth
244                 @param args : barcode
245         /
246 );
247
248
249 # ---------------------------------------------------------------------
250 # Sets a circulation to lost.  updates copy status to lost
251 # applies copy and/or prcoessing fees depending on org settings
252 # ---------------------------------------------------------------------
253 sub new_set_circ_lost {
254     my( $self, $conn, $auth, $args ) = @_;
255
256     my $e = new_editor(authtoken=>$auth, xact=>1);
257     return $e->die_event unless $e->checkauth;
258
259     my $copy = $e->search_asset_copy({barcode=>$$args{barcode}, deleted=>'f'})->[0]
260         or return $e->die_event;
261
262     my $evt = OpenILS::Application::Cat::AssetCommon->set_item_lost($e, $copy->id);
263     return $evt if $evt;
264
265     $e->commit;
266     return 1;
267 }
268
269
270 __PACKAGE__->register_method(
271         method  => "set_circ_claims_returned",
272         api_name        => "open-ils.circ.circulation.set_claims_returned",
273         signature => {
274         desc => q/Sets the circ for a given item as claims returned
275                 If a backdate is provided, overdue fines will be voided
276                 back to the backdate/,
277         params => [
278             {desc => 'Authentication token', type => 'string'},
279             {desc => 'Arguments, including "barcode" and optional "backdate"', type => 'object'}
280         ],
281         return => {desc => q/1 on success, failure event on error, and 
282             PATRON_EXCEEDS_CLAIMS_RETURN_COUNT if the patron exceeds the 
283             configured claims return maximum/}
284     }
285 );
286
287 __PACKAGE__->register_method(
288         method  => "set_circ_claims_returned",
289         api_name        => "open-ils.circ.circulation.set_claims_returned.override",
290         signature => {
291         desc => q/This adds support for overrideing the configured max 
292                 claims returned amount. 
293                 @see open-ils.circ.circulation.set_claims_returned./,
294     }
295 );
296
297 sub set_circ_claims_returned {
298     my( $self, $conn, $auth, $args ) = @_;
299
300     my $e = new_editor(authtoken=>$auth, xact=>1);
301     return $e->die_event unless $e->checkauth;
302
303     my $barcode = $$args{barcode};
304     my $backdate = $$args{backdate};
305
306     $logger->info("marking circ for item $barcode as claims returned".
307         (($backdate) ? " with backdate $backdate" : ''));
308
309     my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0] 
310         or return $e->die_event;
311
312     my $circ = $e->search_action_circulation(
313         {checkin_time => undef, target_copy => $copy->id})->[0]
314             or return $e->die_event;
315
316     my $patron = $e->retrieve_actor_user($circ->usr);
317     my $max_count = $U->ou_ancestor_setting_value(
318         $circ->circ_lib, 'circ.max_patron_claim_return_count', $e);
319
320     # If the patron has too instances of many claims returned, 
321     # require an override to continue.  A configured max of 
322     # 0 means all attempts require an override
323     if(defined $max_count and $patron->claims_returned_count >= $max_count) {
324
325         if($self->api_name =~ /override/) {
326
327             # see if we're allowed to override
328             return $e->die_event unless 
329                 $e->allowed('SET_CIRC_CLAIMS_RETURNED.override', $circ->circ_lib);
330
331         } else {
332
333             # exit early and return the max claims return event
334             $e->rollback;
335             return OpenILS::Event->new(
336                 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT', 
337                 payload => {
338                     patron_count => $patron->claims_returned_count,
339                     max_count => $max_count
340                 }
341             );
342         }
343     }
344
345     $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib) 
346         or return $e->die_event;
347
348     $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
349         $circ->stop_fines_time('now') unless $circ->stop_fines_time;
350
351     if( $backdate ) {
352         # make it look like the circ stopped at the cliams returned time
353         $circ->stop_fines_time(clense_ISO8601($backdate));
354         my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
355         return $evt if $evt;
356     }
357
358     $e->update_action_circulation($circ) or return $e->die_event;
359
360     # see if there is a configured post-claims-return copy status
361     if(my $stat = $U->ou_ancestor_setting_value($circ->circ_lib, 'circ.claim_return.copy_status')) {
362             $copy->status($stat);
363             $copy->edit_date('now');
364             $copy->editor($e->requestor->id);
365             $e->update_asset_copy($copy) or return $e->die_event;
366     }
367
368     $e->commit;
369     return 1;
370 }
371
372
373 __PACKAGE__->register_method(
374         method  => "post_checkin_backdate_circ",
375         api_name        => "open-ils.circ.post_checkin_backdate",
376         signature => {
377         desc => q/Back-date an already checked in circulation/,
378         params => [
379             {desc => 'Authentication token', type => 'string'},
380             {desc => 'Circ ID', type => 'number'},
381             {desc => 'ISO8601 backdate', type => 'string'},
382         ],
383         return => {desc => q/1 on success, failure event on error/}
384     }
385 );
386
387 __PACKAGE__->register_method(
388         method  => "post_checkin_backdate_circ",
389         api_name        => "open-ils.circ.post_checkin_backdate.batch",
390     stream => 1,
391         signature => {
392         desc => q/@see open-ils.circ.post_checkin_backdate.  Batch mode/,
393         params => [
394             {desc => 'Authentication token', type => 'string'},
395             {desc => 'List of Circ ID', type => 'array'},
396             {desc => 'ISO8601 backdate', type => 'string'},
397         ],
398         return => {desc => q/Set of: 1 on success, failure event on error/}
399     }
400 );
401
402
403 sub post_checkin_backdate_circ {
404     my( $self, $conn, $auth, $circ_id, $backdate ) = @_;
405     my $e = new_editor(authtoken=>$auth);
406     return $e->die_event unless $e->checkauth;
407     if($self->api_name =~ /batch/) {
408         foreach my $c (@$circ_id) {
409             $conn->respond(post_checkin_backdate_circ_impl($e, $c, $backdate));
410         }
411     } else {
412         $conn->respond_complete(post_checkin_backdate_circ_impl($e, $circ_id, $backdate));
413     }
414
415     $e->disconnect;
416     return undef;
417 }
418
419
420 sub post_checkin_backdate_circ_impl {
421     my($e, $circ_id, $backdate) = @_;
422
423     $e->xact_begin;
424
425     my $circ = $e->retrieve_action_circulation($circ_id)
426         or return $e->die_event;
427
428     # anyone with checkin perms can backdate (more restrictive?)
429     return $e->die_event unless $e->allowed('COPY_CHECKIN', $circ->circ_lib);
430
431     # don't allow back-dating an open circulation
432     return OpenILS::Event->new('BAD_PARAMS') unless 
433         $backdate and $circ->checkin_time;
434
435     # update the checkin and stop_fines times to reflect the new backdate
436     $circ->stop_fines_time(clense_ISO8601($backdate));
437     $circ->checkin_time(clense_ISO8601($backdate));
438     $e->update_action_circulation($circ) or return $e->die_event;
439
440     # now void the overdues "erased" by the back-dating
441     my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
442     return $evt if $evt;
443
444     # If the circ was closed before and the balance owned !=0, re-open the transaction
445     $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
446     return $evt if $evt;
447
448     $e->xact_commit;
449     return 1;
450 }
451
452
453
454 __PACKAGE__->register_method (
455         method          => 'set_circ_due_date',
456         api_name                => 'open-ils.circ.circulation.due_date.update',
457         signature       => q/
458                 Updates the due_date on the given circ
459                 @param authtoken
460                 @param circid The id of the circ to update
461                 @param date The timestamp of the new due date
462         /
463 );
464
465 sub set_circ_due_date {
466         my( $self, $conn, $auth, $circ_id, $date ) = @_;
467
468     my $e = new_editor(xact=>1, authtoken=>$auth);
469     return $e->die_event unless $e->checkauth;
470     my $circ = $e->retrieve_action_circulation($circ_id)
471         or return $e->die_event;
472
473     return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
474         $date = clense_ISO8601($date);
475         $circ->due_date($date);
476     $e->update_action_circulation($circ) or return $e->die_event;
477     $e->commit;
478
479     return $circ->id;
480 }
481
482
483 __PACKAGE__->register_method(
484         method          => "create_in_house_use",
485         api_name                => 'open-ils.circ.in_house_use.create',
486         signature       =>      q/
487                 Creates an in-house use action.
488                 @param $authtoken The login session key
489                 @param params A hash of params including
490                         'location' The org unit id where the in-house use occurs
491                         'copyid' The copy in question
492                         'count' The number of in-house uses to apply to this copy
493                 @return An array of id's representing the id's of the newly created
494                 in-house use objects or an event on an error
495         /);
496
497 __PACKAGE__->register_method(
498         method          => "create_in_house_use",
499         api_name                => 'open-ils.circ.non_cat_in_house_use.create',
500 );
501
502
503 sub create_in_house_use {
504         my( $self, $client, $auth, $params ) = @_;
505
506         my( $evt, $copy );
507         my $org                 = $params->{location};
508         my $copyid              = $params->{copyid};
509         my $count               = $params->{count} || 1;
510         my $nc_type             = $params->{non_cat_type};
511         my $use_time    = $params->{use_time} || 'now';
512
513         my $e = new_editor(xact=>1,authtoken=>$auth);
514         return $e->event unless $e->checkauth;
515         return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
516
517         my $non_cat = 1 if $self->api_name =~ /non_cat/;
518
519         unless( $non_cat ) {
520                 if( $copyid ) {
521                         $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
522                 } else {
523                         $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
524                                 or return $e->event;
525                         $copyid = $copy->id;
526                 }
527         }
528
529         if( $use_time ne 'now' ) {
530                 $use_time = clense_ISO8601($use_time);
531                 $logger->debug("in_house_use setting use time to $use_time");
532         }
533
534         my @ids;
535         for(1..$count) {
536
537                 my $ihu;
538                 my $method;
539                 my $cmeth;
540
541                 if($non_cat) {
542                         $ihu = Fieldmapper::action::non_cat_in_house_use->new;
543                         $ihu->item_type($nc_type);
544                         $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
545                         $cmeth = "create_action_non_cat_in_house_use";
546
547                 } else {
548                         $ihu = Fieldmapper::action::in_house_use->new;
549                         $ihu->item($copyid);
550                         $method = 'open-ils.storage.direct.action.in_house_use.create';
551                         $cmeth = "create_action_in_house_use";
552                 }
553
554                 $ihu->staff($e->requestor->id);
555                 $ihu->org_unit($org);
556                 $ihu->use_time($use_time);
557
558                 $ihu = $e->$cmeth($ihu) or return $e->event;
559                 push( @ids, $ihu->id );
560         }
561
562         $e->commit;
563         return \@ids;
564 }
565
566
567
568
569
570 __PACKAGE__->register_method(
571         method  => "view_circs",
572         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
573         notes           => q/
574                 Retrieves the last X circs for a given copy
575                 @param authtoken The login session key
576                 @param copyid The copy to check
577                 @param count How far to go back in the item history
578                 @return An array of circ ids
579         /);
580
581 # ----------------------------------------------------------------------
582 # Returns $count most recent circs.  If count exceeds the configured 
583 # max, use the configured max instead
584 # ----------------------------------------------------------------------
585 sub view_circs {
586         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
587
588     my $e = new_editor(authtoken => $authtoken);
589     return $e->event unless $e->checkauth;
590     
591     my $copy = $e->retrieve_asset_copy([
592         $copyid,
593         {   flesh => 1,
594             flesh_fields => {acp => ['call_number']}
595         }
596     ]) or return $e->event;
597
598     return $e->event unless $e->allowed(
599         'VIEW_COPY_CHECKOUT_HISTORY', 
600         ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ? 
601             $copy->circ_lib : $copy->call_number->owning_lib);
602         
603     my $max_history = $U->ou_ancestor_setting_value(
604         $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
605
606     if(defined $max_history) {
607         $count = $max_history unless defined $count and $count < $max_history;
608     } else {
609         $count = 4 unless defined $count;
610     }
611
612     return $e->search_action_circulation([
613         {target_copy => $copyid}, 
614         {limit => $count, order_by => { circ => "xact_start DESC" }} 
615     ]);
616 }
617
618
619 __PACKAGE__->register_method(
620         method  => "circ_count",
621         api_name        => "open-ils.circ.circulation.count",
622         notes           => q/
623                 Returns the number of times the item has circulated
624                 @param copyid The copy to check
625         /);
626
627 sub circ_count {
628         my( $self, $client, $copyid, $range ) = @_; 
629         my $e = OpenILS::Utils::Editor->new;
630         return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
631 }
632
633
634
635 __PACKAGE__->register_method(
636         method          => 'fetch_notes',
637         api_name                => 'open-ils.circ.copy_note.retrieve.all',
638         signature       => q/
639                 Returns an array of copy note objects.  
640                 @param args A named hash of parameters including:
641                         authtoken       : Required if viewing non-public notes
642                         itemid          : The id of the item whose notes we want to retrieve
643                         pub                     : True if all the caller wants are public notes
644                 @return An array of note objects
645         /);
646
647 __PACKAGE__->register_method(
648         method          => 'fetch_notes',
649         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
650         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
651
652 __PACKAGE__->register_method(
653         method          => 'fetch_notes',
654         api_name                => 'open-ils.circ.title_note.retrieve.all',
655         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
656
657
658 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
659 sub fetch_notes {
660         my( $self, $connection, $args ) = @_;
661
662         my $id = $$args{itemid};
663         my $authtoken = $$args{authtoken};
664         my( $r, $evt);
665
666         if( $self->api_name =~ /copy/ ) {
667                 if( $$args{pub} ) {
668                         return $U->cstorereq(
669                                 'open-ils.cstore.direct.asset.copy_note.search.atomic',
670                                 { owning_copy => $id, pub => 't' } );
671                 } else {
672                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
673                         return $evt if $evt;
674                         return $U->cstorereq(
675                                 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
676                 }
677
678         } elsif( $self->api_name =~ /call_number/ ) {
679                 if( $$args{pub} ) {
680                         return $U->cstorereq(
681                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
682                                 { call_number => $id, pub => 't' } );
683                 } else {
684                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
685                         return $evt if $evt;
686                         return $U->cstorereq(
687                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
688                 }
689
690         } elsif( $self->api_name =~ /title/ ) {
691                 if( $$args{pub} ) {
692                         return $U->cstorereq(
693                                 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
694                                 { record => $id, pub => 't' } );
695                 } else {
696                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
697                         return $evt if $evt;
698                         return $U->cstorereq(
699                                 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
700                 }
701         }
702
703         return undef;
704 }
705
706 __PACKAGE__->register_method(
707         method  => 'has_notes',
708         api_name        => 'open-ils.circ.copy.has_notes');
709 __PACKAGE__->register_method(
710         method  => 'has_notes',
711         api_name        => 'open-ils.circ.call_number.has_notes');
712 __PACKAGE__->register_method(
713         method  => 'has_notes',
714         api_name        => 'open-ils.circ.title.has_notes');
715
716
717 sub has_notes {
718         my( $self, $conn, $authtoken, $id ) = @_;
719         my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
720         return $editor->event unless $editor->checkauth;
721
722         my $n = $editor->search_asset_copy_note(
723                 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
724
725         $n = $editor->search_asset_call_number_note(
726                 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
727
728         $n = $editor->search_biblio_record_note(
729                 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
730
731         return scalar @$n;
732 }
733
734
735
736 __PACKAGE__->register_method(
737         method          => 'create_copy_note',
738         api_name                => 'open-ils.circ.copy_note.create',
739         signature       => q/
740                 Creates a new copy note
741                 @param authtoken The login session key
742                 @param note     The note object to create
743                 @return The id of the new note object
744         /);
745
746 sub create_copy_note {
747         my( $self, $connection, $authtoken, $note ) = @_;
748
749         my $e = new_editor(xact=>1, authtoken=>$authtoken);
750         return $e->event unless $e->checkauth;
751         my $copy = $e->retrieve_asset_copy(
752                 [
753                         $note->owning_copy,
754                         {       flesh => 1,
755                                 flesh_fields => { 'acp' => ['call_number'] }
756                         }
757                 ]
758         );
759
760         return $e->event unless 
761                 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
762
763         $note->create_date('now');
764         $note->creator($e->requestor->id);
765         $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
766         $note->clear_id;
767
768         $e->create_asset_copy_note($note) or return $e->event;
769         $e->commit;
770         return $note->id;
771 }
772
773
774 __PACKAGE__->register_method(
775         method          => 'delete_copy_note',
776         api_name                =>      'open-ils.circ.copy_note.delete',
777         signature       => q/
778                 Deletes an existing copy note
779                 @param authtoken The login session key
780                 @param noteid The id of the note to delete
781                 @return 1 on success - Event otherwise.
782                 /);
783 sub delete_copy_note {
784         my( $self, $conn, $authtoken, $noteid ) = @_;
785
786         my $e = new_editor(xact=>1, authtoken=>$authtoken);
787         return $e->die_event unless $e->checkauth;
788
789         my $note = $e->retrieve_asset_copy_note([
790                 $noteid,
791                 { flesh => 2,
792                         flesh_fields => {
793                                 'acpn' => [ 'owning_copy' ],
794                                 'acp' => [ 'call_number' ],
795                         }
796                 }
797         ]) or return $e->die_event;
798
799         if( $note->creator ne $e->requestor->id ) {
800                 return $e->die_event unless 
801                         $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
802         }
803
804         $e->delete_asset_copy_note($note) or return $e->die_event;
805         $e->commit;
806         return 1;
807 }
808
809
810 __PACKAGE__->register_method(
811         method => 'age_hold_rules',
812         api_name        =>  'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
813 );
814
815 sub age_hold_rules {
816         my( $self, $conn ) = @_;
817         return new_editor()->retrieve_all_config_rules_age_hold_protect();
818 }
819
820
821
822 __PACKAGE__->register_method(
823         method => 'copy_details_barcode',
824     authoritative => 1,
825         api_name => 'open-ils.circ.copy_details.retrieve.barcode');
826 sub copy_details_barcode {
827         my( $self, $conn, $auth, $barcode ) = @_;
828     my $e = new_editor();
829     my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
830     return $e->event unless $cid;
831         return copy_details( $self, $conn, $auth, $cid );
832 }
833
834
835 __PACKAGE__->register_method(
836         method => 'copy_details',
837         api_name => 'open-ils.circ.copy_details.retrieve');
838
839 sub copy_details {
840         my( $self, $conn, $auth, $copy_id ) = @_;
841         my $e = new_editor(authtoken=>$auth);
842         return $e->event unless $e->checkauth;
843
844         my $flesh = { flesh => 1 };
845
846         my $copy = $e->retrieve_asset_copy(
847                 [
848                         $copy_id,
849                         {
850                                 flesh => 2,
851                                 flesh_fields => {
852                                         acp => ['call_number'],
853                                         acn => ['record']
854                                 }
855                         }
856                 ]) or return $e->event;
857
858
859         # De-flesh the copy for backwards compatibility
860         my $mvr;
861         my $vol = $copy->call_number;
862         if( ref $vol ) {
863                 $copy->call_number($vol->id);
864                 my $record = $vol->record;
865                 if( ref $record ) {
866                         $vol->record($record->id);
867                         $mvr = $U->record_to_mvr($record);
868                 }
869         }
870
871
872         my $hold = $e->search_action_hold_request(
873                 { 
874                         current_copy            => $copy_id, 
875                         capture_time            => { "!=" => undef },
876                         fulfillment_time        => undef,
877                         cancel_time                     => undef,
878                 }
879         )->[0];
880
881         OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
882
883         my $transit = $e->search_action_transit_copy(
884                 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
885
886         # find the latest circ, open or closed
887         my $circ = $e->search_action_circulation(
888                 [
889                         { target_copy => $copy_id },
890                         { 
891                 flesh => 1,
892                 flesh_fields => {
893                     circ => [
894                         'workstation',
895                         'checkin_workstation', 
896                         'duration_rule', 
897                         'max_fine_rule', 
898                         'recuring_fine_rule'
899                     ]
900                 },
901                 order_by => { circ => 'xact_start desc' }, 
902                 limit => 1 
903             }
904                 ]
905         )->[0];
906
907
908         return {
909                 copy            => $copy,
910                 hold            => $hold,
911                 transit => $transit,
912                 circ            => $circ,
913                 volume  => $vol,
914                 mvr             => $mvr,
915         };
916 }
917
918
919
920
921 __PACKAGE__->register_method(
922         method => 'mark_item',
923         api_name => 'open-ils.circ.mark_item_damaged',
924         signature       => q/
925                 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
926                 @param authtoken The login session key
927                 @param copy_id The ID of the copy to mark as damaged
928                 @return 1 on success - Event otherwise.
929                 /
930 );
931 __PACKAGE__->register_method(
932         method => 'mark_item',
933         api_name => 'open-ils.circ.mark_item_missing',
934         signature       => q/
935                 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
936                 @param authtoken The login session key
937                 @param copy_id The ID of the copy to mark as missing 
938                 @return 1 on success - Event otherwise.
939                 /
940 );
941 __PACKAGE__->register_method(
942         method => 'mark_item',
943         api_name => 'open-ils.circ.mark_item_bindery',
944         signature       => q/
945                 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
946                 @param authtoken The login session key
947                 @param copy_id The ID of the copy to mark as bindery
948                 @return 1 on success - Event otherwise.
949                 /
950 );
951 __PACKAGE__->register_method(
952         method => 'mark_item',
953         api_name => 'open-ils.circ.mark_item_on_order',
954         signature       => q/
955                 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
956                 @param authtoken The login session key
957                 @param copy_id The ID of the copy to mark as on order 
958                 @return 1 on success - Event otherwise.
959                 /
960 );
961 __PACKAGE__->register_method(
962         method => 'mark_item',
963         api_name => 'open-ils.circ.mark_item_ill',
964         signature       => q/
965                 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
966                 @param authtoken The login session key
967                 @param copy_id The ID of the copy to mark as inter-library loan
968                 @return 1 on success - Event otherwise.
969                 /
970 );
971 __PACKAGE__->register_method(
972         method => 'mark_item',
973         api_name => 'open-ils.circ.mark_item_cataloging',
974         signature       => q/
975                 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
976                 @param authtoken The login session key
977                 @param copy_id The ID of the copy to mark as cataloging 
978                 @return 1 on success - Event otherwise.
979                 /
980 );
981 __PACKAGE__->register_method(
982         method => 'mark_item',
983         api_name => 'open-ils.circ.mark_item_reserves',
984         signature       => q/
985                 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
986                 @param authtoken The login session key
987                 @param copy_id The ID of the copy to mark as reserves
988                 @return 1 on success - Event otherwise.
989                 /
990 );
991 __PACKAGE__->register_method(
992         method => 'mark_item',
993         api_name => 'open-ils.circ.mark_item_discard',
994         signature       => q/
995                 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
996                 @param authtoken The login session key
997                 @param copy_id The ID of the copy to mark as discard
998                 @return 1 on success - Event otherwise.
999                 /
1000 );
1001
1002 sub mark_item {
1003         my( $self, $conn, $auth, $copy_id, $args ) = @_;
1004         my $e = new_editor(authtoken=>$auth, xact =>1);
1005         return $e->die_event unless $e->checkauth;
1006     $args ||= {};
1007
1008     my $copy = $e->retrieve_asset_copy([
1009         $copy_id,
1010         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
1011             or return $e->die_event;
1012
1013     my $owning_lib = 
1014         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
1015             $copy->circ_lib : $copy->call_number->owning_lib;
1016
1017     return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
1018
1019
1020         my $perm = 'MARK_ITEM_MISSING';
1021         my $stat = OILS_COPY_STATUS_MISSING;
1022
1023         if( $self->api_name =~ /damaged/ ) {
1024                 $perm = 'MARK_ITEM_DAMAGED';
1025                 $stat = OILS_COPY_STATUS_DAMAGED;
1026         my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
1027         return $evt if $evt;
1028
1029         my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1030         $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
1031
1032         } elsif ( $self->api_name =~ /bindery/ ) {
1033                 $perm = 'MARK_ITEM_BINDERY';
1034                 $stat = OILS_COPY_STATUS_BINDERY;
1035         } elsif ( $self->api_name =~ /on_order/ ) {
1036                 $perm = 'MARK_ITEM_ON_ORDER';
1037                 $stat = OILS_COPY_STATUS_ON_ORDER;
1038         } elsif ( $self->api_name =~ /ill/ ) {
1039                 $perm = 'MARK_ITEM_ILL';
1040                 $stat = OILS_COPY_STATUS_ILL;
1041         } elsif ( $self->api_name =~ /cataloging/ ) {
1042                 $perm = 'MARK_ITEM_CATALOGING';
1043                 $stat = OILS_COPY_STATUS_CATALOGING;
1044         } elsif ( $self->api_name =~ /reserves/ ) {
1045                 $perm = 'MARK_ITEM_RESERVES';
1046                 $stat = OILS_COPY_STATUS_RESERVES;
1047         } elsif ( $self->api_name =~ /discard/ ) {
1048                 $perm = 'MARK_ITEM_DISCARD';
1049                 $stat = OILS_COPY_STATUS_DISCARD;
1050         }
1051
1052
1053         $copy->status($stat);
1054         $copy->edit_date('now');
1055         $copy->editor($e->requestor->id);
1056
1057         $e->update_asset_copy($copy) or return $e->die_event;
1058
1059         my $holds = $e->search_action_hold_request(
1060                 { 
1061                         current_copy => $copy->id,
1062                         fulfillment_time => undef,
1063                         cancel_time => undef,
1064                 }
1065         );
1066
1067         $e->commit;
1068
1069         $logger->debug("resetting holds that target the marked copy");
1070         OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1071
1072         return 1;
1073 }
1074
1075 sub handle_mark_damaged {
1076     my($e, $copy, $owning_lib, $args) = @_;
1077
1078     my $apply = $args->{apply_fines} || '';
1079     return undef if $apply eq 'noapply';
1080
1081     my $new_amount = $args->{override_amount};
1082     my $new_btype = $args->{override_btype};
1083     my $new_note = $args->{override_note};
1084
1085     # grab the last circulation
1086     my $circ = $e->search_action_circulation([
1087         {   target_copy => $copy->id}, 
1088         {   limit => 1, 
1089             order_by => {circ => "xact_start DESC"},
1090             flesh => 2,
1091             flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
1092         }
1093     ])->[0];
1094
1095     return undef unless $circ;
1096
1097     my $charge_price = $U->ou_ancestor_setting_value(
1098         $owning_lib, 'circ.charge_on_damaged', $e);
1099
1100     my $proc_fee = $U->ou_ancestor_setting_value(
1101         $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
1102
1103     my $void_overdue = $U->ou_ancestor_setting_value(
1104         $owning_lib, 'circ.damaged.void_ovedue', $e) || 0;
1105
1106     return undef unless $charge_price or $proc_fee;
1107
1108     my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
1109     my $total = $copy_price + $proc_fee;
1110
1111     if($apply) {
1112         
1113         if($new_amount and $new_btype) {
1114
1115             # Allow staff to override the amount to charge for a damaged item
1116             # Consider the case where the item is only partially damaged
1117             # This value is meant to take the place of the item price and
1118             # optional processing fee.
1119
1120             my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1121                 $e, $new_amount, $new_btype, 'Damaged Item Override', $circ->id, $new_note);
1122             return $evt if $evt;
1123
1124         } else {
1125
1126             if($charge_price and $copy_price) {
1127                 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1128                     $e, $copy_price, 7, 'Damaged Item', $circ->id);
1129                 return $evt if $evt;
1130             }
1131
1132             if($proc_fee) {
1133                 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
1134                     $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
1135                 return $evt if $evt;
1136             }
1137         }
1138
1139         # the assumption is that you would not void the overdues unless you 
1140         # were also charging for the item and/or applying a processing fee
1141         if($void_overdue) {
1142             my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
1143             return $evt if $evt;
1144         }
1145
1146         my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
1147         return $evt if $evt;
1148
1149         my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1150         $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
1151
1152         return undef;
1153
1154     } else {
1155         return OpenILS::Event->new('DAMAGE_CHARGE', 
1156             payload => {
1157                 circ => $circ,
1158                 charge => $total
1159             }
1160         );
1161     }
1162 }
1163
1164
1165
1166
1167
1168
1169 # ----------------------------------------------------------------------
1170 __PACKAGE__->register_method(
1171         method => 'magic_fetch',
1172         api_name => 'open-ils.agent.fetch'
1173 );
1174
1175 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1176
1177 sub magic_fetch {
1178         my( $self, $conn, $auth, $args ) = @_;
1179         my $e = new_editor( authtoken => $auth );
1180         return $e->event unless $e->checkauth;
1181
1182         my $hint = $$args{hint};
1183         my $id  = $$args{id};
1184
1185         # Is the call allowed to fetch this type of object?
1186         return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1187
1188         # Find the class the implements the given hint
1189         my ($class) = grep { 
1190                 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1191
1192         $class =~ s/Fieldmapper:://og;
1193         $class =~ s/::/_/og;
1194         my $method = "retrieve_$class";
1195
1196         my $obj = $e->$method($id) or return $e->event;
1197         return $obj;
1198 }
1199 # ----------------------------------------------------------------------
1200
1201
1202 __PACKAGE__->register_method(
1203         method  => "fleshed_circ_retrieve",
1204     authoritative => 1,
1205         api_name        => "open-ils.circ.fleshed.retrieve",);
1206
1207 sub fleshed_circ_retrieve {
1208         my( $self, $client, $id ) = @_;
1209         my $e = new_editor();
1210         my $circ = $e->retrieve_action_circulation(
1211                 [
1212                         $id,
1213                         { 
1214                                 flesh                           => 4,
1215                                 flesh_fields    => { 
1216                                         circ => [ qw/ target_copy / ],
1217                                         acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1218                                         ascecm => [ qw/ stat_cat stat_cat_entry / ],
1219                                         acn => [ qw/ record / ],
1220                                 }
1221                         }
1222                 ]
1223         ) or return $e->event;
1224         
1225         my $copy = $circ->target_copy;
1226         my $vol = $copy->call_number;
1227         my $rec = $circ->target_copy->call_number->record;
1228
1229         $vol->record($rec->id);
1230         $copy->call_number($vol->id);
1231         $circ->target_copy($copy->id);
1232
1233         my $mvr;
1234
1235         if( $rec->id == OILS_PRECAT_RECORD ) {
1236                 $rec = undef;
1237                 $vol = undef;
1238         } else { 
1239                 $mvr = $U->record_to_mvr($rec);
1240                 $rec->marc(''); # drop the bulky marc data
1241         }
1242
1243         return {
1244                 circ => $circ,
1245                 copy => $copy,
1246                 volume => $vol,
1247                 record => $rec,
1248                 mvr => $mvr,
1249         };
1250 }
1251
1252
1253
1254 __PACKAGE__->register_method(
1255         method  => "test_batch_circ_events",
1256         api_name        => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1257 );
1258
1259 #  method for testing the behavior of a given event definition
1260 sub test_batch_circ_events {
1261     my($self, $conn, $auth, $event_def, $barcode) = @_;
1262
1263     my $e = new_editor(authtoken => $auth);
1264         return $e->event unless $e->checkauth;
1265     return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1266
1267     my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1268         or return $e->event;
1269
1270     my $circ = $e->search_action_circulation(
1271         {target_copy => $copy->id, checkin_time => undef})->[0]
1272         or return $e->event;
1273         
1274     return undef unless $circ;
1275
1276     return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1277 }
1278
1279
1280
1281 __PACKAGE__->register_method(
1282         method  => "user_payments_list",
1283         api_name        => "open-ils.circ.user_payments.filtered.batch",
1284     stream => 1,
1285         signature => {
1286         desc => q/Returns a fleshed, date-limited set of all payments a user
1287                 has made.  By default, ordered by payment date.  Optionally
1288                 ordered by other columns in the top-level "mp" object/,
1289         params => [
1290             {desc => 'Authentication token', type => 'string'},
1291             {desc => 'User ID', type => 'number'},
1292             {desc => 'Order by column(s), optional.  Array of "mp" class columns', type => 'array'}
1293         ],
1294         return => {desc => q/List of "mp" objects, fleshed with the billable transaction 
1295             and the related fully-realized payment object (e.g money.cash_payment)/}
1296     }
1297 );
1298
1299 sub user_payments_list {
1300     my($self, $conn, $auth, $user_id, $start_date, $end_date, $order_by) = @_;
1301
1302     my $e = new_editor(authtoken => $auth);
1303     return $e->event unless $e->checkauth;
1304
1305     my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1306     return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $user->home_ou);
1307
1308     $order_by ||= ['payment_ts'];
1309
1310     # all payments by user, between start_date and end_date
1311     my $payments = $e->json_query({
1312         select => {mp => ['id']}, 
1313         from => {
1314             mp => {
1315                 mbt => {
1316                     fkey => 'xact', field => 'id'}
1317             }
1318         }, 
1319         where => {
1320             '+mbt' => {usr => $user_id}, 
1321             '+mp' => {payment_ts => {between => [$start_date, $end_date]}}
1322         },
1323         order_by => {mp => $order_by}
1324     });
1325
1326     for my $payment_id (@$payments) {
1327         my $payment = $e->retrieve_money_payment([
1328             $payment_id->{id}, 
1329             {   
1330                 flesh => 2,
1331                 flesh_fields => {
1332                     mp => [
1333                         'xact',
1334                         'cash_payment',
1335                         'credit_card_payment',
1336                         'credit_payment',
1337                         'check_payment',
1338                         'work_payment',
1339                         'forgive_payment',
1340                         'goods_payment'
1341                     ],
1342                     mbt => [
1343                         'circulation', 
1344                         'grocery'
1345                     ]
1346                 }
1347             }
1348         ]);
1349         $conn->respond($payment);
1350     }
1351
1352     return undef;
1353 }
1354
1355
1356 __PACKAGE__->register_method(
1357         method  => "retrieve_circ_chain",
1358         api_name        => "open-ils.circ.renewal_chain.retrieve_by_circ",
1359     stream => 1,
1360         signature => {
1361         desc => q/Given a circulation, this returns all circulation objects
1362                 that are part of the same chain of renewals./,
1363         params => [
1364             {desc => 'Authentication token', type => 'string'},
1365             {desc => 'Circ ID', type => 'number'},
1366         ],
1367         return => {desc => q/List of circ objects, orderd by oldest circ first/}
1368     }
1369 );
1370
1371 sub retrieve_circ_chain {
1372     my($self, $conn, $auth, $circ_id) = @_;
1373
1374     my $e = new_editor(authtoken => $auth);
1375     return $e->event unless $e->checkauth;
1376
1377     # grab the base circ and all parent (previous) circs by fleshing
1378     my $base_circ = $e->retrieve_action_circulation([
1379         $circ_id,
1380         {
1381             flesh => -1,
1382             flesh_fields => {circ => [qw/parent_circ workstation checkin_workstation/]}
1383         }
1384     ]) or return $e->event;
1385
1386     return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $base_circ->circ_lib);
1387
1388     # send each circ to the caller, starting with the oldest circulation
1389     my @chain;
1390     my $circ = $base_circ;
1391     while($circ) {
1392         push(@chain, $circ);
1393
1394         # unflesh for consistency
1395         my $parent = $circ->parent_circ;
1396         $circ->parent_circ($parent->id) if $parent; 
1397
1398         $circ = $parent;
1399     }
1400     $conn->respond($_) for reverse(@chain);
1401
1402     # base circ may not be the end of the chain.  see if there are any subsequent circs
1403     $circ = $base_circ;
1404     $conn->respond($circ) while ($circ = $e->search_action_circulation([
1405         {parent_circ => $circ->id},
1406         {flesh => 1, flesh_fields => {circ => [qw/workstation checkin_workstation/]}}
1407     ])->[0]);
1408
1409     return undef;
1410 }
1411
1412
1413
1414 # {"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}}
1415
1416
1417 1;