]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
disallow marking an item lost that is already marked lost
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ.pm
1 package OpenILS::Application::Circ;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
4 use strict; use warnings;
5
6 use OpenILS::Application::Circ::Circulate;
7 use OpenILS::Application::Circ::Survey;
8 use OpenILS::Application::Circ::StatCat;
9 use OpenILS::Application::Circ::Holds;
10 use OpenILS::Application::Circ::HoldNotify;
11 use OpenILS::Application::Circ::Money;
12 use OpenILS::Application::Circ::NonCat;
13 use OpenILS::Application::Circ::CopyLocations;
14
15 use DateTime;
16 use DateTime::Format::ISO8601;
17
18 use OpenILS::Application::AppUtils;
19
20 use OpenSRF::Utils qw/:datetime/;
21 use OpenILS::Utils::ModsParser;
22 use OpenILS::Event;
23 use OpenSRF::EX qw(:try);
24 use OpenSRF::Utils::Logger qw(:logger);
25 use OpenILS::Utils::Fieldmapper;
26 use OpenILS::Utils::Editor;
27 use OpenILS::Utils::CStoreEditor q/:funcs/;
28 use OpenILS::Const qw/:const/;
29 use OpenSRF::Utils::SettingsClient;
30
31 my $apputils = "OpenILS::Application::AppUtils";
32 my $U = $apputils;
33
34
35 # ------------------------------------------------------------------------
36 # Top level Circ package;
37 # ------------------------------------------------------------------------
38
39 sub initialize {
40         my $self = shift;
41         OpenILS::Application::Circ::Circulate->initialize();
42 }
43
44
45 __PACKAGE__->register_method(
46         method => 'retrieve_circ',
47         api_name        => 'open-ils.circ.retrieve',
48         signature => q/
49                 Retrieve a circ object by id
50                 @param authtoken Login session key
51                 @pararm circid The id of the circ object
52         /
53 );
54 sub retrieve_circ {
55         my( $s, $c, $a, $i ) = @_;
56         my $e = new_editor(authtoken => $a);
57         return $e->event unless $e->checkauth;
58         my $circ = $e->retrieve_action_circulation($i) or return $e->event;
59         if( $e->requestor->id ne $circ->usr ) {
60                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS');
61         }
62         return $circ;
63 }
64
65
66 __PACKAGE__->register_method(
67         method => 'fetch_circ_mods',
68         api_name => 'open-ils.circ.circ_modifier.retrieve.all');
69 sub fetch_circ_mods {
70         my $conf = OpenSRF::Utils::SettingsClient->new;
71         return $conf->config_value(
72                 'apps', 'open-ils.circ', 'app_settings', 'circ_modifiers', 'mod' );
73 }
74
75 __PACKAGE__->register_method(
76         method => 'fetch_bill_types',
77         api_name => 'open-ils.circ.billing_type.retrieve.all');
78 sub fetch_bill_types {
79         my $conf = OpenSRF::Utils::SettingsClient->new;
80         return $conf->config_value(
81                 'apps', 'open-ils.circ', 'app_settings', 'billing_types', 'type' );
82 }
83
84
85 # ------------------------------------------------------------------------
86 # Returns an array of {circ, record} hashes checked out by the user.
87 # ------------------------------------------------------------------------
88 __PACKAGE__->register_method(
89         method  => "checkouts_by_user",
90         api_name        => "open-ils.circ.actor.user.checked_out",
91         NOTES           => <<"  NOTES");
92         Returns a list of open circulations as a pile of objects.  each object
93         contains the relevant copy, circ, and record
94         NOTES
95
96 sub checkouts_by_user {
97         my( $self, $client, $user_session, $user_id ) = @_;
98
99         my( $requestor, $target, $copy, $record, $evt );
100
101         ( $requestor, $target, $evt ) = 
102                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
103         return $evt if $evt;
104
105         my $circs = $apputils->simplereq(
106                 'open-ils.cstore',
107                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
108                 { usr => $target->id, checkin_time => undef } );
109 #               { usr => $target->id } );
110
111         my @results;
112         for my $circ (@$circs) {
113
114                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
115                 return $evt if $evt;
116
117                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
118
119                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
120                 return $evt if $evt;
121
122                 my $mods = $apputils->record_to_mvr($record);
123
124                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
125         }
126
127         return \@results;
128
129 }
130
131
132
133 __PACKAGE__->register_method(
134         method  => "checkouts_by_user_slim",
135         api_name        => "open-ils.circ.actor.user.checked_out.slim",
136         NOTES           => <<"  NOTES");
137         Returns a list of open circulation objects
138         NOTES
139
140 # DEPRECAT ME?? XXX
141 sub checkouts_by_user_slim {
142         my( $self, $client, $user_session, $user_id ) = @_;
143
144         my( $requestor, $target, $copy, $record, $evt );
145
146         ( $requestor, $target, $evt ) = 
147                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
148         return $evt if $evt;
149
150         $logger->debug( 'User ' . $requestor->id . 
151                 " retrieving checked out items for user " . $target->id );
152
153         # XXX Make the call correct..
154         return $apputils->simplereq(
155                 'open-ils.cstore',
156                 "open-ils.cstore.direct.action.open_circulation.search.atomic", 
157                 { usr => $target->id, checkin_time => undef } );
158 #               { usr => $target->id } );
159 }
160
161
162 __PACKAGE__->register_method(
163         method  => "checkouts_by_user_opac",
164         api_name        => "open-ils.circ.actor.user.checked_out.opac",);
165
166 # XXX Deprecate Me
167 sub checkouts_by_user_opac {
168         my( $self, $client, $auth, $user_id ) = @_;
169
170         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
171         return $e->event unless $e->checkauth;
172         $user_id ||= $e->requestor->id;
173         return $e->event unless 
174                 my $patron = $e->retrieve_actor_user($user_id);
175
176         my $data;
177         my $search = {usr => $user_id, stop_fines => undef};
178
179         if( $user_id ne $e->requestor->id ) {
180                 $data = $e->search_action_circulation(
181                         $search, {checkperm=>1, permorg=>$patron->home_ou})
182                         or return $e->event;
183
184         } else {
185                 $data = $e->search_action_circulation($search);
186         }
187
188         return $data;
189 }
190
191
192 __PACKAGE__->register_method(
193         method  => "title_from_transaction",
194         api_name        => "open-ils.circ.circ_transaction.find_title",
195         NOTES           => <<"  NOTES");
196         Returns a mods object for the title that is linked to from the 
197         copy from the hold that created the given transaction
198         NOTES
199
200 sub title_from_transaction {
201         my( $self, $client, $login_session, $transactionid ) = @_;
202
203         my( $user, $circ, $title, $evt );
204
205         ( $user, $evt ) = $apputils->checkses( $login_session );
206         return $evt if $evt;
207
208         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
209         return $evt if $evt;
210         
211         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
212         return $evt if $evt;
213
214         return $apputils->record_to_mvr($title);
215 }
216
217
218
219 __PACKAGE__->register_method(
220         method  => "new_set_circ_lost",
221         api_name        => "open-ils.circ.circulation.set_lost",
222         signature       => q/
223         Sets the copy and related open circulation to lost
224                 @param auth
225                 @param args : barcode
226         /
227 );
228
229
230 # ---------------------------------------------------------------------
231 # Sets a circulation to lost.  updates copy status to lost
232 # applies copy and/or prcoessing fees depending on org settings
233 # ---------------------------------------------------------------------
234 sub new_set_circ_lost {
235     my( $self, $conn, $auth, $args ) = @_;
236
237     my $e = new_editor(authtoken=>$auth, xact=>1);
238     return $e->die_event unless $e->checkauth;
239
240     my $barcode = $$args{barcode};
241     $logger->info("marking item lost $barcode");
242
243     # ---------------------------------------------------------------------
244     # gather the pieces
245     my $copy = $e->search_asset_copy([
246         {barcode=>$barcode, deleted=>'f'},
247         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])->[0] 
248             or return $e->die_event;
249
250     my $owning_lib = 
251         ($copy->call_number == OILS_PRECAT_CALL_NUMBER) ? 
252             $copy->circ_lib : $copy->call_number->owning_lib;
253
254     my $circ = $e->search_action_circulation(
255         {checkin_time => undef, target_copy => $copy->id} )->[0]
256             or return $e->die_event;
257
258     $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
259
260     return OpenILS::Event->new('COPY_MARKED_LOST')
261             if $copy->status == OILS_COPY_STATUS_LOST;
262
263     # ---------------------------------------------------------------------
264     # fetch the related org settings
265     my $default_price = $U->ou_ancestor_setting_value(
266         $owning_lib, OILS_SETTING_DEF_ITEM_PRICE, $e) || 0;
267     my $proc_fee = $U->ou_ancestor_setting_value(
268         $owning_lib, OILS_SETTING_LOST_PROCESSING_FEE, $e) || 0;
269     my $charge_on_0 = $U->ou_ancestor_setting_value(
270         $owning_lib, OILS_SETTING_CHARGE_LOST_ON_ZERO, $e) || 0;
271     my $void_overdue = $U->ou_ancestor_setting_value(
272         $owning_lib, OILS_SETTING_VOID_OVERDUE_ON_LOST, $e) || 0;
273
274     $logger->info("org settings: default price = $default_price, ".
275         "processing fee = $proc_fee, charge on 0 = $charge_on_0, void overdues = $void_overdue");
276
277     # ---------------------------------------------------------------------
278     # move the copy into LOST status
279     $copy->status(OILS_COPY_STATUS_LOST);
280     $copy->editor($e->requestor->id);
281     $copy->edit_date('now');
282     $e->update_asset_copy($copy) or return $e->die_event;
283
284     # ---------------------------------------------------------------------
285     # determine the appropriate item price to charge and create the billing
286     my $price = $copy->price;
287     $price = $default_price unless defined $price;
288     $price = 0 if $price < 0;
289     $price = $default_price if $price == 0 and $charge_on_0;
290
291     if( $price > 0 ) {
292         my $evt = create_bill($e, $price, 'Lost Materials', $circ->id);
293         return $evt if $evt;
294     }
295
296     # ---------------------------------------------------------------------
297     # if there is a processing fee, charge that too
298     if( $proc_fee > 0 ) {
299         my $evt = create_bill($e, $proc_fee, 'Lost Materials Processing Fee', $circ->id);
300         return $evt if $evt;
301     }
302
303     # ---------------------------------------------------------------------
304     # mark the circ as lost and stop the fines
305     $circ->stop_fines(OILS_STOP_FINES_LOST);
306     $circ->stop_fines_time('now') unless $circ->stop_fines_time;
307     $e->update_action_circulation($circ) or return $e->die_event;
308
309     # ---------------------------------------------------------------------
310     # void all overdue fines on this circ if configured
311     if( $void_overdue ) {
312         my $evt = void_overdues($e, $circ);
313         return $evt if $evt;
314     }
315
316     my $evt = reopen_xact($e, $circ->id);
317     return $evt if $evt;
318
319     $e->commit;
320     return 1;
321 }
322
323 sub reopen_xact {
324     my($e, $xactid) = @_;
325
326     # -----------------------------------------------------------------
327     # make sure the transaction is not closed
328     my $xact = $e->retrieve_money_billable_transaction($xactid)
329         or return $e->die_event;
330
331     if( $xact->xact_finish ) {
332         my ($mbts) = $U->fetch_mbts($xactid, $e);
333         if( $mbts->balance_owed != 0 ) {
334             $logger->info("* re-opening xact $xactid, orig xact_finish is ".$xact->xact_finish);
335             $xact->clear_xact_finish;
336             $e->update_money_billable_transaction($xact)
337                 or return $e->die_event;
338         } 
339     }
340 }
341
342
343 sub create_bill {
344         my( $e, $amount, $type, $xactid ) = @_;
345
346         $logger->info("The system is charging $amount [$type] on xact $xactid");
347
348     # -----------------------------------------------------------------
349     # now create the billing
350         my $bill = Fieldmapper::money::billing->new;
351         $bill->xact($xactid);
352         $bill->amount($amount);
353         $bill->billing_type($type); 
354         $bill->note('SYSTEM GENERATED');
355     $e->create_money_billing($bill) or return $e->die_event;
356
357         return undef;
358 }
359
360
361
362 # -----------------------------------------------------------------
363 # Voids overdue fines on the given circ.  if a backdate is 
364 # provided, then we only void back to the backdate
365 # -----------------------------------------------------------------
366 sub void_overdues {
367     my( $e, $circ, $backdate ) = @_;
368
369     my $bill_search = { 
370         xact => $circ->id, 
371         billing_type => OILS_BILLING_TYPE_OVERDUE_MATERIALS 
372     };
373
374     if( $backdate ) {
375         # ------------------------------------------------------------------
376         # Fines for overdue materials are assessed up to, but not including,
377         # one fine interval after the fines are applicable.  Here, we add
378         # one fine interval to the backdate to ensure that we are not 
379         # voiding fines that were applicable before the backdate.
380         # ------------------------------------------------------------------
381         my $interval = OpenSRF::Utils->interval_to_seconds($circ->fine_interval);
382         my $date = DateTime::Format::ISO8601->parse_datetime($backdate);
383         $backdate = $U->epoch2ISO8601($date->epoch + $interval);
384         $logger->info("applying backdate $backdate in overdue voiding");
385         $$bill_search{billing_ts} = {'>=' => $backdate};
386     }
387
388     my $bills = $e->search_money_billing($bill_search);
389     
390     for my $bill (@$bills) {
391         next if $U->is_true($bill->voided);
392         $logger->info("voiding overdue bill ".$bill->id);
393         $bill->voided('t');
394         $bill->void_time('now');
395         $bill->voider($e->requestor->id);
396         my $n = $bill->note || "";
397         $bill->note("$n\nSystem: VOIDED FOR BACKDATE");
398         $e->update_money_billing($bill) or return $e->die_event;
399     }
400
401         return undef;
402 }
403
404
405
406
407 __PACKAGE__->register_method(
408         method  => "set_circ_claims_returned",
409         api_name        => "open-ils.circ.circulation.set_claims_returned",
410         signature       => q/
411         Sets the circ for the given item as claims returned
412         If a backdate is provided, overdue fines will be voided
413         back to the backdate
414                 @param auth
415                 @param args : barcode, backdate
416         /
417 );
418
419 sub set_circ_claims_returned {
420     my( $self, $conn, $auth, $args ) = @_;
421
422     my $e = new_editor(authtoken=>$auth, xact=>1);
423     return $e->die_event unless $e->checkauth;
424
425     my $barcode = $$args{barcode};
426     my $backdate = $$args{backdate};
427
428     $logger->info("marking circ for item $barcode as claims returned".
429         (($backdate) ? " with backdate $backdate" : ''));
430
431     my $copy = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'})->[0] 
432         or return $e->die_event;
433
434     my $circ = $e->search_action_circulation(
435         {checkin_time => undef, target_copy => $copy->id})->[0]
436             or return $e->die_event;
437
438     $e->allowed('SET_CIRC_CLAIMS_RETURNED', $circ->circ_lib) 
439         or return $e->die_event;
440
441     $circ->stop_fines(OILS_STOP_FINES_CLAIMSRETURNED);
442         $circ->stop_fines_time('now') unless $circ->stop_fines_time;
443
444     if( $backdate ) {
445         # make it look like the circ stopped at the cliams returned time
446         $circ->stop_fines_time(clense_ISO8601($backdate));
447         my $evt = void_overdues($e, $circ, $backdate);
448         return $evt if $evt;
449     }
450
451     $e->update_action_circulation($circ) or return $e->die_event;
452     $e->commit;
453     return 1;
454 }
455
456
457
458
459
460 __PACKAGE__->register_method (
461         method          => 'set_circ_due_date',
462         api_name                => 'open-ils.circ.circulation.due_date.update',
463         signature       => q/
464                 Updates the due_date on the given circ
465                 @param authtoken
466                 @param circid The id of the circ to update
467                 @param date The timestamp of the new due date
468         /
469 );
470
471 sub set_circ_due_date {
472         my( $self, $conn, $auth, $circ_id, $date ) = @_;
473
474     my $e = new_editor(xact=>1, authtoken=>$auth);
475     return $e->die_event unless $e->checkauth;
476     my $circ = $e->retrieve_action_circulation($circ_id)
477         or return $e->die_event;
478
479     return $e->die_event unless $e->allowed('CIRC_OVERRIDE_DUE_DATE', $circ->circ_lib);
480         $date = clense_ISO8601($date);
481         $circ->due_date($date);
482     $e->update_action_circulation($circ) or return $e->die_event;
483     $e->commit;
484
485     return $circ->id;
486 }
487
488
489 __PACKAGE__->register_method(
490         method          => "create_in_house_use",
491         api_name                => 'open-ils.circ.in_house_use.create',
492         signature       =>      q/
493                 Creates an in-house use action.
494                 @param $authtoken The login session key
495                 @param params A hash of params including
496                         'location' The org unit id where the in-house use occurs
497                         'copyid' The copy in question
498                         'count' The number of in-house uses to apply to this copy
499                 @return An array of id's representing the id's of the newly created
500                 in-house use objects or an event on an error
501         /);
502
503 __PACKAGE__->register_method(
504         method          => "create_in_house_use",
505         api_name                => 'open-ils.circ.non_cat_in_house_use.create',
506 );
507
508 =head OLD CODE
509 sub ___create_in_house_use {
510         my( $self, $client, $authtoken, $params ) = @_;
511
512         my( $staff, $evt, $copy );
513         my $org                 = $params->{location};
514         my $copyid              = $params->{copyid};
515         my $count               = $params->{count} || 1;
516         my $nc_type             = $params->{non_cat_type};
517         my $use_time    = $params->{use_time} || 'now';
518
519         my $non_cat = 1 if $self->api_name =~ /non_cat/;
520
521         unless( $non_cat ) {
522                 unless( $copyid ) {
523                         my $barcode = $params->{barcode};
524                         ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
525                         return $evt if $evt;
526                         $copyid = $copy->id;
527                 }
528                 ($copy, $evt) = $U->fetch_copy($copyid) unless $copy;
529                 return $evt if $evt;
530         }
531
532         ($staff, $evt) = $U->checkses($authtoken);
533         return $evt if $evt;
534
535         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
536         return $evt if $evt;
537
538         if( $use_time ne 'now' ) {
539                 $use_time = clense_ISO8601($use_time);
540                 $logger->debug("in_house_use setting use time to $use_time");
541         }
542
543         my @ids;
544         for(1..$count) {
545
546                 my $ihu;
547                 my $method;
548
549                 if($non_cat) {
550                         $ihu = Fieldmapper::action::non_cat_in_house_use->new;
551                         $ihu->noncat_type($nc_type);
552                         $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
553                 } else {
554                         $ihu = Fieldmapper::action::in_house_use->new;
555                         $ihu->item($copyid);
556                         $method = 'open-ils.storage.direct.action.in_house_use.create';
557                 }
558
559                 $ihu->staff($staff->id);
560                 $ihu->org_unit($org);
561                 $ihu->use_time($use_time);
562
563                 my $id = $U->simplereq('open-ils.storage', $method, $ihu );
564
565                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
566                 push @ids, $id;
567         }
568
569         return \@ids;
570 }
571 =cut
572
573
574
575
576 sub create_in_house_use {
577         my( $self, $client, $auth, $params ) = @_;
578
579         my( $evt, $copy );
580         my $org                 = $params->{location};
581         my $copyid              = $params->{copyid};
582         my $count               = $params->{count} || 1;
583         my $nc_type             = $params->{non_cat_type};
584         my $use_time    = $params->{use_time} || 'now';
585
586         my $e = new_editor(xact=>1,authtoken=>$auth);
587         return $e->event unless $e->checkauth;
588         return $e->event unless $e->allowed('CREATE_IN_HOUSE_USE');
589
590         my $non_cat = 1 if $self->api_name =~ /non_cat/;
591
592         unless( $non_cat ) {
593                 if( $copyid ) {
594                         $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
595                 } else {
596                         $copy = $e->search_asset_copy({barcode=>$params->{barcode}, deleted => 'f'})->[0]
597                                 or return $e->event;
598                         $copyid = $copy->id;
599                 }
600         }
601
602         if( $use_time ne 'now' ) {
603                 $use_time = clense_ISO8601($use_time);
604                 $logger->debug("in_house_use setting use time to $use_time");
605         }
606
607         my @ids;
608         for(1..$count) {
609
610                 my $ihu;
611                 my $method;
612                 my $cmeth;
613
614                 if($non_cat) {
615                         $ihu = Fieldmapper::action::non_cat_in_house_use->new;
616                         $ihu->item_type($nc_type);
617                         $method = 'open-ils.storage.direct.action.non_cat_in_house_use.create';
618                         $cmeth = "create_action_non_cat_in_house_use";
619
620                 } else {
621                         $ihu = Fieldmapper::action::in_house_use->new;
622                         $ihu->item($copyid);
623                         $method = 'open-ils.storage.direct.action.in_house_use.create';
624                         $cmeth = "create_action_in_house_use";
625                 }
626
627                 $ihu->staff($e->requestor->id);
628                 $ihu->org_unit($org);
629                 $ihu->use_time($use_time);
630
631                 $ihu = $e->$cmeth($ihu) or return $e->event;
632                 push( @ids, $ihu->id );
633         }
634
635         $e->commit;
636         return \@ids;
637 }
638
639
640
641
642
643 __PACKAGE__->register_method(
644         method  => "view_circs",
645         api_name        => "open-ils.circ.copy_checkout_history.retrieve",
646         notes           => q/
647                 Retrieves the last X circs for a given copy
648                 @param authtoken The login session key
649                 @param copyid The copy to check
650                 @param count How far to go back in the item history
651                 @return An array of circ ids
652         /);
653
654
655
656 sub view_circs {
657         my( $self, $client, $authtoken, $copyid, $count ) = @_; 
658
659         my( $requestor, $evt ) = $U->checksesperm(
660                         $authtoken, 'VIEW_COPY_CHECKOUT_HISTORY' );
661         return $evt if $evt;
662
663         return [] unless $count;
664
665         my $circs = $U->cstorereq(
666                 'open-ils.cstore.direct.action.circulation.search.atomic',
667                         { 
668                                 target_copy => $copyid, 
669                         }, 
670                         { 
671                                 limit => $count, 
672                                 order_by => { circ => "xact_start DESC" }
673                         } 
674         );
675
676         return $circs;
677 }
678
679
680 __PACKAGE__->register_method(
681         method  => "circ_count",
682         api_name        => "open-ils.circ.circulation.count",
683         notes           => q/
684                 Returns the number of times the item has circulated
685                 @param copyid The copy to check
686         /);
687
688 sub circ_count {
689         my( $self, $client, $copyid, $range ) = @_; 
690         my $e = OpenILS::Utils::Editor->new;
691         return $e->request('open-ils.storage.asset.copy.circ_count', $copyid, $range);
692 }
693
694
695
696 __PACKAGE__->register_method(
697         method          => 'fetch_notes',
698         api_name                => 'open-ils.circ.copy_note.retrieve.all',
699         signature       => q/
700                 Returns an array of copy note objects.  
701                 @param args A named hash of parameters including:
702                         authtoken       : Required if viewing non-public notes
703                         itemid          : The id of the item whose notes we want to retrieve
704                         pub                     : True if all the caller wants are public notes
705                 @return An array of note objects
706         /);
707
708 __PACKAGE__->register_method(
709         method          => 'fetch_notes',
710         api_name                => 'open-ils.circ.call_number_note.retrieve.all',
711         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
712
713 __PACKAGE__->register_method(
714         method          => 'fetch_notes',
715         api_name                => 'open-ils.circ.title_note.retrieve.all',
716         signature       => q/@see open-ils.circ.copy_note.retrieve.all/);
717
718
719 # NOTE: VIEW_COPY/VOLUME/TITLE_NOTES perms should always be global
720 sub fetch_notes {
721         my( $self, $connection, $args ) = @_;
722
723         my $id = $$args{itemid};
724         my $authtoken = $$args{authtoken};
725         my( $r, $evt);
726
727         if( $self->api_name =~ /copy/ ) {
728                 if( $$args{pub} ) {
729                         return $U->cstorereq(
730                                 'open-ils.cstore.direct.asset.copy_note.search.atomic',
731                                 { owning_copy => $id, pub => 't' } );
732                 } else {
733                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
734                         return $evt if $evt;
735                         return $U->cstorereq(
736                                 'open-ils.cstore.direct.asset.copy_note.search.atomic', {owning_copy => $id} );
737                 }
738
739         } elsif( $self->api_name =~ /call_number/ ) {
740                 if( $$args{pub} ) {
741                         return $U->cstorereq(
742                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic',
743                                 { call_number => $id, pub => 't' } );
744                 } else {
745                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_VOLUME_NOTES');
746                         return $evt if $evt;
747                         return $U->cstorereq(
748                                 'open-ils.cstore.direct.asset.call_number_note.search.atomic', { call_number => $id } );
749                 }
750
751         } elsif( $self->api_name =~ /title/ ) {
752                 if( $$args{pub} ) {
753                         return $U->cstorereq(
754                                 'open-ils.cstore.direct.bilbio.record_note.search.atomic',
755                                 { record => $id, pub => 't' } );
756                 } else {
757                         ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_TITLE_NOTES');
758                         return $evt if $evt;
759                         return $U->cstorereq(
760                                 'open-ils.cstore.direct.biblio.record_note.search.atomic', { record => $id } );
761                 }
762         }
763
764         return undef;
765 }
766
767 __PACKAGE__->register_method(
768         method  => 'has_notes',
769         api_name        => 'open-ils.circ.copy.has_notes');
770 __PACKAGE__->register_method(
771         method  => 'has_notes',
772         api_name        => 'open-ils.circ.call_number.has_notes');
773 __PACKAGE__->register_method(
774         method  => 'has_notes',
775         api_name        => 'open-ils.circ.title.has_notes');
776
777
778 sub has_notes {
779         my( $self, $conn, $authtoken, $id ) = @_;
780         my $editor = OpenILS::Utils::Editor->new(authtoken => $authtoken);
781         return $editor->event unless $editor->checkauth;
782
783         my $n = $editor->search_asset_copy_note(
784                 {owning_copy=>$id}, {idlist=>1}) if $self->api_name =~ /copy/;
785
786         $n = $editor->search_asset_call_number_note(
787                 {call_number=>$id}, {idlist=>1}) if $self->api_name =~ /call_number/;
788
789         $n = $editor->search_biblio_record_note(
790                 {record=>$id}, {idlist=>1}) if $self->api_name =~ /title/;
791
792         return scalar @$n;
793 }
794
795
796
797 __PACKAGE__->register_method(
798         method          => 'create_copy_note',
799         api_name                => 'open-ils.circ.copy_note.create',
800         signature       => q/
801                 Creates a new copy note
802                 @param authtoken The login session key
803                 @param note     The note object to create
804                 @return The id of the new note object
805         /);
806
807 sub create_copy_note {
808         my( $self, $connection, $authtoken, $note ) = @_;
809
810         my $e = new_editor(xact=>1, authtoken=>$authtoken);
811         return $e->event unless $e->checkauth;
812         my $copy = $e->retrieve_asset_copy(
813                 [
814                         $note->owning_copy,
815                         {       flesh => 1,
816                                 flesh_fields => { 'acp' => ['call_number'] }
817                         }
818                 ]
819         );
820
821         return $e->event unless 
822                 $e->allowed('CREATE_COPY_NOTE', $copy->call_number->owning_lib);
823
824         $note->create_date('now');
825         $note->creator($e->requestor->id);
826         $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
827         $note->clear_id;
828
829         $e->create_asset_copy_note($note) or return $e->event;
830         $e->commit;
831         return $note->id;
832 }
833
834
835 __PACKAGE__->register_method(
836         method          => 'delete_copy_note',
837         api_name                =>      'open-ils.circ.copy_note.delete',
838         signature       => q/
839                 Deletes an existing copy note
840                 @param authtoken The login session key
841                 @param noteid The id of the note to delete
842                 @return 1 on success - Event otherwise.
843                 /);
844 sub delete_copy_note {
845         my( $self, $conn, $authtoken, $noteid ) = @_;
846
847         my $e = new_editor(xact=>1, authtoken=>$authtoken);
848         return $e->die_event unless $e->checkauth;
849
850         my $note = $e->retrieve_asset_copy_note([
851                 $noteid,
852                 { flesh => 2,
853                         flesh_fields => {
854                                 'acpn' => [ 'owning_copy' ],
855                                 'acp' => [ 'call_number' ],
856                         }
857                 }
858         ]) or return $e->die_event;
859
860         if( $note->creator ne $e->requestor->id ) {
861                 return $e->die_event unless 
862                         $e->allowed('DELETE_COPY_NOTE', $note->owning_copy->call_number->owning_lib);
863         }
864
865         $e->delete_asset_copy_note($note) or return $e->die_event;
866         $e->commit;
867         return 1;
868 }
869
870
871 __PACKAGE__->register_method(
872         method => 'age_hold_rules',
873         api_name        =>  'open-ils.circ.config.rules.age_hold_protect.retrieve.all',
874 );
875
876 sub age_hold_rules {
877         my( $self, $conn ) = @_;
878         return new_editor()->retrieve_all_config_rules_age_hold_protect();
879 }
880
881
882
883 __PACKAGE__->register_method(
884         method => 'copy_details_barcode',
885         api_name => 'open-ils.circ.copy_details.retrieve.barcode');
886 sub copy_details_barcode {
887         my( $self, $conn, $auth, $barcode ) = @_;
888     my $e = new_editor();
889     my $cid = $e->search_asset_copy({barcode=>$barcode, deleted=>'f'}, {idlist=>1})->[0];
890     return $e->event unless $cid;
891         return $self->copy_details( $conn, $auth, $cid );
892 }
893
894
895 __PACKAGE__->register_method(
896         method => 'copy_details',
897         api_name => 'open-ils.circ.copy_details.retrieve');
898
899 sub copy_details {
900         my( $self, $conn, $auth, $copy_id ) = @_;
901         my $e = new_editor(authtoken=>$auth);
902         return $e->event unless $e->checkauth;
903
904         my $flesh = { flesh => 1 };
905
906         my $copy = $e->retrieve_asset_copy(
907                 [
908                         $copy_id,
909                         {
910                                 flesh => 2,
911                                 flesh_fields => {
912                                         acp => ['call_number'],
913                                         acn => ['record']
914                                 }
915                         }
916                 ]) or return $e->event;
917
918
919         # De-flesh the copy for backwards compatibility
920         my $mvr;
921         my $vol = $copy->call_number;
922         if( ref $vol ) {
923                 $copy->call_number($vol->id);
924                 my $record = $vol->record;
925                 if( ref $record ) {
926                         $vol->record($record->id);
927                         $mvr = $U->record_to_mvr($record);
928                 }
929         }
930
931
932         my $hold = $e->search_action_hold_request(
933                 { 
934                         current_copy            => $copy_id, 
935                         capture_time            => { "!=" => undef },
936                         fulfillment_time        => undef,
937                         cancel_time                     => undef,
938                 }
939         )->[0];
940
941         OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
942
943         my $transit = $e->search_action_transit_copy(
944                 { target_copy => $copy_id, dest_recv_time => undef } )->[0];
945
946         # find the latest circ, open or closed
947         my $circ = $e->search_action_circulation(
948                 [
949                         { target_copy => $copy_id },
950                         { order_by => { circ => 'xact_start desc' }, limit => 1 }
951                 ]
952         )->[0];
953
954
955         return {
956                 copy            => $copy,
957                 hold            => $hold,
958                 transit => $transit,
959                 circ            => $circ,
960                 volume  => $vol,
961                 mvr             => $mvr,
962         };
963 }
964
965
966
967
968 __PACKAGE__->register_method(
969         method => 'mark_item',
970         api_name => 'open-ils.circ.mark_item_damaged',
971 );
972 __PACKAGE__->register_method(
973         method => 'mark_item',
974         api_name => 'open-ils.circ.mark_item_missing',
975 );
976
977 sub mark_item {
978         my( $self, $conn, $auth, $copy_id ) = @_;
979         my $e = new_editor(authtoken=>$auth, xact =>1);
980         return $e->event unless $e->checkauth;
981
982         my $perm = 'MARK_ITEM_MISSING';
983         my $stat = OILS_COPY_STATUS_MISSING;
984
985         if( $self->api_name =~ /damaged/ ) {
986                 $perm = 'MARK_ITEM_DAMAGED';
987                 $stat = OILS_COPY_STATUS_DAMAGED;
988         }
989
990         my $copy = $e->retrieve_asset_copy($copy_id)
991                 or return $e->event;
992         $copy->status($stat);
993         $copy->edit_date('now');
994         $copy->editor($e->requestor->id);
995
996         $e->update_asset_copy($copy) or return $e->event;
997
998
999         my $holds = $e->search_action_hold_request(
1000                 { 
1001                         current_copy => $copy->id,
1002                         fulfillment_time => undef,
1003                         cancel_time => undef,
1004                 }
1005         );
1006
1007         $e->commit;
1008
1009         $logger->debug("reseting holds that target the marked copy");
1010         OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
1011
1012         return 1;
1013 }
1014
1015
1016
1017
1018
1019
1020 # ----------------------------------------------------------------------
1021 __PACKAGE__->register_method(
1022         method => 'magic_fetch',
1023         api_name => 'open-ils.agent.fetch'
1024 );
1025
1026 my @FETCH_ALLOWED = qw/ aou aout acp acn bre /;
1027
1028 sub magic_fetch {
1029         my( $self, $conn, $auth, $args ) = @_;
1030         my $e = new_editor( authtoken => $auth );
1031         return $e->event unless $e->checkauth;
1032
1033         my $hint = $$args{hint};
1034         my $id  = $$args{id};
1035
1036         # Is the call allowed to fetch this type of object?
1037         return undef unless grep { $_ eq $hint } @FETCH_ALLOWED;
1038
1039         # Find the class the iplements the given hint
1040         my ($class) = grep { 
1041                 $Fieldmapper::fieldmap->{$_}{hint} eq $hint } Fieldmapper->classes;
1042
1043         $class =~ s/Fieldmapper:://og;
1044         $class =~ s/::/_/og;
1045         my $method = "retrieve_$class";
1046
1047         my $obj = $e->$method($id) or return $e->event;
1048         return $obj;
1049 }
1050 # ----------------------------------------------------------------------
1051
1052
1053 __PACKAGE__->register_method(
1054         method  => "fleshed_circ_retrieve",
1055         api_name        => "open-ils.circ.fleshed.retrieve",);
1056
1057 sub fleshed_circ_retrieve {
1058         my( $self, $client, $id ) = @_;
1059         my $e = new_editor();
1060         my $circ = $e->retrieve_action_circulation(
1061                 [
1062                         $id,
1063                         { 
1064                                 flesh                           => 4,
1065                                 flesh_fields    => { 
1066                                         circ => [ qw/ target_copy / ],
1067                                         acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect call_number / ],
1068                                         ascecm => [ qw/ stat_cat stat_cat_entry / ],
1069                                         acn => [ qw/ record / ],
1070                                 }
1071                         }
1072                 ]
1073         ) or return $e->event;
1074         
1075         my $copy = $circ->target_copy;
1076         my $vol = $copy->call_number;
1077         my $rec = $circ->target_copy->call_number->record;
1078
1079         $vol->record($rec->id);
1080         $copy->call_number($vol->id);
1081         $circ->target_copy($copy->id);
1082
1083         my $mvr;
1084
1085         if( $rec->id == OILS_PRECAT_RECORD ) {
1086                 $rec = undef;
1087                 $vol = undef;
1088         } else { 
1089                 $mvr = $U->record_to_mvr($rec);
1090                 $rec->marc(''); # drop the bulky marc data
1091         }
1092
1093         return {
1094                 circ => $circ,
1095                 copy => $copy,
1096                 volume => $vol,
1097                 record => $rec,
1098                 mvr => $mvr,
1099         };
1100 }
1101
1102
1103
1104
1105 1;