authorize('expenses.use'); if ($expense !== null) { $summary = $expenses->find($expense); abort_if($summary === null, 404); $this->authorize('expenses.change', [$summary->userId, $summary->recordedBy]); $this->expenseId = $summary->id; $this->jobId = $summary->job->id; $this->form->fillFrom($summary); return; } $this->authorize('expenses.record'); abort_if($job === null || $jobs->find($job) === null, 404); $this->authorize('jobs.view-job', $job); $this->jobId = $job; $this->form->user_id = (string) auth()->id(); $this->form->spent_on = $clock->today()->format('Y-m-d'); $this->form->markup = $this->defaultMarkup($pricing); } #[Computed] public function job(): ExpenseJob { return app(ExpenseJobs::class)->find($this->jobId) ?? abort(404); } #[Computed] public function canRecordForOthers(): bool { return auth()->user()?->can('expenses.record-for-others') ?? false; } /** * @return list */ #[Computed] public function userOptions(): array { return $this->canRecordForOthers ? app(UserAccountQueries::class)->activeUserOptions() : []; } /** * Kilometres get no markup by default; purchases get the default markup. */ public function updatedFormCategory(): void { if ($this->form->isMileage()) { $this->form->markup = ''; } elseif ($this->form->markup === '') { $this->form->markup = $this->defaultMarkup(app(ExpensePricing::class)); } $this->resetErrorBag(); } /** * The cost of the kilometres typed, e.g. "40 km × 0,59 € = 23,60 €"; empty while it cannot be worked out. */ public function mileageCost(): string { $rate = app(ExpensePricing::class)->mileageRateCents(); if ($rate === null) { return __('expenses.errors.mileage_rate_missing'); } try { $kilometers = Kilometers::parse($this->form->kilometers); } catch (InvalidKilometers) { return ''; } if ($kilometers === null) { return __('expenses.form.mileage_rate', ['rate' => Money::fromCents($rate)->format()]); } return __('expenses.form.mileage_cost', [ 'km' => $kilometers->format(), 'rate' => Money::fromCents($rate)->format(), 'cost' => $kilometers->priceAt(Money::fromCents($rate))->format(), ]); } /** * The amount billed when the billed amount is left empty: cost plus markup. */ public function suggestedBilledAmount(): string { try { $cost = $this->form->isMileage() ? $this->mileageCostMoney() : Money::parse($this->form->cost); $markup = MarkupPercentage::parse($this->form->markup); } catch (InvalidMoneyAmount|InvalidKilometers|InvalidMarkup) { return ''; } if ($cost === null) { return ''; } return __('expenses.form.suggested_billed', ['amount' => $cost->addPercent($markup->basisPoints ?? 0)->format()]); } public function save(RecordExpense $recordExpense, UpdateExpense $updateExpense): void { $this->authorize('expenses.use'); if (! $this->canRecordForOthers) { $this->form->user_id = (string) ($this->expenseId === null ? auth()->id() : $this->form->user_id); } $data = $this->form->toData(); if ($this->canRecordForOthers && ! collect($this->userOptions)->contains('id', $data->userId)) { $this->form->addError('user_id', __('expenses.errors.user_required')); return; } try { if ($this->expenseId === null) { $this->authorize('expenses.record'); $recordExpense->execute($this->jobId, $data, (int) auth()->id()); session()->flash('status', __('expenses.saved')); } else { $stored = app(ExpenseQueries::class)->find($this->expenseId) ?? abort(404); $this->authorize('expenses.change', [$stored->userId, $stored->recordedBy]); $updateExpense->execute($this->expenseId, $data); session()->flash('status', __('expenses.updated')); } } catch (ExpenseLocked) { $this->addError('form.spent_on', __('expenses.errors.locked')); return; } catch (ExpenseNotFound|ExpenseJobNotFound) { abort(404); } catch (DomainException $exception) { $this->form->showDomainError($exception); return; } $this->redirectRoute(auth()->user()?->can('expenses.record') ? 'expenses.job' : 'expenses.mine', auth()->user()?->can('expenses.record') ? ['job' => $this->jobId] : [], navigate: true); } public function heading(): string { return __($this->expenseId === null ? 'expenses.form.create_title' : 'expenses.form.edit_title'); } public function render(): mixed { return $this->view()->title($this->heading().' – '.$this->job->label()); } private function mileageCostMoney(): ?Money { $rate = app(ExpensePricing::class)->mileageRateCents(); $kilometers = Kilometers::parse($this->form->kilometers); return $rate === null || $kilometers === null ? null : $kilometers->priceAt(Money::fromCents($rate)); } private function defaultMarkup(ExpensePricing $pricing): string { $basisPoints = $pricing->defaultMarkupBasisPoints(); return $basisPoints === 0 ? '' : (new MarkupPercentage($basisPoints))->inputValue(); } }; ?> @php($job = $this->job)

{{ $this->heading() }}

{{ __('expenses.fields.job') }}: {{ $job->label() }}

@if ($job->isLocked)

@else
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach (ExpenseCategory::cases() as $category) @endforeach
@foreach (ExpensePayer::cases() as $payer) @endforeach
@if ($this->canRecordForOthers) @foreach ($this->userOptions as $option) @endforeach @endif @if ($form->isMileage())

{{ $this->mileageCost() }}

@else @endif
{{ __('expenses.form.billing') }} @if ($form->is_billable)

{{ $form->billed_amount === '' ? $this->suggestedBilledAmount() : '' }}

@endif
@endif