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,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Contracts;
use App\Modules\Expenses\Domain\Entities\Expense;
interface ExpenseRepository
{
public function findById(int $id): ?Expense;
/**
* Inserts a new expense or updates a stored one, and returns the stored expense.
*/
public function save(Expense $expense): Expense;
public function delete(Expense $expense): void;
}
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Entities;
use App\Modules\Expenses\Domain\ValueObjects\ExpenseDetails;
/**
* An expense on a customer job: whose it is, who recorded it (management
* records subcontractors' expenses) and what it is.
*/
final class Expense
{
private function __construct(
private ?int $id,
private int $jobId,
private int $userId,
private int $recordedBy,
private ExpenseDetails $details,
) {}
public static function record(int $jobId, int $userId, int $recordedBy, ExpenseDetails $details): self
{
return new self(null, $jobId, $userId, $recordedBy, $details);
}
public static function reconstitute(int $id, int $jobId, int $userId, int $recordedBy, ExpenseDetails $details): self
{
return new self($id, $jobId, $userId, $recordedBy, $details);
}
public function change(int $userId, ExpenseDetails $details): void
{
$this->userId = $userId;
$this->details = $details;
}
public function id(): ?int
{
return $this->id;
}
public function jobId(): int
{
return $this->jobId;
}
public function userId(): int
{
return $this->userId;
}
public function recordedBy(): int
{
return $this->recordedBy;
}
public function details(): ExpenseDetails
{
return $this->details;
}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Enums;
/**
* Kind of expense (kululaji). Finnish labels are in `expenses.categories`.
*/
enum ExpenseCategory: string
{
/** Products, supplies and materials. */
case Materials = 'materials';
/** Driven kilometres, priced with the mileage rate. */
case Mileage = 'mileage';
/** Other purchases and tools. */
case Other = 'other';
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Enums;
/**
* Who paid the expense. An employee's own payments are reimbursed through
* another system; this one only records them.
*/
enum ExpensePayer: string
{
case Company = 'company';
case Employee = 'employee';
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ExpenseCostRequired extends DomainException
{
public function __construct()
{
parent::__construct('A materials or other expense needs a cost.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ExpenseDescriptionRequired extends DomainException
{
public function __construct()
{
parent::__construct('A materials or other expense needs a description.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ExpenseJobNotFound extends DomainException
{
public function __construct(public readonly int $jobId)
{
parent::__construct("Customer job {$jobId} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ExpenseLocked extends DomainException
{
public function __construct()
{
parent::__construct('The job is done, so its expenses are locked.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ExpenseNotFound extends DomainException
{
public function __construct(public readonly int $expenseId)
{
parent::__construct("Expense {$expenseId} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidKilometers extends DomainException
{
public function __construct()
{
parent::__construct('Kilometres must be 0.1–10 000 to one decimal.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidMarkup extends DomainException
{
public function __construct()
{
parent::__construct('A markup must be 0–1000 % with at most two decimals.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MileageDetailsRequired extends DomainException
{
public function __construct()
{
parent::__construct('A mileage expense needs kilometres, a destination and a purpose.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MileageRateMissing extends DomainException
{
public function __construct()
{
parent::__construct('The mileage rate is not set in the price list.');
}
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\ValueObjects;
use App\Modules\Shared\Domain\ValueObjects\Money;
/**
* How a billable expense is billed to the customer: the amount (VAT 0) and
* the markup it came from. The amount is the one given, or else the cost
* plus the markup.
*/
final readonly class ExpenseBilling
{
private function __construct(
public Money $amount,
public ?MarkupPercentage $markup,
) {}
public static function for(Money $cost, ?MarkupPercentage $markup, ?Money $amount): self
{
return new self($amount ?? $cost->addPercent($markup->basisPoints ?? 0), $markup);
}
}
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\ValueObjects;
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\MileageDetailsRequired;
use App\Modules\Shared\Domain\ValueObjects\Money;
use DateTimeImmutable;
/**
* What an expense is: a purchase (materials or other) with a description
* and cost, or driven kilometres priced with the mileage rate. Amounts are
* without VAT. $billing is null when the expense is not billed to the customer.
*/
final readonly class ExpenseDetails
{
private function __construct(
public ExpenseCategory $category,
public ExpensePayer $paidBy,
public DateTimeImmutable $spentOn,
public ?string $description,
public Money $cost,
public ?Kilometers $kilometers,
public ?string $destination,
public ?string $purpose,
public ?ExpenseBilling $billing,
) {}
/**
* @throws ExpenseCostRequired
* @throws ExpenseDescriptionRequired
*/
public static function purchase(
ExpenseCategory $category,
ExpensePayer $paidBy,
DateTimeImmutable $spentOn,
?string $description,
?Money $cost,
bool $billable,
?MarkupPercentage $markup = null,
?Money $billedAmount = null,
): self {
$description = self::text($description);
if ($category === ExpenseCategory::Mileage || $description === null) {
throw new ExpenseDescriptionRequired;
}
if ($cost === null) {
throw new ExpenseCostRequired;
}
return new self($category, $paidBy, $spentOn, $description, $cost, null, null, null, self::billing($billable, $cost, $markup, $billedAmount));
}
/**
* @throws MileageDetailsRequired
*/
public static function mileage(
ExpensePayer $paidBy,
DateTimeImmutable $spentOn,
?Kilometers $kilometers,
?string $destination,
?string $purpose,
Money $ratePerKilometer,
bool $billable,
?MarkupPercentage $markup = null,
?Money $billedAmount = null,
?string $description = null,
): self {
$destination = self::text($destination);
$purpose = self::text($purpose);
if ($kilometers === null || $destination === null || $purpose === null) {
throw new MileageDetailsRequired;
}
$cost = $kilometers->priceAt($ratePerKilometer);
return new self(
ExpenseCategory::Mileage,
$paidBy,
$spentOn,
self::text($description),
$cost,
$kilometers,
$destination,
$purpose,
self::billing($billable, $cost, $markup, $billedAmount),
);
}
/**
* Rebuilds stored details without re-checking them. Used only by repositories.
*/
public static function reconstitute(
ExpenseCategory $category,
ExpensePayer $paidBy,
DateTimeImmutable $spentOn,
?string $description,
Money $cost,
?Kilometers $kilometers,
?string $destination,
?string $purpose,
?ExpenseBilling $billing,
): self {
return new self($category, $paidBy, $spentOn, $description, $cost, $kilometers, $destination, $purpose, $billing);
}
public function isBillable(): bool
{
return $this->billing !== null;
}
private static function billing(bool $billable, Money $cost, ?MarkupPercentage $markup, ?Money $billedAmount): ?ExpenseBilling
{
return $billable ? ExpenseBilling::for($cost, $markup, $billedAmount) : null;
}
private static function text(?string $value): ?string
{
$value = trim((string) $value);
return $value === '' ? null : $value;
}
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\ValueObjects;
use App\Modules\Expenses\Domain\Exceptions\InvalidKilometers;
use App\Modules\Shared\Domain\ValueObjects\Money;
/**
* Driven kilometres to one decimal, stored in tenths of a kilometre.
*/
final readonly class Kilometers
{
public const int MAX_TENTHS = 100_000;
public int $tenths;
public function __construct(int $tenths)
{
if ($tenths < 1 || $tenths > self::MAX_TENTHS) {
throw new InvalidKilometers;
}
$this->tenths = $tenths;
}
/**
* Parses "12", "12,5" or "12.5"; null for an empty value.
*
* @throws InvalidKilometers
*/
public static function parse(string $value): ?self
{
$value = str_replace(',', '.', preg_replace('/[\s\x{00A0}]+|km/u', '', $value) ?? '');
if ($value === '') {
return null;
}
if (preg_match('/^(\d{1,6})(?:\.(\d))?$/', $value, $matches) !== 1) {
throw new InvalidKilometers;
}
return new self((int) $matches[1] * 10 + (int) ($matches[2] ?? 0));
}
/**
* The price of these kilometres at a rate per kilometre, rounded half up to the cent.
*/
public function priceAt(Money $ratePerKilometer): Money
{
return Money::fromCents(intdiv($this->tenths * $ratePerKilometer->cents * 2 + 10, 20));
}
/**
* Finnish notation, e.g. "12,5" or "40".
*/
public function format(): string
{
return $this->tenths % 10 === 0
? (string) intdiv($this->tenths, 10)
: intdiv($this->tenths, 10).','.($this->tenths % 10);
}
}
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Domain\ValueObjects;
use App\Modules\Expenses\Domain\Exceptions\InvalidMarkup;
/**
* Markup added to an expense's cost when it is billed, e.g. 15 %, stored
* in hundredths of a percent.
*/
final readonly class MarkupPercentage
{
public const int MAX_BASIS_POINTS = 100_000;
public int $basisPoints;
public function __construct(int $basisPoints)
{
if ($basisPoints < 0 || $basisPoints > self::MAX_BASIS_POINTS) {
throw new InvalidMarkup;
}
$this->basisPoints = $basisPoints;
}
/**
* Parses "15", "12,5" or "15 %"; null for an empty value.
*
* @throws InvalidMarkup
*/
public static function parse(string $value): ?self
{
$value = str_replace(',', '.', preg_replace('/[\s\x{00A0}%]+/u', '', $value) ?? '');
if ($value === '') {
return null;
}
if (preg_match('/^(\d{1,4})(?:\.(\d{1,2}))?$/', $value, $matches) !== 1) {
throw new InvalidMarkup;
}
return new self((int) $matches[1] * 100 + (int) str_pad($matches[2] ?? '', 2, '0'));
}
/**
* The percentage for an input field, e.g. "12,5".
*/
public function inputValue(): string
{
return str_replace('.', ',', rtrim(rtrim(number_format($this->basisPoints / 100, 2, '.', ''), '0'), '.'));
}
}