$otherMinutes Y-m-d => minutes booked by the assignee's other open tasks. * @param list $holidays Y-m-d dates of the holidays in use. */ public static function schedule( DateTimeImmutable $from, DateTimeImmutable $to, int $minutes, array $otherMinutes, int $capacityMinutes, array $holidays, ): TaskSchedule { if ($minutes <= 0) { return new TaskSchedule([], $to, false); } $holidaySet = array_flip($holidays); $days = self::days($from, $to); $targets = array_values(array_filter($days, fn (string $day): bool => self::isWorkingDay($day, $holidaySet))); if ($targets === []) { $targets = $days; } $shares = self::split($minutes, count($targets)); $booked = []; $carry = 0; foreach ($targets as $index => $day) { $wanted = $shares[$index] + $carry; $booking = min($wanted, self::free($day, $otherMinutes, $capacityMinutes)); $carry = $wanted - $booking; if ($booking > 0) { $booked[$day] = $booking; } } $day = $to; for ($extra = 0; $carry > 0 && $extra < self::MAX_EXTRA_DAYS; $extra++) { $day = $day->add(new DateInterval('P1D')); $date = $day->format('Y-m-d'); if (! self::isWorkingDay($date, $holidaySet)) { continue; } $booking = min($carry, self::free($date, $otherMinutes, $capacityMinutes)); $carry -= $booking; if ($booking > 0) { $booked[$date] = $booking; } } if ($carry > 0) { $date = $day->format('Y-m-d'); $booked[$date] = ($booked[$date] ?? 0) + $carry; } ksort($booked); $lastDay = array_key_last($booked); $end = $lastDay !== null && $lastDay > $to->format('Y-m-d') ? new DateTimeImmutable($lastDay) : $to; return new TaskSchedule($booked, $end, $end > $to); } /** * The end of a task placed on a day without an end: as many working * days as the estimate needs at the daily maximum; without an estimate, the day itself. * * @param list $holidays */ public static function defaultEnd(DateTimeImmutable $from, int $minutes, int $capacityMinutes, array $holidays): DateTimeImmutable { if ($minutes <= 0 || $capacityMinutes <= 0) { return $from; } $needed = (int) ceil($minutes / $capacityMinutes); $holidaySet = array_flip($holidays); $day = $from; $counted = self::isWorkingDay($from->format('Y-m-d'), $holidaySet) ? 1 : 0; for ($extra = 0; $counted < $needed && $extra < self::MAX_EXTRA_DAYS; $extra++) { $day = $day->add(new DateInterval('P1D')); if (self::isWorkingDay($day->format('Y-m-d'), $holidaySet)) { $counted++; } } return $day; } /** * @param array $holidaySet */ public static function isWorkingDay(string $date, array $holidaySet): bool { $day = new DateTimeImmutable($date); return (int) $day->format('N') <= 5 && ! isset($holidaySet[$date]); } /** * @return list */ private static function days(DateTimeImmutable $from, DateTimeImmutable $to): array { $days = []; for ($day = $from; $day <= $to; $day = $day->add(new DateInterval('P1D'))) { $days[] = $day->format('Y-m-d'); } return $days; } /** * Splits the minutes into equal quarter-hour shares; the first days get * the remainder. Odd minutes go to the first day. * * @return list */ private static function split(int $minutes, int $parts): array { $steps = intdiv($minutes, self::STEP_MINUTES); $odd = $minutes % self::STEP_MINUTES; $shares = []; for ($index = 0; $index < $parts; $index++) { $shares[] = (intdiv($steps, $parts) + ($index < $steps % $parts ? 1 : 0)) * self::STEP_MINUTES; } $shares[0] += $odd; return $shares; } /** * @param array $otherMinutes */ private static function free(string $day, array $otherMinutes, int $capacityMinutes): int { return max(0, $capacityMinutes - ($otherMinutes[$day] ?? 0)); } }