Files
Toiminnanohjaus/app/Modules/Expenses/Infrastructure/Persistence/Repositories/EloquentExpenseRepository.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

83 lines
3.1 KiB
PHP

<?php
namespace App\Modules\Expenses\Infrastructure\Persistence\Repositories;
use App\Modules\Expenses\Domain\Contracts\ExpenseRepository;
use App\Modules\Expenses\Domain\Entities\Expense;
use App\Modules\Expenses\Domain\ValueObjects\ExpenseBilling;
use App\Modules\Expenses\Domain\ValueObjects\ExpenseDetails;
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
use App\Modules\Expenses\Domain\ValueObjects\MarkupPercentage;
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
use App\Modules\Shared\Domain\ValueObjects\Money;
use DateTimeImmutable;
class EloquentExpenseRepository implements ExpenseRepository
{
public function findById(int $id): ?Expense
{
$model = ExpenseModel::query()->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function save(Expense $expense): Expense
{
$model = $expense->id() === null ? new ExpenseModel : ExpenseModel::query()->findOrFail($expense->id());
$details = $expense->details();
$model->fill([
'customer_job_id' => $expense->jobId(),
'user_id' => $expense->userId(),
'recorded_by' => $expense->recordedBy(),
'category' => $details->category,
'paid_by' => $details->paidBy,
'spent_on' => $details->spentOn->format('Y-m-d'),
'description' => $details->description,
'cost_cents' => $details->cost->cents,
'kilometers' => $details->kilometers === null ? null : $details->kilometers->tenths / 10,
'destination' => $details->destination,
'purpose' => $details->purpose,
'is_billable' => $details->isBillable(),
'markup_basis_points' => $details->billing?->markup?->basisPoints,
'billed_amount_cents' => $details->billing?->amount->cents,
])->save();
return $this->toEntity($model->refresh());
}
public function delete(Expense $expense): void
{
ExpenseModel::query()->whereKey($expense->id())->delete();
}
private function toEntity(ExpenseModel $model): Expense
{
$billing = $model->is_billable && $model->billed_amount_cents !== null
? ExpenseBilling::for(
Money::fromCents($model->cost_cents),
$model->markup_basis_points === null ? null : new MarkupPercentage($model->markup_basis_points),
Money::fromCents($model->billed_amount_cents),
)
: null;
return Expense::reconstitute(
$model->id,
$model->customer_job_id,
$model->user_id,
$model->recorded_by,
ExpenseDetails::reconstitute(
$model->category,
$model->paid_by,
DateTimeImmutable::createFromInterface($model->spent_on),
$model->description,
Money::fromCents($model->cost_cents),
$model->kilometers === null ? null : new Kilometers((int) round((float) $model->kilometers * 10)),
$model->destination,
$model->purpose,
$billing,
),
);
}
}