- 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]>
66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
<?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);
|
|
}
|
|
}
|