47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Machines\Domain\Services;
|
|
|
|
/**
|
|
* Where a task goes in a day's work list by default (spec 19.4): after
|
|
* quick sale orders, then by priority (higher first) and due date (earlier
|
|
* first, none last). Rows moved by hand keep their places; a new task
|
|
* goes before the first task it outranks.
|
|
*/
|
|
final class QueueOrder
|
|
{
|
|
/**
|
|
* @param list<array{isOrder: bool, rank: int, dueOn: ?string}> $rows The day's open rows in order.
|
|
* @return int The index to insert the new task at.
|
|
*/
|
|
public static function insertionIndex(array $rows, int $rank, ?string $dueOn): int
|
|
{
|
|
foreach ($rows as $index => $row) {
|
|
if ($row['isOrder']) {
|
|
continue;
|
|
}
|
|
|
|
if (self::before($rank, $dueOn, $row['rank'], $row['dueOn'])) {
|
|
return $index;
|
|
}
|
|
}
|
|
|
|
return count($rows);
|
|
}
|
|
|
|
private static function before(int $rank, ?string $dueOn, int $otherRank, ?string $otherDueOn): bool
|
|
{
|
|
if ($rank !== $otherRank) {
|
|
return $rank > $otherRank;
|
|
}
|
|
|
|
if ($dueOn === null) {
|
|
return false;
|
|
}
|
|
|
|
return $otherDueOn === null || $dueOn < $otherDueOn;
|
|
}
|
|
}
|