Add Reports module with the hours by person report

Management's reports (spec 13) get their own module that reads the other
modules through ports. The first report sums hours by person in a period,
narrowed by service unit or customer, with breakdowns, a chart and CSV.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 08:39:19 +02:00
co-authored by Claude Sonnet 5.5
parent f906f9a43f
commit 64f1e7c54a
45 changed files with 1828 additions and 3 deletions
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\Contracts;
use App\Modules\TimeTracking\Application\DTOs\ReportedHours;
use App\Modules\TimeTracking\Application\DTOs\UnbilledHours;
/**
* Hours summed for management's reports.
*/
interface TimeReportQueries
{
/**
* Hours worked from the first to the last day (Y-m-d, both included),
* summed by user, job and work type.
*
* @return list<ReportedHours>
*/
public function hoursBetween(string $from, string $to): array;
/**
* Hours not yet billed, worked on or before the day (Y-m-d), summed by
* job and work type.
*
* @return list<UnbilledHours>
*/
public function unbilledUntil(string $day): array;
}
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
/**
* A user's hours on a job's work type in a period, in quarter hours: all
* of them, those still waiting for approval and those already billed.
*/
final readonly class ReportedHours
{
public function __construct(
public int $userId,
public int $jobId,
public int $workTypeId,
public int $quarters,
public int $unapprovedQuarters,
public int $billedQuarters,
) {}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
/**
* A job's unbilled hours on a work type in quarter hours, approved and
* waiting for approval, with the first day they were worked (Y-m-d).
*/
final readonly class UnbilledHours
{
public function __construct(
public int $jobId,
public int $workTypeId,
public int $approvedQuarters,
public int $unapprovedQuarters,
public string $oldestDay,
) {}
}
@@ -0,0 +1,69 @@
<?php
namespace App\Modules\TimeTracking\Infrastructure\Persistence\Repositories;
use App\Modules\TimeTracking\Application\Contracts\TimeReportQueries;
use App\Modules\TimeTracking\Application\DTOs\ReportedHours;
use App\Modules\TimeTracking\Application\DTOs\UnbilledHours;
use DateTimeImmutable;
use Illuminate\Support\Facades\DB;
/**
* Sums hours in the database. Hours are kept in quarters (0,25 h), so four
* times their sum is a whole number.
*/
class EloquentTimeReportQueries implements TimeReportQueries
{
public function hoursBetween(string $from, string $to): array
{
return DB::table('time_entries')
->where('worked_on', '>=', $from)
->where('worked_on', '<', self::dayAfter($to))
->groupBy('user_id', 'customer_job_id', 'work_type_id')
->orderBy('user_id')
->select('user_id', 'customer_job_id', 'work_type_id')
->selectRaw('SUM(hours) * 4 as quarters')
->selectRaw('SUM(CASE WHEN approved_at IS NULL THEN hours ELSE 0 END) * 4 as unapproved_quarters')
->selectRaw('SUM(CASE WHEN billing_export_id IS NULL THEN 0 ELSE hours END) * 4 as billed_quarters')
->get()
->map(fn (object $row): ReportedHours => new ReportedHours(
(int) $row->user_id,
(int) $row->customer_job_id,
(int) $row->work_type_id,
(int) round((float) $row->quarters),
(int) round((float) $row->unapproved_quarters),
(int) round((float) $row->billed_quarters),
))
->all();
}
public function unbilledUntil(string $day): array
{
return DB::table('time_entries')
->whereNull('billing_export_id')
->where('worked_on', '<', self::dayAfter($day))
->groupBy('customer_job_id', 'work_type_id')
->orderBy('customer_job_id')
->select('customer_job_id', 'work_type_id')
->selectRaw('SUM(CASE WHEN approved_at IS NULL THEN 0 ELSE hours END) * 4 as approved_quarters')
->selectRaw('SUM(CASE WHEN approved_at IS NULL THEN hours ELSE 0 END) * 4 as unapproved_quarters')
->selectRaw('MIN(worked_on) as oldest_day')
->get()
->map(fn (object $row): UnbilledHours => new UnbilledHours(
(int) $row->customer_job_id,
(int) $row->work_type_id,
(int) round((float) $row->approved_quarters),
(int) round((float) $row->unapproved_quarters),
substr((string) $row->oldest_day, 0, 10),
))
->all();
}
/**
* Days may be stored with a time, so a period ends before the next day.
*/
private static function dayAfter(string $day): string
{
return (new DateTimeImmutable($day))->modify('+1 day')->format('Y-m-d');
}
}
@@ -6,9 +6,11 @@ use App\Models\User;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\Contracts\TaskUsage;
use App\Modules\TimeTracking\Application\Contracts\TimeEntryQueries;
use App\Modules\TimeTracking\Application\Contracts\TimeReportQueries;
use App\Modules\TimeTracking\Domain\Contracts\TimeEntryRepository;
use App\Modules\TimeTracking\Infrastructure\Persistence\Repositories\EloquentTimeEntryQueries;
use App\Modules\TimeTracking\Infrastructure\Persistence\Repositories\EloquentTimeEntryRepository;
use App\Modules\TimeTracking\Infrastructure\Persistence\Repositories\EloquentTimeReportQueries;
use App\Modules\TimeTracking\Infrastructure\TaskUsageFromTimeEntries;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Support\Facades\Gate;
@@ -26,6 +28,7 @@ class TimeTrackingServiceProvider extends ServiceProvider
public array $bindings = [
TimeEntryRepository::class => EloquentTimeEntryRepository::class,
TimeEntryQueries::class => EloquentTimeEntryQueries::class,
TimeReportQueries::class => EloquentTimeReportQueries::class,
TaskUsage::class => TaskUsageFromTimeEntries::class,
];