Show a monthly fee job's billed periods on a page of its own

The new billing::job-months page lists the months that created billing
materials have billed for a monthly fee job, with links to the materials
and the total, and the months still to bill up to the current month,
counted the same way as a new material counts them. The job page links
to it next to the payment basis for those who manage billing.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 15:56:15 +02:00
co-authored by Claude Sonnet 5.5
parent e817cca329
commit bc3dbf16a4
16 changed files with 470 additions and 3 deletions
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Modules\Billing\Application\Contracts;
use App\Modules\Billing\Application\DTOs\BilledMonth;
use App\Modules\Billing\Application\DTOs\ExportSummary;
/**
@@ -17,4 +18,11 @@ interface BillingQueries
* @return list<ExportSummary>
*/
public function exports(): array;
/**
* The monthly fee months of a job billed on created materials, oldest first.
*
* @return list<BilledMonth>
*/
public function billedMonthsOf(int $jobId): array;
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
use DateTimeImmutable;
/**
* One month of a monthly fee job billed on a created billing material.
*/
final readonly class BilledMonth
{
/**
* @param string $month Y-m.
*/
public function __construct(
public string $month,
public int $exportId,
public int $exportNumber,
public ?DateTimeImmutable $finalizedAt,
public int $netCents,
) {}
}
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
/**
* The billing periods of a monthly fee job: the months billed on created
* materials and the months still to bill up to the current month.
*/
final readonly class MonthlyBillingView
{
/**
* @param list<BilledMonth> $billed Oldest first.
* @param list<string> $unbilled Y-m, oldest first, up to the current month.
* @param string|null $billingEndsOn Y-m-d: the job's last day or completion day, after which nothing is billed.
*/
public function __construct(
public BillableJob $job,
public array $billed,
public array $unbilled,
public string $currentMonth,
public ?string $billingEndsOn,
) {}
public function billedNetCents(): int
{
return array_sum(array_map(fn (BilledMonth $month): int => $month->netCents, $this->billed));
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\Services;
use App\Modules\Billing\Application\Contracts\BillingJobs;
use App\Modules\Billing\Application\Contracts\BillingQueries;
use App\Modules\Billing\Application\DTOs\BilledMonth;
use App\Modules\Billing\Application\DTOs\MonthlyBillingView;
use App\Modules\Billing\Domain\Enums\BillingMode;
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
use App\Modules\Users\Application\Contracts\Clock;
/**
* The billing periods of a monthly fee job (spec 5.5): which months the
* created billing materials have billed, and which months from the job's
* start up to the current month are still to bill, the same way a new
* material counts them.
*/
final class MonthlyBillingViews
{
public function __construct(
private BillingJobs $jobs,
private BillingQueries $queries,
private Clock $clock,
) {}
/**
* Null when the job does not exist or is not billed by a monthly fee.
*/
public function forJob(int $jobId): ?MonthlyBillingView
{
$job = $this->jobs->find([$jobId])[$jobId] ?? null;
if ($job === null || $job->mode !== BillingMode::MonthlyFee) {
return null;
}
$billed = $this->queries->billedMonthsOf($jobId);
$currentMonth = $this->clock->today()->format('Y-m');
$unbilled = $job->billingStartsOn === ''
? []
: BillingMonths::unbilled($job->billingStartsOn, $job->billingEndsOn, $currentMonth, array_map(fn (BilledMonth $month): string => $month->month, $billed));
return new MonthlyBillingView($job, $billed, $unbilled, $currentMonth, $job->billingEndsOn);
}
}
@@ -3,13 +3,16 @@
namespace App\Modules\Billing\Infrastructure\Persistence\Repositories;
use App\Modules\Billing\Application\Contracts\BillingQueries;
use App\Modules\Billing\Application\DTOs\BilledMonth;
use App\Modules\Billing\Application\DTOs\ExportSummary;
use App\Modules\Billing\Domain\Enums\ExportStatus;
use App\Modules\Billing\Domain\Enums\LineKind;
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingExportModel;
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingInvoiceJobModel;
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingLineModel;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use DateTimeImmutable;
use DateTimeZone;
class EloquentBillingQueries implements BillingQueries
{
@@ -54,4 +57,32 @@ class EloquentBillingQueries implements BillingQueries
$model->status === ExportStatus::Created ? (int) ($netSums[$model->id] ?? 0) : null,
))->values()->all();
}
public function billedMonthsOf(int $jobId): array
{
return BillingLineModel::query()
->join('billing_invoices', 'billing_invoices.id', '=', 'billing_lines.billing_invoice_id')
->join('billing_exports', 'billing_exports.id', '=', 'billing_invoices.billing_export_id')
->where('billing_lines.customer_job_id', $jobId)
->where('billing_lines.kind', LineKind::MonthlyFee)
->whereNotNull('billing_lines.period')
->where('billing_exports.status', ExportStatus::Created)
->orderBy('billing_lines.period')
->get([
'billing_lines.period',
'billing_lines.net_cents',
'billing_exports.id as export_id',
'billing_exports.number as export_number',
'billing_exports.finalized_at',
])
->map(fn (BillingLineModel $line): BilledMonth => new BilledMonth(
(string) $line->getAttribute('period'),
(int) $line->getAttribute('export_id'),
(int) $line->getAttribute('export_number'),
$line->getAttribute('finalized_at') === null ? null : new DateTimeImmutable((string) $line->getAttribute('finalized_at'), new DateTimeZone('UTC')),
$line->net_cents,
))
->values()
->all();
}
}
@@ -0,0 +1,157 @@
<?php
use App\Modules\Billing\Application\DTOs\MonthlyBillingView;
use App\Modules\Billing\Application\Services\MonthlyBillingViews;
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
use App\Modules\Billing\Presentation\Livewire\Concerns\FormatsBilling;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* The billing periods of a monthly fee job (spec 5.5): the months billed
* on created billing materials, with links to them, and the months from
* the job's start up to the current month that are still to bill.
* Management only.
*/
new class extends Component
{
use FormatsBilling;
#[Locked]
public int $jobId = 0;
public function mount(int $job): void
{
$this->authorize('billing.manage');
$this->jobId = $job;
if (app(MonthlyBillingViews::class)->forJob($job) === null) {
abort(404);
}
}
#[Computed]
public function billing(): MonthlyBillingView
{
return app(MonthlyBillingViews::class)->forJob($this->jobId) ?? abort(404);
}
public function monthLabel(string $month): string
{
return BillingMonths::label($month);
}
public function dayLabel(string $date): string
{
return (new DateTimeImmutable($date))->format('j.n.Y');
}
public function heading(): string
{
return __('billing.job_months.title', ['job' => $this->billing->job->number.' '.$this->billing->job->name]);
}
/**
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
*/
public function headers(): array
{
return [
['index' => 'month', 'label' => __('billing.job_months.columns.month'), 'sortable' => false],
['index' => 'export', 'label' => __('billing.job_months.columns.export'), 'sortable' => false],
['index' => 'finalized', 'label' => __('billing.job_months.columns.finalized'), 'sortable' => false],
['index' => 'net', 'label' => __('billing.job_months.columns.net'), 'sortable' => false, 'align' => 'right'],
];
}
public function render(): mixed
{
return $this->view()->title($this->heading());
}
};
?>
@php($billing = $this->billing)
@php($job = $billing->job)
<div class="mx-auto max-w-4xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<p><x-link :href="route('jobs.show', $job->id)" navigate>{{ __('billing.job_months.back') }}</x-link></p>
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
<p class="max-w-3xl text-sm">{{ __('billing.job_months.intro') }}</p>
</div>
<section aria-labelledby="summary-heading" class="space-y-2">
<h2 id="summary-heading" class="sr-only">{{ __('billing.job_months.summary') }}</h2>
<dl class="grid grid-cols-1 gap-x-6 gap-y-1 text-sm sm:grid-cols-[auto_1fr]">
<dt class="font-medium">{{ __('billing.job_months.customer') }}</dt>
<dd>{{ $job->customerName }}</dd>
<dt class="font-medium">{{ __('billing.job_months.monthly_fee') }}</dt>
<dd>{{ $job->monthlyFeeCents === null ? __('billing.job_months.no_fee') : $this->money($job->monthlyFeeCents).' '.__('billing.job_months.vat_excluded') }}</dd>
<dt class="font-medium">{{ __('billing.job_months.starts') }}</dt>
<dd>{{ $job->billingStartsOn === '' ? '–' : $this->monthLabel(substr($job->billingStartsOn, 0, 7)) }}</dd>
<dt class="font-medium">{{ __('billing.job_months.ends') }}</dt>
<dd>{{ $billing->billingEndsOn === null ? __('billing.job_months.no_end') : $this->dayLabel($billing->billingEndsOn) }}</dd>
<dt class="font-medium">{{ __('billing.job_months.billing_unit') }}</dt>
<dd>{{ $job->billingUnitName ?? __('billing.invoices.no_billing_unit') }}</dd>
</dl>
@if (! $job->inBillableStatus)
<p class="flex items-center gap-1 text-sm font-medium">
<x-icon name="information-circle" class="size-5 shrink-0" aria-hidden="true" />
{{ __('billing.job_months.not_running') }}
</p>
@endif
</section>
<section aria-labelledby="billed-heading" class="space-y-3">
<h2 id="billed-heading" class="text-xl font-semibold">{{ __('billing.job_months.billed_title', ['count' => count($billing->billed)]) }}</h2>
@if ($billing->billed === [])
<p class="text-sm">{{ __('billing.job_months.none_billed') }}</p>
@else
<x-table :headers="$this->headers()" :rows="$billing->billed">
@interact('column_month', $month)
<span class="whitespace-nowrap">{{ $this->monthLabel($month->month) }}</span>
@endinteract
@interact('column_export', $month)
<x-link :href="route('billing.export', $month->exportId)" navigate>{{ __('billing.index.export_label', ['number' => $month->exportNumber]) }}</x-link>
@endinteract
@interact('column_finalized', $month)
<span class="whitespace-nowrap">{{ $month->finalizedAt === null ? '' : $this->timeLabel($month->finalizedAt) }}</span>
@endinteract
@interact('column_net', $month)
<span class="whitespace-nowrap">{{ $this->money($month->netCents) }}</span>
@endinteract
</x-table>
<p class="text-sm font-medium">{{ __('billing.job_months.billed_total', ['net' => $this->money($billing->billedNetCents())]) }}</p>
@endif
</section>
<section aria-labelledby="unbilled-heading" class="space-y-3">
<h2 id="unbilled-heading" class="text-xl font-semibold">{{ __('billing.job_months.unbilled_title', ['month' => $this->monthLabel($billing->currentMonth)]) }}</h2>
@if ($billing->unbilled === [])
<p class="flex items-center gap-1 text-sm">
<x-icon name="check-circle" class="size-5 shrink-0" aria-hidden="true" />
{{ __('billing.job_months.none_unbilled') }}
</p>
@else
<ul class="flex flex-wrap gap-2" aria-label="{{ __('billing.job_months.unbilled_list') }}">
@foreach ($billing->unbilled as $month)
<li class="rounded-md border border-zinc-300 px-3 py-1 text-sm dark:border-zinc-600">{{ $this->monthLabel($month) }}</li>
@endforeach
</ul>
<p class="text-sm">{{ __('billing.job_months.unbilled_help') }}</p>
@endif
</section>
</div>
@@ -10,4 +10,5 @@ Route::middleware(['web', 'auth', 'can:billing.manage'])
Route::livewire('/', 'billing::index')->name('index');
Route::livewire('/{export}', 'billing::export')->whereNumber('export')->name('export');
Route::get('/{export}/csv', DownloadBillingCsv::class)->whereNumber('export')->name('csv');
Route::livewire('/jobs/{job}/months', 'billing::job-months')->whereNumber('job')->name('job-months');
});