Add stock items, groups and movements at a moving average cost

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:18:59 +02:00
co-authored by Claude Sonnet 5.5
parent 361a2974cf
commit 30132deb0e
81 changed files with 4968 additions and 115 deletions
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Contracts;
use App\Modules\Inventory\Domain\Entities\StockItemGroup;
interface StockItemGroupRepository
{
public function findById(int $id): ?StockItemGroup;
public function save(StockItemGroup $group): StockItemGroup;
public function isNameTaken(string $name, ?int $exceptId = null): bool;
public function nextSortOrder(): int;
/**
* The group just before ($up) or after it in order, if any.
*/
public function neighbour(StockItemGroup $group, bool $up): ?StockItemGroup;
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Contracts;
use App\Modules\Inventory\Domain\Entities\StockItem;
interface StockItemRepository
{
public function findById(int $id): ?StockItem;
/**
* The item, locked until the end of the transaction, so that its
* balance and average cost change one movement at a time.
*/
public function lockById(int $id): ?StockItem;
public function save(StockItem $item): StockItem;
public function isNameTaken(string $name, ?int $exceptId = null): bool;
}
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Contracts;
use App\Modules\Inventory\Domain\Entities\StockMovement;
interface StockMovementRepository
{
public function add(StockMovement $movement): StockMovement;
public function findById(int $id): ?StockMovement;
public function isReversed(int $movementId): bool;
/**
* The item's balance in thousandths: the sum of its movements.
*/
public function balanceOf(int $itemId): int;
}
@@ -0,0 +1,169 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Entities;
use App\Modules\Inventory\Domain\Enums\StockUnit;
use App\Modules\Inventory\Domain\Exceptions\StockItemInactive;
use App\Modules\Inventory\Domain\Services\StockValuation;
use App\Modules\Inventory\Domain\ValueObjects\StockName;
/**
* A stock item (nimike), e.g. a photo paper or an ink, counted in its unit.
* Its balance is the sum of its movements and is not kept here; the item
* keeps its moving average cost, which only purchases change. Items are
* never deleted: one taken out of use cannot be used, but its movements
* stay.
*/
final class StockItem
{
private function __construct(
private ?int $id,
private int $groupId,
private StockName $name,
private StockUnit $unit,
private ?string $code,
private ?string $supplier,
private ?int $reorderLevelMilli,
private int $averageCostHundredths,
private ?string $imagePath,
private ?string $thumbnailPath,
private bool $active,
) {}
public static function create(int $groupId, StockName $name, StockUnit $unit, ?string $code, ?string $supplier, ?int $reorderLevelMilli): self
{
return new self(null, $groupId, $name, $unit, self::text($code), self::text($supplier), $reorderLevelMilli, 0, null, null, true);
}
public static function reconstitute(
int $id,
int $groupId,
StockName $name,
StockUnit $unit,
?string $code,
?string $supplier,
?int $reorderLevelMilli,
int $averageCostHundredths,
?string $imagePath,
?string $thumbnailPath,
bool $active,
): self {
return new self($id, $groupId, $name, $unit, $code, $supplier, $reorderLevelMilli, $averageCostHundredths, $imagePath, $thumbnailPath, $active);
}
public function change(int $groupId, StockName $name, StockUnit $unit, ?string $code, ?string $supplier, ?int $reorderLevelMilli): void
{
$this->groupId = $groupId;
$this->name = $name;
$this->unit = $unit;
$this->code = self::text($code);
$this->supplier = self::text($supplier);
$this->reorderLevelMilli = $reorderLevelMilli;
}
/**
* Updates the moving average cost for a purchase (or, with a negative
* quantity and value, the reversal of one) received on top of the
* given balance.
*/
public function receive(int $balanceMilli, int $quantityMilli, int $valueCents): void
{
$this->averageCostHundredths = StockValuation::averageAfterPurchase($balanceMilli, $this->averageCostHundredths, $quantityMilli, $valueCents);
}
/**
* @throws StockItemInactive
*/
public function ensureUsable(): void
{
if (! $this->active) {
throw new StockItemInactive;
}
}
public function setImage(string $imagePath, string $thumbnailPath): void
{
$this->imagePath = $imagePath;
$this->thumbnailPath = $thumbnailPath;
}
public function removeImage(): void
{
$this->imagePath = null;
$this->thumbnailPath = null;
}
public function activate(): void
{
$this->active = true;
}
public function deactivate(): void
{
$this->active = false;
}
public function id(): ?int
{
return $this->id;
}
public function groupId(): int
{
return $this->groupId;
}
public function name(): StockName
{
return $this->name;
}
public function unit(): StockUnit
{
return $this->unit;
}
public function code(): ?string
{
return $this->code;
}
public function supplier(): ?string
{
return $this->supplier;
}
public function reorderLevelMilli(): ?int
{
return $this->reorderLevelMilli;
}
public function averageCostHundredths(): int
{
return $this->averageCostHundredths;
}
public function imagePath(): ?string
{
return $this->imagePath;
}
public function thumbnailPath(): ?string
{
return $this->thumbnailPath;
}
public function isActive(): bool
{
return $this->active;
}
private static function text(?string $value): ?string
{
$value = $value === null ? '' : trim($value);
return $value === '' ? null : mb_substr($value, 0, 150);
}
}
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Entities;
use App\Modules\Inventory\Domain\ValueObjects\StockName;
/**
* A group of stock items (nimikeryhmä), e.g. Paperit ja kartongit. Groups
* are ordered, and never deleted, only taken out of use.
*/
final class StockItemGroup
{
private function __construct(
private ?int $id,
private StockName $name,
private int $sortOrder,
private bool $active,
) {}
public static function create(StockName $name, int $sortOrder): self
{
return new self(null, $name, $sortOrder, true);
}
public static function reconstitute(int $id, StockName $name, int $sortOrder, bool $active): self
{
return new self($id, $name, $sortOrder, $active);
}
public function rename(StockName $name): void
{
$this->name = $name;
}
public function activate(): void
{
$this->active = true;
}
public function deactivate(): void
{
$this->active = false;
}
public function swapOrderWith(self $other): void
{
[$this->sortOrder, $other->sortOrder] = [$other->sortOrder, $this->sortOrder];
}
public function id(): ?int
{
return $this->id;
}
public function name(): StockName
{
return $this->name;
}
public function sortOrder(): int
{
return $this->sortOrder;
}
public function isActive(): bool
{
return $this->active;
}
}
@@ -0,0 +1,225 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Entities;
use App\Modules\Inventory\Domain\Enums\StockMovementKind;
use App\Modules\Inventory\Domain\Exceptions\StockMovementNotReversible;
use App\Modules\Inventory\Domain\Exceptions\StockReasonRequired;
use App\Modules\Inventory\Domain\Services\StockValuation;
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
use App\Modules\Inventory\Domain\ValueObjects\StockUseTarget;
use DateTimeImmutable;
/**
* A change in an item's balance (varastotapahtuma, spec 19.2). Movements
* are never changed or deleted: a mistake is corrected with a reversal,
* the same movement negated, which refers to the original. The quantity
* is signed (in thousandths) and the value is at the unit cost of the
* moment: the purchase price for a purchase, the moving average cost
* otherwise.
*/
final class StockMovement
{
private function __construct(
private ?int $id,
private int $itemId,
private StockMovementKind $kind,
private int $quantityMilli,
private int $unitCostHundredths,
private int $valueCents,
private DateTimeImmutable $occurredOn,
private DateTimeImmutable $recordedAt,
private int $userId,
private ?StockUseTarget $target,
private ?string $reason,
private ?string $supplier,
private ?string $documentNumber,
private ?int $inventoryCountId,
private ?int $reversesMovementId,
) {}
public static function purchase(StockItem $item, StockQuantity $quantity, int $valueCents, DateTimeImmutable $occurredOn, DateTimeImmutable $recordedAt, int $userId, ?string $supplier, ?string $documentNumber): self
{
return new self(
null, (int) $item->id(), StockMovementKind::Purchase, $quantity->milli,
StockValuation::unitCostOf($quantity->milli, $valueCents), $valueCents,
$occurredOn, $recordedAt, $userId, null, null, self::text($supplier), self::text($documentNumber), null, null,
);
}
public static function consumption(StockItem $item, StockQuantity $quantity, StockUseTarget $target, DateTimeImmutable $occurredOn, DateTimeImmutable $recordedAt, int $userId): self
{
return self::outgoing(StockMovementKind::Consumption, $item, $quantity, $target, $target->reason, $occurredOn, $recordedAt, $userId);
}
/**
* @throws StockReasonRequired
*/
public static function writeOff(StockItem $item, StockQuantity $quantity, string $reason, DateTimeImmutable $occurredOn, DateTimeImmutable $recordedAt, int $userId, ?StockUseTarget $target = null): self
{
$reason = self::text($reason) ?? throw new StockReasonRequired;
return self::outgoing(StockMovementKind::WriteOff, $item, $quantity, $target, $reason, $occurredOn, $recordedAt, $userId);
}
/**
* A stock count's correction: a signed difference at the average cost.
*/
public static function adjustment(StockItem $item, int $differenceMilli, int $inventoryCountId, DateTimeImmutable $occurredOn, DateTimeImmutable $recordedAt, int $userId): self
{
$cost = $item->averageCostHundredths();
return new self(
null, (int) $item->id(), StockMovementKind::Adjustment, $differenceMilli, $cost,
StockValuation::valueCents($differenceMilli, $cost),
$occurredOn, $recordedAt, $userId, null, null, null, null, $inventoryCountId, null,
);
}
/**
* @throws StockMovementNotReversible
*/
public static function reversalOf(self $original, DateTimeImmutable $occurredOn, DateTimeImmutable $recordedAt, int $userId): self
{
if (! $original->isReversible()) {
throw new StockMovementNotReversible;
}
return new self(
null, $original->itemId, StockMovementKind::Reversal, -$original->quantityMilli,
$original->unitCostHundredths, -$original->valueCents,
$occurredOn, $recordedAt, $userId, $original->target, $original->reason, null, null, null, $original->id,
);
}
public static function reconstitute(
int $id,
int $itemId,
StockMovementKind $kind,
int $quantityMilli,
int $unitCostHundredths,
int $valueCents,
DateTimeImmutable $occurredOn,
DateTimeImmutable $recordedAt,
int $userId,
?StockUseTarget $target,
?string $reason,
?string $supplier,
?string $documentNumber,
?int $inventoryCountId,
?int $reversesMovementId,
): self {
return new self($id, $itemId, $kind, $quantityMilli, $unitCostHundredths, $valueCents, $occurredOn, $recordedAt, $userId, $target, $reason, $supplier, $documentNumber, $inventoryCountId, $reversesMovementId);
}
public function isReversible(): bool
{
return $this->kind !== StockMovementKind::Reversal && $this->kind !== StockMovementKind::Adjustment;
}
/**
* Whether the user may reverse this movement without the right to
* correct anyone's: their own use, on the day it was recorded.
*/
public function isOwnUseOf(int $userId, DateTimeImmutable $today): bool
{
return $this->kind === StockMovementKind::Consumption
&& $this->userId === $userId
&& $this->occurredOn->format('Y-m-d') === $today->format('Y-m-d');
}
public function id(): ?int
{
return $this->id;
}
public function itemId(): int
{
return $this->itemId;
}
public function kind(): StockMovementKind
{
return $this->kind;
}
public function quantityMilli(): int
{
return $this->quantityMilli;
}
public function unitCostHundredths(): int
{
return $this->unitCostHundredths;
}
public function valueCents(): int
{
return $this->valueCents;
}
public function occurredOn(): DateTimeImmutable
{
return $this->occurredOn;
}
public function recordedAt(): DateTimeImmutable
{
return $this->recordedAt;
}
public function userId(): int
{
return $this->userId;
}
public function target(): ?StockUseTarget
{
return $this->target;
}
public function reason(): ?string
{
return $this->reason;
}
public function supplier(): ?string
{
return $this->supplier;
}
public function documentNumber(): ?string
{
return $this->documentNumber;
}
public function inventoryCountId(): ?int
{
return $this->inventoryCountId;
}
public function reversesMovementId(): ?int
{
return $this->reversesMovementId;
}
private static function outgoing(StockMovementKind $kind, StockItem $item, StockQuantity $quantity, ?StockUseTarget $target, ?string $reason, DateTimeImmutable $occurredOn, DateTimeImmutable $recordedAt, int $userId): self
{
$cost = $item->averageCostHundredths();
return new self(
null, (int) $item->id(), $kind, -$quantity->milli, $cost,
StockValuation::valueCents(-$quantity->milli, $cost),
$occurredOn, $recordedAt, $userId, $target, $reason, null, null, null, null,
);
}
private static function text(?string $value): ?string
{
$value = $value === null ? '' : trim($value);
return $value === '' ? null : mb_substr($value, 0, 255);
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Enums;
/**
* What a stock movement is (spec 19.2): a purchase adds stock, use and a
* write-off take it, a stock count adjusts it either way, and a reversal
* cancels an earlier movement with the opposite quantity.
*/
enum StockMovementKind: string
{
case Purchase = 'purchase';
case Consumption = 'consumption';
case WriteOff = 'write_off';
case Adjustment = 'adjustment';
case Reversal = 'reversal';
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Enums;
/**
* The unit a stock item is counted in (spec 19.1). Labels are in
* lang/*\/inventory.php under units.
*/
enum StockUnit: string
{
case Piece = 'piece';
case Sheet = 'sheet';
case Roll = 'roll';
case Metre = 'metre';
case SquareMetre = 'square_metre';
case Litre = 'litre';
case Package = 'package';
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidStockCost extends DomainException
{
public function __construct()
{
parent::__construct('A purchase price must be zero or more, with at most four decimals for a unit price.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidStockDay extends DomainException
{
public function __construct()
{
parent::__construct('The day must be a valid date.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidStockImage extends DomainException
{
public function __construct()
{
parent::__construct('The file is not a supported image.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidStockName extends DomainException
{
public function __construct()
{
parent::__construct('A stock name must be 1-150 characters.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidStockQuantity extends DomainException
{
public function __construct()
{
parent::__construct('A stock quantity must be a number with at most three decimals.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockItemGroupNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The stock item group was not found.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockItemInactive extends DomainException
{
public function __construct()
{
parent::__construct('The stock item is no longer in use.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockItemNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The stock item was not found.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockMovementAlreadyReversed extends DomainException
{
public function __construct()
{
parent::__construct('The stock movement has already been reversed.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockMovementNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The stock movement was not found.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockMovementNotReversible extends DomainException
{
public function __construct()
{
parent::__construct('Reversals and stock count adjustments cannot be reversed.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockNameTaken extends DomainException
{
public function __construct()
{
parent::__construct('The name is already in use.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockReasonRequired extends DomainException
{
public function __construct()
{
parent::__construct('A reason is required.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockReversalNotAllowed extends DomainException
{
public function __construct()
{
parent::__construct('Only your own use recorded today may be reversed.');
}
}
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Services;
/**
* Matches stock items to a search typed on a keyboard or touch screen, or
* read by a barcode scanner: every word of the search must be found in the
* name or the code, regardless of case and of the dots of å, ä and ö
* (spec 19.6).
*/
final class StockSearch
{
public static function matches(string $search, string $name, ?string $code): bool
{
$words = preg_split('/\s+/u', self::normalize($search), -1, PREG_SPLIT_NO_EMPTY) ?: [];
$haystack = self::normalize($name.' '.($code ?? ''));
foreach ($words as $word) {
if (! str_contains($haystack, $word)) {
return false;
}
}
return true;
}
public static function normalize(string $value): string
{
return strtr(mb_strtolower(trim($value)), ['å' => 'a', 'ä' => 'a', 'ö' => 'o', 'é' => 'e', 'ü' => 'u']);
}
}
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Services;
/**
* Valuation of stock at a moving average cost (liukuva keskihinta, spec
* 19.2). Unit costs are in hundredths of a cent per whole unit, so that
* cheap units such as a millilitre of ink keep their precision; values
* are in cents, rounded half away from zero.
*/
final class StockValuation
{
/**
* The value of a quantity (in thousandths) at a unit cost.
*/
public static function valueCents(int $quantityMilli, int $unitCostHundredths): int
{
return self::divideRounded($quantityMilli * $unitCostHundredths, 100_000);
}
/**
* The unit cost of a purchase of a quantity for a total value.
*/
public static function unitCostOf(int $quantityMilli, int $valueCents): int
{
return $quantityMilli === 0 ? 0 : self::divideRounded($valueCents * 100_000, $quantityMilli);
}
/**
* The average unit cost after a purchase: (old balance × old average +
* purchase value) / new balance. When the stock before was empty or
* negative, or nothing is left after, the purchase's own unit cost.
*/
public static function averageAfterPurchase(int $balanceMilli, int $averageHundredths, int $purchasedMilli, int $purchaseValueCents): int
{
$newBalance = $balanceMilli + $purchasedMilli;
if ($balanceMilli <= 0 || $newBalance <= 0) {
return max(0, self::unitCostOf($purchasedMilli, $purchaseValueCents));
}
$oldValueHundredthsMilli = $balanceMilli * $averageHundredths;
return max(0, self::divideRounded($oldValueHundredthsMilli + $purchaseValueCents * 100_000, $newBalance));
}
private static function divideRounded(int $dividend, int $divisor): int
{
$sign = ($dividend < 0) !== ($divisor < 0) ? -1 : 1;
$dividend = abs($dividend);
$divisor = abs($divisor);
return $sign * intdiv($dividend + intdiv($divisor, 2), $divisor);
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\ValueObjects;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockName;
/**
* Name of a stock item or an item group, e.g. "Valokuvapaperi 10×15".
*/
final readonly class StockName
{
public const int MAX_LENGTH = 150;
public string $value;
/**
* @throws InvalidStockName
*/
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidStockName;
}
$this->value = $value;
}
}
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\ValueObjects;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
/**
* A quantity of stock in thousandths of the item's unit (e.g. 2,5 m² =
* 2500), so three decimals are kept exactly. Typed in as "2,5", "2.5" or
* "1 200".
*/
final readonly class StockQuantity
{
public const int MAX_MILLI = 9_999_999_999;
private function __construct(public int $milli) {}
/**
* @throws InvalidStockQuantity
*/
public static function ofMilli(int $milli): self
{
if (abs($milli) > self::MAX_MILLI) {
throw new InvalidStockQuantity;
}
return new self($milli);
}
/**
* A quantity greater than zero, as typed in.
*
* @throws InvalidStockQuantity
*/
public static function parsePositive(string $value): self
{
$quantity = self::parse($value);
if ($quantity === null || $quantity->milli <= 0) {
throw new InvalidStockQuantity;
}
return $quantity;
}
/**
* A quantity of zero or more as typed in, or null for an empty value.
*
* @throws InvalidStockQuantity
*/
public static function parse(string $value): ?self
{
$value = preg_replace('/[\s\x{00A0}\x{202F}]+/u', '', $value) ?? '';
$value = str_replace(',', '.', $value);
if ($value === '') {
return null;
}
if (preg_match('/^(\d{1,7})(?:\.(\d{1,3}))?$/', $value, $matches) !== 1) {
throw new InvalidStockQuantity;
}
return new self((int) $matches[1] * 1000 + (int) str_pad($matches[2] ?? '', 3, '0'));
}
public function negated(): self
{
return new self(-$this->milli);
}
/**
* Finnish format without trailing zeros: "2,5", "-1", "1 200".
*/
public static function format(int $milli): string
{
$whole = intdiv(abs($milli), 1000);
$fraction = rtrim(str_pad((string) (abs($milli) % 1000), 3, '0', STR_PAD_LEFT), '0');
return ($milli < 0 ? '-' : '').number_format($whole, 0, ',', "\u{00A0}").($fraction === '' ? '' : ','.$fraction);
}
/**
* The value for an input field: "2,5" without thousands separators.
*/
public static function inputValue(int $milli): string
{
return str_replace("\u{00A0}", '', self::format($milli));
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\ValueObjects;
use App\Modules\Inventory\Domain\Exceptions\StockReasonRequired;
/**
* What stock was used for (spec 19.2): a job's task, a machine, a quick
* sale order line or a machine's maintenance, each possibly with a reason.
* Use without a target (e.g. a test run) needs a reason.
*/
final readonly class StockUseTarget
{
public ?string $reason;
/**
* @throws StockReasonRequired
*/
public function __construct(
public ?int $taskId = null,
public ?int $jobId = null,
public ?int $machineId = null,
public ?int $orderLineId = null,
public ?int $maintenanceRecordId = null,
?string $reason = null,
) {
$reason = $reason === null ? null : trim($reason);
$this->reason = $reason === '' || $reason === null ? null : mb_substr($reason, 0, 255);
if ($this->reason === null && $taskId === null && $machineId === null && $orderLineId === null && $maintenanceRecordId === null) {
throw new StockReasonRequired;
}
}
/**
* @throws StockReasonRequired
*/
public static function reason(?string $reason): self
{
return new self(reason: $reason);
}
}