JBAS-2166 Org open date range HOO calculation fix
authorBill Erickson <berickxx@gmail.com>
Wed, 26 Dec 2018 20:35:18 +0000 (15:35 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
When calculating open day ranges, be sure a date selected to avoid an
hours of operation collision is not a closed date.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm

index deed9a9..053f2e1 100644 (file)
@@ -2518,9 +2518,9 @@ sub org_next_open_day {
     # Look for the next open hours-of-operation day.  If no open day is
     # found after 7 attempts, give up to avoid looping on orgs that 
     # have no open hours of operation.
-    my $counter = 0;
+    my $hoo_counter = 0;
     my $hoo_date = $end->clone;
-    while ($counter++ < 7) {
+    while ($hoo_counter++ < 7) {
         if (org_operates_on_date($org_id, $hoo_date, $hoo)) {
             $end = $hoo_date;
             last;
@@ -2528,10 +2528,31 @@ sub org_next_open_day {
         $hoo_date->add(days => 1);
     }
 
+    if ($hoo_counter == 7) {
+        # The org unit is closed every day, which means this 
+        # function can never return a meaningful value.
+        $logger->warn("Org unit $org_id is closed every day");
+        return undef;
+    }
+
     my $start = $begin->strftime('%FT%T%z');
     my $stop = $end->strftime('%FT%T%z');
 
+    # If the start and end date match, no date modifications were
+    # required.  Selected date is open.
     return undef if ($start eq $stop);
+
+    if ($hoo_counter > 1) {
+        # End date pushed ahead due to an hours of operation collision.
+        # Confirm the new date is not a closed date.
+
+        my $final_date = org_next_open_day(
+            $org_id, $stop, $closure_spanset, $hoo);
+
+        # date was bumped ahead even further.
+        return $final_date if $final_date;
+    }
+
     return $stop;
 }