Add expenses on customer jobs
- New Expenses module: purchases (materials, other) with a description and cost, and mileage priced with the price list's mileage rate. - Who paid and whether the expense is billed are separate: an employee's own payments are reimbursed elsewhere. A billed expense has a markup and a billed amount, cost plus markup unless given. - A job's expenses page lists, adds, edits and removes them; amounts of others' expenses need the expenses permission, own ones always show. Done jobs lock their expenses. - The Kulut menu shows the user's own expenses by month. Management records subcontractors' expenses, which they see but cannot change. - The price list gets a default markup for new purchases. - Number fields that update a calculation now use wire:model.live.blur: in Livewire 4 plain .blur no longer sends the value to the server, which also fixes the product form's VAT calculation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,310 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpensePricing;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
|
||||
use App\Modules\Expenses\Application\DTOs\ExpenseJob;
|
||||
use App\Modules\Expenses\Application\UseCases\RecordExpense;
|
||||
use App\Modules\Expenses\Application\UseCases\UpdateExpense;
|
||||
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
|
||||
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
|
||||
use App\Modules\Expenses\Domain\Exceptions\ExpenseJobNotFound;
|
||||
use App\Modules\Expenses\Domain\Exceptions\ExpenseLocked;
|
||||
use App\Modules\Expenses\Domain\Exceptions\ExpenseNotFound;
|
||||
use App\Modules\Expenses\Domain\Exceptions\InvalidKilometers;
|
||||
use App\Modules\Expenses\Domain\Exceptions\InvalidMarkup;
|
||||
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
|
||||
use App\Modules\Expenses\Domain\ValueObjects\MarkupPercentage;
|
||||
use App\Modules\Expenses\Presentation\Livewire\Forms\ExpenseForm;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use App\Modules\Users\Application\DTOs\UserOption;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Add page (with {job}) and edit page (with {expense}) of an expense. The
|
||||
* fields follow the kind: a purchase has a description and cost, mileage
|
||||
* has kilometres, destination and purpose. A billable expense gets a markup
|
||||
* or a billed amount of its own.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $jobId;
|
||||
|
||||
#[Locked]
|
||||
public ?int $expenseId = null;
|
||||
|
||||
public ExpenseForm $form;
|
||||
|
||||
public function mount(ExpenseJobs $jobs, ExpenseQueries $expenses, ExpensePricing $pricing, Clock $clock, ?int $job = null, ?int $expense = null): void
|
||||
{
|
||||
$this->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<UserOption>
|
||||
*/
|
||||
#[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)
|
||||
|
||||
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
|
||||
<div class="space-y-1">
|
||||
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('expenses.fields.job') }}: {{ $job->label() }}</p>
|
||||
</div>
|
||||
|
||||
@if ($job->isLocked)
|
||||
<p class="flex items-start gap-2 rounded-lg border border-zinc-300 p-3 text-sm dark:border-zinc-600">
|
||||
<x-icon name="lock-closed" class="size-5 shrink-0" aria-hidden="true" />
|
||||
{{ __('expenses.locked') }}
|
||||
</p>
|
||||
@else
|
||||
<form wire:submit="save" class="space-y-6" novalidate>
|
||||
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||
<x-select.native wire:model.live="form.category" :label="__('expenses.fields.category')" :aria-label="__('expenses.fields.category')">
|
||||
@foreach (ExpenseCategory::cases() as $category)
|
||||
<option wire:key="category-{{ $category->value }}" value="{{ $category->value }}">{{ __('expenses.categories.'.$category->value) }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
|
||||
<div class="grid gap-6 sm:grid-cols-2">
|
||||
<x-input wire:model="form.spent_on" type="date" :label="__('expenses.fields.spent_on')" required />
|
||||
|
||||
<x-select.native wire:model="form.paid_by" :label="__('expenses.fields.paid_by')" :aria-label="__('expenses.fields.paid_by')">
|
||||
@foreach (ExpensePayer::cases() as $payer)
|
||||
<option wire:key="payer-{{ $payer->value }}" value="{{ $payer->value }}">{{ __('expenses.payers.'.$payer->value) }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
</div>
|
||||
|
||||
@if ($this->canRecordForOthers)
|
||||
<x-select.native
|
||||
wire:model="form.user_id"
|
||||
:label="__('expenses.fields.user')"
|
||||
:aria-label="__('expenses.fields.user')"
|
||||
:hint="__('expenses.form.user_help')"
|
||||
>
|
||||
@foreach ($this->userOptions as $option)
|
||||
<option wire:key="user-{{ $option->id }}" value="{{ $option->id }}">{{ $option->name }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
@endif
|
||||
|
||||
@if ($form->isMileage())
|
||||
<div class="space-y-1">
|
||||
<x-input wire:model.live.blur="form.kilometers" inputmode="decimal" :label="__('expenses.fields.kilometers')" :hint="__('expenses.form.kilometers_help')" autocomplete="off" required />
|
||||
<p aria-live="polite" class="min-h-5 text-sm">{{ $this->mileageCost() }}</p>
|
||||
</div>
|
||||
<x-input wire:model="form.destination" :label="__('expenses.fields.destination')" autocomplete="off" required />
|
||||
<x-input wire:model="form.purpose" :label="__('expenses.fields.purpose')" autocomplete="off" required />
|
||||
<x-input wire:model="form.description" :label="__('expenses.fields.note')" autocomplete="off" />
|
||||
@else
|
||||
<x-input wire:model="form.description" :label="__('expenses.fields.description')" :hint="__('expenses.form.description_help')" autocomplete="off" required />
|
||||
<x-input wire:model.live.blur="form.cost" inputmode="decimal" :label="__('expenses.fields.cost')" :hint="__('expenses.form.cost_help')" autocomplete="off" required />
|
||||
@endif
|
||||
|
||||
<fieldset class="space-y-4 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700">
|
||||
<legend class="px-1 font-medium">{{ __('expenses.form.billing') }}</legend>
|
||||
|
||||
<x-checkbox id="expense-billable" wire:model.live="form.is_billable" :label="__('expenses.fields.is_billable')" />
|
||||
|
||||
@if ($form->is_billable)
|
||||
<div class="grid gap-6 sm:grid-cols-2">
|
||||
<x-input wire:model.live.blur="form.markup" inputmode="decimal" :label="__('expenses.fields.markup')" :hint="__('expenses.form.markup_help')" autocomplete="off" />
|
||||
<x-input wire:model.live.blur="form.billed_amount" inputmode="decimal" :label="__('expenses.fields.billed_amount')" :hint="__('expenses.form.billed_amount_help')" autocomplete="off" />
|
||||
</div>
|
||||
<p aria-live="polite" class="min-h-5 text-sm">{{ $form->billed_amount === '' ? $this->suggestedBilledAmount() : '' }}</p>
|
||||
@endif
|
||||
</fieldset>
|
||||
|
||||
<div class="flex flex-wrap gap-3">
|
||||
<x-button submit :text="__('expenses.save')" />
|
||||
<x-button
|
||||
:href="auth()->user()?->can('expenses.record') ? route('expenses.job', $job->id) : route('expenses.mine')"
|
||||
wire:navigate
|
||||
subtle
|
||||
color="black"
|
||||
:text="__('expenses.cancel')"
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
@endif
|
||||
</div>
|
||||
Reference in New Issue
Block a user