- 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]>
132 lines
3.9 KiB
PHP
132 lines
3.9 KiB
PHP
<?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;
|
|
}
|
|
}
|