]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
fixed field name on copy note
[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;
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         $circ = new_editor()->search_action_circulation(
258                 { checkin_time => undef, target_copy => $copy->id } )->[0];
259         return 1 unless $circ;
260
261         if($islost) {
262                 $evt  = _set_circ_lost($copy, $circ, $user, $session) if $islost;
263                 return $evt if $evt;
264         }
265
266         if($isclaims) {
267                 $evt = _set_circ_claims_returned(
268                         $user, $circ, $session, $backdate );
269                 return $evt if $evt;
270
271         }
272
273         $circ->stop_fines_time('now') unless $circ->stop_fines_time;
274         my $s = $session->request(
275                 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
276
277         return $U->DB_UPDATE_FAILED($circ) unless defined($s);
278         $U->commit_db_session($session);
279
280         return 1;
281 }
282
283 sub _set_circ_lost {
284         my( $copy, $circ, $reqr, $session ) = @_;
285
286         my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_LOST');
287         return $evt if $evt;
288
289         $logger->activity("user ".$reqr->id." marking copy ".$copy->id.
290                 " lost  for circ ".  $circ->id. " and checking for necessary charges");
291
292         if( $copy->status ne OILS_COPY_STATUS_LOST ) {
293                 $copy->status(OILS_COPY_STATUS_LOST);
294                 $U->update_copy(
295                         copy            => $copy, 
296                         editor  => $reqr->id, 
297                         session => $session);
298         }
299
300         # if the copy has a price defined and/or a processing fee, bill the patron
301
302         # if the location that owns the copy has a processing fee, charge the user
303         my $owner = $U->fetch_copy_owner($copy->id);
304         $logger->info("circ fetching org settings for $owner ".
305                 "to determine processing fee and default copy price");
306
307         my $settings = $U->simplereq(
308                 'open-ils.actor', 'open-ils.actor.org_unit.settings.retrieve', $owner );
309         my $fee = $settings->{'circ.lost_materials_processing_fee'} || 0;
310
311         # If the copy has a price configured, charge said price to the user
312         # otherwise use the default price
313         my $s = OILS_SETTING_DEF_ITEM_PRICE;
314         my $copy_price = $copy->price;
315         $copy_price = $settings->{$s} unless defined $copy_price;
316         if($copy_price and $copy_price > 0) {
317                 $logger->debug("lost copy has a price of $copy_price");
318                 $evt = _make_bill($session, $copy_price, 'Lost Materials', $circ->id);
319                 return $evt if $evt;
320         }
321
322
323         if( $fee ) {
324                 $evt = _make_bill($session, $fee, 'Lost Materials Processing Fee', $circ->id);
325                 return $evt if $evt;
326         }
327         
328         $circ->stop_fines(OILS_STOP_FINES_LOST);                
329         return undef;
330 }
331
332 sub _make_bill {
333         my( $session, $amount, $type, $xactid ) = @_;
334
335         $logger->activity("The system is charging $amount ".
336                 " [$type] for lost materials on circulation $xactid");
337
338         my $bill = Fieldmapper::money::billing->new;
339
340         $bill->xact($xactid);
341         $bill->amount($amount);
342         $bill->billing_type($type); # - XXX these strings should be configurable some day
343         $bill->note('SYSTEM GENERATED');
344
345         my $id = $session->request(
346                 'open-ils.storage.direct.money.billing.create', $bill )->gather(1);
347
348         return $U->DB_UPDATE_FAILED($bill) unless defined $id;
349         return undef;
350 }
351
352 sub _set_circ_claims_returned {
353         my( $reqr, $circ, $session, $backdate ) = @_;
354
355         my $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'SET_CIRC_CLAIMS_RETURNED');
356         return $evt if $evt;
357         $circ->stop_fines("CLAIMSRETURNED");
358
359         $logger->activity("user ".$reqr->id.
360                 " marking circ".  $circ->id. " as claims returned");
361
362         # allow the caller to backdate the circulation and void any fines
363         # that occurred after the backdate
364         if($backdate) {
365                 OpenILS::Application::Circ::Circulate::_checkin_handle_backdate(
366                         $backdate, $circ, $reqr, $session );
367                 $circ->stop_fines_time(clense_ISO8601($backdate))
368         }
369
370         return undef;
371 }
372
373
374
375 __PACKAGE__->register_method (
376         method          => 'set_circ_due_date',
377         api_name                => 'open-ils.circ.circulation.due_date.update',
378         signature       => q/
379                 Updates the due_date on the given circ
380                 @param authtoken
381                 @param circid The id of the circ to update
382                 @param date The timestamp of the new due date
383         /
384 );
385
386 sub set_circ_due_date {
387         my( $s, $c, $authtoken, $circid, $date ) = @_;
388         my ($circ, $evt) = $U->fetch_circulation($circid);
389         return $evt if $evt;
390
391         my $reqr;
392         ($reqr, $evt) = $U->checkses($authtoken);
393         return $evt if $evt;
394
395         $evt = $U->check_perms($reqr->id, $circ->circ_lib, 'CIRC_OVERRIDE_DUE_DATE');
396         return $evt if $evt;
397
398         $date = clense_ISO8601($date);
399         $logger->activity("user ".$reqr->id.
400                 " updating due_date on circ $circid: $date");
401
402         $circ->due_date($date);
403         my $stat = $U->storagereq(
404                 'open-ils.storage.direct.action.circulation.update', $circ);
405         return $U->DB_UPDATE_FAILED unless defined $stat;
406         return $stat;
407 }
408
409
410 __PACKAGE__->register_method(
411         method          => "create_in_house_use",
412         api_name                => 'open-ils.circ.in_house_use.create',
413         signature       =>      q/
414                 Creates an in-house use action.
415                 @param $authtoken The login session key
416                 @param params A hash of params including
417                         'location' The org unit id where the in-house use occurs
418                         'copyid' The copy in question
419                         'count' The number of in-house uses to apply to this copy
420                 @return An array of id's representing the id's of the newly created
421                 in-house use objects or an event on an error
422         /);
423
424 __PACKAGE__->register_method(
425         method          => "create_in_house_use",
426         api_name                => 'open-ils.circ.non_cat_in_house_use.create',
427 );
428
429 =head OLD CODE
430 sub ___create_in_house_use {
431         my( $self, $client, $authtoken, $params ) = @_;
432
433         my( $staff, $evt, $copy );
434         my $org                 = $params->{location};
435         my $copyid              = $params->{copyid};
436         my $count               = $params->{count} || 1;
437         my $nc_type             = $params->{non_cat_type};
438         my $use_time    = $params->{use_time} || 'now';
439
440         my $non_cat = 1 if $self->api_name =~ /non_cat/;
441
442         unless( $non_cat ) {
443                 unless( $copyid ) {
444                         my $barcode = $params->{barcode};
445                         ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
446                         return $evt if $evt;
447                         $copyid = $copy->id;
448                 }
449                 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
450                 return $evt if $evt;
451         }
452
453         ($staff, $evt) = $U->checkses($authtoken);
454         return $evt if $evt;
455
456         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
457         return $evt if $evt;
458
459         if( $use_time ne 'now' ) {
460                 $use_time = clense_ISO8601($use_time);
461                 $logger->debug("in_house_use setting use time to $use_time");
462         }
463
464         my @ids;
465         for(1..$count) {
466
467                 my $ihu;
468                 my $method;
469
470                 if($non_cat) {
471                         $ihu = Fieldmapper::action::non_cat_in_house_use->new;
472                         $ihu->noncat_type($nc_type);
473                         $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
474                 } else {
475                         $ihu = Fieldmapper::action::in_house_use->new;
476                         $ihu->item($copyid);
477                         $method = 'open-ils.storage.direct.action.in_house_use.create';
478                 }
479
480                 $ihu->staff($staff->id);
481                 $ihu->org_unit($org);
482                 $ihu->use_time($use_time);
483
484                 my $id = $U->simplereq('open-ils.storage', $method, $ihu );
485
486                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
487                 push @ids, $id;
488         }
489
490         return \@ids;
491 }
492 =cut
493
494
495
496
497 sub create_in_house_use {
498         my( $self, $client, $auth, $params ) = @_;
499
500         my( $evt, $copy );
501         my $org                 = $params->{location};
502         my $copyid              = $params->{copyid};
503         my $count               = $params->{count} || 1;
504         my $nc_type             = $params->{non_cat_type};
505         my $use_time    = $params->{use_time} || 'now';
506
507         my $e = new_editor(xact=>1,authtoken=>$auth);
508         return $e->event unless $e->checkauth;
509         return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
510
511         my $non_cat = 1 if $self->api_name =~ /non_cat/;
512
513         unless( $non_cat ) {
514                 if( $copyid ) {
515                         $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
516                 } else {
517                         $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
518                                 or return $e->event;
519                         $copyid = $copy->id;
520                 }
521         }
522
523         if( $use_time ne 'now' ) {
524                 $use_time = clense_ISO8601($use_time);
525                 $logger->debug("in_house_use setting use time to $use_time");
526         }
527
528         my @ids;
529         for(1..$count) {
530
531                 my $ihu;
532                 my $method;
533                 my $cmeth;
534
535                 if($non_cat) {
536                         $ihu = Fieldmapper::action::non_cat_in_house_use->new;
537                         $ihu->item_type($nc_type);
538                         $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
539                         $cmeth = "create_action_non_cat_in_house_use";
540
541                 } else {
542                         $ihu = Fieldmapper::action::in_house_use->new;
543                         $ihu->item($copyid);
544                         $method = 'open-ils.storage.direct.action.in_house_use.create';
545                         $cmeth = "create_action_in_house_use";
546                 }
547
548                 $ihu->staff($e->requestor->id);
549                 $ihu->org_unit($org);
550                 $ihu->use_time($use_time);
551
552                 $ihu = $e->$cmeth($ihu) or return $e->event;
553                 push( @ids, $ihu->id );
554         }
555
556         return \@ids;
557 }
558
559
560
561
562
563 __PACKAGE__->register_method(
564         method  => "view_circs",
565         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
566         notes           => q/
567                 Retrieves the last X circs for a given copy
568                 @param authtoken The login session key
569                 @param copyid The copy to check
570                 @param count How far to go back in the item history
571                 @return An array of circ ids
572         /);
573
574
575
576 sub view_circs {
577         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
578
579         my( $requestor, $evt ) = $U->checksesperm(
580                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
581         return $evt if $evt;
582
583         return [] unless $count;
584
585         my $circs = $U->cstorereq(
586                 'open-ils.cstore.direct.action.circulation.search.atomic',
587                         { 
588                                 target_copy => $copyid, 
589                         }, 
590                         { 
591                                 limit => $count, 
592                                 order_by => { circ => "xact_start DESC" }
593                         } 
594         );
595
596         return $circs;
597 }
598
599
600 __PACKAGE__->register_method(
601         method  => "circ_count",
602         api_name        => "open-ils.circ.circulation.count",
603         notes           => q/
604                 Returns the number of times the item has circulated
605                 @param copyid The copy to check
606         /);
607
608 sub circ_count {
609         my( $self, $client, $copyid, $range ) = @_; 
610         my $e = OpenILS::Utils::Editor->new;
611         return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
612 }
613
614
615
616 __PACKAGE__->register_method(
617         method          => 'fetch_notes',
618         api_name                => 'open-ils.circ.copy_note.retrieve.all',
619         signature       => q/
620                 Returns an array of copy note objects.  
621                 @param args A named hash of parameters including:
622                         authtoken       : Required if viewing non-public notes
623                         itemid          : The id of the item whose notes we want to retrieve
624                         pub                     : True if all the caller wants are public notes
625                 @return An array of note objects
626         /);
627
628 __PACKAGE__->register_method(
629         method          => 'fetch_notes',
630         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
631         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
632
633 __PACKAGE__->register_method(
634         method          => 'fetch_notes',
635         api_name                => 'open-ils.circ.title_note.retrieve.all',
636         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
637
638
639 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
640 sub fetch_notes {
641         my( $self, $connection, $args ) = @_;
642
643         my $id = $$args{itemid};
644         my $authtoken = $$args{authtoken};
645         my( $r, $evt);
646
647         if( $self->api_name =~ /copy/ ) {
648                 if( $$args{pub} ) {
649                         return $U->cstorereq(
650                                 'open-ils.cstore.direct.asset.copy_note.search.atomic',
651                                 { owning_copy => $id, pub => 't' } );
652                 } else {
653                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
654                         return $evt if $evt;
655                         return $U->cstorereq(
656                                 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
657                 }
658
659         } elsif( $self->api_name =~ /call_number/ ) {
660                 if( $$args{pub} ) {
661                         return $U->cstorereq(
662                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
663                                 { call_number => $id, pub => 't' } );
664                 } else {
665                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
666                         return $evt if $evt;
667                         return $U->cstorereq(
668                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
669                 }
670
671         } elsif( $self->api_name =~ /title/ ) {
672                 if( $$args{pub} ) {
673                         return $U->cstorereq(
674                                 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
675                                 { record => $id, pub => 't' } );
676                 } else {
677                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
678                         return $evt if $evt;
679                         return $U->cstorereq(
680                                 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
681                 }
682         }
683
684         return undef;
685 }
686
687 __PACKAGE__->register_method(
688         method  => 'has_notes',
689         api_name        => 'open-ils.circ.copy.has_notes');
690 __PACKAGE__->register_method(
691         method  => 'has_notes',
692         api_name        => 'open-ils.circ.call_number.has_notes');
693 __PACKAGE__->register_method(
694         method  => 'has_notes',
695         api_name        => 'open-ils.circ.title.has_notes');
696
697
698 sub has_notes {
699         my( $self, $conn, $authtoken, $id ) = @_;
700         my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
701         return $editor->event unless $editor->checkauth;
702
703         my $n = $editor->search_asset_copy_note(
704                 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
705
706         $n = $editor->search_asset_call_number_note(
707                 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
708
709         $n = $editor->search_biblio_record_note(
710                 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
711
712         return scalar @$n;
713 }
714
715
716
717 __PACKAGE__->register_method(
718         method          => 'create_copy_note',
719         api_name                => 'open-ils.circ.copy_note.create',
720         signature       => q/
721                 Creates a new copy note
722                 @param authtoken The login session key
723                 @param note     The note object to create
724                 @return The id of the new note object
725         /);
726
727 sub create_copy_note {
728         my( $self, $connection, $authtoken, $note ) = @_;
729
730         my $e = new_editor(xact=>1, authtoken=>$authtoken);
731         return $e->event unless $e->checkauth;
732         my $copy = $e->retrieve_asset_copy(
733                 [
734                         $note->owning_copy,
735                         {       flesh => 1,
736                                 flesh_fields => { 'acp' => ['call_number'] }
737                         }
738                 ]
739         );
740
741         return $e->event unless 
742                 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
743
744         $note->create_date('now');
745         $note->creator($e->requestor->id);
746         $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
747         $note->clear_id;
748
749         $e->create_asset_copy_note($note) or return $e->event;
750         $e->commit;
751         return $note->id;
752 }
753
754
755 __PACKAGE__->register_method(
756         method          => 'delete_copy_note',
757         api_name                =>      'open-ils.circ.copy_note.delete',
758         signature       => q/
759                 Deletes an existing copy note
760                 @param authtoken The login session key
761                 @param noteid The id of the note to delete
762                 @return 1 on success - Event otherwise.
763                 /);
764 sub delete_copy_note {
765         my( $self, $conn, $authtoken, $noteid ) = @_;
766
767         my $e = new_editor(xact=>1, authtoken=>$authtoken);
768         return $e->die_event unless $e->checkauth;
769
770         my $note = $e->retrieve_asset_copy_note([
771                 $noteid,
772                 { flesh => 2,
773                         flesh_fields => {
774                                 'acpn' => [ 'owning_copy' ],
775                                 'acp' => [ 'call_number' ],
776                         }
777                 }
778         ]) or return $e->die_event;
779
780         if( $note->creator ne $e->requestor->id ) {
781                 return $e->die_event unless 
782                         $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
783         }
784
785         $e->delete_asset_copy_note($note) or return $e->die_event;
786         $e->commit;
787         return 1;
788 }
789
790
791 __PACKAGE__->register_method(
792         method => 'age_hold_rules',
793         api_name        =>  'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
794 );
795
796 sub age_hold_rules {
797         my( $self, $conn ) = @_;
798         return new_editor()->retrieve_all_config_rules_age_hold_protect();
799 }
800
801
802
803
804 __PACKAGE__->register_method(
805         method => 'copy_details',
806         api_name => 'open-ils.circ.copy_details.retrieve',
807         signature => q/
808         /
809 );
810
811 sub copy_details {
812         my( $self, $conn, $auth, $copy_id ) = @_;
813         my $e = new_editor(authtoken=>$auth);
814         return $e->event unless $e->checkauth;
815
816         my $copy = $e->retrieve_asset_copy($copy_id)
817                 or return $e->event;
818
819         my $hold = $e->search_action_hold_request(
820                 { 
821                         current_copy            => $copy_id, 
822                         capture_time            => { "!=" => undef },
823                         fulfillment_time        => undef,
824                         cancel_time                     => undef,
825                 }
826         )->[0];
827
828         OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
829
830         my $transit = $e->search_action_transit_copy(
831                 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
832
833         # find the latest circ, open or closed
834         my $circ = $e->search_action_circulation(
835                 [
836                         { target_copy => $copy_id },
837                         { order_by => { circ => 'xact_start desc' }, limit => 1 }
838                 ]
839         )->[0];
840
841         return {
842                 copy            => $copy,
843                 hold            => $hold,
844                 transit => $transit,
845                 circ            => $circ,
846         };
847 }
848
849
850
851
852 __PACKAGE__->register_method(
853         method => 'mark_item',
854         api_name => 'open-ils.circ.mark_item_damaged',
855 );
856 __PACKAGE__->register_method(
857         method => 'mark_item',
858         api_name => 'open-ils.circ.mark_item_missing',
859 );
860
861 sub mark_item {
862         my( $self, $conn, $auth, $copy_id ) = @_;
863         my $e = new_editor(authtoken=>$auth, xact =>1);
864         return $e->event unless $e->checkauth;
865
866         my $perm = 'MARK_ITEM_MISSING';
867         my $stat = OILS_COPY_STATUS_MISSING;
868
869         if( $self->api_name =~ /damaged/ ) {
870                 $perm = 'MARK_ITEM_DAMAGED';
871                 $stat = OILS_COPY_STATUS_DAMAGED;
872         }
873
874         my $copy = $e->retrieve_asset_copy($copy_id)
875                 or return $e->event;
876         $copy->status($stat);
877
878         $e->update_asset_copy($copy) or return $e->event;
879
880
881         my $holds = $e->search_action_hold_request(
882                 { 
883                         current_copy => $copy->id,
884                         fulfillment_time => undef,
885                         cancel_time => undef,
886                 }
887         );
888
889         $e->commit;
890
891         $logger->debug("reseting holds that target the marked copy");
892         OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
893
894         return 1;
895 }
896
897
898
899
900
901
902 # ----------------------------------------------------------------------
903 __PACKAGE__->register_method(
904         method => 'magic_fetch',
905         api_name => 'open-ils.agent.fetch'
906 );
907
908 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
909
910 sub magic_fetch {
911         my( $self, $conn, $auth, $args ) = @_;
912         my $e = new_editor( authtoken => $auth );
913         return $e->event unless $e->checkauth;
914
915         my $hint = $$args{hint};
916         my $id  = $$args{id};
917
918         # Is the call allowed to fetch this type of object?
919         return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
920
921         # Find the class the iplements the given hint
922         my ($class) = grep { 
923                 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
924
925         $class =~ s/Fieldmapper:://og;
926         $class =~ s/::/_/og;
927         my $method = "retrieve_$class";
928
929         my $obj = $e->$method($id) or return $e->event;
930         return $obj;
931 }
932 # ----------------------------------------------------------------------
933
934
935 __PACKAGE__->register_method(
936         method  => "fleshed_circ_retrieve",
937         api_name        => "open-ils.circ.fleshed.retrieve",);
938
939 sub fleshed_circ_retrieve {
940         my( $self, $client, $id ) = @_;
941         my $e = new_editor();
942         my $circ = $e->retrieve_action_circulation(
943                 [
944                         $id,
945                         { 
946                                 flesh                           => 4,
947                                 flesh_fields    => { 
948                                         circ => [ qw/ target_copy / ],
949                                         acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
950                                         ascecm => [ qw/ stat_cat stat_cat_entry / ],
951                                         acn => [ qw/ record / ],
952                                 }
953                         }
954                 ]
955         ) or return $e->event;
956         
957         my $copy = $circ->target_copy;
958         my $vol = $copy->call_number;
959         my $rec = $circ->target_copy->call_number->record;
960
961         $vol->record($rec->id);
962         $copy->call_number($vol->id);
963         $circ->target_copy($copy->id);
964
965         my $mvr;
966
967         if( $rec->id == OILS_PRECAT_RECORD ) {
968                 $rec = undef;
969                 $vol = undef;
970         } else { 
971                 $mvr = $U->record_to_mvr($rec);
972                 $rec->marc(''); # drop the bulky marc data
973         }
974
975         return {
976                 circ => $circ,
977                 copy => $copy,
978                 volume => $vol,
979                 record => $rec,
980                 mvr => $mvr,
981         };
982 }
983
984
985
986
987 1;