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