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,170 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
|
||||
use App\Modules\Expenses\Application\DTOs\ExpenseSummary;
|
||||
use App\Modules\Expenses\Presentation\Livewire\Concerns\ShowsExpenses;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* The signed-in user's own expenses month by month, with their amounts.
|
||||
* Subcontractors see the expenses management recorded for them.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
use ShowsExpenses;
|
||||
|
||||
/**
|
||||
* The month shown (Y-m). Read from the URL on mount and then changed
|
||||
* only by showMonth(), so the browser cannot set it.
|
||||
*/
|
||||
#[Locked]
|
||||
#[Url(as: 'kuukausi', except: '')]
|
||||
public string $month = '';
|
||||
|
||||
public function mount(Clock $clock): void
|
||||
{
|
||||
$this->authorize('expenses.use');
|
||||
|
||||
$this->month = $this->monthStart($this->month)?->format('Y-m') ?? $clock->today()->format('Y-m');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<ExpenseSummary>
|
||||
*/
|
||||
#[Computed]
|
||||
public function expenses(): array
|
||||
{
|
||||
return app(ExpenseQueries::class)->forUserInMonth((int) auth()->id(), $this->month);
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function total(): int
|
||||
{
|
||||
return array_sum(array_map(fn (ExpenseSummary $expense): int => $expense->costCents, $this->expenses));
|
||||
}
|
||||
|
||||
public function showMonth(string $which, Clock $clock): void
|
||||
{
|
||||
$current = $this->monthStart($this->month) ?? CarbonImmutable::instance($clock->today());
|
||||
|
||||
$this->month = match ($which) {
|
||||
'previous' => $current->subMonthNoOverflow()->format('Y-m'),
|
||||
'next' => $current->addMonthNoOverflow()->format('Y-m'),
|
||||
default => $clock->today()->format('Y-m'),
|
||||
};
|
||||
|
||||
unset($this->expenses, $this->total);
|
||||
}
|
||||
|
||||
public function monthLabel(): string
|
||||
{
|
||||
$date = $this->monthStart($this->month);
|
||||
|
||||
return $date === null ? $this->month : __('expenses.months')[$date->month - 1].' '.$date->year;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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' => 'job', 'label' => __('expenses.fields.job'), 'sortable' => false],
|
||||
['index' => 'what', 'label' => __('expenses.fields.what'), 'sortable' => false],
|
||||
['index' => 'cost', 'label' => __('expenses.fields.cost_column'), 'sortable' => false, 'align' => 'right'],
|
||||
['index' => 'actions', 'label' => __('expenses.actions'), 'sortable' => false],
|
||||
];
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('expenses.mine.title'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The first day of a Y-m month, or null when the value is not one.
|
||||
*/
|
||||
private function monthStart(string $month): ?CarbonImmutable
|
||||
{
|
||||
return preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) === 1
|
||||
? CarbonImmutable::createFromFormat('!Y-m', $month)
|
||||
: null;
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<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">{{ __('expenses.mine.title') }}</h1>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">
|
||||
{{ __(auth()->user()?->can('expenses.record') ? 'expenses.mine.help' : 'expenses.mine.help_subcontractor') }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<nav aria-label="{{ __('expenses.mine.month_navigation') }}" class="flex flex-wrap items-center gap-2">
|
||||
<x-button wire:click="showMonth('previous')" subtle color="black" sm icon="chevron-left" :text="__('expenses.mine.previous')" />
|
||||
<x-button wire:click="showMonth('current')" subtle color="black" sm :text="__('expenses.mine.current')" />
|
||||
<x-button wire:click="showMonth('next')" subtle color="black" sm icon="chevron-right" position="right" :text="__('expenses.mine.next')" />
|
||||
</nav>
|
||||
|
||||
<section aria-labelledby="month-heading" class="space-y-3">
|
||||
<h2 id="month-heading" class="text-xl font-semibold" aria-live="polite">{{ $this->monthLabel() }}</h2>
|
||||
|
||||
@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_job', $expense)
|
||||
@can('expenses.record')
|
||||
<x-link :href="route('expenses.job', $expense->job->id)" navigate class="whitespace-normal">{{ $expense->job->label() }}</x-link>
|
||||
@else
|
||||
<span class="whitespace-normal">{{ $expense->job->label() }}</span>
|
||||
@endcan
|
||||
@if ($expense->job->isLocked)
|
||||
<span class="mt-1 flex items-center gap-1 text-xs">
|
||||
<x-icon name="lock-closed" class="size-4" aria-hidden="true" /> {{ __('expenses.locked_short') }}
|
||||
</span>
|
||||
@endif
|
||||
@endinteract
|
||||
|
||||
@interact('column_what', $expense)
|
||||
<span class="block 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_cost', $expense)
|
||||
<span class="whitespace-nowrap">{{ $this->amount($expense, $expense->costCents) }}</span>
|
||||
@endinteract
|
||||
|
||||
@interact('column_actions', $expense)
|
||||
@if ($this->canChange($expense))
|
||||
<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)])"
|
||||
/>
|
||||
@endif
|
||||
@endinteract
|
||||
</x-table>
|
||||
|
||||
<p class="text-sm font-medium">{{ __('expenses.mine.total', ['total' => $this->money($this->total)]) }}</p>
|
||||
@endif
|
||||
</section>
|
||||
</div>
|
||||
Reference in New Issue
Block a user