- A task now has one optional assignee (tasks.assignee_user_id), whose calendar row it is on and whose days its estimate books, and members (task_members) who see it among their own jobs and in the weekly sheet but get no bookings. A migration moves existing assignees over: the one with most booked time becomes the assignee, the others members. - Adding tasks from a workflow template asks for each step's assignee and, by default, schedules tasks with a due date to end on it, booking the estimate backwards from the due date (not before today); what does not fit runs past the due date and is reported as late. - The task calendar leaves quick sale tasks out of the unscheduled list, shows a bar's job details in a popover on hover and focus, and shows the day load on its own grid line. - The template table stacks its cells on narrow screens to reflow at 320 px. Co-Authored-By: Claude Opus 5.5 <[email protected]>
283 lines
9.3 KiB
PHP
283 lines
9.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Jobs\Domain\Services;
|
|
|
|
use App\Modules\Jobs\Domain\ValueObjects\TaskSchedule;
|
|
use DateInterval;
|
|
use DateTimeImmutable;
|
|
|
|
/**
|
|
* Books a task's estimated time from its assignee's days (spec 18.3).
|
|
*
|
|
* The estimate is split evenly over the working days of the planned
|
|
* period (not Saturdays, Sundays or holidays); a period of only such days
|
|
* splits it over all its days. No day gets more than the assignee's free
|
|
* capacity (the daily maximum minus what other open tasks already book):
|
|
* what does not fit moves to the next day of the period, and past its
|
|
* end to the next working days, which makes the task end later. Other
|
|
* tasks' bookings never move.
|
|
*/
|
|
final class TaskScheduler
|
|
{
|
|
/** Bookings are made in quarter hours, like time entries. */
|
|
private const int STEP_MINUTES = 15;
|
|
|
|
/** How far past the planned end the overflow may go. */
|
|
private const int MAX_EXTRA_DAYS = 366;
|
|
|
|
/**
|
|
* @param array<string, int> $otherMinutes Y-m-d => minutes booked by the assignee's other open tasks.
|
|
* @param list<string> $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;
|
|
}
|
|
}
|
|
|
|
return self::finish($booked, self::bookAfter($to, $carry, $otherMinutes, $capacityMinutes, $holidaySet), $to);
|
|
}
|
|
|
|
/**
|
|
* Books a task's estimated time backwards from its due date: first the
|
|
* assignee's free time on the due date, then on the working days before
|
|
* it, but not before the earliest day (today). What does not fit goes to
|
|
* the working days after the due date, which makes the task end late.
|
|
*
|
|
* @param array<string, int> $otherMinutes Y-m-d => minutes booked by the assignee's other open tasks.
|
|
* @param list<string> $holidays Y-m-d dates of the holidays in use.
|
|
*/
|
|
public static function scheduleBackwards(
|
|
DateTimeImmutable $due,
|
|
int $minutes,
|
|
array $otherMinutes,
|
|
int $capacityMinutes,
|
|
array $holidays,
|
|
DateTimeImmutable $earliest,
|
|
): TaskSchedule {
|
|
if ($minutes <= 0) {
|
|
return new TaskSchedule([], $due, false);
|
|
}
|
|
|
|
$holidaySet = array_flip($holidays);
|
|
$booked = [];
|
|
$carry = $minutes;
|
|
$first = $earliest->format('Y-m-d');
|
|
|
|
for ($day = $due, $back = 0; $carry > 0 && $day->format('Y-m-d') >= $first && $back < self::MAX_EXTRA_DAYS; $day = $day->sub(new DateInterval('P1D')), $back++) {
|
|
$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;
|
|
}
|
|
}
|
|
|
|
// A due date before the earliest day: the rest starts on the earliest day.
|
|
$after = $due->format('Y-m-d') >= $first ? $due : $earliest->sub(new DateInterval('P1D'));
|
|
|
|
return self::finish($booked, self::bookAfter($after, $carry, $otherMinutes, $capacityMinutes, $holidaySet), $due);
|
|
}
|
|
|
|
/**
|
|
* The start of a task that ends on a day: as many working days before
|
|
* it as the estimate needs at the daily maximum, but not before the
|
|
* earliest day unless the end is; without an estimate, the day itself.
|
|
*
|
|
* @param list<string> $holidays
|
|
*/
|
|
public static function defaultStart(DateTimeImmutable $to, int $minutes, int $capacityMinutes, array $holidays, DateTimeImmutable $earliest): DateTimeImmutable
|
|
{
|
|
$first = $earliest->format('Y-m-d');
|
|
|
|
if ($minutes <= 0 || $capacityMinutes <= 0 || $to->format('Y-m-d') <= $first) {
|
|
return $to;
|
|
}
|
|
|
|
$needed = (int) ceil($minutes / $capacityMinutes);
|
|
$holidaySet = array_flip($holidays);
|
|
$day = $to;
|
|
$counted = self::isWorkingDay($to->format('Y-m-d'), $holidaySet) ? 1 : 0;
|
|
|
|
while ($counted < $needed && $day->format('Y-m-d') > $first) {
|
|
$day = $day->sub(new DateInterval('P1D'));
|
|
|
|
if (self::isWorkingDay($day->format('Y-m-d'), $holidaySet)) {
|
|
$counted++;
|
|
}
|
|
}
|
|
|
|
return $day;
|
|
}
|
|
|
|
/**
|
|
* 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<string> $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<string, int> $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<string>
|
|
*/
|
|
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<int>
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Books the carried minutes on the working days after the day; what
|
|
* finds no free time within MAX_EXTRA_DAYS goes on the last day tried.
|
|
*
|
|
* @param array<string, int> $otherMinutes
|
|
* @param array<string, int> $holidaySet
|
|
* @return array<string, int> Y-m-d => minutes.
|
|
*/
|
|
private static function bookAfter(DateTimeImmutable $day, int $carry, array $otherMinutes, int $capacityMinutes, array $holidaySet): array
|
|
{
|
|
$booked = [];
|
|
|
|
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;
|
|
}
|
|
|
|
return $booked;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, int> $booked
|
|
* @param array<string, int> $overflow Bookings after the planned end.
|
|
*/
|
|
private static function finish(array $booked, array $overflow, DateTimeImmutable $to): TaskSchedule
|
|
{
|
|
foreach ($overflow as $date => $minutes) {
|
|
$booked[$date] = ($booked[$date] ?? 0) + $minutes;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, int> $otherMinutes
|
|
*/
|
|
private static function free(string $day, array $otherMinutes, int $capacityMinutes): int
|
|
{
|
|
return max(0, $capacityMinutes - ($otherMinutes[$day] ?? 0));
|
|
}
|
|
}
|