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