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:
@@ -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'), '.'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user