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