Add the unbilled hours report
Hours on customers' billable jobs that no billing material has taken, by job and work type up to a day, valued at the hourly rates for jobs billed by the hour, with a chart by service unit and CSV. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Presentation\Csv;
|
||||
|
||||
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Reads the report pages' URL filters from a CSV download's query string.
|
||||
*/
|
||||
final class CsvFilters
|
||||
{
|
||||
/**
|
||||
* @throws InvalidReportPeriod
|
||||
*/
|
||||
public static function period(Request $request): ReportPeriod
|
||||
{
|
||||
return new ReportPeriod(self::text($request, 'alku'), self::text($request, 'loppu'));
|
||||
}
|
||||
|
||||
/**
|
||||
* A single day (Y-m-d).
|
||||
*
|
||||
* @throws InvalidReportPeriod
|
||||
*/
|
||||
public static function day(Request $request, string $key): string
|
||||
{
|
||||
$day = self::text($request, $key);
|
||||
|
||||
return (new ReportPeriod($day, $day))->from;
|
||||
}
|
||||
|
||||
public static function id(Request $request, string $key): ?int
|
||||
{
|
||||
$value = self::text($request, $key);
|
||||
|
||||
return ctype_digit($value) ? (int) $value : null;
|
||||
}
|
||||
|
||||
public static function flag(Request $request, string $key): bool
|
||||
{
|
||||
return self::text($request, $key) === '1';
|
||||
}
|
||||
|
||||
private static function text(Request $request, string $key): string
|
||||
{
|
||||
$value = $request->query($key);
|
||||
|
||||
return is_string($value) ? $value : '';
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Modules\Reports\Presentation\Csv;
|
||||
|
||||
use App\Modules\Reports\Application\UseCases\HoursByPerson;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
use App\Modules\Reports\Presentation\ReportFormat;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@@ -14,14 +13,21 @@ class HoursCsv implements ReportCsv
|
||||
{
|
||||
public function __construct(private HoursByPerson $report) {}
|
||||
|
||||
public function fileName(Request $request): string
|
||||
{
|
||||
$period = CsvFilters::period($request);
|
||||
|
||||
return __('reports.hours.file').'-'.$period->from.'-'.$period->to;
|
||||
}
|
||||
|
||||
public function headers(): array
|
||||
{
|
||||
return array_values(__('reports.hours.csv'));
|
||||
}
|
||||
|
||||
public function rows(ReportPeriod $period, Request $request): iterable
|
||||
public function rows(Request $request): iterable
|
||||
{
|
||||
$report = $this->report->execute($period, self::id($request, 'yksikko'), self::id($request, 'asiakas'));
|
||||
$report = $this->report->execute(CsvFilters::period($request), CsvFilters::id($request, 'yksikko'), CsvFilters::id($request, 'asiakas'));
|
||||
|
||||
foreach ($report->lines as $line) {
|
||||
yield [
|
||||
@@ -38,11 +44,4 @@ class HoursCsv implements ReportCsv
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public static function id(Request $request, string $key): ?int
|
||||
{
|
||||
$value = $request->query($key);
|
||||
|
||||
return is_string($value) && ctype_digit($value) ? (int) $value : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,22 @@
|
||||
|
||||
namespace App\Modules\Reports\Presentation\Csv;
|
||||
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* A report as CSV rows, with the filters of the report page read from the
|
||||
* request's query string.
|
||||
* request's query string (see CsvFilters).
|
||||
*/
|
||||
interface ReportCsv
|
||||
{
|
||||
/**
|
||||
* The file name without the extension, e.g. "tunnit-2026-11-01-2026-11-30".
|
||||
*
|
||||
* @throws InvalidReportPeriod
|
||||
*/
|
||||
public function fileName(Request $request): string;
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
@@ -18,6 +25,8 @@ interface ReportCsv
|
||||
|
||||
/**
|
||||
* @return iterable<list<string|int>>
|
||||
*
|
||||
* @throws InvalidReportPeriod
|
||||
*/
|
||||
public function rows(ReportPeriod $period, Request $request): iterable;
|
||||
public function rows(Request $request): iterable;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Presentation\Csv;
|
||||
|
||||
use App\Modules\Reports\Application\UseCases\UnbilledHoursReport;
|
||||
use App\Modules\Reports\Presentation\ReportFormat;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Unbilled hours as CSV: a row per job and work type, values in cents.
|
||||
*/
|
||||
class UnbilledCsv implements ReportCsv
|
||||
{
|
||||
public function __construct(private UnbilledHoursReport $report) {}
|
||||
|
||||
public function fileName(Request $request): string
|
||||
{
|
||||
return __('reports.unbilled.file').'-'.CsvFilters::day($request, 'paiva');
|
||||
}
|
||||
|
||||
public function headers(): array
|
||||
{
|
||||
return array_values(__('reports.unbilled.csv'));
|
||||
}
|
||||
|
||||
public function rows(Request $request): iterable
|
||||
{
|
||||
$report = $this->report->execute(CsvFilters::day($request, 'paiva'), CsvFilters::id($request, 'yksikko'));
|
||||
|
||||
foreach ($report->jobs as $row) {
|
||||
foreach ($row->workTypes as $line) {
|
||||
yield [
|
||||
$row->job->number,
|
||||
$row->job->name,
|
||||
$row->job->customerName,
|
||||
__('jobs.statuses.'.$row->job->status),
|
||||
$row->job->paymentBasis === null ? '' : __('jobs.payment_bases.'.$row->job->paymentBasis),
|
||||
$line->serviceUnitName,
|
||||
$line->name,
|
||||
ReportFormat::csvHours($line->approvedQuarters),
|
||||
ReportFormat::csvHours($line->unapprovedQuarters),
|
||||
$line->rateCents ?? '',
|
||||
$line->valueCents ?? '',
|
||||
$row->oldestDay,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,39 +3,38 @@
|
||||
namespace App\Modules\Reports\Presentation\Http\Controllers;
|
||||
|
||||
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
use App\Modules\Reports\Presentation\Csv\HoursCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\ReportCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
/**
|
||||
* Downloads a report as a CSV file for Excel, with the filters of its page
|
||||
* (alku, loppu and the report's own): semicolons, a UTF-8 BOM, amounts in
|
||||
* whole cents and hours with a decimal comma. Text that Excel would read
|
||||
* as a formula gets a leading apostrophe, and the file is never cached.
|
||||
* in the query string: semicolons, a UTF-8 BOM, amounts in whole cents and
|
||||
* hours with a decimal comma. Text that Excel would read as a formula gets
|
||||
* a leading apostrophe, and the file is never cached.
|
||||
*/
|
||||
class DownloadReport
|
||||
{
|
||||
/** The reports that have a CSV, by their URL key. */
|
||||
public const array REPORTS = ['hours'];
|
||||
public const array REPORTS = ['hours', 'unbilled'];
|
||||
|
||||
public function __invoke(string $report, Request $request): StreamedResponse
|
||||
{
|
||||
Gate::authorize('reports.view');
|
||||
|
||||
$csv = $this->csv($report);
|
||||
|
||||
try {
|
||||
$period = new ReportPeriod((string) $request->query('alku', ''), (string) $request->query('loppu', ''));
|
||||
// Reading the file name checks the filters before anything is streamed.
|
||||
$fileName = $csv->fileName($request).'.csv';
|
||||
} catch (InvalidReportPeriod) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$csv = $this->csv($report);
|
||||
$rows = $csv->rows($period, $request);
|
||||
$headers = $csv->headers();
|
||||
|
||||
return response()->streamDownload(function () use ($headers, $rows): void {
|
||||
return response()->streamDownload(function () use ($csv, $request): void {
|
||||
$out = fopen('php://output', 'w');
|
||||
|
||||
if ($out === false) {
|
||||
@@ -43,14 +42,14 @@ class DownloadReport
|
||||
}
|
||||
|
||||
fwrite($out, "\u{FEFF}");
|
||||
fputcsv($out, $headers, ';', '"', '');
|
||||
fputcsv($out, $csv->headers(), ';', '"', '');
|
||||
|
||||
foreach ($rows as $row) {
|
||||
foreach ($csv->rows($request) as $row) {
|
||||
fputcsv($out, array_map(self::cell(...), $row), ';', '"', '');
|
||||
}
|
||||
|
||||
fclose($out);
|
||||
}, __('reports.csv_file', ['report' => __('reports.'.$report.'.file'), 'from' => $period->from, 'to' => $period->to]), [
|
||||
}, $fileName, [
|
||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||
'Cache-Control' => 'no-store, private',
|
||||
]);
|
||||
@@ -73,6 +72,7 @@ class DownloadReport
|
||||
{
|
||||
return match ($report) {
|
||||
'hours' => app(HoursCsv::class),
|
||||
'unbilled' => app(UnbilledCsv::class),
|
||||
default => abort(404),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Presentation\Livewire\Concerns;
|
||||
|
||||
use App\Modules\Reports\Presentation\ReportFormat;
|
||||
|
||||
/**
|
||||
* The number, money and date formats of the report pages.
|
||||
*/
|
||||
trait FormatsReports
|
||||
{
|
||||
public function money(int $cents): string
|
||||
{
|
||||
return ReportFormat::money($cents);
|
||||
}
|
||||
|
||||
public function hours(int $quarters): string
|
||||
{
|
||||
return ReportFormat::hours($quarters);
|
||||
}
|
||||
|
||||
public function dateLabel(string $day): string
|
||||
{
|
||||
return ReportFormat::date($day);
|
||||
}
|
||||
|
||||
public function percent(float $ratio): string
|
||||
{
|
||||
return ReportFormat::percent($ratio);
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,13 @@ use Livewire\Attributes\Url;
|
||||
|
||||
/**
|
||||
* The period every report page covers, kept in the URL (alku, loppu), its
|
||||
* quick choices and the shared number formats. Without a valid period in
|
||||
* quick choices and the shared formats. Without a valid period in
|
||||
* the URL a page starts with the current month.
|
||||
*/
|
||||
trait ReportPage
|
||||
{
|
||||
use FormatsReports;
|
||||
|
||||
#[Url(as: 'alku', except: '')]
|
||||
public string $from = '';
|
||||
|
||||
@@ -82,21 +84,6 @@ trait ReportPage
|
||||
return $period === null ? '' : ReportFormat::period($period);
|
||||
}
|
||||
|
||||
public function money(int $cents): string
|
||||
{
|
||||
return ReportFormat::money($cents);
|
||||
}
|
||||
|
||||
public function hours(int $quarters): string
|
||||
{
|
||||
return ReportFormat::hours($quarters);
|
||||
}
|
||||
|
||||
public function dateLabel(string $day): string
|
||||
{
|
||||
return ReportFormat::date($day);
|
||||
}
|
||||
|
||||
/**
|
||||
* The CSV of the report with the filters shown.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Reports\Application\DTOs\UnbilledJob;
|
||||
use App\Modules\Reports\Application\DTOs\UnbilledReport;
|
||||
use App\Modules\Reports\Application\UseCases\UnbilledHoursReport;
|
||||
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
use App\Modules\Reports\Presentation\Livewire\Concerns\FormatsReports;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Unbilled hours (spec 13): hours on customers' billable jobs up to a day
|
||||
* that no billing material has taken yet, by job and work type, with
|
||||
* their value at the hourly rates when the job bills its hours.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
use FormatsReports;
|
||||
|
||||
#[Url(as: 'paiva', except: '')]
|
||||
public string $day = '';
|
||||
|
||||
#[Url(as: 'yksikko', except: '')]
|
||||
public string $serviceUnit = '';
|
||||
|
||||
public function mount(Clock $clock): void
|
||||
{
|
||||
$this->authorize('reports.view');
|
||||
|
||||
if (! $this->dayIsValid()) {
|
||||
$this->day = $clock->today()->format('Y-m-d');
|
||||
}
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function report(): ?UnbilledReport
|
||||
{
|
||||
if (! $this->dayIsValid()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return app(UnbilledHoursReport::class)->execute($this->day, ctype_digit($this->serviceUnit) ? (int) $this->serviceUnit : null);
|
||||
}
|
||||
|
||||
public function updated(): void
|
||||
{
|
||||
$this->authorize('reports.view');
|
||||
unset($this->report);
|
||||
}
|
||||
|
||||
public function dayIsValid(): bool
|
||||
{
|
||||
try {
|
||||
new ReportPeriod($this->day, $this->day);
|
||||
|
||||
return true;
|
||||
} catch (InvalidReportPeriod) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function csv(): string
|
||||
{
|
||||
return route('reports.csv', array_filter(['report' => 'unbilled', 'paiva' => $this->day, 'yksikko' => $this->serviceUnit]));
|
||||
}
|
||||
|
||||
public function valueText(UnbilledJob $row): string
|
||||
{
|
||||
if ($row->valueCents === null) {
|
||||
return __('reports.unbilled.in_price');
|
||||
}
|
||||
|
||||
return $this->money($row->valueCents).($row->missingRate ? ' '.__('reports.unbilled.missing_rate_short') : '');
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('reports.unbilled.title'));
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-6xl space-y-6 p-4 sm:p-6">
|
||||
<div class="space-y-2">
|
||||
<h1 class="text-2xl font-semibold">{{ __('reports.unbilled.title') }}</h1>
|
||||
<p class="max-w-3xl text-sm">{{ __('reports.unbilled.intro') }}</p>
|
||||
</div>
|
||||
|
||||
@php($report = $this->report)
|
||||
|
||||
<div class="grid max-w-2xl gap-4 sm:grid-cols-2">
|
||||
<x-input id="report-day" wire:model.live.change="day" type="date" :label="__('reports.unbilled.day')" required />
|
||||
|
||||
@if ($report !== null)
|
||||
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||
<x-select.native wire:model.live="serviceUnit" :label="__('reports.filters.service_unit')" :aria-label="__('reports.filters.service_unit')">
|
||||
<option value="">{{ __('reports.filters.all_service_units') }}</option>
|
||||
@foreach ($report->serviceUnits as $id => $name)
|
||||
<option value="{{ $id }}">{{ $name }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
@if ($report === null)
|
||||
<p role="alert" class="flex items-center gap-2 text-sm font-medium text-red-700 dark:text-red-300">
|
||||
<x-icon name="exclamation-triangle" class="size-5" aria-hidden="true" />
|
||||
{{ __('reports.unbilled.invalid_day') }}
|
||||
</p>
|
||||
@else
|
||||
<div class="flex flex-wrap items-center justify-between gap-3">
|
||||
<h2 class="text-xl font-semibold" role="status" aria-live="polite">{{ __('reports.unbilled.until', ['day' => $this->dateLabel($day)]) }}</h2>
|
||||
<x-button :href="$this->csv()" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
|
||||
</div>
|
||||
|
||||
@if ($report->jobs === [])
|
||||
<p>{{ __('reports.unbilled.none') }}</p>
|
||||
@else
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<caption class="sr-only">{{ __('reports.unbilled.table', ['day' => $this->dateLabel($day)]) }}</caption>
|
||||
<thead>
|
||||
<tr class="border-b border-zinc-300 text-left dark:border-zinc-600">
|
||||
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.job') }}</th>
|
||||
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.status') }}</th>
|
||||
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.oldest') }}</th>
|
||||
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.unbilled.approved') }}</th>
|
||||
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.unbilled.unapproved') }}</th>
|
||||
<th scope="col" class="py-2 text-right">{{ __('reports.unbilled.value') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
|
||||
@foreach ($report->jobs as $row)
|
||||
<tr wire:key="job-{{ $row->job->id }}" class="align-top">
|
||||
<th scope="row" class="py-2 pr-3 text-left font-normal">
|
||||
<x-link :href="route('jobs.show', $row->job->id)" navigate>{{ $row->job->number }} {{ $row->job->name }}</x-link>
|
||||
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ $row->job->customerName }}</span>
|
||||
<details class="mt-1">
|
||||
<summary class="cursor-pointer text-xs font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600">
|
||||
{{ __('reports.unbilled.by_work_type', ['job' => $row->job->number]) }}
|
||||
</summary>
|
||||
<ul class="mt-1 space-y-0.5 text-xs">
|
||||
@foreach ($row->workTypes as $line)
|
||||
<li>
|
||||
{{ __('reports.unbilled.work_type_line', [
|
||||
'name' => $line->name,
|
||||
'approved' => $this->hours($line->approvedQuarters),
|
||||
'unapproved' => $this->hours($line->unapprovedQuarters),
|
||||
]) }}@if ($line->valueCents !== null), {{ $this->money($line->valueCents) }}@elseif ($row->job->billsHours) ({{ __('reports.unbilled.missing_rate') }})@endif
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</details>
|
||||
</th>
|
||||
<td class="py-2 pr-3">
|
||||
{{ __('jobs.statuses.'.$row->job->status) }}
|
||||
@if ($row->job->paymentBasis !== null)
|
||||
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ __('jobs.payment_bases.'.$row->job->paymentBasis) }}</span>
|
||||
@endif
|
||||
</td>
|
||||
<td class="py-2 pr-3 whitespace-nowrap">{{ $this->dateLabel($row->oldestDay) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->approvedQuarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->unapprovedQuarters) }}</td>
|
||||
<td class="py-2 text-right">{{ $this->valueText($row) }}</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr class="border-t-2 border-zinc-400 font-semibold dark:border-zinc-500">
|
||||
<th scope="row" colspan="3" class="py-2 pr-3 text-left">{{ __('reports.total') }}</th>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->approvedQuarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->unapprovedQuarters) }}</td>
|
||||
<td class="py-2 text-right whitespace-nowrap">{{ $this->money($report->valueCents) }}</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<x-reports.chart
|
||||
:title="__('reports.unbilled.chart')"
|
||||
:summary="implode(', ', array_map(fn ($unit, $cents) => $unit.' '.$this->money($cents), array_keys($report->valueByServiceUnit), $report->valueByServiceUnit))"
|
||||
:labels="array_keys($report->valueByServiceUnit)"
|
||||
:values="array_map(fn ($cents) => $cents / 100, array_values($report->valueByServiceUnit))"
|
||||
:formatter="fn (float $value) => number_format($value, 0, ',', ' ').' €'"
|
||||
/>
|
||||
@endif
|
||||
@endif
|
||||
</div>
|
||||
@@ -8,5 +8,6 @@ Route::middleware(['web', 'auth', 'can:reports.view'])
|
||||
->name('reports.')
|
||||
->group(function () {
|
||||
Route::livewire('/hours', 'reports::hours')->name('hours');
|
||||
Route::livewire('/unbilled', 'reports::unbilled')->name('unbilled');
|
||||
Route::get('/{report}/csv', DownloadReport::class)->whereIn('report', DownloadReport::REPORTS)->name('csv');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user