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:
2026-11-24 08:11:27 +02:00
co-authored by Claude Opus 5.5
parent bdd8d446d8
commit 54d98d9d2f
64 changed files with 3092 additions and 9 deletions
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\Contracts;
use App\Modules\Expenses\Application\DTOs\ExpenseJob;
/**
* The customer jobs expenses are recorded on. Implemented with the Jobs
* module's queries.
*/
interface ExpenseJobs
{
public function find(int $jobId): ?ExpenseJob;
/**
* @param list<int> $jobIds
* @return array<int, ExpenseJob> Keyed by job id.
*/
public function findMany(array $jobIds): array;
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\Contracts;
/**
* Prices expenses need from the price list. Implemented with the Catalog
* module's queries.
*/
interface ExpensePricing
{
/**
* Price of a driven kilometre in cents (VAT 0), null when not set.
*/
public function mileageRateCents(): ?int;
/**
* Markup suggested for new purchases, in hundredths of a percent.
*/
public function defaultMarkupBasisPoints(): int;
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\Contracts;
use App\Modules\Expenses\Application\DTOs\ExpenseSummary;
/**
* Read side of expenses, newest first.
*/
interface ExpenseQueries
{
/**
* @return list<ExpenseSummary>
*/
public function forJob(int $jobId): array;
/**
* A user's own expenses in a month.
*
* @param string $month As Y-m.
* @return list<ExpenseSummary>
*/
public function forUserInMonth(int $userId, string $month): array;
public function find(int $id): ?ExpenseSummary;
}
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\DTOs;
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
/**
* An expense's fields as the user typed them: amounts "12,50", kilometres
* "12,5", markup "15". An empty billed amount means cost plus markup.
*/
final readonly class ExpenseData
{
/**
* @param string $spentOn Date as Y-m-d.
*/
public function __construct(
public int $userId,
public ExpenseCategory $category,
public ExpensePayer $paidBy,
public string $spentOn,
public string $description = '',
public string $cost = '',
public string $kilometers = '',
public string $destination = '',
public string $purpose = '',
public bool $isBillable = true,
public string $markup = '',
public string $billedAmount = '',
) {}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\DTOs;
/**
* A customer job as expenses see it: its number and name, and whether it is
* done, which locks its expenses.
*/
final readonly class ExpenseJob
{
public function __construct(
public int $id,
public string $number,
public string $name,
public bool $isLocked,
) {}
/**
* E.g. "2026-0005 Messuvideo".
*/
public function label(): string
{
return $this->number.' '.$this->name;
}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\DTOs;
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
/**
* An expense for lists and forms. Amounts are in cents without VAT; pages
* decide per row whether the viewer may see them.
*/
final readonly class ExpenseSummary
{
/**
* @param string $spentOn Date as Y-m-d.
*/
public function __construct(
public int $id,
public ExpenseJob $job,
public int $userId,
public string $userName,
public int $recordedBy,
public string $recordedByName,
public ExpenseCategory $category,
public ExpensePayer $paidBy,
public string $spentOn,
public ?string $description,
public int $costCents,
public ?int $kilometerTenths,
public ?string $destination,
public ?string $purpose,
public bool $isBillable,
public ?int $markupBasisPoints,
public ?int $billedAmountCents,
) {}
}
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\UseCases;
use App\Modules\Expenses\Application\Contracts\ExpensePricing;
use App\Modules\Expenses\Application\DTOs\ExpenseData;
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
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\ExpenseDetails;
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
use App\Modules\Expenses\Domain\ValueObjects\MarkupPercentage;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
use DateTimeImmutable;
use InvalidArgumentException;
/**
* Turns typed expense fields into ExpenseDetails, pricing kilometres with
* the current mileage rate. Shared by RecordExpense and UpdateExpense.
*/
final class ExpenseDetailsFactory
{
public function __construct(private ExpensePricing $pricing) {}
/**
* @throws ExpenseCostRequired
* @throws ExpenseDescriptionRequired
* @throws InvalidKilometers
* @throws InvalidMarkup
* @throws InvalidMoneyAmount
* @throws MileageDetailsRequired
* @throws MileageRateMissing
*/
public function build(ExpenseData $data): ExpenseDetails
{
$spentOn = self::day($data->spentOn);
$markup = $data->isBillable ? MarkupPercentage::parse($data->markup) : null;
$billedAmount = $data->isBillable ? Money::parse($data->billedAmount) : null;
if ($data->category !== ExpenseCategory::Mileage) {
return ExpenseDetails::purchase(
$data->category,
$data->paidBy,
$spentOn,
$data->description,
Money::parse($data->cost),
$data->isBillable,
$markup,
$billedAmount,
);
}
$kilometers = Kilometers::parse($data->kilometers);
$rate = $this->pricing->mileageRateCents() ?? throw new MileageRateMissing;
return ExpenseDetails::mileage(
$data->paidBy,
$spentOn,
$kilometers,
$data->destination,
$data->purpose,
Money::fromCents($rate),
$data->isBillable,
$markup,
$billedAmount,
$data->description,
);
}
private static function day(string $value): DateTimeImmutable
{
$day = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
if ($day === false || $day->format('Y-m-d') !== $value) {
throw new InvalidArgumentException("Not a Y-m-d date: {$value}");
}
return $day;
}
}
@@ -0,0 +1,49 @@
<?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();
});
}
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\UseCases;
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
use App\Modules\Expenses\Domain\Contracts\ExpenseRepository;
use App\Modules\Expenses\Domain\Exceptions\ExpenseLocked;
use App\Modules\Expenses\Domain\Exceptions\ExpenseNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Removes an expense of a job that is not done yet.
*/
final class RemoveExpense
{
public function __construct(
private ExpenseRepository $expenses,
private ExpenseJobs $jobs,
private TransactionManager $transactions,
) {}
/**
* @throws ExpenseLocked
* @throws ExpenseNotFound
*/
public function execute(int $expenseId): void
{
$this->transactions->run(function () use ($expenseId): void {
$expense = $this->expenses->findById($expenseId) ?? throw new ExpenseNotFound($expenseId);
if ($this->jobs->find($expense->jobId())->isLocked ?? true) {
throw new ExpenseLocked;
}
$this->expenses->delete($expense);
});
}
}
@@ -0,0 +1,47 @@
<?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\Exceptions\ExpenseLocked;
use App\Modules\Expenses\Domain\Exceptions\ExpenseNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* Changes an expense of a job that is not done yet. The job stays the same.
*/
final class UpdateExpense
{
public function __construct(
private ExpenseRepository $expenses,
private ExpenseJobs $jobs,
private ExpenseDetailsFactory $detailsFactory,
private TransactionManager $transactions,
) {}
/**
* @throws ExpenseLocked
* @throws ExpenseNotFound
* @throws DomainException When the fields break an expense rule.
*/
public function execute(int $expenseId, ExpenseData $data): void
{
$details = $this->detailsFactory->build($data);
$this->transactions->run(function () use ($expenseId, $data, $details): void {
$expense = $this->expenses->findById($expenseId) ?? throw new ExpenseNotFound($expenseId);
if ($this->jobs->find($expense->jobId())->isLocked ?? true) {
throw new ExpenseLocked;
}
$expense->change($data->userId, $details);
$this->expenses->save($expense);
});
}
}