Add monthly fee billing lines to the billing material

Jobs billed by a fixed monthly fee get a monthly fee field, kept behind
the quotes permission like the quoted amount. A billing material now has
a billing month: a new draft adds every monthly fee job with something
to bill, each on an invoice of its own, and bills one line for every
unbilled month from the job's start up to the billing month (or its end
or completion), so skipped months are caught up and no month is billed
twice. Their expenses are billed alongside, their hours are covered by
the fee and marked billed, and the job keeps running.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 13:23:46 +02:00
co-authored by Claude Opus 5.5
parent 97bd23bed5
commit 5f9423a3c3
49 changed files with 799 additions and 82 deletions
@@ -10,12 +10,15 @@ use App\Modules\Billing\Application\UseCases\MoveJobToInvoice;
use App\Modules\Billing\Application\UseCases\RemoveJobFromDraft;
use App\Modules\Billing\Application\UseCases\SeparateJob;
use App\Modules\Billing\Application\UseCases\SetBillableHours;
use App\Modules\Billing\Application\UseCases\SetBillingPeriod;
use App\Modules\Billing\Domain\Enums\BillingMode;
use App\Modules\Billing\Domain\Enums\LineKind;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportEmpty;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\InvalidBillableHours;
use App\Modules\Billing\Domain\Exceptions\InvalidBillingPeriod;
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
use App\Modules\Billing\Domain\Exceptions\InvoicesCannotMerge;
use App\Modules\Billing\Domain\Exceptions\JobAlreadyInDraft;
use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
@@ -120,6 +123,26 @@ new class extends Component
unset($this->export);
}
public function savePeriod(string $value, SetBillingPeriod $setBillingPeriod): void
{
$this->authorize('billing.manage');
$this->resetErrorBag('period');
$this->statusMessage = '';
$this->errorMessage = '';
try {
$setBillingPeriod->execute($this->exportId, mb_substr(trim($value), 0, 7));
unset($this->export);
$this->statusMessage = __('billing.export.period_saved', ['month' => BillingMonths::label($this->export->period)]);
} catch (InvalidBillingPeriod) {
$this->addError('period', __('billing.errors.period'));
} catch (ExportNotDraft|BillingExportNotFound $exception) {
$this->errorMessage = $this->errorText($exception, '');
}
unset($this->export);
}
public function confirmFinalize(): void
{
$this->authorize('billing.manage');
@@ -304,6 +327,23 @@ new class extends Component
<p id="billing-error" tabindex="-1" role="alert" class="text-sm font-medium text-red-700 focus:outline-none dark:text-red-400">{{ $errorMessage }}</p>
@if ($export->isDraft())
<div class="max-w-xs space-y-1">
<label for="billing-period" class="block text-sm font-semibold">{{ __('billing.export.period') }}</label>
<input
id="billing-period"
type="month"
value="{{ $export->period }}"
wire:change="savePeriod($event.target.value)"
aria-describedby="billing-period-help @error('period') billing-period-error @enderror"
@error('period') aria-invalid="true" @enderror
class="w-full rounded-md border border-zinc-400 bg-white px-2 py-1.5 text-zinc-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 aria-invalid:border-red-700 dark:border-zinc-500 dark:bg-zinc-900 dark:text-zinc-100 dark:aria-invalid:border-red-400 dark:[color-scheme:dark]"
/>
<p id="billing-period-help" class="text-xs text-zinc-600 dark:text-zinc-300">{{ __('billing.export.period_help') }}</p>
@error('period')
<p id="billing-period-error" class="text-xs text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
</div>
@if ($export->hasProblems())
<p class="flex items-start gap-2 rounded-lg border border-amber-700 p-4 text-sm dark:border-amber-400">
<x-icon name="exclamation-triangle" class="size-5 shrink-0" aria-hidden="true" />
@@ -352,7 +392,7 @@ new class extends Component
</p>
@endif
@if ($export->isDraft() && $job->unapprovedQuarters > 0)
@if ($export->isDraft() && $job->unapprovedQuarters > 0 && $job->mode !== BillingMode::MonthlyFee)
<p class="flex items-start gap-2 text-sm">
<x-icon name="exclamation-triangle" class="size-5 shrink-0" aria-hidden="true" />
<span>
@@ -363,7 +403,34 @@ new class extends Component
@endif
@if ($export->isDraft())
@if ($job->mode === BillingMode::FixedPrice)
@if ($job->mode === BillingMode::MonthlyFee)
@php($months = $this->linesOfKind($job, LineKind::MonthlyFee))
@if ($months === [])
<p class="text-sm">{{ __('billing.jobs.no_months') }}</p>
@else
<table class="w-full text-sm">
<caption class="mb-1 text-left font-medium">{{ __('billing.jobs.months_caption', ['job' => $job->label()]) }}</caption>
<thead>
<tr class="border-b border-zinc-300 text-left dark:border-zinc-600">
<th scope="col" class="py-2 pr-3">{{ __('billing.jobs.month') }}</th>
<th scope="col" class="py-2 text-right">{{ __('billing.jobs.amount') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($months as $line)
<tr wire:key="month-{{ $job->jobId }}-{{ $line->period }}">
<td class="py-2 pr-3">{{ BillingMonths::label((string) $line->period) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->money($line->netCents) }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
@php($approved = array_sum(array_map(fn ($row) => $row->approvedQuarters, $job->hours)))
@if ($approved > 0)
<p class="text-sm">{{ __('billing.jobs.monthly_hours', ['hours' => $this->hours($approved)]) }}</p>
@endif
@elseif ($job->mode === BillingMode::FixedPrice)
@php($fixed = $this->linesOfKind($job, LineKind::FixedPrice))
<p class="text-sm">
{{ $fixed === [] ? __('billing.jobs.fixed_price_billed') : __('billing.jobs.fixed_price', ['amount' => $this->money($fixed[0]->netCents)]) }}