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,244 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
|
||||
use App\Modules\Expenses\Application\DTOs\ExpenseJob;
|
||||
use App\Modules\Expenses\Application\DTOs\ExpenseSummary;
|
||||
use App\Modules\Expenses\Application\UseCases\RemoveExpense;
|
||||
use App\Modules\Expenses\Domain\Exceptions\ExpenseLocked;
|
||||
use App\Modules\Expenses\Domain\Exceptions\ExpenseNotFound;
|
||||
use App\Modules\Expenses\Presentation\Livewire\Concerns\ShowsExpenses;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* The expenses of one customer job. Amounts of others' expenses are shown
|
||||
* only with the expenses permission; everyone sees their own.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
use ShowsExpenses;
|
||||
|
||||
#[Locked]
|
||||
public int $jobId;
|
||||
|
||||
#[Locked]
|
||||
public ?int $removalExpenseId = null;
|
||||
|
||||
public string $statusMessage = '';
|
||||
|
||||
public function mount(ExpenseJobs $jobs, int $job): void
|
||||
{
|
||||
$this->authorize('expenses.record');
|
||||
$this->authorize('jobs.view-job', $job);
|
||||
|
||||
abort_if($jobs->find($job) === null, 404);
|
||||
|
||||
$this->jobId = $job;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function job(): ExpenseJob
|
||||
{
|
||||
return app(ExpenseJobs::class)->find($this->jobId) ?? abort(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<ExpenseSummary>
|
||||
*/
|
||||
#[Computed]
|
||||
public function expenses(): array
|
||||
{
|
||||
return app(ExpenseQueries::class)->forJob($this->jobId);
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function canSeeTotals(): bool
|
||||
{
|
||||
return auth()->user()?->can('money.expenses') ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{cost: int, billed: int}
|
||||
*/
|
||||
#[Computed]
|
||||
public function totals(): array
|
||||
{
|
||||
return [
|
||||
'cost' => array_sum(array_map(fn (ExpenseSummary $expense): int => $expense->costCents, $this->expenses)),
|
||||
'billed' => array_sum(array_map(fn (ExpenseSummary $expense): int => $expense->billedAmountCents ?? 0, $this->expenses)),
|
||||
];
|
||||
}
|
||||
|
||||
public function confirmRemoval(int $expenseId): void
|
||||
{
|
||||
$expense = $this->expense($expenseId);
|
||||
|
||||
if ($expense === null || ! $this->canChange($expense)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->removalExpenseId = $expenseId;
|
||||
|
||||
$this->js("document.getElementById('remove-expense-dialog').showModal()");
|
||||
}
|
||||
|
||||
public function removeExpense(RemoveExpense $removeExpense): void
|
||||
{
|
||||
$expense = $this->expense((int) $this->removalExpenseId);
|
||||
|
||||
if ($expense !== null) {
|
||||
$this->authorize('expenses.change', [$expense->userId, $expense->recordedBy]);
|
||||
|
||||
try {
|
||||
$removeExpense->execute($expense->id);
|
||||
$this->statusMessage = __('expenses.removed', ['expense' => $this->summaryText($expense)]);
|
||||
} catch (ExpenseLocked) {
|
||||
$this->statusMessage = __('expenses.errors.locked');
|
||||
} catch (ExpenseNotFound) {
|
||||
$this->statusMessage = __('expenses.errors.not_found');
|
||||
}
|
||||
}
|
||||
|
||||
$this->reset('removalExpenseId');
|
||||
unset($this->expenses, $this->totals);
|
||||
|
||||
$this->js("document.getElementById('remove-expense-dialog').close(); document.getElementById('expenses-heading').focus()");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
|
||||
*/
|
||||
public function headers(): array
|
||||
{
|
||||
return [
|
||||
['index' => 'spentOn', 'label' => __('expenses.fields.spent_on'), 'sortable' => false],
|
||||
['index' => 'what', 'label' => __('expenses.fields.what'), 'sortable' => false],
|
||||
['index' => 'user', 'label' => __('expenses.fields.user'), 'sortable' => false],
|
||||
['index' => 'cost', 'label' => __('expenses.fields.cost_column'), 'sortable' => false, 'align' => 'right'],
|
||||
['index' => 'billed', 'label' => __('expenses.fields.billed_column'), 'sortable' => false, 'align' => 'right'],
|
||||
['index' => 'actions', 'label' => __('expenses.actions'), 'sortable' => false],
|
||||
];
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('expenses.job_page.title', ['job' => $this->job->label()]));
|
||||
}
|
||||
|
||||
private function expense(int $expenseId): ?ExpenseSummary
|
||||
{
|
||||
return collect($this->expenses)->firstWhere('id', $expenseId);
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
@php($job = $this->job)
|
||||
|
||||
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
|
||||
<div class="space-y-2">
|
||||
<h1 class="text-2xl font-semibold break-words">{{ __('expenses.job_page.title', ['job' => $job->label()]) }}</h1>
|
||||
<x-link :href="route('jobs.show', $job->id)" navigate class="text-sm">{{ __('expenses.job_page.back_to_job') }}</x-link>
|
||||
</div>
|
||||
|
||||
<p role="status" aria-live="polite" class="min-h-5 text-sm">{{ $statusMessage }}</p>
|
||||
|
||||
@if (session('status'))
|
||||
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
|
||||
@endif
|
||||
|
||||
@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
|
||||
<x-button :href="route('expenses.create', $job->id)" wire:navigate icon="plus" :text="__('expenses.add')" />
|
||||
@endif
|
||||
|
||||
<section aria-labelledby="expenses-heading" class="space-y-3">
|
||||
<h2 id="expenses-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">{{ __('expenses.job_page.list') }}</h2>
|
||||
|
||||
@unless ($this->canSeeTotals)
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('expenses.amounts_hidden_help') }}</p>
|
||||
@endunless
|
||||
|
||||
@if ($this->expenses === [])
|
||||
<p class="text-sm">{{ __('expenses.none') }}</p>
|
||||
@else
|
||||
<x-table :headers="$this->headers()" :rows="$this->expenses">
|
||||
@interact('column_spentOn', $expense)
|
||||
<span class="whitespace-nowrap">{{ $this->dayLabel($expense->spentOn) }}</span>
|
||||
@endinteract
|
||||
|
||||
@interact('column_what', $expense)
|
||||
<span class="block font-medium break-words whitespace-normal">{{ $this->summaryText($expense) }}</span>
|
||||
<span class="block text-xs text-zinc-600 dark:text-zinc-300">
|
||||
{{ __('expenses.categories.'.$expense->category->value) }}, {{ __('expenses.payers.'.$expense->paidBy->value) }}
|
||||
</span>
|
||||
@endinteract
|
||||
|
||||
@interact('column_user', $expense)
|
||||
{{ $expense->userName }}
|
||||
@endinteract
|
||||
|
||||
@interact('column_cost', $expense)
|
||||
<span class="whitespace-nowrap">{{ $this->amount($expense, $expense->costCents) }}</span>
|
||||
@endinteract
|
||||
|
||||
@interact('column_billed', $expense)
|
||||
<span class="whitespace-nowrap">{{ $this->amount($expense, $expense->billedAmountCents) }}</span>
|
||||
@endinteract
|
||||
|
||||
@interact('column_actions', $expense)
|
||||
@if ($this->canChange($expense))
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<x-button
|
||||
:href="route('expenses.edit', $expense->id)"
|
||||
wire:navigate
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="pencil"
|
||||
:text="__('expenses.edit')"
|
||||
:aria-label="__('expenses.edit_named', ['expense' => $this->summaryText($expense), 'day' => $this->dayLabel($expense->spentOn)])"
|
||||
/>
|
||||
<x-button
|
||||
wire:click="confirmRemoval({{ $expense->id }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="trash"
|
||||
:text="__('expenses.remove')"
|
||||
:aria-label="__('expenses.remove_named', ['expense' => $this->summaryText($expense), 'day' => $this->dayLabel($expense->spentOn)])"
|
||||
/>
|
||||
</div>
|
||||
@endif
|
||||
@endinteract
|
||||
</x-table>
|
||||
|
||||
@if ($this->canSeeTotals)
|
||||
<dl class="grid gap-x-6 gap-y-1 text-sm sm:grid-cols-[max-content_1fr]">
|
||||
<dt class="font-medium">{{ __('expenses.totals.cost') }}</dt>
|
||||
<dd>{{ $this->money($this->totals['cost']) }}</dd>
|
||||
<dt class="font-medium">{{ __('expenses.totals.billed') }}</dt>
|
||||
<dd>{{ $this->money($this->totals['billed']) }}</dd>
|
||||
</dl>
|
||||
@endif
|
||||
@endif
|
||||
</section>
|
||||
|
||||
<x-confirm-dialog id="remove-expense-dialog" :title="__('expenses.removal_title')">
|
||||
<p>
|
||||
@php($removal = collect($this->expenses)->firstWhere('id', $removalExpenseId))
|
||||
@if ($removal !== null)
|
||||
{{ __('expenses.remove_question', ['expense' => $this->summaryText($removal), 'day' => $this->dayLabel($removal->spentOn)]) }}
|
||||
@endif
|
||||
</p>
|
||||
|
||||
<x-slot:actions>
|
||||
<x-button wire:click="removeExpense" color="red" icon="trash" :text="__('expenses.remove')" />
|
||||
</x-slot:actions>
|
||||
</x-confirm-dialog>
|
||||
</div>
|
||||
Reference in New Issue
Block a user