Files
2026-11-30 06:56:14 +02:00

92 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\Services;
use App\Modules\TimeTracking\Application\DTOs\TimesheetCustomerGroup;
use App\Modules\TimeTracking\Application\DTOs\TimesheetJobGroup;
use App\Modules\TimeTracking\Application\DTOs\TimesheetTaskRow;
use Collator;
/**
* Splits a week's task rows into the time sheet's two parts and groups each
* customer A–Ö (Finnish order) → job by number → task by title
* (docs/ui-timesheet.md 4):
*
* - "Lukitut työt": pinned tasks that can still take hours, or have hours
* this week; a pin of a done or invoiced job or task thus lapses on later
* weeks but stays on the weeks with hours;
* - "Aikataulutettu": tasks scheduled for the week or with hours this week
* that are not in the pinned part.
*/
final class TimesheetLayout
{
/**
* @param list<TimesheetTaskRow> $rows every candidate row, pinned or not
* @param list<int> $scheduledTaskIds
* @param array<int, int> $accumulatedQuarters by job id
* @return array{scheduled: list<TimesheetCustomerGroup>, pinned: list<TimesheetCustomerGroup>}
*/
public function arrange(array $rows, array $scheduledTaskIds, array $accumulatedQuarters): array
{
$scheduled = [];
$pinned = [];
foreach ($rows as $row) {
$open = ! $row->task->isLocked() && $row->task->taskStatus->value !== 'done';
if ($row->pinned && ($open || $row->totalQuarters > 0)) {
$pinned[] = $row;
} elseif ($row->totalQuarters > 0 || ($open && in_array($row->task->taskId, $scheduledTaskIds, true))) {
$scheduled[] = $row;
}
}
return [
'scheduled' => $this->grouped($scheduled, $accumulatedQuarters),
'pinned' => $this->grouped($pinned, $accumulatedQuarters),
];
}
/**
* @param list<TimesheetTaskRow> $rows
* @param array<int, int> $accumulatedQuarters
* @return list<TimesheetCustomerGroup>
*/
private function grouped(array $rows, array $accumulatedQuarters): array
{
$collator = class_exists(Collator::class) ? new Collator('fi_FI') : null;
$compare = fn (string $a, string $b): int => $collator?->compare($a, $b) ?: strcmp(mb_strtolower($a), mb_strtolower($b));
usort($rows, fn (TimesheetTaskRow $a, TimesheetTaskRow $b): int => $compare($a->task->customerName, $b->task->customerName)
?: ($a->task->customerId <=> $b->task->customerId)
?: strnatcmp($a->task->jobNumber, $b->task->jobNumber)
?: $compare($a->task->taskTitle, $b->task->taskTitle)
?: ($a->task->taskId <=> $b->task->taskId));
$customers = [];
foreach ($rows as $row) {
$customers[$row->task->customerId]['name'] = $row->task->customerName;
$customers[$row->task->customerId]['color'] = $row->task->customerHeaderColor;
$customers[$row->task->customerId]['jobs'][$row->task->jobId][] = $row;
}
$groups = [];
foreach ($customers as $customerId => $customer) {
$jobs = [];
foreach ($customer['jobs'] as $jobId => $jobRows) {
$task = $jobRows[0]->task;
$jobs[] = new TimesheetJobGroup($jobId, $task->jobNumber, $task->jobName, $task->jobStatus, $task->paymentBasis, $accumulatedQuarters[$jobId] ?? 0, $jobRows);
}
$groups[] = new TimesheetCustomerGroup($customerId, $customer['name'], $customer['color'], $jobs);
}
return $groups;
}
}