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:
+69
@@ -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,
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user