Files
Toiminnanohjaus/app/Modules/Expenses/Presentation/Livewire/Forms/ExpenseForm.php
T
petriandClaude Opus 5.5 54d98d9d2f 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]>
2026-11-24 08:11:27 +02:00

187 lines
7.0 KiB
PHP

<?php
namespace App\Modules\Expenses\Presentation\Livewire\Forms;
use App\Modules\Expenses\Application\DTOs\ExpenseData;
use App\Modules\Expenses\Application\DTOs\ExpenseSummary;
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
use App\Modules\Expenses\Domain\Exceptions\ExpenseCostRequired;
use App\Modules\Expenses\Domain\Exceptions\ExpenseDescriptionRequired;
use App\Modules\Expenses\Domain\Exceptions\InvalidKilometers;
use App\Modules\Expenses\Domain\Exceptions\InvalidMarkup;
use App\Modules\Expenses\Domain\Exceptions\MileageDetailsRequired;
use App\Modules\Expenses\Domain\Exceptions\MileageRateMissing;
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
use App\Modules\Expenses\Domain\ValueObjects\MarkupPercentage;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
use Illuminate\Validation\Rule;
use Livewire\Form;
/**
* Fields of an expense as typed. Amounts, kilometres and markup are parsed
* by the use case; this form checks only their shape.
*/
class ExpenseForm extends Form
{
public string $user_id = '';
public string $category = 'materials';
public string $paid_by = 'company';
public string $spent_on = '';
public string $description = '';
public string $cost = '';
public string $kilometers = '';
public string $destination = '';
public string $purpose = '';
public bool $is_billable = true;
public string $markup = '';
public string $billed_amount = '';
public function isMileage(): bool
{
return $this->category === ExpenseCategory::Mileage->value;
}
public function fillFrom(ExpenseSummary $expense): void
{
$this->user_id = (string) $expense->userId;
$this->category = $expense->category->value;
$this->paid_by = $expense->paidBy->value;
$this->spent_on = $expense->spentOn;
$this->description = $expense->description ?? '';
$this->cost = $expense->category === ExpenseCategory::Mileage ? '' : Money::fromCents($expense->costCents)->inputValue();
$this->kilometers = $expense->kilometerTenths === null ? '' : (new Kilometers($expense->kilometerTenths))->format();
$this->destination = $expense->destination ?? '';
$this->purpose = $expense->purpose ?? '';
$this->is_billable = $expense->isBillable;
$this->markup = $expense->markupBasisPoints === null ? '' : (new MarkupPercentage($expense->markupBasisPoints))->inputValue();
$this->billed_amount = $expense->billedAmountCents === null ? '' : Money::fromCents($expense->billedAmountCents)->inputValue();
}
public function toData(): ExpenseData
{
$this->validate();
$mileage = $this->isMileage();
return new ExpenseData(
userId: (int) $this->user_id,
category: ExpenseCategory::from($this->category),
paidBy: ExpensePayer::from($this->paid_by),
spentOn: $this->spent_on,
description: $this->description,
cost: $mileage ? '' : $this->cost,
kilometers: $mileage ? $this->kilometers : '',
destination: $mileage ? $this->destination : '',
purpose: $mileage ? $this->purpose : '',
isBillable: $this->is_billable,
markup: $this->is_billable ? $this->markup : '',
billedAmount: $this->is_billable ? $this->billed_amount : '',
);
}
/**
* Shows a broken business rule as an error on the field it concerns.
*/
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match ($exception::class) {
ExpenseDescriptionRequired::class => ['description', 'expenses.errors.description_required'],
ExpenseCostRequired::class => ['cost', 'expenses.errors.cost_required'],
InvalidMoneyAmount::class => [$this->invalidAmountField(), 'expenses.errors.invalid_amount'],
InvalidKilometers::class => ['kilometers', 'expenses.errors.invalid_kilometers'],
MileageDetailsRequired::class => [$this->firstMissingMileageField(), 'expenses.errors.mileage_details_required'],
MileageRateMissing::class => ['kilometers', 'expenses.errors.mileage_rate_missing'],
InvalidMarkup::class => ['markup', 'expenses.errors.invalid_markup'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, mixed>>
*/
protected function rules(): array
{
return [
'user_id' => ['required', 'integer'],
'category' => ['required', Rule::enum(ExpenseCategory::class)],
'paid_by' => ['required', Rule::enum(ExpensePayer::class)],
'spent_on' => ['required', 'date_format:Y-m-d'],
'description' => ['nullable', 'string', 'max:255'],
'cost' => ['nullable', 'string', 'max:20'],
'kilometers' => ['nullable', 'string', 'max:12'],
'destination' => ['nullable', 'string', 'max:255'],
'purpose' => ['nullable', 'string', 'max:255'],
'is_billable' => ['boolean'],
'markup' => ['nullable', 'string', 'max:10'],
'billed_amount' => ['nullable', 'string', 'max:20'],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'user_id.required' => __('expenses.errors.user_required'),
'user_id.integer' => __('expenses.errors.user_required'),
'category.*' => __('expenses.errors.category_required'),
'paid_by.*' => __('expenses.errors.payer_required'),
'spent_on.required' => __('expenses.errors.date_required'),
'spent_on.date_format' => __('expenses.errors.invalid_date'),
'description.max' => __('expenses.errors.text_too_long'),
'destination.max' => __('expenses.errors.text_too_long'),
'purpose.max' => __('expenses.errors.text_too_long'),
'cost.max' => __('expenses.errors.invalid_amount'),
'billed_amount.max' => __('expenses.errors.invalid_amount'),
'kilometers.max' => __('expenses.errors.invalid_kilometers'),
'markup.max' => __('expenses.errors.invalid_markup'),
];
}
/**
* Which of the two amount fields holds the amount that does not parse.
*/
private function invalidAmountField(): string
{
if ($this->isMileage()) {
return 'billed_amount';
}
try {
Money::parse($this->cost);
} catch (InvalidMoneyAmount) {
return 'cost';
}
return 'billed_amount';
}
private function firstMissingMileageField(): string
{
foreach (['kilometers', 'destination', 'purpose'] as $field) {
if (trim($this->{$field}) === '') {
return $field;
}
}
return 'kilometers';
}
}