]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
added simple mark-damaged/mark-missing api - unused, needs testing and probably some...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ.pm
1 package OpenILS::Application::Circ;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::Application::Circ::Circulate;
6 use OpenILS::Application::Circ::Survey;
7 use OpenILS::Application::Circ::StatCat;
8 use OpenILS::Application::Circ::Holds;
9 use OpenILS::Application::Circ::HoldNotify;
10 use OpenILS::Application::Circ::Money;
11 use OpenILS::Application::Circ::NonCat;
12 use OpenILS::Application::Circ::CopyLocations;
13
14 use DateTime;
15 use DateTime::Format::ISO8601;
16
17 use OpenILS::Application::AppUtils;
18
19 use OpenSRF::Utils qw/:datetime/;
20 use OpenILS::Utils::ModsParser;
21 use OpenILS::Event;
22 use OpenSRF::EX qw(:try);
23 use OpenSRF::Utils::Logger qw(:logger);
24 use OpenILS::Utils::Fieldmapper;
25 use OpenILS::Utils::Editor q/:funcs/;
26 use OpenILS::Utils::CStoreEditor q/:funcs/;
27 use OpenILS::Const qw/:const/;
28 use OpenSRF::Utils::SettingsClient;
29
30 my $apputils = "OpenILS::Application::AppUtils";
31 my $U = $apputils;
32
33
34 # ------------------------------------------------------------------------
35 # Top level Circ package;
36 # ------------------------------------------------------------------------
37
38 sub initialize {
39         my $self = shift;
40         OpenILS::Application::Circ::Circulate->initialize();
41 }
42
43
44 __PACKAGE__->register_method(
45         method => 'retrieve_circ',
46         api_name        => 'open-ils.circ.retrieve',
47         signature => q/
48                 Retrieve a circ object by id
49                 @param authtoken Login session key
50                 @pararm circid The id of the circ object
51         /
52 );
53 sub retrieve_circ {
54         my( $s, $c, $a, $i ) = @_;
55         my $e = new_editor(authtoken => $a);
56         return $e->event unless $e->checkauth;
57         my $circ = $e->retrieve_action_circulation($i) or return $e->event;
58         if( $e->requestor->id ne $circ->usr ) {
59                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
60         }
61         return $circ;
62 }
63
64
65 __PACKAGE__->register_method(
66         method => 'fetch_circ_mods',
67         api_name => 'open-ils.circ.circ_modifier.retrieve.all');
68 sub fetch_circ_mods {
69         my $conf = OpenSRF::Utils::SettingsClient->new;
70         return $conf->config_value(
71                 'apps', 'open-ils.circ', 'app_settings', 'circ_modifiers', 'mod' );
72 }
73
74 __PACKAGE__->register_method(
75         method => 'fetch_bill_types',
76         api_name => 'open-ils.circ.billing_type.retrieve.all');
77 sub fetch_bill_types {
78         my $conf = OpenSRF::Utils::SettingsClient->new;
79         return $conf->config_value(
80                 'apps', 'open-ils.circ', 'app_settings', 'billing_types', 'type' );
81 }
82
83
84 # ------------------------------------------------------------------------
85 # Returns an array of {circ, record} hashes checked out by the user.
86 # ------------------------------------------------------------------------
87 __PACKAGE__->register_method(
88         method  => "checkouts_by_user",
89         api_name        => "open-ils.circ.actor.user.checked_out",
90         NOTES           => <<"  NOTES");
91         Returns a list of open circulations as a pile of objects.  each object
92         contains the relevant copy, circ, and record
93         NOTES
94
95 sub checkouts_by_user {
96         my( $self, $client, $user_session, $user_id ) = @_;
97
98         my( $requestor, $target, $copy, $record, $evt );
99
100         ( $requestor, $target, $evt ) = 
101                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
102         return $evt if $evt;
103
104         my $circs = $apputils->simplereq(
105                 'open-ils.cstore',
106                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
107                 { usr => $target->id, checkin_time => undef } );
108 #               { usr => $target->id } );
109
110         my @results;
111         for my $circ (@$circs) {
112
113                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
114                 return $evt if $evt;
115
116                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
117
118                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
119                 return $evt if $evt;
120
121                 my $mods = $apputils->record_to_mvr($record);
122
123                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
124         }
125
126         return \@results;
127
128 }
129
130
131
132 __PACKAGE__->register_method(
133         method  => "checkouts_by_user_slim",
134         api_name        => "open-ils.circ.actor.user.checked_out.slim",
135         NOTES           => <<"  NOTES");
136         Returns a list of open circulation objects
137         NOTES
138
139 # DEPRECAT ME?? XXX
140 sub checkouts_by_user_slim {
141         my( $self, $client, $user_session, $user_id ) = @_;
142
143         my( $requestor, $target, $copy, $record, $evt );
144
145         ( $requestor, $target, $evt ) = 
146                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
147         return $evt if $evt;
148
149         $logger->debug( 'User ' . $requestor->id . 
150                 " retrieving checked out items for user " . $target->id );
151
152         # XXX Make the call correct..
153         return $apputils->simplereq(
154                 'open-ils.cstore',
155                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
156                 { usr => $target->id, checkin_time => undef } );
157 #               { usr => $target->id } );
158 }
159
160
161 __PACKAGE__->register_method(
162         method  => "checkouts_by_user_opac",
163         api_name        => "open-ils.circ.actor.user.checked_out.opac",);
164
165 # XXX Deprecate Me
166 sub checkouts_by_user_opac {
167         my( $self, $client, $auth, $user_id ) = @_;
168
169         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
170         return $e->event unless $e->checkauth;
171         $user_id ||= $e->requestor->id;
172         return $e->event unless 
173                 my $patron = $e->retrieve_actor_user($user_id);
174
175         my $data;
176         my $search = {usr => $user_id, stop_fines => undef};
177
178         if( $user_id ne $e->requestor->id ) {
179                 $data = $e->search_action_circulation(
180                         $search, {checkperm=>1, permorg=>$patron->home_ou})
181                         or return $e->event;
182
183         } else {
184                 $data = $e->search_action_circulation($search);
185         }
186
187         return $data;
188 }
189
190
191 __PACKAGE__->register_method(
192         method  => "title_from_transaction",
193         api_name        => "open-ils.circ.circ_transaction.find_title",
194         NOTES           => <<"  NOTES");
195         Returns a mods object for the title that is linked to from the 
196         copy from the hold that created the given transaction
197         NOTES
198
199 sub title_from_transaction {
200         my( $self, $client, $login_session, $transactionid ) = @_;
201
202         my( $user, $circ, $title, $evt );
203
204         ( $user, $evt ) = $apputils->checkses( $login_session );
205         return $evt if $evt;
206
207         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
208         return $evt if $evt;
209         
210         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
211         return $evt if $evt;
212
213         return $apputils->record_to_mvr($title);
214 }
215
216
217 __PACKAGE__->register_method(
218         method  => "set_circ_lost",
219         api_name        => "open-ils.circ.circulation.set_lost",
220         NOTES           => <<"  NOTES");
221         Params are login, barcode
222         login must have SET_CIRC_LOST perms
223         Sets a circulation to lost
224         NOTES
225
226 __PACKAGE__->register_method(
227         method  => "set_circ_lost",
228         api_name        => "open-ils.circ.circulation.set_claims_returned",
229         NOTES           => <<"  NOTES");
230         Params are login, barcode
231         login must have SET_CIRC_MISSING perms
232         Sets a circulation to lost
233         NOTES
234
235 sub set_circ_lost {
236         my( $self, $client, $login, $args ) = @_;
237         my( $user, $circ, $copy, $evt );
238
239         my $barcode             = $$args{barcode};
240         my $backdate    = $$args{backdate};
241
242         ( $user, $evt ) = $U->checkses($login);
243         return $evt if $evt;
244
245         # Grab the related copy
246         ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
247         return $evt if $evt;
248
249         my $isclaims    = $self->api_name =~ /claims_returned/;
250         my $islost              = $self->api_name =~ /lost/;
251         my $session             = $U->start_db_session(); 
252
253         # grab the circulation
254         ( $circ ) = $U->fetch_open_circulation( $copy->id );
255         return 1 unless $circ;
256
257         if($islost) {
258                 $evt  = _set_circ_lost($copy, $circ, $user, $session) if $islost;
259                 return $evt if $evt;
260         }
261
262         if($isclaims) {
263                 $evt = _set_circ_claims_returned(
264                         $user, $circ, $session, $backdate );
265                 return $evt if $evt;
266
267         }
268
269         $circ->stop_fines_time('now') unless $circ->stop_fines_time;
270         my $s = $session->request(
271                 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
272
273         return $U->DB_UPDATE_FAILED($circ) unless defined($s);
274         $U->commit_db_session($session);
275
276         return 1;
277 }
278
279 sub _set_circ_lost {
280         my( $copy, $circ, $reqr, $session ) = @_;
281
282         my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
283         return $evt if $evt;
284
285         $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
286                 " lost  for circ ".  $circ->id. " and checking for necessary charges");
287
288         if( $copy->status ne OILS_COPY_STATUS_LOST ) {
289                 $copy->status(OILS_COPY_STATUS_LOST);
290                 $U->update_copy(
291                         copy            => $copy, 
292                         editor  => $reqr->id, 
293                         session => $session);
294         }
295
296         # if the copy has a price defined and/or a processing fee, bill the patron
297
298         my $copy_price = $copy->price || 0;
299
300         $logger->debug("lost copy has a price of $copy_price");
301
302         # If the copy has a price configured, charge said price to the user
303         if($copy_price and $copy_price > 0) {
304                 $evt = _make_bill($session, $copy_price, 'Lost Materials', $circ->id);
305                 return $evt if $evt;
306         }
307
308         # if the location that owns the copy has a processing fee, charge the user
309         my $owner = $U->fetch_copy_owner($copy->id);
310         $logger->info("circ fetching org settings for $owner to determine processing fee");
311
312         my $settings = $U->simplereq(
313                 'open-ils.actor', 'open-ils.actor.org_unit.settings.retrieve', $owner );
314         my $fee = $settings->{'circ.lost_materials_processing_fee'} || 0;
315
316         if( $fee ) {
317                 $evt = _make_bill($session, $fee, 'Lost Materials Processing Fee', $circ->id);
318                 return $evt if $evt;
319         }
320         
321         $circ->stop_fines(OILS_STOP_FINES_LOST);                
322         return undef;
323 }
324
325 sub _make_bill {
326         my( $session, $amount, $type, $xactid ) = @_;
327
328         $logger->activity("The system is charging $amount ".
329                 " [$type] for lost materials on circulation $xactid");
330
331         my $bill = Fieldmapper::money::billing->new;
332
333         $bill->xact($xactid);
334         $bill->amount($amount);
335         $bill->billing_type($type); # - XXX these strings should be configurable some day
336         $bill->note('SYSTEM GENERATED');
337
338         my $id = $session->request(
339                 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
340
341         return $U->DB_UPDATE_FAILED($bill) unless defined $id;
342         return undef;
343 }
344
345 sub _set_circ_claims_returned {
346         my( $reqr, $circ, $session, $backdate ) = @_;
347
348         my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
349         return $evt if $evt;
350         $circ->stop_fines("CLAIMSRETURNED");
351
352         $logger->activity("user ".$reqr->id.
353                 " marking circ".  $circ->id. " as claims returned");
354
355         # allow the caller to backdate the circulation and void any fines
356         # that occurred after the backdate
357         if($backdate) {
358                 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
359                         $backdate, $circ, $reqr, $session );
360         }
361
362         return undef;
363 }
364
365
366
367 __PACKAGE__->register_method (
368         method          => 'set_circ_due_date',
369         api_name                => 'open-ils.circ.circulation.due_date.update',
370         signature       => q/
371                 Updates the due_date on the given circ
372                 @param authtoken
373                 @param circid The id of the circ to update
374                 @param date The timestamp of the new due date
375         /
376 );
377
378 sub set_circ_due_date {
379         my( $s, $c, $authtoken, $circid, $date ) = @_;
380         my ($circ, $evt) = $U->fetch_circulation($circid);
381         return $evt if $evt;
382
383         my $reqr;
384         ($reqr, $evt) = $U->checkses($authtoken);
385         return $evt if $evt;
386
387         $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
388         return $evt if $evt;
389
390         $date = clense_ISO8601($date);
391         $logger->activity("user ".$reqr->id.
392                 " updating due_date on circ $circid: $date");
393
394         $circ->due_date($date);
395         my $stat = $U->storagereq(
396                 'open-ils.storage.direct.action.circulation.update', $circ);
397         return $U->DB_UPDATE_FAILED unless defined $stat;
398         return $stat;
399 }
400
401
402 __PACKAGE__->register_method(
403         method          => "create_in_house_use",
404         api_name                => 'open-ils.circ.in_house_use.create',
405         signature       =>      q/
406                 Creates an in-house use action.
407                 @param $authtoken The login session key
408                 @param params A hash of params including
409                         'location' The org unit id where the in-house use occurs
410                         'copyid' The copy in question
411                         'count' The number of in-house uses to apply to this copy
412                 @return An array of id's representing the id's of the newly created
413                 in-house use objects or an event on an error
414         /);
415
416 sub create_in_house_use {
417         my( $self, $client, $authtoken, $params ) = @_;
418
419         my( $staff, $evt, $copy );
420         my $org                 = $params->{location};
421         my $copyid              = $params->{copyid};
422         my $count               = $params->{count} || 1;
423         my $use_time    = $params->{use_time} || 'now';
424
425         if(!$copyid) {
426                 my $barcode = $params->{barcode};
427                 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
428                 return $evt if $evt;
429                 $copyid = $copy->id;
430         }
431
432         ($staff, $evt) = $U->checkses($authtoken);
433         return $evt if $evt;
434
435         ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
436         return $evt if $evt;
437
438         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
439         return $evt if $evt;
440
441         $logger->activity("User " . $staff->id .
442                 " creating $count in-house use(s) for copy $copyid at location $org");
443
444         if( $use_time ne 'now' ) {
445                 $use_time = clense_ISO8601($use_time);
446                 $logger->debug("in_house_use setting use time to $use_time");
447         }
448
449         my @ids;
450         for(1..$count) {
451                 my $ihu = Fieldmapper::action::in_house_use->new;
452
453                 $ihu->item($copyid);
454                 $ihu->staff($staff->id);
455                 $ihu->org_unit($org);
456                 $ihu->use_time($use_time);
457
458                 my $id = $U->simplereq(
459                         'open-ils.storage',
460                         'open-ils.storage.direct.action.in_house_use.create', $ihu );
461
462                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
463                 push @ids, $id;
464         }
465
466         return \@ids;
467 }
468
469
470
471 __PACKAGE__->register_method(
472         method  => "view_circs",
473         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
474         notes           => q/
475                 Retrieves the last X circs for a given copy
476                 @param authtoken The login session key
477                 @param copyid The copy to check
478                 @param count How far to go back in the item history
479                 @return An array of circ ids
480         /);
481
482
483
484 sub view_circs {
485         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
486
487         my( $requestor, $evt ) = $U->checksesperm(
488                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
489         return $evt if $evt;
490
491         return [] unless $count;
492
493         my $circs = $U->cstorereq(
494                 'open-ils.cstore.direct.action.circulation.search.atomic',
495                         { 
496                                 target_copy => $copyid, 
497                         }, 
498                         { 
499                                 limit => $count, 
500                                 order_by => { ac => "xact_start DESC" }
501                         } 
502         );
503
504         return $circs;
505 }
506
507
508 __PACKAGE__->register_method(
509         method  => "circ_count",
510         api_name        => "open-ils.circ.circulation.count",
511         notes           => q/
512                 Returns the number of times the item has circulated
513                 @param copyid The copy to check
514         /);
515
516 sub circ_count {
517         my( $self, $client, $copyid, $range ) = @_; 
518         my $e = OpenILS::Utils::Editor->new;
519         return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
520 }
521
522
523
524 __PACKAGE__->register_method(
525         method          => 'fetch_notes',
526         api_name                => 'open-ils.circ.copy_note.retrieve.all',
527         signature       => q/
528                 Returns an array of copy note objects.  
529                 @param args A named hash of parameters including:
530                         authtoken       : Required if viewing non-public notes
531                         itemid          : The id of the item whose notes we want to retrieve
532                         pub                     : True if all the caller wants are public notes
533                 @return An array of note objects
534         /);
535
536 __PACKAGE__->register_method(
537         method          => 'fetch_notes',
538         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
539         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
540
541 __PACKAGE__->register_method(
542         method          => 'fetch_notes',
543         api_name                => 'open-ils.circ.title_note.retrieve.all',
544         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
545
546
547 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
548 sub fetch_notes {
549         my( $self, $connection, $args ) = @_;
550
551         my $id = $$args{itemid};
552         my $authtoken = $$args{authtoken};
553         my( $r, $evt);
554
555         if( $self->api_name =~ /copy/ ) {
556                 if( $$args{pub} ) {
557                         return $U->cstorereq(
558                                 'open-ils.cstore.direct.asset.copy_note.search.atomic',
559                                 { owning_copy => $id, pub => 't' } );
560                 } else {
561                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
562                         return $evt if $evt;
563                         return $U->cstorereq(
564                                 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
565                 }
566
567         } elsif( $self->api_name =~ /call_number/ ) {
568                 if( $$args{pub} ) {
569                         return $U->cstorereq(
570                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
571                                 { call_number => $id, pub => 't' } );
572                 } else {
573                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
574                         return $evt if $evt;
575                         return $U->cstorereq(
576                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
577                 }
578
579         } elsif( $self->api_name =~ /title/ ) {
580                 if( $$args{pub} ) {
581                         return $U->cstorereq(
582                                 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
583                                 { record => $id, pub => 't' } );
584                 } else {
585                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
586                         return $evt if $evt;
587                         return $U->cstorereq(
588                                 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
589                 }
590         }
591
592         return undef;
593 }
594
595 __PACKAGE__->register_method(
596         method  => 'has_notes',
597         api_name        => 'open-ils.circ.copy.has_notes');
598 __PACKAGE__->register_method(
599         method  => 'has_notes',
600         api_name        => 'open-ils.circ.call_number.has_notes');
601 __PACKAGE__->register_method(
602         method  => 'has_notes',
603         api_name        => 'open-ils.circ.title.has_notes');
604
605
606 sub has_notes {
607         my( $self, $conn, $authtoken, $id ) = @_;
608         my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
609         return $editor->event unless $editor->checkauth;
610
611         my $n = $editor->search_asset_copy_note(
612                 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
613
614         $n = $editor->search_asset_call_number_note(
615                 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
616
617         $n = $editor->search_biblio_record_note(
618                 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
619
620         return scalar @$n;
621 }
622
623 __PACKAGE__->register_method(
624         method          => 'create_copy_note',
625         api_name                => 'open-ils.circ.copy_note.create',
626         signature       => q/
627                 Creates a new copy note
628                 @param authtoken The login session key
629                 @param note     The note object to create
630                 @return The id of the new note object
631         /);
632
633 sub create_copy_note {
634         my( $self, $connection, $authtoken, $note ) = @_;
635         my( $cnowner, $requestor, $evt );
636
637         ($cnowner, $evt) = $U->fetch_copy_owner($note->owning_copy);
638         return $evt if $evt;
639         ($requestor, $evt) = $U->checkses($authtoken);
640         return $evt if $evt;
641         $evt = $U->check_perms($requestor->id, $cnowner, 'CREATE_COPY_NOTE');
642         return $evt if $evt;
643
644         $note->create_date('now');
645         $note->creator($requestor->id);
646         $note->pub( ($U->is_true($note->pub)) ? 1 : 0 );
647
648         my $id = $U->storagereq(
649                 'open-ils.storage.direct.asset.copy_note.create', $note );
650         return $U->DB_UPDATE_FAILED($note) unless $id;
651
652         $logger->activity("User ".$requestor->id." created a new copy ".
653                 "note [$id] for copy ".$note->owning_copy." with text ".$note->value);
654
655         return $id;
656 }
657
658 __PACKAGE__->register_method(
659         method          => 'delete_copy_note',
660         api_name                =>      'open-ils.circ.copy_note.delete',
661         signature       => q/
662                 Deletes an existing copy note
663                 @param authtoken The login session key
664                 @param noteid The id of the note to delete
665                 @return 1 on success - Event otherwise.
666                 /);
667
668 sub delete_copy_note {
669         my( $self, $conn, $authtoken, $noteid ) = @_;
670         my( $requestor, $note, $owner, $evt );
671
672         ($requestor, $evt) = $U->checkses($authtoken);
673         return $evt if $evt;
674
675         ($note, $evt) = $U->fetch_copy_note($noteid);
676         return $evt if $evt;
677
678         if( $note->creator ne $requestor->id ) {
679                 ($owner, $evt) = $U->fetch_copy_onwer($note->owning_copy);
680                 return $evt if $evt;
681                 $evt = $U->check_perms($requestor->id, $owner, 'DELETE_COPY_NOTE');
682                 return $evt if $evt;
683         }
684
685         my $stat = $U->storagereq(
686                 'open-ils.storage.direct.asset.copy_note.delete', $noteid );
687         return $U->DB_UPDATE_FAILED($noteid) unless $stat;
688
689         $logger->activity("User ".$requestor->id." deleted copy note $noteid");
690         return 1;
691 }
692
693
694 __PACKAGE__->register_method(
695         method => 'age_hold_rules',
696         api_name        =>  'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
697 );
698
699 sub age_hold_rules {
700         my( $self, $conn ) = @_;
701         return new_editor()->retrieve_all_config_rules_age_hold_protect();
702 }
703
704
705
706
707 __PACKAGE__->register_method(
708         method => 'copy_details',
709         api_name => 'open-ils.circ.copy_details.retrieve',
710         signature => q/
711         /
712 );
713
714 sub copy_details {
715         my( $self, $conn, $auth, $copy_id ) = @_;
716         my $e = new_editor(authtoken=>$auth);
717         return $e->event unless $e->checkauth;
718
719         my $copy = $e->retrieve_asset_copy($copy_id)
720                 or return $e->event;
721
722         my $hold = $e->search_action_hold_request(
723                 { 
724                         current_copy => $copy_id, 
725                         capture_time => { "!=" => undef },
726                         fulfillment_time => undef,
727                 }
728         )->[0];
729
730         OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
731
732         my $transit = $e->search_action_transit_copy(
733                 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
734
735         # find the latest circ, open or closed
736         my $circ = $e->search_action_circulation(
737                 [
738                         { target_copy => $copy_id },
739                         { order_by => { circ => 'xact_start desc' }, limit => 1 }
740                 ]
741         )->[0];
742
743         return {
744                 copy            => $copy,
745                 hold            => $hold,
746                 transit => $transit,
747                 circ            => $circ,
748         };
749 }
750
751
752
753
754 __PACKAGE__->register_method(
755         method => 'mark_item',
756         api_name => 'open-ils.circ.mark_item_damaged',
757 );
758 __PACKAGE__->register_method(
759         method => 'mark_item',
760         api_name => 'open-ils.circ.mark_item_missing',
761 );
762
763 sub mark_item {
764         my( $self, $conn, $auth, $copy_id ) = @_;
765         my $e = new_editor(authtoken=>$auth, xact =>1);
766         return $e->event unless $e->checkauth;
767
768         my $perm = 'MARK_ITEM_MISSING';
769         my $stat = OILS_COPY_STATUS_MISSING;
770
771         if( $self->api_name =~ /damaged/ ) {
772                 $perm = 'MARK_ITEM_DAMAGED';
773                 $stat = OILS_COPY_STATUS_DAMAGED;
774         }
775
776         my $copy = $e->retrieve_asset_copy($copy_id)
777                 or return $e->event;
778         $copy->status($stat);
779
780         $e->update_asset_copy($copy) or return $e->event;
781
782         $e->commit;
783         return 1;
784 }
785
786
787
788
789
790 1;