Fine generator caches org settings per run
authorBill Erickson <berickxx@gmail.com>
Wed, 5 Jul 2017 16:21:51 +0000 (12:21 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 19 Jul 2017 15:02:27 +0000 (11:02 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm

index 1675712..3d23154 100644 (file)
@@ -219,13 +219,17 @@ sub create_bill {
 }
 
 sub extend_grace_period {
-    my($class, $circ_lib, $due_date, $grace_period, $e, $h) = @_;
+    my($class, $circ_lib, $due_date, $grace_period, $e, $h, $ous_cache) = @_;
+    $ous_cache ||= {};
+
     if ($grace_period >= 86400) { # Only extend grace periods greater than or equal to a full day
         my $parser = DateTime::Format::ISO8601->new;
         my $due_dt = $parser->parse_datetime( cleanse_ISO8601( $due_date ) );
         my $due = $due_dt->epoch;
 
-        my $grace_extend = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend');
+        my $grace_extend = $class->get_cached_ou_setting(
+            $e, $ous_cache, $circ_lib, 'circ.grace.extend');
+
         $e = new_editor() if (!$e);
         $h = $e->retrieve_actor_org_unit_hours_of_operation($circ_lib) if (!$h);
         if ($grace_extend and $h) { 
@@ -251,10 +255,14 @@ sub extend_grace_period {
             } else {
                 # Extra nice grace periods
                 # AKA, merge closed dates trailing the grace period into the grace period
-                my $grace_extend_into_closed = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend.into_closed');
+
+                my $grace_extend_into_closed = $class->get_cached_ou_setting(
+                    $e, $ous_cache, $circ_lib, 'circ.grace.extend.into_closed');
+
                 $due += 86400 if $grace_extend_into_closed;
 
-                my $grace_extend_all = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend.all');
+                my $grace_extend_all = $class->get_cached_ou_setting(
+                    $e, $ous_cache, $circ_lib, 'circ.grace.extend.all');
 
                 if ( $grace_extend_all ) {
                     # Start checking the day after the item was due
@@ -425,6 +433,17 @@ sub seconds_to_interval_hash {
         return %output;
 }
 
+# return org setting value, using the value found in $cache if available.
+sub get_cached_ou_setting {
+    my ($class, $e, $cache, $org_id, $setting) = @_;
+    $cache ||= {};
+    $cache->{$org_id} = {} unless $cache->{$org_id};
+    $cache->{$org_id}->{$setting} = 
+        $U->ou_ancestor_setting_value($org_id, $setting)
+        unless exists $cache->{$org_id}->{$setting};
+    return $cache->{$org_id}->{$setting};
+}
+
 sub generate_fines {
     my ($class, $args) = @_;
     my $circs = $args->{circs};
@@ -444,6 +463,9 @@ sub generate_fines {
 
     my %hoo = map { ( $_->id => $_ ) } @{ $e->retrieve_all_actor_org_unit_hours_of_operation };
 
+    # cache org unit setting values per fine generator instance.
+    my $ous_cache = {};
+
     my $handling_resvs = 0;
     for my $c (@$circs) {
 
@@ -546,7 +568,10 @@ sub generate_fines {
                 $logger->info( "Potential first billing for circ ".$c->id );
                 $last_fine = $due;
 
-                $grace_period = extend_grace_period($class, $c->$circ_lib_method,$c->$due_date_method,$grace_period,undef,$hoo{$c->$circ_lib_method});
+                $grace_period = extend_grace_period(
+                    $class, $c->$circ_lib_method, $c->$due_date_method, 
+                    $grace_period, $e, $hoo{$c->$circ_lib_method}, $ous_cache
+                );
             }
 
             return if ($last_fine > $now);
@@ -568,13 +593,13 @@ sub generate_fines {
             my $recurring_fine = int($c->$recurring_fine_method * 100);
             my $max_fine = int($c->max_fine * 100);
 
-            my $skip_closed_check = $U->ou_ancestor_setting_value(
-                $c->$circ_lib_method, 'circ.fines.charge_when_closed');
-            $skip_closed_check = $U->is_true($skip_closed_check);
+            my $skip_closed_check = $U->is_true(
+                $class->get_cached_ou_setting($e, $ous_cache, 
+                    $c->$circ_lib_method, 'circ.fines.charge_when_closed'));
 
-            my $truncate_to_max_fine = $U->ou_ancestor_setting_value(
-                $c->$circ_lib_method, 'circ.fines.truncate_to_max_fine');
-            $truncate_to_max_fine = $U->is_true($truncate_to_max_fine);
+            my $truncate_to_max_fine = $U->is_true(
+                $class->get_cached_ou_setting($e, $ous_cache, 
+                    $c->$circ_lib_method, 'circ.fines.truncate_to_max_fine'));
 
             my ($latest_billing_ts, $latest_amount) = ('',0);
             for (my $bill = 1; $bill <= $pending_fine_count; $bill++) {