# 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;
$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;
}