Files
Toiminnanohjaus/app/Modules/Expenses/Application/UseCases/RecordExpense.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

50 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\UseCases;
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
use App\Modules\Expenses\Application\DTOs\ExpenseData;
use App\Modules\Expenses\Domain\Contracts\ExpenseRepository;
use App\Modules\Expenses\Domain\Entities\Expense;
use App\Modules\Expenses\Domain\Exceptions\ExpenseJobNotFound;
use App\Modules\Expenses\Domain\Exceptions\ExpenseLocked;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* Records an expense on a job that is not done yet.
*/
final class RecordExpense
{
public function __construct(
private ExpenseRepository $expenses,
private ExpenseJobs $jobs,
private ExpenseDetailsFactory $detailsFactory,
private TransactionManager $transactions,
) {}
/**
* @return int The new expense's id.
*
* @throws ExpenseJobNotFound
* @throws ExpenseLocked
* @throws DomainException When the fields break an expense rule.
*/
public function execute(int $jobId, ExpenseData $data, int $recordedBy): int
{
$details = $this->detailsFactory->build($data);
return $this->transactions->run(function () use ($jobId, $data, $details, $recordedBy): int {
$job = $this->jobs->find($jobId) ?? throw new ExpenseJobNotFound($jobId);
if ($job->isLocked) {
throw new ExpenseLocked;
}
return (int) $this->expenses->save(Expense::record($jobId, $data->userId, $recordedBy, $details))->id();
});
}
}