*/ public array $moveTargets = []; public function mount(int $export): void { $this->authorize('billing.manage'); $this->exportId = $export; if (app(ExportViews::class)->of($export) === null) { abort(404); } } #[Computed] public function export(): ExportView { return app(ExportViews::class)->of($this->exportId) ?? abort(404); } public function add(int $jobId, AddJobToDraft $addJobToDraft): void { $label = $this->availableLabel($jobId); $this->attempt(fn () => $addJobToDraft->execute($this->exportId, $jobId), __('billing.available.added', ['job' => $label]), $label); } public function remove(int $jobId, RemoveJobFromDraft $removeJobFromDraft): void { $label = $this->jobLabel($jobId); $this->attempt(fn () => $removeJobFromDraft->execute($this->exportId, $jobId), __('billing.jobs.removed', ['job' => $label]), $label); } public function move(int $jobId, MoveJobToInvoice $moveJobToInvoice): void { $label = $this->jobLabel($jobId); $target = (int) ($this->moveTargets[$jobId] ?? 0); if ($target === 0) { return; } $this->attempt(fn () => $moveJobToInvoice->execute($this->exportId, $jobId, $target), __('billing.jobs.moved', ['job' => $label]), $label); unset($this->moveTargets[$jobId]); } public function separate(int $jobId, SeparateJob $separateJob): void { $label = $this->jobLabel($jobId); $this->attempt(fn () => $separateJob->execute($this->exportId, $jobId), __('billing.jobs.separated', ['job' => $label]), $label); } public function saveHours(int $jobId, int $workTypeId, string $value, SetBillableHours $setBillableHours): void { $this->authorize('billing.manage'); $field = 'hours.'.$jobId.'.'.$workTypeId; $this->resetErrorBag($field); $label = $this->jobLabel($jobId); $workType = $this->workTypeName($jobId, $workTypeId); try { $setBillableHours->execute($this->exportId, $jobId, $workTypeId, mb_substr($value, 0, 20)); $this->statusMessage = __('billing.jobs.hours_saved', ['job' => $label, 'work_type' => $workType]); $this->errorMessage = ''; } catch (InvalidBillableHours) { $this->addError($field, __('billing.errors.hours')); } catch (ExportNotDraft|BillingExportNotFound|JobNotInDraft $exception) { $this->errorMessage = $this->errorText($exception, $label); } 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'); $this->js("document.getElementById('finalize-dialog').showModal()"); } public function finalize(FinalizeBillingExport $finalizeBillingExport): void { $this->authorize('billing.manage'); $this->statusMessage = ''; $this->errorMessage = ''; try { $invoices = $finalizeBillingExport->execute($this->exportId, (int) auth()->id()); $this->statusMessage = __('billing.export.finalized', ['invoices' => $invoices]); } catch (ExportNotDraft|BillingExportNotFound|ExportEmpty|JobNotBillable $exception) { $this->errorMessage = $this->errorText($exception, $exception instanceof JobNotBillable ? $this->jobLabel($exception->jobId) : ''); } unset($this->export); $this->js("document.getElementById('finalize-dialog').close(); document.getElementById('billing-status').focus()"); } public function confirmDiscard(): void { $this->authorize('billing.manage'); $this->js("document.getElementById('discard-dialog').showModal()"); } public function discard(DiscardDraft $discardDraft): void { $this->authorize('billing.manage'); try { $discardDraft->execute($this->exportId); session()->flash('billing-status', __('billing.export.discarded')); $this->redirectRoute('billing.index', navigate: true); } catch (ExportNotDraft|BillingExportNotFound $exception) { $this->errorMessage = $this->errorText($exception, ''); unset($this->export); $this->js("document.getElementById('discard-dialog').close(); document.getElementById('billing-error').focus()"); } } /** * @return list */ public function linesOfKind(InvoiceJobView $job, LineKind $kind): array { return array_values(array_filter($job->lines, fn (InvoiceLine $line): bool => $line->kind === $kind)); } /** * @return list */ public function availableHeaders(): array { return [ ['index' => 'job', 'label' => __('billing.available.columns.job'), 'sortable' => false], ['index' => 'customer', 'label' => __('billing.available.columns.customer'), 'sortable' => false], ['index' => 'mode', 'label' => __('billing.available.columns.mode'), 'sortable' => false], ['index' => 'hours', 'label' => __('billing.available.columns.hours'), 'sortable' => false, 'align' => 'right'], ['index' => 'expenses', 'label' => __('billing.available.columns.expenses'), 'sortable' => false, 'align' => 'right'], ['index' => 'actions', 'label' => __('billing.available.columns.actions'), 'sortable' => false], ]; } /** * A draft has no number yet: it is reserved when the material is created. */ public function heading(): string { $number = $this->export->number; return $number === null ? __('billing.export.draft_title') : __('billing.export.title', ['number' => $number]); } public function render(): mixed { return $this->view()->title($this->heading()); } /** * Runs a change of the draft and reports it in the status or error area. */ private function attempt(callable $change, string $success, string $jobLabel): void { $this->authorize('billing.manage'); $this->statusMessage = ''; $this->errorMessage = ''; try { $change(); $this->statusMessage = $success; } catch (BillingExportNotFound|ExportNotDraft|JobAlreadyInDraft|JobNotInDraft|JobNotBillable|InvoicesCannotMerge $exception) { $this->errorMessage = $this->errorText($exception, $jobLabel); } unset($this->export); } private function errorText(\Throwable $exception, string $jobLabel): string { return match (true) { $exception instanceof JobNotBillable => __('billing.errors.not_billable', ['job' => $jobLabel, 'reason' => $this->reasonText($exception->reason, $this->missingRates($exception->jobId))]), $exception instanceof InvoicesCannotMerge => __('billing.merge_refusals.'.$exception->refusal->value), $exception instanceof ExportNotDraft => __('billing.errors.not_draft'), $exception instanceof ExportEmpty => __('billing.errors.empty'), $exception instanceof JobAlreadyInDraft => __('billing.errors.already_in_draft'), $exception instanceof JobNotInDraft => __('billing.errors.not_in_draft'), default => __('billing.errors.not_found'), }; } /** * @return list */ private function missingRates(int $jobId): array { foreach ($this->export->availableJobs as $available) { if ($available->job->id === $jobId) { return $available->missingRateNames; } } return $this->findJob($jobId)->missingRateNames ?? []; } private function availableLabel(int $jobId): string { foreach ($this->export->availableJobs as $available) { if ($available->job->id === $jobId) { return $available->job->label(); } } return (string) $jobId; } private function jobLabel(int $jobId): string { return $this->findJob($jobId)?->label() ?? $this->availableLabel($jobId); } private function workTypeName(int $jobId, int $workTypeId): string { foreach ($this->findJob($jobId)->hours ?? [] as $row) { if ($row->workTypeId === $workTypeId) { return $row->name; } } return ''; } private function findJob(int $jobId): ?InvoiceJobView { foreach ($this->export->invoices as $invoice) { foreach ($invoice->jobs as $job) { if ($job->jobId === $jobId) { return $job; } } } return null; } }; ?>
@php($export = $this->export)

{{ __('billing.export.back') }}

{{ $this->heading() }}

@if ($export->isDraft())

{{ __('billing.export.summary', ['invoices' => count($export->invoices), 'jobs' => $export->jobCount(), 'net' => $this->money($export->totals->netCents)]) }}

@if (! $export->isDraft())

{{ __('billing.csv.sensitive') }}

@endif

{{ $statusMessage }}

@if ($export->isDraft())

{{ __('billing.export.period_help') }}

@error('period')

{{ $message }}

@enderror
@if ($export->hasProblems())

@endif
@if ($export->invoices !== []) @endif
@endif

{{ __('billing.invoices.title') }}

@if ($export->invoices === []) {{ __('billing.invoices.none') }} @endif @foreach ($export->invoices as $invoiceNumber => $invoice)

{{ __('billing.invoices.heading', ['number' => $invoiceNumber + 1, 'customer' => $invoice->customerName]) }}

{{ __('billing.invoices.billing_unit') }}
{{ $invoice->billingUnitName ?? __('billing.invoices.no_billing_unit') }}
{{ __('billing.invoices.reference') }}
{{ $invoice->reference ?? __('billing.invoices.no_reference') }}
@foreach ($invoice->jobs as $job)

{{ $job->label() }}

{{ __('billing.jobs.mode', ['mode' => $this->modeLabel($job->mode)]) }}

@if ($job->problem !== null)

@endif @if ($export->isDraft() && $job->unapprovedQuarters > 0 && $job->mode !== BillingMode::MonthlyFee)

@endif @if ($export->isDraft()) @if ($job->mode === BillingMode::MonthlyFee) @php($months = $this->linesOfKind($job, LineKind::MonthlyFee)) @if ($months === [])

{{ __('billing.jobs.no_months') }}

@else @foreach ($months as $line) @endforeach
{{ __('billing.jobs.months_caption', ['job' => $job->label()]) }}
{{ __('billing.jobs.month') }} {{ __('billing.jobs.amount') }}
{{ BillingMonths::label((string) $line->period) }} {{ $this->money($line->netCents) }}
@endif @php($approved = array_sum(array_map(fn ($row) => $row->approvedQuarters, $job->hours))) @if ($approved > 0)

{{ __('billing.jobs.monthly_hours', ['hours' => $this->hours($approved)]) }}

@endif @elseif ($job->mode === BillingMode::FixedPrice) @php($fixed = $this->linesOfKind($job, LineKind::FixedPrice))

{{ $fixed === [] ? __('billing.jobs.fixed_price_billed') : __('billing.jobs.fixed_price', ['amount' => $this->money($fixed[0]->netCents)]) }}

@php($approved = array_sum(array_map(fn ($row) => $row->approvedQuarters, $job->hours))) @if ($approved > 0)

{{ __('billing.jobs.fixed_hours', ['hours' => $this->hours($approved)]) }}

@endif @elseif ($job->hours === [])

{{ __('billing.jobs.no_hours') }}

@else
@foreach ($job->hours as $row) @php($fieldId = 'hours-'.$job->jobId.'-'.$row->workTypeId) @endforeach
{{ __('billing.jobs.hours_caption', ['job' => $job->label()]) }}
{{ __('billing.jobs.work_type') }} {{ __('billing.jobs.approved_hours') }} {{ __('billing.jobs.billable_hours') }} {{ __('billing.jobs.rate') }} {{ __('billing.jobs.net') }}
{{ $row->name }} {{ $this->hours($row->approvedQuarters) }} jobId.'.'.$row->workTypeId) aria-invalid="true" aria-errormessage="{{ $fieldId }}-error" @enderror class="w-20 rounded-md border border-zinc-400 bg-white px-2 py-1.5 text-right text-zinc-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 dark:focus-visible:outline-primary-300 aria-invalid:border-red-700 dark:border-zinc-500 dark:bg-zinc-900 dark:text-zinc-100 dark:aria-invalid:border-red-400" /> @if ($row->isAdjusted) ({{ __('billing.jobs.adjusted') }}) @endif @error('hours.'.$job->jobId.'.'.$row->workTypeId) {{ $message }} @enderror {{ $row->rateCents === null ? __('billing.jobs.no_rate') : $this->money($row->rateCents) }} {{ $this->money($row->netCents) }}

{{ __('billing.jobs.billable_help') }}

@endif @php($expenseLines = $this->linesOfKind($job, LineKind::Expense)) @if ($expenseLines !== []) @foreach ($expenseLines as $line) @endforeach
{{ __('billing.jobs.expenses_caption', ['job' => $job->label()]) }}
{{ __('billing.jobs.expense') }} {{ __('billing.jobs.amount') }}
{{ $line->description }} {{ $this->money($line->netCents) }}
@endif
@if (($invoice->mergeTargets[$job->jobId] ?? []) !== [])
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach ($invoice->mergeTargets[$job->jobId] as $target) @endforeach
@endif @if (count($invoice->jobs) > 1) @endif
@else
@foreach ($job->lines as $index => $line) @endforeach
{{ __('billing.lines.caption', ['job' => $job->label()]) }}
{{ __('billing.lines.description') }} {{ __('billing.lines.recorded') }} {{ __('billing.lines.quantity') }} {{ __('billing.lines.unit_price') }} {{ __('billing.lines.net') }}
{{ __('billing.kinds.'.$line->kind->value) }} {{ $line->description }} {{ $line->recordedQuarters === null ? '' : $this->hours($line->recordedQuarters) }} {{ $line->quantityQuarters === null ? '' : $this->hours($line->quantityQuarters) }} {{ $this->money($line->unitPriceCents) }} {{ $this->money($line->netCents) }}
@endif
@endforeach @foreach ($invoice->totals->byVatRate as $basisPoints => $amounts) @endforeach
{{ __('billing.invoices.totals') }}
{{ __('billing.invoices.net') }} {{ $this->money($invoice->totals->netCents) }}
{{ __('billing.invoices.vat', ['rate' => $this->vatRate($basisPoints)]) }} {{ $this->money($amounts['vat']) }}
{{ __('billing.invoices.gross') }} {{ $this->money($invoice->totals->grossCents()) }}
@endforeach @if (count($export->invoices) > 1)
{{ __('billing.invoices.export_totals') }}
{{ __('billing.invoices.net') }} {{ $this->money($export->totals->netCents) }}
{{ __('billing.invoices.gross') }} {{ $this->money($export->totals->grossCents()) }}
@endif
@if ($export->isDraft())

{{ __('billing.available.title') }}

{{ __('billing.available.intro') }}

@if ($export->availableJobs === []) {{ __('billing.available.none') }} @else @interact('column_job', $available) {{ $available->job->label() }} @endinteract @interact('column_customer', $available) {{ $available->job->customerName }} @endinteract @interact('column_mode', $available) {{ $this->modeLabel($available->job->mode) }} @endinteract @interact('column_hours', $available) {{ $this->hours($available->approvedQuarters) }} @if ($available->unapprovedQuarters > 0) {{ __('billing.available.unapproved', ['hours' => $this->hours($available->unapprovedQuarters)]) }} @endif @endinteract @interact('column_expenses', $available) {{ $available->expenseCount }} @endinteract @interact('column_actions', $available) @if ($available->reason === null) @else @endif @endinteract @endif

{{ __('billing.export.finalize_text', ['invoices' => count($export->invoices), 'net' => $this->money($export->totals->netCents)]) }}

{{ __('billing.export.discard_text') }}

@endif