]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
flesh checkin_workstation on copy detail retrieve. changed reltyp for workstation...
[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       => q/
274         Sets the circ for the given item as claims returned
275         If a backdate is provided, overdue fines will be voided
276         back to the backdate
277                 @param auth
278                 @param args : barcode, backdate
279         /
280 );
281
282 sub set_circ_claims_returned {
283     my( $self, $conn, $auth, $args ) = @_;
284
285     my $e = new_editor(authtoken=>$auth, xact=>1);
286     return $e->die_event unless $e->checkauth;
287
288     my $barcode = $$args{barcode};
289     my $backdate = $$args{backdate};
290
291     $logger->info("marking circ for item $barcode as claims returned".
292         (($backdate) ? " with backdate $backdate" : ''));
293
294     my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0] 
295         or return $e->die_event;
296
297     my $circ = $e->search_action_circulation(
298         {checkin_time => undef, target_copy => $copy->id})->[0]
299             or return $e->die_event;
300
301     $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib) 
302         or return $e->die_event;
303
304     $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
305         $circ->stop_fines_time('now') unless $circ->stop_fines_time;
306
307     if( $backdate ) {
308         # make it look like the circ stopped at the cliams returned time
309         $circ->stop_fines_time(clense_ISO8601($backdate));
310         my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ, $backdate);
311         return $evt if $evt;
312     }
313
314     $e->update_action_circulation($circ) or return $e->die_event;
315     $e->commit;
316     return 1;
317 }
318
319
320
321
322
323 __PACKAGE__->register_method (
324         method          => 'set_circ_due_date',
325         api_name                => 'open-ils.circ.circulation.due_date.update',
326         signature       => q/
327                 Updates the due_date on the given circ
328                 @param authtoken
329                 @param circid The id of the circ to update
330                 @param date The timestamp of the new due date
331         /
332 );
333
334 sub set_circ_due_date {
335         my( $self, $conn, $auth, $circ_id, $date ) = @_;
336
337     my $e = new_editor(xact=>1, authtoken=>$auth);
338     return $e->die_event unless $e->checkauth;
339     my $circ = $e->retrieve_action_circulation($circ_id)
340         or return $e->die_event;
341
342     return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
343         $date = clense_ISO8601($date);
344         $circ->due_date($date);
345     $e->update_action_circulation($circ) or return $e->die_event;
346     $e->commit;
347
348     return $circ->id;
349 }
350
351
352 __PACKAGE__->register_method(
353         method          => "create_in_house_use",
354         api_name                => 'open-ils.circ.in_house_use.create',
355         signature       =>      q/
356                 Creates an in-house use action.
357                 @param $authtoken The login session key
358                 @param params A hash of params including
359                         'location' The org unit id where the in-house use occurs
360                         'copyid' The copy in question
361                         'count' The number of in-house uses to apply to this copy
362                 @return An array of id's representing the id's of the newly created
363                 in-house use objects or an event on an error
364         /);
365
366 __PACKAGE__->register_method(
367         method          => "create_in_house_use",
368         api_name                => 'open-ils.circ.non_cat_in_house_use.create',
369 );
370
371
372 sub create_in_house_use {
373         my( $self, $client, $auth, $params ) = @_;
374
375         my( $evt, $copy );
376         my $org                 = $params->{location};
377         my $copyid              = $params->{copyid};
378         my $count               = $params->{count} || 1;
379         my $nc_type             = $params->{non_cat_type};
380         my $use_time    = $params->{use_time} || 'now';
381
382         my $e = new_editor(xact=>1,authtoken=>$auth);
383         return $e->event unless $e->checkauth;
384         return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
385
386         my $non_cat = 1 if $self->api_name =~ /non_cat/;
387
388         unless( $non_cat ) {
389                 if( $copyid ) {
390                         $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
391                 } else {
392                         $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
393                                 or return $e->event;
394                         $copyid = $copy->id;
395                 }
396         }
397
398         if( $use_time ne 'now' ) {
399                 $use_time = clense_ISO8601($use_time);
400                 $logger->debug("in_house_use setting use time to $use_time");
401         }
402
403         my @ids;
404         for(1..$count) {
405
406                 my $ihu;
407                 my $method;
408                 my $cmeth;
409
410                 if($non_cat) {
411                         $ihu = Fieldmapper::action::non_cat_in_house_use->new;
412                         $ihu->item_type($nc_type);
413                         $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
414                         $cmeth = "create_action_non_cat_in_house_use";
415
416                 } else {
417                         $ihu = Fieldmapper::action::in_house_use->new;
418                         $ihu->item($copyid);
419                         $method = 'open-ils.storage.direct.action.in_house_use.create';
420                         $cmeth = "create_action_in_house_use";
421                 }
422
423                 $ihu->staff($e->requestor->id);
424                 $ihu->org_unit($org);
425                 $ihu->use_time($use_time);
426
427                 $ihu = $e->$cmeth($ihu) or return $e->event;
428                 push( @ids, $ihu->id );
429         }
430
431         $e->commit;
432         return \@ids;
433 }
434
435
436
437
438
439 __PACKAGE__->register_method(
440         method  => "view_circs",
441         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
442         notes           => q/
443                 Retrieves the last X circs for a given copy
444                 @param authtoken The login session key
445                 @param copyid The copy to check
446                 @param count How far to go back in the item history
447                 @return An array of circ ids
448         /);
449
450 # ----------------------------------------------------------------------
451 # Returns $count most recent circs.  If count exceeds the configured 
452 # max, use the configured max instead
453 # ----------------------------------------------------------------------
454 sub view_circs {
455         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
456
457     my $e = new_editor(authtoken => $authtoken);
458     return $e->event unless $e->checkauth;
459     
460     my $copy = $e->retrieve_asset_copy([
461         $copyid,
462         {   flesh => 1,
463             flesh_fields => {acp => ['call_number']}
464         }
465     ]) or return $e->event;
466
467     return $e->event unless $e->allowed(
468         'VIEW_COPY_CHECKOUT_HISTORY', 
469         ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ? 
470             $copy->circ_lib : $copy->call_number->owning_lib);
471         
472     my $max_history = $U->ou_ancestor_setting_value(
473         $e->requestor->ws_ou, 'circ.item_checkout_history.max', $e);
474
475     if(defined $max_history) {
476         $count = $max_history unless defined $count and $count < $max_history;
477     } else {
478         $count = 4 unless defined $count;
479     }
480
481     return $e->search_action_circulation([
482         {target_copy => $copyid}, 
483         {limit => $count, order_by => { circ => "xact_start DESC" }} 
484     ]);
485 }
486
487
488 __PACKAGE__->register_method(
489         method  => "circ_count",
490         api_name        => "open-ils.circ.circulation.count",
491         notes           => q/
492                 Returns the number of times the item has circulated
493                 @param copyid The copy to check
494         /);
495
496 sub circ_count {
497         my( $self, $client, $copyid, $range ) = @_; 
498         my $e = OpenILS::Utils::Editor->new;
499         return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
500 }
501
502
503
504 __PACKAGE__->register_method(
505         method          => 'fetch_notes',
506         api_name                => 'open-ils.circ.copy_note.retrieve.all',
507         signature       => q/
508                 Returns an array of copy note objects.  
509                 @param args A named hash of parameters including:
510                         authtoken       : Required if viewing non-public notes
511                         itemid          : The id of the item whose notes we want to retrieve
512                         pub                     : True if all the caller wants are public notes
513                 @return An array of note objects
514         /);
515
516 __PACKAGE__->register_method(
517         method          => 'fetch_notes',
518         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
519         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
520
521 __PACKAGE__->register_method(
522         method          => 'fetch_notes',
523         api_name                => 'open-ils.circ.title_note.retrieve.all',
524         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
525
526
527 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
528 sub fetch_notes {
529         my( $self, $connection, $args ) = @_;
530
531         my $id = $$args{itemid};
532         my $authtoken = $$args{authtoken};
533         my( $r, $evt);
534
535         if( $self->api_name =~ /copy/ ) {
536                 if( $$args{pub} ) {
537                         return $U->cstorereq(
538                                 'open-ils.cstore.direct.asset.copy_note.search.atomic',
539                                 { owning_copy => $id, pub => 't' } );
540                 } else {
541                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
542                         return $evt if $evt;
543                         return $U->cstorereq(
544                                 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
545                 }
546
547         } elsif( $self->api_name =~ /call_number/ ) {
548                 if( $$args{pub} ) {
549                         return $U->cstorereq(
550                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
551                                 { call_number => $id, pub => 't' } );
552                 } else {
553                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
554                         return $evt if $evt;
555                         return $U->cstorereq(
556                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
557                 }
558
559         } elsif( $self->api_name =~ /title/ ) {
560                 if( $$args{pub} ) {
561                         return $U->cstorereq(
562                                 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
563                                 { record => $id, pub => 't' } );
564                 } else {
565                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
566                         return $evt if $evt;
567                         return $U->cstorereq(
568                                 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
569                 }
570         }
571
572         return undef;
573 }
574
575 __PACKAGE__->register_method(
576         method  => 'has_notes',
577         api_name        => 'open-ils.circ.copy.has_notes');
578 __PACKAGE__->register_method(
579         method  => 'has_notes',
580         api_name        => 'open-ils.circ.call_number.has_notes');
581 __PACKAGE__->register_method(
582         method  => 'has_notes',
583         api_name        => 'open-ils.circ.title.has_notes');
584
585
586 sub has_notes {
587         my( $self, $conn, $authtoken, $id ) = @_;
588         my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
589         return $editor->event unless $editor->checkauth;
590
591         my $n = $editor->search_asset_copy_note(
592                 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
593
594         $n = $editor->search_asset_call_number_note(
595                 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
596
597         $n = $editor->search_biblio_record_note(
598                 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
599
600         return scalar @$n;
601 }
602
603
604
605 __PACKAGE__->register_method(
606         method          => 'create_copy_note',
607         api_name                => 'open-ils.circ.copy_note.create',
608         signature       => q/
609                 Creates a new copy note
610                 @param authtoken The login session key
611                 @param note     The note object to create
612                 @return The id of the new note object
613         /);
614
615 sub create_copy_note {
616         my( $self, $connection, $authtoken, $note ) = @_;
617
618         my $e = new_editor(xact=>1, authtoken=>$authtoken);
619         return $e->event unless $e->checkauth;
620         my $copy = $e->retrieve_asset_copy(
621                 [
622                         $note->owning_copy,
623                         {       flesh => 1,
624                                 flesh_fields => { 'acp' => ['call_number'] }
625                         }
626                 ]
627         );
628
629         return $e->event unless 
630                 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
631
632         $note->create_date('now');
633         $note->creator($e->requestor->id);
634         $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
635         $note->clear_id;
636
637         $e->create_asset_copy_note($note) or return $e->event;
638         $e->commit;
639         return $note->id;
640 }
641
642
643 __PACKAGE__->register_method(
644         method          => 'delete_copy_note',
645         api_name                =>      'open-ils.circ.copy_note.delete',
646         signature       => q/
647                 Deletes an existing copy note
648                 @param authtoken The login session key
649                 @param noteid The id of the note to delete
650                 @return 1 on success - Event otherwise.
651                 /);
652 sub delete_copy_note {
653         my( $self, $conn, $authtoken, $noteid ) = @_;
654
655         my $e = new_editor(xact=>1, authtoken=>$authtoken);
656         return $e->die_event unless $e->checkauth;
657
658         my $note = $e->retrieve_asset_copy_note([
659                 $noteid,
660                 { flesh => 2,
661                         flesh_fields => {
662                                 'acpn' => [ 'owning_copy' ],
663                                 'acp' => [ 'call_number' ],
664                         }
665                 }
666         ]) or return $e->die_event;
667
668         if( $note->creator ne $e->requestor->id ) {
669                 return $e->die_event unless 
670                         $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
671         }
672
673         $e->delete_asset_copy_note($note) or return $e->die_event;
674         $e->commit;
675         return 1;
676 }
677
678
679 __PACKAGE__->register_method(
680         method => 'age_hold_rules',
681         api_name        =>  'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
682 );
683
684 sub age_hold_rules {
685         my( $self, $conn ) = @_;
686         return new_editor()->retrieve_all_config_rules_age_hold_protect();
687 }
688
689
690
691 __PACKAGE__->register_method(
692         method => 'copy_details_barcode',
693     authoritative => 1,
694         api_name => 'open-ils.circ.copy_details.retrieve.barcode');
695 sub copy_details_barcode {
696         my( $self, $conn, $auth, $barcode ) = @_;
697     my $e = new_editor();
698     my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
699     return $e->event unless $cid;
700         return copy_details( $self, $conn, $auth, $cid );
701 }
702
703
704 __PACKAGE__->register_method(
705         method => 'copy_details',
706         api_name => 'open-ils.circ.copy_details.retrieve');
707
708 sub copy_details {
709         my( $self, $conn, $auth, $copy_id ) = @_;
710         my $e = new_editor(authtoken=>$auth);
711         return $e->event unless $e->checkauth;
712
713         my $flesh = { flesh => 1 };
714
715         my $copy = $e->retrieve_asset_copy(
716                 [
717                         $copy_id,
718                         {
719                                 flesh => 2,
720                                 flesh_fields => {
721                                         acp => ['call_number'],
722                                         acn => ['record']
723                                 }
724                         }
725                 ]) or return $e->event;
726
727
728         # De-flesh the copy for backwards compatibility
729         my $mvr;
730         my $vol = $copy->call_number;
731         if( ref $vol ) {
732                 $copy->call_number($vol->id);
733                 my $record = $vol->record;
734                 if( ref $record ) {
735                         $vol->record($record->id);
736                         $mvr = $U->record_to_mvr($record);
737                 }
738         }
739
740
741         my $hold = $e->search_action_hold_request(
742                 { 
743                         current_copy            => $copy_id, 
744                         capture_time            => { "!=" => undef },
745                         fulfillment_time        => undef,
746                         cancel_time                     => undef,
747                 }
748         )->[0];
749
750         OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
751
752         my $transit = $e->search_action_transit_copy(
753                 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
754
755         # find the latest circ, open or closed
756         my $circ = $e->search_action_circulation(
757                 [
758                         { target_copy => $copy_id },
759                         { 
760                 flesh => 1,
761                 flesh_fields => {circ => ['checkin_workstation']},
762                 order_by => { circ => 'xact_start desc' }, 
763                 limit => 1 
764             }
765                 ]
766         )->[0];
767
768
769         return {
770                 copy            => $copy,
771                 hold            => $hold,
772                 transit => $transit,
773                 circ            => $circ,
774                 volume  => $vol,
775                 mvr             => $mvr,
776         };
777 }
778
779
780
781
782 __PACKAGE__->register_method(
783         method => 'mark_item',
784         api_name => 'open-ils.circ.mark_item_damaged',
785         signature       => q/
786                 Changes the status of a copy to "damaged". Requires MARK_ITEM_DAMAGED permission.
787                 @param authtoken The login session key
788                 @param copy_id The ID of the copy to mark as damaged
789                 @return 1 on success - Event otherwise.
790                 /
791 );
792 __PACKAGE__->register_method(
793         method => 'mark_item',
794         api_name => 'open-ils.circ.mark_item_missing',
795         signature       => q/
796                 Changes the status of a copy to "missing". Requires MARK_ITEM_MISSING permission.
797                 @param authtoken The login session key
798                 @param copy_id The ID of the copy to mark as missing 
799                 @return 1 on success - Event otherwise.
800                 /
801 );
802 __PACKAGE__->register_method(
803         method => 'mark_item',
804         api_name => 'open-ils.circ.mark_item_bindery',
805         signature       => q/
806                 Changes the status of a copy to "bindery". Requires MARK_ITEM_BINDERY permission.
807                 @param authtoken The login session key
808                 @param copy_id The ID of the copy to mark as bindery
809                 @return 1 on success - Event otherwise.
810                 /
811 );
812 __PACKAGE__->register_method(
813         method => 'mark_item',
814         api_name => 'open-ils.circ.mark_item_on_order',
815         signature       => q/
816                 Changes the status of a copy to "on order". Requires MARK_ITEM_ON_ORDER permission.
817                 @param authtoken The login session key
818                 @param copy_id The ID of the copy to mark as on order 
819                 @return 1 on success - Event otherwise.
820                 /
821 );
822 __PACKAGE__->register_method(
823         method => 'mark_item',
824         api_name => 'open-ils.circ.mark_item_ill',
825         signature       => q/
826                 Changes the status of a copy to "inter-library loan". Requires MARK_ITEM_ILL permission.
827                 @param authtoken The login session key
828                 @param copy_id The ID of the copy to mark as inter-library loan
829                 @return 1 on success - Event otherwise.
830                 /
831 );
832 __PACKAGE__->register_method(
833         method => 'mark_item',
834         api_name => 'open-ils.circ.mark_item_cataloging',
835         signature       => q/
836                 Changes the status of a copy to "cataloging". Requires MARK_ITEM_CATALOGING permission.
837                 @param authtoken The login session key
838                 @param copy_id The ID of the copy to mark as cataloging 
839                 @return 1 on success - Event otherwise.
840                 /
841 );
842 __PACKAGE__->register_method(
843         method => 'mark_item',
844         api_name => 'open-ils.circ.mark_item_reserves',
845         signature       => q/
846                 Changes the status of a copy to "reserves". Requires MARK_ITEM_RESERVES permission.
847                 @param authtoken The login session key
848                 @param copy_id The ID of the copy to mark as reserves
849                 @return 1 on success - Event otherwise.
850                 /
851 );
852 __PACKAGE__->register_method(
853         method => 'mark_item',
854         api_name => 'open-ils.circ.mark_item_discard',
855         signature       => q/
856                 Changes the status of a copy to "discard". Requires MARK_ITEM_DISCARD permission.
857                 @param authtoken The login session key
858                 @param copy_id The ID of the copy to mark as discard
859                 @return 1 on success - Event otherwise.
860                 /
861 );
862
863 sub mark_item {
864         my( $self, $conn, $auth, $copy_id, $args ) = @_;
865         my $e = new_editor(authtoken=>$auth, xact =>1);
866         return $e->die_event unless $e->checkauth;
867     $args ||= {};
868
869     my $copy = $e->retrieve_asset_copy([
870         $copy_id,
871         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
872             or return $e->die_event;
873
874     my $owning_lib = 
875         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
876             $copy->circ_lib : $copy->call_number->owning_lib;
877
878     return $e->die_event unless $e->allowed('UPDATE_COPY', $owning_lib);
879
880
881         my $perm = 'MARK_ITEM_MISSING';
882         my $stat = OILS_COPY_STATUS_MISSING;
883
884         if( $self->api_name =~ /damaged/ ) {
885                 $perm = 'MARK_ITEM_DAMAGED';
886                 $stat = OILS_COPY_STATUS_DAMAGED;
887         my $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
888         return $evt if $evt;
889
890         my $ses = OpenSRF::AppSession->create('open-ils.trigger');
891         $ses->request('open-ils.trigger.event.autocreate', 'damaged', $copy, $owning_lib);
892
893         } elsif ( $self->api_name =~ /bindery/ ) {
894                 $perm = 'MARK_ITEM_BINDERY';
895                 $stat = OILS_COPY_STATUS_BINDERY;
896         } elsif ( $self->api_name =~ /on_order/ ) {
897                 $perm = 'MARK_ITEM_ON_ORDER';
898                 $stat = OILS_COPY_STATUS_ON_ORDER;
899         } elsif ( $self->api_name =~ /ill/ ) {
900                 $perm = 'MARK_ITEM_ILL';
901                 $stat = OILS_COPY_STATUS_ILL;
902         } elsif ( $self->api_name =~ /cataloging/ ) {
903                 $perm = 'MARK_ITEM_CATALOGING';
904                 $stat = OILS_COPY_STATUS_CATALOGING;
905         } elsif ( $self->api_name =~ /reserves/ ) {
906                 $perm = 'MARK_ITEM_RESERVES';
907                 $stat = OILS_COPY_STATUS_RESERVES;
908         } elsif ( $self->api_name =~ /discard/ ) {
909                 $perm = 'MARK_ITEM_DISCARD';
910                 $stat = OILS_COPY_STATUS_DISCARD;
911         }
912
913
914         $copy->status($stat);
915         $copy->edit_date('now');
916         $copy->editor($e->requestor->id);
917
918         $e->update_asset_copy($copy) or return $e->die_event;
919
920         my $holds = $e->search_action_hold_request(
921                 { 
922                         current_copy => $copy->id,
923                         fulfillment_time => undef,
924                         cancel_time => undef,
925                 }
926         );
927
928         $e->commit;
929
930         $logger->debug("resetting holds that target the marked copy");
931         OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
932
933         return 1;
934 }
935
936 sub handle_mark_damaged {
937     my($e, $copy, $owning_lib, $args) = @_;
938
939     my $apply = $args->{apply_fines} || '';
940     return undef if $apply eq 'noapply';
941
942     # grab the last circulation
943     my $circ = $e->search_action_circulation([
944         {   target_copy => $copy->id}, 
945         {   limit => 1, 
946             order_by => {circ => "xact_start DESC"},
947             flesh => 2,
948             flesh_fields => {circ => ['target_copy', 'usr'], au => ['card']}
949         }
950     ])->[0];
951
952     return undef unless $circ;
953
954     my $charge_price = $U->ou_ancestor_setting_value(
955         $owning_lib, 'circ.charge_on_damaged', $e);
956
957     my $proc_fee = $U->ou_ancestor_setting_value(
958         $owning_lib, 'circ.damaged_item_processing_fee', $e) || 0;
959
960     return undef unless $charge_price or $proc_fee;
961
962     my $copy_price = ($charge_price) ? $U->get_copy_price($e, $copy) : 0;
963     my $total = $copy_price + $proc_fee;
964
965     if($apply) {
966         
967         if($charge_price and $copy_price) {
968             my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
969                 $e, $copy_price, 7, 'Damaged Item', $circ->id);
970             return $evt if $evt;
971         }
972
973         if($proc_fee) {
974             my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
975                 $e, $proc_fee, 8, 'Damaged Item Processing Fee', $circ->id);
976             return $evt if $evt;
977         }
978
979         my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
980         return $evt if $evt;
981
982         my $ses = OpenSRF::AppSession->create('open-ils.trigger');
983         $ses->request('open-ils.trigger.event.autocreate', 'checkout.damaged', $circ, $circ->circ_lib);
984
985         return undef;
986
987     } else {
988         return OpenILS::Event->new('DAMAGE_CHARGE', 
989             payload => {
990                 circ => $circ,
991                 charge => $total
992             }
993         );
994     }
995 }
996
997
998
999
1000
1001
1002 # ----------------------------------------------------------------------
1003 __PACKAGE__->register_method(
1004         method => 'magic_fetch',
1005         api_name => 'open-ils.agent.fetch'
1006 );
1007
1008 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1009
1010 sub magic_fetch {
1011         my( $self, $conn, $auth, $args ) = @_;
1012         my $e = new_editor( authtoken => $auth );
1013         return $e->event unless $e->checkauth;
1014
1015         my $hint = $$args{hint};
1016         my $id  = $$args{id};
1017
1018         # Is the call allowed to fetch this type of object?
1019         return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1020
1021         # Find the class the implements the given hint
1022         my ($class) = grep { 
1023                 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1024
1025         $class =~ s/Fieldmapper:://og;
1026         $class =~ s/::/_/og;
1027         my $method = "retrieve_$class";
1028
1029         my $obj = $e->$method($id) or return $e->event;
1030         return $obj;
1031 }
1032 # ----------------------------------------------------------------------
1033
1034
1035 __PACKAGE__->register_method(
1036         method  => "fleshed_circ_retrieve",
1037     authoritative => 1,
1038         api_name        => "open-ils.circ.fleshed.retrieve",);
1039
1040 sub fleshed_circ_retrieve {
1041         my( $self, $client, $id ) = @_;
1042         my $e = new_editor();
1043         my $circ = $e->retrieve_action_circulation(
1044                 [
1045                         $id,
1046                         { 
1047                                 flesh                           => 4,
1048                                 flesh_fields    => { 
1049                                         circ => [ qw/ target_copy / ],
1050                                         acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1051                                         ascecm => [ qw/ stat_cat stat_cat_entry / ],
1052                                         acn => [ qw/ record / ],
1053                                 }
1054                         }
1055                 ]
1056         ) or return $e->event;
1057         
1058         my $copy = $circ->target_copy;
1059         my $vol = $copy->call_number;
1060         my $rec = $circ->target_copy->call_number->record;
1061
1062         $vol->record($rec->id);
1063         $copy->call_number($vol->id);
1064         $circ->target_copy($copy->id);
1065
1066         my $mvr;
1067
1068         if( $rec->id == OILS_PRECAT_RECORD ) {
1069                 $rec = undef;
1070                 $vol = undef;
1071         } else { 
1072                 $mvr = $U->record_to_mvr($rec);
1073                 $rec->marc(''); # drop the bulky marc data
1074         }
1075
1076         return {
1077                 circ => $circ,
1078                 copy => $copy,
1079                 volume => $vol,
1080                 record => $rec,
1081                 mvr => $mvr,
1082         };
1083 }
1084
1085
1086
1087 __PACKAGE__->register_method(
1088         method  => "test_batch_circ_events",
1089         api_name        => "open-ils.circ.trigger_event_by_def_and_barcode.fire"
1090 );
1091
1092 #  method for testing the behavior of a given event definition
1093 sub test_batch_circ_events {
1094     my($self, $conn, $auth, $event_def, $barcode) = @_;
1095
1096     my $e = new_editor(authtoken => $auth);
1097         return $e->event unless $e->checkauth;
1098     return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
1099
1100     my $copy = $e->search_asset_copy({barcode => $barcode, deleted => 'f'})->[0]
1101         or return $e->event;
1102
1103     my $circ = $e->search_action_circulation(
1104         {target_copy => $copy->id, checkin_time => undef})->[0]
1105         or return $e->event;
1106         
1107     return undef unless $circ;
1108
1109     return $U->fire_object_event($event_def, undef, $circ, $e->requestor->ws_ou)
1110 }
1111
1112
1113
1114
1115 # {"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}}
1116
1117
1118 1;