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