]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Circulate.pm
relying on copy fine_level and loan_duration - not retrieving from script
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Circulate.pm
1 package OpenILS::Application::Circ::Circulate;
2 use strict; use warnings;
3 use base 'OpenSRF::Application';
4 use OpenSRF::EX qw(:try);
5 use OpenSRF::Utils::SettingsClient;
6 use OpenSRF::Utils::Logger qw(:logger);
7 use OpenILS::Const qw/:const/;
8
9 my %scripts;
10 my $script_libs;
11
12 sub initialize {
13
14         my $self = shift;
15         my $conf = OpenSRF::Utils::SettingsClient->new;
16         my @pfx2 = ( "apps", "open-ils.circ","app_settings" );
17         my @pfx = ( @pfx2, "scripts" );
18
19         my $p           = $conf->config_value(  @pfx, 'circ_permit_patron' );
20         my $c           = $conf->config_value(  @pfx, 'circ_permit_copy' );
21         my $d           = $conf->config_value(  @pfx, 'circ_duration' );
22         my $f           = $conf->config_value(  @pfx, 'circ_recurring_fines' );
23         my $m           = $conf->config_value(  @pfx, 'circ_max_fines' );
24         my $pr  = $conf->config_value(  @pfx, 'circ_permit_renew' );
25         my $lb  = $conf->config_value(  @pfx2, 'script_path' );
26
27         $logger->error( "Missing circ script(s)" ) 
28                 unless( $p and $c and $d and $f and $m and $pr );
29
30         $scripts{circ_permit_patron}    = $p;
31         $scripts{circ_permit_copy}              = $c;
32         $scripts{circ_duration}                 = $d;
33         $scripts{circ_recurring_fines}= $f;
34         $scripts{circ_max_fines}                = $m;
35         $scripts{circ_permit_renew}     = $pr;
36
37         $lb = [ $lb ] unless ref($lb);
38         $script_libs = $lb;
39
40         $logger->debug(
41                 "Loaded rules scripts for circ: " .
42                 "circ permit patron = $p, ".
43                 "circ permit copy = $c, ".
44                 "circ duration = $d, ".
45                 "circ recurring fines = $f, " .
46                 "circ max fines = $m, ".
47                 "circ renew permit = $pr.  ".
48                 "lib paths = @$lb");
49 }
50
51
52 __PACKAGE__->register_method(
53         method  => "run_method",
54         api_name        => "open-ils.circ.checkout.permit",
55         notes           => q/
56                 Determines if the given checkout can occur
57                 @param authtoken The login session key
58                 @param params A trailing hash of named params including 
59                         barcode : The copy barcode, 
60                         patron : The patron the checkout is occurring for, 
61                         renew : true or false - whether or not this is a renewal
62                 @return The event that occurred during the permit check.  
63         /);
64
65
66 __PACKAGE__->register_method (
67         method          => 'run_method',
68         api_name                => 'open-ils.circ.checkout.permit.override',
69         signature       => q/@see open-ils.circ.checkout.permit/,
70 );
71
72
73 __PACKAGE__->register_method(
74         method  => "run_method",
75         api_name        => "open-ils.circ.checkout",
76         notes => q/
77                 Checks out an item
78                 @param authtoken The login session key
79                 @param params A named hash of params including:
80                         copy                    The copy object
81                         barcode         If no copy is provided, the copy is retrieved via barcode
82                         copyid          If no copy or barcode is provide, the copy id will be use
83                         patron          The patron's id
84                         noncat          True if this is a circulation for a non-cataloted item
85                         noncat_type     The non-cataloged type id
86                         noncat_circ_lib The location for the noncat circ.  
87                         precat          The item has yet to be cataloged
88                         dummy_title The temporary title of the pre-cataloded item
89                         dummy_author The temporary authr of the pre-cataloded item
90                                 Default is the home org of the staff member
91                 @return The SUCCESS event on success, any other event depending on the error
92         /);
93
94 __PACKAGE__->register_method(
95         method  => "run_method",
96         api_name        => "open-ils.circ.checkin",
97         argc            => 2,
98         signature       => q/
99                 Generic super-method for handling all copies
100                 @param authtoken The login session key
101                 @param params Hash of named parameters including:
102                         barcode - The copy barcode
103                         force           - If true, copies in bad statuses will be checked in and give good statuses
104                         ...
105         /
106 );
107
108 __PACKAGE__->register_method(
109         method  => "run_method",
110         api_name        => "open-ils.circ.checkin.override",
111         signature       => q/@see open-ils.circ.checkin/
112 );
113
114 __PACKAGE__->register_method(
115         method  => "run_method",
116         api_name        => "open-ils.circ.renew.override",
117         signature       => q/@see open-ils.circ.renew/,
118 );
119
120
121 __PACKAGE__->register_method(
122         method  => "run_method",
123         api_name        => "open-ils.circ.renew",
124         notes           => <<"  NOTES");
125         PARAMS( authtoken, circ => circ_id );
126         open-ils.circ.renew(login_session, circ_object);
127         Renews the provided circulation.  login_session is the requestor of the
128         renewal and if the logged in user is not the same as circ->usr, then
129         the logged in user must have RENEW_CIRC permissions.
130         NOTES
131
132
133 sub run_method {
134         my( $self, $conn, $auth, $args ) = @_;
135         translate_legacy_args($args);
136         my $api = $self->api_name;
137
138         my $circulator = 
139                 OpenILS::Application::Circ::Circulator->new($auth, %$args);
140
141         return circ_events($circulator) if $circulator->bail_out;
142
143         # --------------------------------------------------------------------------
144         # Go ahead and load the script runner to make sure we have all 
145         # of the objects we need
146         # --------------------------------------------------------------------------
147         $circulator->is_renewal(1) if $api =~ /renew/;
148         $circulator->mk_script_runner;
149         return circ_events($circulator) if $circulator->bail_out;
150
151         $circulator->circ_permit_patron($scripts{circ_permit_patron});
152         $circulator->circ_permit_copy($scripts{circ_permit_copy});              
153         $circulator->circ_duration($scripts{circ_duration});                     
154         $circulator->circ_permit_renew($scripts{circ_permit_renew});
155         
156         $circulator->override(1) if $api =~ /override/o;
157
158         if( $api =~ /checkout\.permit/ ) {
159                 $circulator->do_permit();
160
161         } elsif( $api =~ /checkout/ ) {
162                 $circulator->do_checkout();
163
164         } elsif( $api =~ /checkin/ ) {
165                 $circulator->do_checkin();
166
167         } elsif( $api =~ /renew/ ) {
168                 $circulator->is_renewal(1);
169                 $circulator->do_renew();
170         }
171
172         if( $circulator->bail_out ) {
173
174                 my @ee;
175                 # make sure no success event accidentally slip in
176                 $circulator->events(
177                         [ grep { $_->{textcode} ne 'SUCCESS' } @{$circulator->events} ]);
178                 my @e = @{$circulator->events};
179                 push( @ee, $_->{textcode} ) for @e;
180                 $logger->info("circulator: bailing out with events: @ee");
181                 $circulator->editor->xact_rollback;
182
183         } else {
184                 $circulator->editor->commit;
185         }
186
187         $circulator->script_runner->cleanup;
188         
189         return circ_events($circulator);
190 }
191
192 sub circ_events {
193         my $circ = shift;
194         my @e = @{$circ->events};
195         return (@e == 1) ? $e[0] : \@e;
196 }
197
198
199
200 sub translate_legacy_args {
201         my $args = shift;
202
203         if( $$args{barcode} ) {
204                 $$args{copy_barcode} = $$args{barcode};
205                 delete $$args{barcode};
206         }
207
208         if( $$args{copyid} ) {
209                 $$args{copy_id} = $$args{copyid};
210                 delete $$args{copyid};
211         }
212
213         if( $$args{patronid} ) {
214                 $$args{patron_id} = $$args{patronid};
215                 delete $$args{patronid};
216         }
217
218         if( $$args{patron} and !ref($$args{patron}) ) {
219                 $$args{patron_id} = $$args{patron};
220                 delete $$args{patron};
221         }
222
223
224         if( $$args{noncat} ) {
225                 $$args{is_noncat} = $$args{noncat};
226                 delete $$args{noncat};
227         }
228
229         if( $$args{precat} ) {
230                 $$args{is_precat} = $$args{precat};
231                 delete $$args{precat};
232         }
233 }
234
235
236
237 # --------------------------------------------------------------------------
238 # This package actually manages all of the circulation logic
239 # --------------------------------------------------------------------------
240 package OpenILS::Application::Circ::Circulator;
241 use strict; use warnings;
242 use vars q/$AUTOLOAD/;
243 use DateTime;
244 use OpenILS::Utils::Fieldmapper;
245 use OpenSRF::Utils::Cache;
246 use Digest::MD5 qw(md5_hex);
247 use DateTime::Format::ISO8601;
248 use OpenILS::Utils::PermitHold;
249 use OpenSRF::Utils qw/:datetime/;
250 use OpenSRF::Utils::SettingsClient;
251 use OpenILS::Application::Circ::Holds;
252 use OpenILS::Application::Circ::Transit;
253 use OpenSRF::Utils::Logger qw(:logger);
254 use OpenILS::Utils::CStoreEditor qw/:funcs/;
255 use OpenILS::Application::Circ::ScriptBuilder;
256 use OpenILS::Const qw/:const/;
257
258 my $U                           = "OpenILS::Application::AppUtils";
259 my $holdcode    = "OpenILS::Application::Circ::Holds";
260 my $transcode   = "OpenILS::Application::Circ::Transit";
261
262 sub DESTROY { }
263
264
265 # --------------------------------------------------------------------------
266 # Add a pile of automagic getter/setter methods
267 # --------------------------------------------------------------------------
268 my @AUTOLOAD_FIELDS = qw/
269         backdate
270         copy
271         copy_id
272         copy_barcode
273         patron
274         patron_id
275         patron_barcode
276         script_runner
277         volume
278         title
279         is_renewal
280         is_noncat
281         is_precat
282         noncat_type
283         editor
284         events
285         cache_handle
286         override
287         circ_permit_patron
288         circ_permit_copy
289         circ_duration
290         circ_recurring_fines
291         circ_max_fines
292         circ_permit_renew
293         circ
294         transit
295         hold
296         permit_key
297         noncat_circ_lib
298         noncat_count
299         checkout_time
300         dummy_title
301         dummy_author
302         circ_lib
303         barcode
304    duration_level
305    recurring_fines_level
306    duration_rule
307    recurring_fines_rule
308    max_fine_rule
309         renewal_remaining
310         due_date
311         fulfilled_holds
312         transit
313         checkin_changed
314         force
315         old_circ
316         permit_override
317 /;
318
319
320 sub AUTOLOAD {
321         my $self = shift;
322         my $type = ref($self) or die "$self is not an object";
323         my $data = shift;
324         my $name = $AUTOLOAD;
325         $name =~ s/.*://o;   
326
327         unless (grep { $_ eq $name } @AUTOLOAD_FIELDS) {
328                 $logger->error("$type: invalid autoload field: $name");
329                 die "$type: invalid autoload field: $name\n" 
330         }
331
332         {
333                 no strict 'refs';
334                 *{"${type}::${name}"} = sub {
335                         my $s = shift;
336                         my $v = shift;
337                         $s->{$name} = $v if defined $v;
338                         return $s->{$name};
339                 }
340         }
341         return $self->$name($data);
342 }
343
344
345 sub new {
346         my( $class, $auth, %args ) = @_;
347         $class = ref($class) || $class;
348         my $self = bless( {}, $class );
349
350         $self->events([]);
351         $self->editor( 
352                 new_editor(xact => 1, authtoken => $auth) );
353
354         unless( $self->editor->checkauth ) {
355                 $self->bail_on_events($self->editor->event);
356                 return $self;
357         }
358
359         $self->cache_handle(OpenSRF::Utils::Cache->new('global'));
360
361         $self->$_($args{$_}) for keys %args;
362
363         $self->circ_lib(
364                 ($self->circ_lib) ? $self->circ_lib : $self->editor->requestor->ws_ou);
365
366         return $self;
367 }
368
369
370 # --------------------------------------------------------------------------
371 # True if we should discontinue processing
372 # --------------------------------------------------------------------------
373 sub bail_out {
374         my( $self, $bool ) = @_;
375         if( defined $bool ) {
376                 $logger->info("circulator: BAILING OUT") if $bool;
377                 $self->{bail_out} = $bool;
378         }
379         return $self->{bail_out};
380 }
381
382
383 sub push_events {
384         my( $self, @evts ) = @_;
385         for my $e (@evts) {
386                 next unless $e;
387                 $logger->info("circulator: pushing event ".$e->{textcode});
388                 push( @{$self->events}, $e ) unless
389                         grep { $_->{textcode} eq $e->{textcode} } @{$self->events};
390         }
391 }
392
393 sub mk_permit_key {
394         my $self = shift;
395         my $key = md5_hex( time() . rand() . "$$" );
396         $self->cache_handle->put_cache( "oils_permit_key_$key", 1, 300 );
397         return $self->permit_key($key);
398 }
399
400 sub check_permit_key {
401         my $self = shift;
402         my $key = $self->permit_key;
403         return 0 unless $key;
404         my $k = "oils_permit_key_$key";
405         my $one = $self->cache_handle->get_cache($k);
406         $self->cache_handle->delete_cache($k);
407         return ($one) ? 1 : 0;
408 }
409
410
411 # --------------------------------------------------------------------------
412 # This builds the script runner environment and fetches most of the
413 # objects we need
414 # --------------------------------------------------------------------------
415 sub mk_script_runner {
416         my $self = shift;
417         my $args = {};
418
419         my @fields = 
420                 qw/copy copy_barcode copy_id patron 
421                         patron_id patron_barcode volume title editor/;
422
423         # Translate our objects into the ScriptBuilder args hash
424         $$args{$_} = $self->$_() for @fields;
425         $$args{fetch_patron_by_circ_copy} = 1;
426         $$args{fetch_patron_circ_info} = 1;
427
428         # This fetches most of the objects we need
429         $self->script_runner(
430                 OpenILS::Application::Circ::ScriptBuilder->build($args));
431
432         # Now we translate the ScriptBuilder objects back into self
433         $self->$_($$args{$_}) for @fields;
434
435         my @evts = @{$args->{_events}} if $args->{_events};
436
437         $logger->debug("script builder returned events: : @evts") if @evts;
438
439
440         if(@evts) {
441                 # Anything besides ASSET_COPY_NOT_FOUND will stop processing
442                 if(!$self->is_noncat and 
443                         @evts == 1 and 
444                         $evts[0]->{textcode} eq 'ASSET_COPY_NOT_FOUND') {
445                                 $self->is_precat(1);
446
447                 } else {
448                         my @e = grep { $_->{textcode} ne 'ASSET_COPY_NOT_FOUND' } @evts;
449                         return $self->bail_on_events(@e);
450                 }
451         }
452
453         $self->is_precat(1) if $self->copy and $self->copy->call_number == OILS_PRECAT_CALL_NUMBER;
454
455         # Set some circ-specific flags in the script environment
456         my $evt = "environment";
457         $self->script_runner->insert("$evt.isRenewal", ($self->is_renewal) ? 1 : undef);
458
459         if( $self->is_noncat ) {
460       $self->script_runner->insert("$evt.isNonCat", 1);
461       $self->script_runner->insert("$evt.nonCatType", $self->noncat_type);
462         }
463
464         $self->script_runner->add_path( $_ ) for @$script_libs;
465
466         return 1;
467 }
468
469
470
471
472 # --------------------------------------------------------------------------
473 # Does the circ permit work
474 # --------------------------------------------------------------------------
475 sub do_permit {
476         my $self = shift;
477
478         $self->log_me("do_permit()");
479
480         unless( $self->editor->requestor->id == $self->patron->id ) {
481                 return $self->bail_on_events($self->editor->event)
482                         unless( $self->editor->allowed('VIEW_PERMIT_CHECKOUT') );
483         }
484
485         $self->do_copy_checks();
486         return if $self->bail_out;
487         $self->run_patron_permit_scripts();
488         $self->run_copy_permit_scripts() 
489                 unless $self->is_precat or $self->is_noncat;
490         $self->override_events() unless $self->is_renewal;
491         return if $self->bail_out;
492
493         if( $self->is_precat ) {
494                 $self->push_events(
495                         OpenILS::Event->new(
496                                 'ITEM_NOT_CATALOGED', payload => $self->mk_permit_key));
497                 return $self->bail_out(1) unless $self->is_renewal;
498         }
499
500         $self->push_events(
501       OpenILS::Event->new(
502                         'SUCCESS', 
503                         payload => $self->mk_permit_key));
504 }
505
506
507 sub do_copy_checks {
508         my $self = shift;
509         my $copy = $self->copy;
510         return unless $copy;
511
512         my $stat = $U->copy_status($copy->status)->id;
513
514         # We cannot check out a copy if it is in-transit
515         if( $stat == OILS_COPY_STATUS_IN_TRANSIT ) {
516                 return $self->bail_on_events(OpenILS::Event->new('COPY_IN_TRANSIT'));
517         }
518
519         $self->handle_claims_returned();
520         return if $self->bail_out;
521
522         # no claims returned circ was found, check if there is any open circ
523         unless( $self->is_renewal ) {
524                 my $circs = $self->editor->search_action_circulation(
525                         { target_copy => $copy->id, stop_fines_time => undef }
526                 );
527
528                 return $self->bail_on_events(
529                         OpenILS::Event->new('OPEN_CIRCULATION_EXISTS')) if @$circs;
530         }
531 }
532
533
534 # ---------------------------------------------------------------------
535 # This pushes any patron-related events into the list but does not
536 # set bail_out for any events
537 # ---------------------------------------------------------------------
538 sub run_patron_permit_scripts {
539         my $self                = shift;
540         my $runner              = $self->script_runner;
541         my $patronid    = $self->patron->id;
542
543         # ---------------------------------------------------------------------
544         # Find all of the fatal penalties currently set on the user
545         # ---------------------------------------------------------------------
546         my $penalties = $U->update_patron_penalties( 
547                 authtoken => $self->editor->authtoken,
548                 patron    => $self->patron,
549         );
550
551         $penalties = $penalties->{fatal_penalties};
552
553         # ---------------------------------------------------------------------
554         # Now run the patron permit script 
555         # ---------------------------------------------------------------------
556         $runner->load($self->circ_permit_patron);
557         my $result = $runner->run or 
558                 throw OpenSRF::EX::ERROR ("Circ Permit Patron Script Died: $@");
559
560         my $patron_events = $result->{events};
561         my @allevents; 
562         push( @allevents, OpenILS::Event->new($_)) for (@$penalties, @$patron_events);
563
564         $logger->info("circulator: permit_patron script returned events: @allevents") if @allevents;
565
566         $self->push_events(@allevents);
567 }
568
569
570 sub run_copy_permit_scripts {
571         my $self = shift;
572         my $copy = $self->copy || return;
573         my $runner = $self->script_runner;
574         
575    # ---------------------------------------------------------------------
576    # Capture all of the copy permit events
577    # ---------------------------------------------------------------------
578    $runner->load($self->circ_permit_copy);
579    my $result = $runner->run or 
580                 throw OpenSRF::EX::ERROR ("Circ Permit Copy Script Died: $@");
581    my $copy_events = $result->{events};
582
583    # ---------------------------------------------------------------------
584    # Now collect all of the events together
585    # ---------------------------------------------------------------------
586         my @allevents;
587    push( @allevents, OpenILS::Event->new($_)) for @$copy_events;
588
589         # See if this copy has an alert message
590         my $ae = $self->check_copy_alert();
591         push( @allevents, $ae ) if $ae;
592
593    # uniquify the events
594    my %hash = map { ($_->{ilsevent} => $_) } @allevents;
595    @allevents = values %hash;
596
597    for (@allevents) {
598       $_->{payload} = $copy if 
599                         ($_->{textcode} eq 'COPY_NOT_AVAILABLE');
600    }
601
602         $logger->info("circulator: permit_copy script returned events: @allevents") if @allevents;
603
604         $self->push_events(@allevents);
605 }
606
607
608 sub check_copy_alert {
609         my $self = shift;
610         return OpenILS::Event->new(
611                 'COPY_ALERT_MESSAGE', payload => $self->copy->alert_message)
612                 if $self->copy and $self->copy->alert_message;
613         return undef;
614 }
615
616
617
618 # --------------------------------------------------------------------------
619 # If the call is overriding and has permissions to override every collected
620 # event, the are cleared.  Any event that the caller does not have
621 # permission to override, will be left in the event list and bail_out will
622 # be set
623 # XXX We need code in here to cancel any holds/transits on copies 
624 # that are being force-checked out
625 # --------------------------------------------------------------------------
626 sub override_events {
627         my $self = shift;
628         my @events = @{$self->events};
629         return unless @events;
630
631         if(!$self->override) {
632                 return $self->bail_out(1) 
633                         if( @events > 1 or $events[0]->{textcode} ne 'SUCCESS' );
634         }       
635
636         $self->events([]);
637         
638    for my $e (@events) {
639       my $tc = $e->{textcode};
640       next if $tc eq 'SUCCESS';
641       my $ov = "$tc.override";
642       $logger->info("circulator: attempting to override event: $ov");
643
644                 return $self->bail_on_events($self->editor->event)
645                         unless( $self->editor->allowed($ov)     );
646    }
647 }
648         
649
650 # --------------------------------------------------------------------------
651 # If there is an open claimsreturn circ on the requested copy, close the 
652 # circ if overriding, otherwise bail out
653 # --------------------------------------------------------------------------
654 sub handle_claims_returned {
655         my $self = shift;
656         my $copy = $self->copy;
657
658         my $CR = $self->editor->search_action_circulation(
659                 {       
660                         target_copy             => $copy->id,
661                         stop_fines              => OILS_STOP_FINES_CLAIMSRETURNED,
662                         checkin_time    => undef,
663                 }
664         );
665
666         return unless ($CR = $CR->[0]); 
667
668         my $evt;
669
670         # - If the caller has set the override flag, we will check the item in
671         if($self->override) {
672
673                 $CR->checkin_time('now');       
674                 $CR->checkin_lib($self->editor->requestor->ws_ou);
675                 $CR->checkin_staff($self->editor->requestor->id);
676
677                 $evt = $self->editor->event 
678                         unless $self->editor->update_action_circulation($CR);
679
680         } else {
681                 $evt = OpenILS::Event->new('CIRC_CLAIMS_RETURNED');
682         }
683
684         $self->bail_on_events($evt) if $evt;
685         return;
686 }
687
688
689 # --------------------------------------------------------------------------
690 # This performs the checkout
691 # --------------------------------------------------------------------------
692 sub do_checkout {
693         my $self = shift;
694
695         $self->log_me("do_checkout()");
696
697         # make sure perms are good if this isn't a renewal
698         unless( $self->is_renewal ) {
699                 return $self->bail_on_events($self->editor->event)
700                         unless( $self->editor->allowed('COPY_CHECKOUT') );
701         }
702
703         # verify the permit key
704         unless( $self->check_permit_key ) {
705                 if( $self->permit_override ) {
706                         return $self->bail_on_events($self->editor->event)
707                                 unless $self->editor->allowed('CIRC_PERMIT_OVERRIDE');
708                 } else {
709                         return $self->bail_on_events(OpenILS::Event->new('CIRC_PERMIT_BAD_KEY'))
710                 }       
711         }
712
713         # if this is a non-cataloged circ, build the circ and finish
714         if( $self->is_noncat ) {
715                 $self->checkout_noncat;
716                 $self->push_events(
717                         OpenILS::Event->new('SUCCESS', 
718                         payload => { noncat_circ => $self->circ }));
719                 return;
720         }
721
722         if( $self->is_precat ) {
723                 $self->script_runner->insert("environment.isPrecat", 1, 1);
724                 $self->make_precat_copy;
725                 return if $self->bail_out;
726
727         } elsif( $self->copy->call_number == OILS_PRECAT_CALL_NUMBER ) {
728                 return $self->bail_on_events(OpenILS::Event->new('ITEM_NOT_CATALOGED'));
729         }
730
731         $self->do_copy_checks;
732         return if $self->bail_out;
733
734         $self->run_checkout_scripts();
735         return if $self->bail_out;
736
737         $self->build_checkout_circ_object();
738         return if $self->bail_out;
739
740         $self->apply_modified_due_date();
741         return if $self->bail_out;
742
743         return $self->bail_on_events($self->editor->event)
744                 unless $self->editor->create_action_circulation($self->circ);
745
746         $self->copy->status(OILS_COPY_STATUS_CHECKED_OUT);
747         $self->update_copy;
748         return if $self->bail_out;
749
750         $self->handle_checkout_holds();
751         return if $self->bail_out;
752
753    # ------------------------------------------------------------------------------
754    # Update the patron penalty info in the DB
755    # ------------------------------------------------------------------------------
756    $U->update_patron_penalties(
757       authtoken => $self->editor->authtoken,
758       patron    => $self->patron,
759       background  => 1,
760    );
761
762         my $record = $U->record_to_mvr($self->title) unless $self->is_precat;
763         $self->push_events(
764                 OpenILS::Event->new('SUCCESS',
765                         payload  => {
766                                 copy              => $U->unflesh_copy($self->copy),
767                                 circ              => $self->circ,
768                                 record            => $record,
769                                 holds_fulfilled   => $self->fulfilled_holds,
770                         }
771                 )
772         );
773 }
774
775 sub update_copy {
776         my $self = shift;
777         my $copy = $self->copy;
778
779         my $stat = $copy->status if ref $copy->status;
780         my $loc = $copy->location if ref $copy->location;
781         my $circ_lib = $copy->circ_lib if ref $copy->circ_lib;
782
783         $copy->status($stat->id) if $stat;
784         $copy->location($loc->id) if $loc;
785         $copy->circ_lib($circ_lib->id) if $circ_lib;
786
787         return $self->bail_on_events($self->editor->event)
788                 unless $self->editor->update_asset_copy($self->copy);
789
790         $copy->status($U->copy_status($copy->status));
791         $copy->location($loc) if $loc;
792         $copy->circ_lib($circ_lib) if $circ_lib;
793 }
794
795
796 sub bail_on_events {
797         my( $self, @evts ) = @_;
798         $self->push_events(@evts);
799         $self->bail_out(1);
800 }
801
802 sub handle_checkout_holds {
803    my $self    = shift;
804
805    my $copy    = $self->copy;
806    my $patron  = $self->patron;
807
808         my $holds       = $self->editor->search_action_hold_request(
809                 { 
810                         current_copy            =>  $copy->id , 
811                         cancel_time                     => undef, 
812                         fulfillment_time        => undef 
813                 }
814         );
815
816    my @fulfilled;
817
818    # XXX We should only fulfill one hold here...
819    # XXX If a hold was transited to the user who is checking out
820    # the item, we need to make sure that hold is what's grabbed
821    if(@$holds) {
822
823       # for now, just sort by id to get what should be the oldest hold
824       $holds = [ sort { $a->id <=> $b->id } @$holds ];
825       my @myholds = grep { $_->usr eq $patron->id } @$holds;
826       my @altholds   = grep { $_->usr ne $patron->id } @$holds;
827
828       if(@myholds) {
829          my $hold = $myholds[0];
830
831          $logger->debug("circulator: related hold found in checkout: " . $hold->id );
832
833          # if the hold was never officially captured, capture it.
834          $hold->capture_time('now') unless $hold->capture_time;
835
836                         # just make sure it's set correctly
837          $hold->current_copy($copy->id); 
838
839          $hold->fulfillment_time('now');
840                         $hold->fulfillment_staff($self->editor->requestor->id);
841                         $hold->fulfillment_lib($self->editor->requestor->ws_ou);
842
843                         return $self->bail_on_events($self->editor->event)
844                                 unless $self->editor->update_action_hold_request($hold);
845
846          push( @fulfilled, $hold->id );
847       }
848
849       # If there are any holds placed for other users that point to this copy,
850       # then we need to un-target those holds so the targeter can pick a new copy
851       for(@altholds) {
852
853          $logger->info("circulator: un-targeting hold ".$_->id.
854             " because copy ".$copy->id." is getting checked out");
855
856                         # - make the targeter process this hold at next run
857          $_->clear_prev_check_time; 
858
859                         # - clear out the targetted copy
860          $_->clear_current_copy;
861
862                         return $self->bail_on_event($self->editor->event)
863                                 unless $self->editor->update_action_hold_request($_);
864       }
865    }
866
867         $self->fulfilled_holds(\@fulfilled);
868 }
869
870
871
872 sub run_checkout_scripts {
873         my $self = shift;
874
875         my $evt;
876    my $runner = $self->script_runner;
877    $runner->load($self->circ_duration);
878
879    my $result = $runner->run or 
880                 throw OpenSRF::EX::ERROR ("Circ Duration Script Died: $@");
881
882    my $duration   = $result->{durationRule};
883    #my $dur_level  = $result->{durationLevel};
884    my $recurring  = $result->{recurringFinesRule};
885    my $max_fine   = $result->{maxFine};
886    #my $rec_fines_level = $result->{recurringFinesLevel};
887
888    ($duration, $evt) = $U->fetch_circ_duration_by_name($duration);
889         return $self->bail_on_events($evt) if $evt;
890    ($recurring, $evt) = $U->fetch_recurring_fine_by_name($recurring);
891         return $self->bail_on_events($evt) if $evt;
892    ($max_fine, $evt) = $U->fetch_max_fine_by_name($max_fine);
893         return $self->bail_on_events($evt) if $evt;
894
895    #$self->duration_level($dur_level);
896    #$self->recurring_fines_level($rec_fines_level);
897    $self->duration_rule($duration);
898    $self->recurring_fines_rule($recurring);
899    $self->max_fine_rule($max_fine);
900 }
901
902
903 sub build_checkout_circ_object {
904         my $self = shift;
905
906    my $circ       = Fieldmapper::action::circulation->new;
907    my $duration   = $self->duration_rule;
908    my $max        = $self->max_fine_rule;
909    my $recurring  = $self->recurring_fines_rule;
910    my $copy       = $self->copy;
911    my $patron     = $self->patron;
912    #my $dur_level  = $self->duration_level;
913    #my $rec_level  = $self->recurring_fines_level;
914
915         my $dname = $duration->name;
916         my $mname = $max->name;
917         my $rname = $recurring->name;
918
919         $logger->debug("circulator: building circulation ".
920                 "with duration=$dname, maxfine=$mname, recurring=$rname");
921
922         #$circ->duration( $duration->shrt ) if ($dur_level == OILS_CIRC_DURATION_SHORT);
923    #$circ->duration( $duration->normal ) if ($dur_level == OILS_CIRC_DURATION_NORMAL);
924    #$circ->duration( $duration->extended ) if ($dur_level == OILS_CIRC_DURATION_EXTENDED);
925
926         $circ->duration( $duration->shrt ) 
927                 if $copy->loan_duration == OILS_CIRC_DURATION_SHORT;
928    $circ->duration( $duration->normal ) 
929                 if $copy->loan_duration == OILS_CIRC_DURATION_NORMAL;
930    $circ->duration( $duration->extended ) 
931                 if $copy->loan_duration ==OILS_CIRC_DURATION_EXTENDED;
932
933    $circ->recuring_fine( $recurring->low ) 
934                 if $copy->fine_level == OILS_REC_FINE_LEVEL_LOW;
935    $circ->recuring_fine( $recurring->normal ) 
936                 if $copy->fine_level == OILS_REC_FINE_LEVEL_NORMAL;
937    $circ->recuring_fine( $recurring->high ) 
938                 if $copy->fine_level == OILS_REC_FINE_LEVEL_HIGH;
939
940    $circ->duration_rule( $duration->name );
941    $circ->recuring_fine_rule( $recurring->name );
942    $circ->max_fine_rule( $max->name );
943    $circ->max_fine( $max->amount );
944
945    $circ->fine_interval($recurring->recurance_interval);
946    $circ->renewal_remaining( $duration->max_renewals );
947    $circ->target_copy( $copy->id );
948    $circ->usr( $patron->id );
949    $circ->circ_lib( $self->circ_lib );
950
951    if( $self->is_renewal ) {
952       $circ->opac_renewal(1);
953       $circ->renewal_remaining($self->renewal_remaining);
954       $circ->circ_staff($self->editor->requestor->id);
955    }
956
957
958    # if the user provided an overiding checkout time,
959    # (e.g. the checkout really happened several hours ago), then
960    # we apply that here.  Does this need a perm??
961         $circ->xact_start(clense_ISO8601($self->checkout_time))
962                 if $self->checkout_time;
963
964    # if a patron is renewing, 'requestor' will be the patron
965    $circ->circ_staff($self->editor->requestor->id);
966         $circ->due_date( $self->create_due_date($circ->duration) );
967
968         $self->circ($circ);
969 }
970
971
972 sub apply_modified_due_date {
973         my $self = shift;
974         my $circ = $self->circ;
975         my $copy = $self->copy;
976
977    if( $self->due_date ) {
978
979                 return $self->bail_on_events($self->editor->event)
980                         unless $self->editor->allowed('CIRC_OVERRIDE_DUE_DATE', $self->circ_lib);
981
982       $circ->due_date(clense_ISO8601($self->due_date));
983
984    } else {
985
986       # if the due_date lands on a day when the location is closed
987       return unless $copy;
988
989                 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
990
991       $logger->info("circ searching for closed date overlap on lib $org".
992                         " with an item due date of ".$circ->due_date );
993
994       my $dateinfo = $U->storagereq(
995          'open-ils.storage.actor.org_unit.closed_date.overlap', 
996                         $org, $circ->due_date );
997
998       if($dateinfo) {
999          $logger->info("$dateinfo : circ due data / close date overlap found : due_date=".
1000             $circ->due_date." start=". $dateinfo->{start}.", end=".$dateinfo->{end});
1001
1002             # XXX make the behavior more dynamic
1003             # for now, we just push the due date to after the close date
1004             $circ->due_date($dateinfo->{end});
1005       }
1006    }
1007 }
1008
1009
1010
1011 sub create_due_date {
1012         my( $self, $duration ) = @_;
1013    my ($sec,$min,$hour,$mday,$mon,$year) =
1014       gmtime(OpenSRF::Utils->interval_to_seconds($duration) + int(time()));
1015    $year += 1900; $mon += 1;
1016    my $due_date = sprintf(
1017       '%s-%0.2d-%0.2dT%s:%0.2d:%0.2d-00',
1018       $year, $mon, $mday, $hour, $min, $sec);
1019    return $due_date;
1020 }
1021
1022
1023
1024 sub make_precat_copy {
1025         my $self = shift;
1026         my $copy = $self->copy;
1027
1028    if($copy) {
1029       $logger->debug("Pre-cat copy already exists in checkout: ID=" . $copy->id);
1030
1031       $copy->editor($self->editor->requestor->id);
1032       $copy->edit_date('now');
1033       $copy->dummy_title($self->dummy_title);
1034       $copy->dummy_author($self->dummy_author);
1035
1036                 $self->update_copy();
1037                 return;
1038    }
1039
1040    $logger->info("circulator: Creating a new precataloged ".
1041                 "copy in checkout with barcode " . $self->copy_barcode);
1042
1043    $copy = Fieldmapper::asset::copy->new;
1044    $copy->circ_lib($self->circ_lib);
1045    $copy->creator($self->editor->requestor->id);
1046    $copy->editor($self->editor->requestor->id);
1047    $copy->barcode($self->copy_barcode);
1048    $copy->call_number(OILS_PRECAT_CALL_NUMBER); 
1049    $copy->loan_duration(OILS_PRECAT_COPY_LOAN_DURATION);
1050    $copy->fine_level(OILS_PRECAT_COPY_FINE_LEVEL);
1051
1052    $copy->dummy_title($self->dummy_title || "");
1053    $copy->dummy_author($self->dummy_author || "");
1054
1055         unless( $self->copy($self->editor->create_asset_copy($copy)) ) {
1056                 $self->bail_out(1);
1057                 $self->push_events($self->editor->event);
1058                 return;
1059         }       
1060
1061         # this is a little bit of a hack, but we need to 
1062         # get the copy into the script runner
1063         $self->script_runner->insert("environment.copy", $copy, 1);
1064 }
1065
1066
1067 sub checkout_noncat {
1068         my $self = shift;
1069
1070         my $circ;
1071         my $evt;
1072
1073    my $lib              = $self->noncat_circ_lib || $self->editor->requestor->ws_ou;
1074    my $count    = $self->noncat_count || 1;
1075    my $cotime   = clense_ISO8601($self->checkout_time) || "";
1076
1077    $logger->info("circ creating $count noncat circs with checkout time $cotime");
1078
1079    for(1..$count) {
1080
1081       ( $circ, $evt ) = OpenILS::Application::Circ::NonCat::create_non_cat_circ(
1082          $self->editor->requestor->id, 
1083                         $self->patron->id, 
1084                         $lib, 
1085                         $self->noncat_type, 
1086                         $cotime,
1087                         $self->editor );
1088
1089                 if( $evt ) {
1090                         $self->push_events($evt);
1091                         $self->bail_out(1);
1092                         return; 
1093                 }
1094                 $self->circ($circ);
1095    }
1096 }
1097
1098
1099 sub do_checkin {
1100         my $self = shift;
1101         $self->log_me("do_checkin()");
1102
1103         return $self->bail_on_events(
1104                 OpenILS::Event->new('ASSET_COPY_NOT_FOUND')) 
1105                 unless $self->copy;
1106
1107         unless( $self->is_renewal ) {
1108                 return $self->bail_on_events($self->editor->event)
1109                         unless $self->editor->allowed('COPY_CHECKIN');
1110         }
1111
1112         $self->push_events($self->check_copy_alert());
1113         $self->push_events($self->check_checkin_copy_status());
1114
1115
1116         # the renew code will have already found our circulation object
1117         unless( $self->is_renewal and $self->circ ) {
1118
1119                 # first lets see if we have a good old fashioned open circulation
1120                 my $circ = $self->editor->search_action_circulation(
1121                         { target_copy => $self->copy->id, stop_fines => undef } )->[0];
1122
1123                 if(!$circ) {
1124                         # if not, lets look for other circs we can check in
1125                         $circ = $self->editor->search_action_circulation(
1126                                 { 
1127                                         target_copy => $self->copy->id, 
1128                                         xact_finish => undef,
1129                                         stop_fines      => [ 
1130                                                 OILS_STOP_FINES_CLAIMSRETURNED, 
1131                                                 OILS_STOP_FINES_LOST, 
1132                                                 OILS_STOP_FINES_LONGOVERDUE, 
1133                                         ]
1134                                 } )->[0];
1135                 }
1136
1137                 $self->circ($circ);
1138         }
1139
1140
1141         # if the circ is marked as 'claims returned', add the event to the list
1142         $self->push_events(OpenILS::Event->new('CIRC_CLAIMS_RETURNED'))
1143                 if ($self->circ and $self->circ->stop_fines 
1144                                 and $self->circ->stop_fines eq OILS_STOP_FINES_CLAIMSRETURNED);
1145
1146         # handle the overridable events 
1147         $self->override_events unless $self->is_renewal;
1148         return if $self->bail_out;
1149         
1150         if( $self->copy ) {
1151                 $self->transit(
1152                         $self->editor->search_action_transit_copy(
1153                         { target_copy => $self->copy->id, dest_recv_time => undef })->[0]);     
1154         }
1155
1156         if( $self->circ ) {
1157                 $self->checkin_handle_circ;
1158                 return if $self->bail_out;
1159                 $self->checkin_changed(1);
1160
1161         } elsif( $self->transit ) {
1162                 my $hold_transit = $self->process_received_transit;
1163                 $self->checkin_changed(1);
1164
1165                 if( $self->bail_out ) { 
1166                         $self->checkin_flesh_events;
1167                         return;
1168                 }
1169                 
1170                 if( my $e = $self->check_checkin_copy_status() ) {
1171                         # If the original copy status is special, alert the caller
1172                         return $self->bail_on_events($e);       
1173                 }
1174
1175                 if( $hold_transit ) {
1176                         $self->checkin_flesh_events;
1177                         return;
1178                 } 
1179         }
1180
1181         if( $self->is_renewal ) {
1182                 $self->push_events(OpenILS::Event->new('SUCCESS'));
1183                 return;
1184         }
1185
1186    # ------------------------------------------------------------------------------
1187    # Circulations and transits are now closed where necessary.  Now go on to see if
1188    # this copy can fulfill a hold or needs to be routed to a different location
1189    # ------------------------------------------------------------------------------
1190
1191         if( $self->attempt_checkin_hold_capture() ) {
1192                 return if $self->bail_out;
1193
1194    } else { # not needed for a hold
1195
1196                 my $circ_lib = (ref $self->copy->circ_lib) ? 
1197                                 $self->copy->circ_lib->id : $self->copy->circ_lib;
1198
1199                 $logger->debug("circulator: circlib=$circ_lib, workstation=".$self->editor->requestor->ws_ou);
1200
1201       if( $circ_lib == $self->editor->requestor->ws_ou ) {
1202
1203                         $self->checkin_handle_precat();
1204                         return if $self->bail_out;
1205
1206       } else {
1207
1208                         $self->checkin_build_copy_transit();
1209                         return if $self->bail_out;
1210                         $self->push_events(OpenILS::Event->new('ROUTE_ITEM', org => $circ_lib));
1211       }
1212    }
1213
1214
1215         $self->reshelve_copy;
1216         return if $self->bail_out;
1217
1218         unless($self->checkin_changed) {
1219
1220                 $self->push_events(OpenILS::Event->new('NO_CHANGE'));
1221                 my $stat = $U->copy_status($self->copy->status)->id;
1222
1223         $self->hold($U->fetch_open_hold_by_copy($self->copy->id))
1224          if( $stat == OILS_COPY_STATUS_ON_HOLDS_SHELF );
1225                 $self->bail_out(1); # no need to commit anything
1226
1227         } else {
1228                 $self->push_events(OpenILS::Event->new('SUCCESS')) 
1229                         unless @{$self->events};
1230         }
1231
1232         $self->checkin_flesh_events;
1233         return;
1234 }
1235
1236 sub reshelve_copy {
1237    my $self    = shift;
1238    my $copy    = $self->copy;
1239    my $force   = $self->force;
1240
1241    my $stat = $U->copy_status($copy->status)->id;
1242
1243    if($force || (
1244       $stat != OILS_COPY_STATUS_ON_HOLDS_SHELF and
1245       $stat != OILS_COPY_STATUS_AVAILABLE and
1246       $stat != OILS_COPY_STATUS_CATALOGING and
1247       $stat != OILS_COPY_STATUS_IN_TRANSIT and
1248       $stat != OILS_COPY_STATUS_RESHELVING  )) {
1249
1250         $copy->status( OILS_COPY_STATUS_RESHELVING );
1251                         $self->update_copy;
1252                         $self->checkin_changed(1);
1253         }
1254 }
1255
1256
1257 sub checkin_handle_precat {
1258         my $self        = shift;
1259    my $copy    = $self->copy;
1260
1261    if( $self->is_precat and ($copy->status != OILS_COPY_STATUS_CATALOGING) ) {
1262       $copy->status(OILS_COPY_STATUS_CATALOGING);
1263                 $self->update_copy();
1264                 $self->checkin_changed(1);
1265                 $self->push_events(OpenILS::Event->new('ITEM_NOT_CATALOGED'));
1266    }
1267 }
1268
1269
1270 sub checkin_build_copy_transit {
1271         my $self                        = shift;
1272         my $copy       = $self->copy;
1273    my $transit    = Fieldmapper::action::transit_copy->new;
1274
1275    $transit->source($self->editor->requestor->ws_ou);
1276    $transit->dest( (ref($copy->circ_lib)) ? $copy->circ_lib->id : $copy->circ_lib );
1277    $transit->target_copy($copy->id);
1278    $transit->source_send_time('now');
1279    $transit->copy_status( $U->copy_status($copy->status)->id );
1280
1281         return $self->bail_on_events($self->editor->event)
1282                 unless $self->editor->create_action_transit_copy($transit);
1283
1284    $copy->status(OILS_COPY_STATUS_IN_TRANSIT);
1285         $self->update_copy;
1286         $self->checkin_changed(1);
1287 }
1288
1289
1290 sub attempt_checkin_hold_capture {
1291         my $self = shift;
1292         my $copy = $self->copy;
1293
1294         # See if this copy can fulfill any holds
1295         my ($hold) = $holdcode->find_nearest_permitted_hold(
1296                 OpenSRF::AppSession->create('open-ils.storage'), 
1297                 $copy, $self->editor->requestor );
1298
1299         if(!$hold) {
1300                 $logger->debug("circulator: no potential permitted".
1301                         "holds found for copy ".$copy->barcode);
1302                 return undef;
1303         }
1304
1305         $logger->info("circulator: found permitted hold ".
1306                 $hold->id . " for copy, capturing...");
1307
1308         $hold->current_copy($copy->id);
1309         $hold->capture_time('now');
1310
1311         # prevent some DB errors
1312         $hold->clear_fulfillment_time;
1313         $hold->clear_fulfillment_staff;
1314         $hold->clear_fulfillment_lib;
1315         $hold->clear_expire_time; 
1316
1317         $self->bail_on_events($self->editor->event)
1318                 unless $self->editor->update_action_hold_request($hold);
1319         $self->hold($hold);
1320         $self->checkin_changed(1);
1321
1322         return 1 if $self->bail_out;
1323
1324         if( $hold->pickup_lib == $self->editor->requestor->ws_ou ) {
1325
1326                 # This hold was captured in the correct location
1327         $copy->status(OILS_COPY_STATUS_ON_HOLDS_SHELF);
1328                 $self->push_events(OpenILS::Event->new('SUCCESS'));
1329         
1330         } else {
1331         
1332                 # Hold needs to be picked up elsewhere.  Build a hold
1333                 # transit and route the item.
1334                 $self->checkin_build_hold_transit();
1335         $copy->status(OILS_COPY_STATUS_IN_TRANSIT);
1336                 return 1 if $self->bail_out;
1337                 $self->push_events(
1338                         OpenILS::Event->new('ROUTE_ITEM', org => $hold->pickup_lib));
1339         }
1340
1341         # make sure we save the copy status
1342         $self->update_copy;
1343         return 1;
1344 }
1345
1346
1347 sub checkin_build_hold_transit {
1348         my $self = shift;
1349
1350    my $copy = $self->copy;
1351    my $hold = $self->hold;
1352    my $trans = Fieldmapper::action::hold_transit_copy->new;
1353
1354    $trans->hold($hold->id);
1355    $trans->source($self->editor->requestor->ws_ou);
1356    $trans->dest($hold->pickup_lib);
1357    $trans->source_send_time("now");
1358    $trans->target_copy($copy->id);
1359
1360         # when the copy gets to its destination, it will recover
1361         # this status - put it onto the holds shelf
1362    $trans->copy_status(OILS_COPY_STATUS_IN_TRANSIT);
1363
1364         return $self->bail_on_events($self->editor->event)
1365                 unless $self->editor->create_action_hold_transit_copy($trans);
1366 }
1367
1368
1369
1370 sub process_received_transit {
1371         my $self = shift;
1372         my $copy = $self->copy;
1373    my $copyid = $self->copy->id;
1374
1375         my $status_name = $U->copy_status($copy->status)->name;
1376    $logger->debug("circulator: attempting transit receive on ".
1377                 "copy $copyid. Copy status is $status_name");
1378
1379         my $transit = $self->transit;
1380
1381    if( $transit->dest != $self->editor->requestor->ws_ou ) {
1382       $logger->activity("Fowarding transit on copy which is destined ".
1383          "for a different location. copy=$copyid,current ".
1384          "location=".$self->editor->requestor->ws_ou.",destination location=".$transit->dest);
1385
1386                 $self->bail_on_events(
1387                         OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest ));
1388    }
1389
1390    # The transit is received, set the receive time
1391    $transit->dest_recv_time('now');
1392         $self->bail_on_events($self->editor->event)
1393                 unless $self->editor->update_action_transit_copy($transit);
1394
1395         my $hold_transit = $self->editor->search_action_hold_transit_copy(
1396                 { hold => $transit->id }
1397         );
1398
1399    $logger->info("Recovering original copy status in transit: ".$transit->copy_status);
1400    $copy->status( $transit->copy_status );
1401         $self->update_copy();
1402         return if $self->bail_out;
1403
1404         my $ishold = ($hold_transit) ? 1 : 0;
1405
1406         $self->push_events( 
1407                 OpenILS::Event->new(
1408                 'SUCCESS', 
1409                 ishold => $ishold,
1410       payload => { transit => $transit, holdtransit => $hold_transit } ));
1411
1412         return $hold_transit;
1413 }
1414
1415
1416 sub checkin_handle_circ {
1417    my $self = shift;
1418         $U->logmark;
1419
1420    my $circ = $self->circ;
1421    my $copy = $self->copy;
1422    my $evt;
1423    my $obt;
1424
1425    # backdate the circ if necessary
1426    if($self->backdate) {
1427                 $self->checkin_handle_backdate;
1428                 return if $self->bail_out;
1429    }
1430
1431    if(!$circ->stop_fines) {
1432       $circ->stop_fines(OILS_STOP_FINES_CHECKIN);
1433       $circ->stop_fines(OILS_STOP_FINES_RENEW) if $self->is_renewal;
1434       $circ->stop_fines_time('now');
1435    }
1436
1437    # see if there are any fines owed on this circ.  if not, close it
1438         $obt = $self->editor->retrieve_money_open_billable_transaction_summary($circ->id);
1439    $circ->xact_finish('now') if( $obt->balance_owed == 0 );
1440
1441    # Set the checkin vars since we have the item
1442    $circ->checkin_time('now');
1443    $circ->checkin_staff($self->editor->requestor->id);
1444    $circ->checkin_lib($self->editor->requestor->ws_ou);
1445
1446         $self->copy->status($U->copy_status(OILS_COPY_STATUS_RESHELVING));
1447         $self->update_copy;
1448
1449         return $self->bail_on_events($self->editor->event)
1450                 unless $self->editor->update_action_circulation($circ);
1451 }
1452
1453
1454 sub checkin_handle_backdate {
1455         my $self = shift;
1456
1457         my $bills = $self->editor->search_money_billing(
1458                 { billing_ts => { ">=" => $self->backdate }, "xact" => $self->circ->id }
1459         );
1460
1461         for my $bill (@$bills) {        
1462                 if( !$bill->voided or $bill->voided =~ /f/i ) {
1463                         $bill->voided('t');
1464                         my $n = $bill->note || "";
1465                         $bill->note("$n\nSystem: VOIDED FOR BACKDATE");
1466
1467                         $self->bail_on_events($self->editor->event)
1468                                 unless $self->editor->update_money_billing($bill);
1469                 }
1470         }
1471 }
1472
1473
1474
1475 # XXX Legacy version for Circ.pm support
1476 sub _checkin_handle_backdate {
1477    my( $backdate, $circ, $requestor, $session, $closecirc ) = @_;
1478
1479    my $bills = $session->request(
1480       "open-ils.storage.direct.money.billing.search_where.atomic",
1481       billing_ts => { ">=" => $backdate }, "xact" => $circ->id )->gather(1);
1482
1483    if($bills) {
1484       for my $bill (@$bills) {
1485          $bill->voided('t');
1486          my $n = $bill->note || "";
1487          $bill->note($n . "\nSystem: VOIDED FOR BACKDATE");
1488          my $s = $session->request(
1489             "open-ils.storage.direct.money.billing.update", $bill)->gather(1);
1490          return $U->DB_UPDATE_FAILED($bill) unless $s;
1491       }
1492    }
1493 }
1494
1495
1496
1497
1498
1499
1500 sub find_patron_from_copy {
1501         my $self = shift;
1502         my $circs = $self->editor->search_action_circulation(
1503                 { target_copy => $self->copy->id, stop_fines_time => undef });
1504         my $circ = $circs->[0];
1505         return unless $circ;
1506         my $u = $self->editor->retrieve_actor_user($circ->usr)
1507                 or return $self->bail_on_events($self->editor->event);
1508         $self->patron($u);
1509 }
1510
1511 sub check_checkin_copy_status {
1512         my $self = shift;
1513    my $copy = $self->copy;
1514
1515    my $islost     = 0;
1516    my $ismissing  = 0;
1517    my $evt        = undef;
1518
1519    my $status = $U->copy_status($copy->status)->id;
1520
1521    return undef
1522       if(   $status == OILS_COPY_STATUS_AVAILABLE   ||
1523             $status == OILS_COPY_STATUS_CHECKED_OUT ||
1524             $status == OILS_COPY_STATUS_IN_PROCESS  ||
1525             $status == OILS_COPY_STATUS_IN_TRANSIT  ||
1526             $status == OILS_COPY_STATUS_RESHELVING );
1527
1528    return OpenILS::Event->new('COPY_STATUS_LOST', payload => $copy )
1529       if( $status == OILS_COPY_STATUS_LOST );
1530
1531    return OpenILS::Event->new('COPY_STATUS_MISSING', payload => $copy )
1532       if( $status == OILS_COPY_STATUS_MISSING );
1533
1534    return OpenILS::Event->new('COPY_BAD_STATUS', payload => $copy );
1535 }
1536
1537
1538
1539 # --------------------------------------------------------------------------
1540 # On checkin, we need to return as many relevant objects as we can
1541 # --------------------------------------------------------------------------
1542 sub checkin_flesh_events {
1543         my $self = shift;
1544
1545         for my $evt (@{$self->events}) {
1546
1547                 my $payload          = {};
1548                 $payload->{copy}     = $U->unflesh_copy($self->copy);
1549                 $payload->{record}   = $U->record_to_mvr($self->title) if($self->title and !$self->is_precat);
1550                 $payload->{circ}     = $self->circ;
1551                 $payload->{transit}  = $self->transit;
1552                 $payload->{hold}     = $self->hold;
1553                 
1554                 $evt->{payload} = $payload;
1555         }
1556 }
1557
1558 sub log_me {
1559         my( $self, $msg ) = @_;
1560         my $bc = ($self->copy) ? $self->copy->barcode :
1561                 $self->barcode;
1562         $bc ||= "";
1563         my $usr = ($self->patron) ? $self->patron->id : "";
1564         $logger->info("circulator: $msg requestor=".$self->editor->requestor->id.
1565                 ", recipient=$usr, copy=$bc");
1566 }
1567
1568
1569 sub do_renew {
1570         my $self = shift;
1571         $self->log_me("do_renew()");
1572         $self->is_renewal(1);
1573
1574         unless( $self->is_renewal ) {
1575                 return $self->bail_on_events($self->editor->events)
1576                         unless $self->editor->allowed('RENEW_CIRC');
1577         }       
1578
1579         # Make sure there is an open circ to renew that is not
1580         # marked as LOST, CLAIMSRETURNED, or LONGOVERDUE
1581         my $circ = $self->editor->search_action_circulation(
1582                         { target_copy => $self->copy->id, stop_fines => undef } )->[0];
1583
1584         return $self->bail_on_events($self->editor->event) unless $circ;
1585
1586         $self->push_events(OpenILS::Event->new('MAX_RENEWALS_REACHED'))
1587                 if $circ->renewal_remaining < 1;
1588
1589         # -----------------------------------------------------------------
1590
1591         $self->renewal_remaining( $circ->renewal_remaining - 1 );
1592         $self->renewal_remaining(0) if $self->renewal_remaining < 0;
1593         $self->circ($circ);
1594
1595         $self->run_renew_permit;
1596
1597         # Check the item in
1598         $self->do_checkin();
1599         return if $self->bail_out;
1600
1601         unless( $self->permit_override ) {
1602                 $self->do_permit();
1603                 return if $self->bail_out;
1604                 $self->is_precat(1) if $self->have_event('ITEM_NOT_CATALOGED');
1605                 $self->remove_event('ITEM_NOT_CATALOGED');
1606         }       
1607
1608         $self->override_events;
1609         return if $self->bail_out;
1610
1611         $self->events([]);
1612         $self->do_checkout();
1613 }
1614
1615
1616 sub remove_event {
1617         my( $self, $evt ) = @_;
1618         $evt = (ref $evt) ? $evt->{textcode} : $evt;
1619         $logger->debug("circulator: removing event from list: $evt");
1620         my @events = @{$self->events};
1621         $self->events( [ grep { $_->{textcode} ne $evt } @events ] );
1622 }
1623
1624
1625 sub have_event {
1626         my( $self, $evt ) = @_;
1627         $evt = (ref $evt) ? $evt->{textcode} : $evt;
1628         return grep { $_->{textcode} eq $evt } @{$self->events};
1629 }
1630
1631
1632
1633 sub run_renew_permit {
1634         my $self = shift;
1635    my $runner = $self->script_runner;
1636
1637    $runner->load($self->circ_permit_renew);
1638    my $result = $runner->run or 
1639                 throw OpenSRF::EX::ERROR ("Circ Permit Renew Script Died: $@");
1640    my $events = $result->{events};
1641
1642    $logger->activity("circ_permit_renew for user ".
1643       $self->patron->id." returned events: @$events") if @$events;
1644
1645         $self->push_events(OpenILS::Event->new($_)) for @$events;
1646 }
1647
1648
1649
1650