Files
Toiminnanohjaus/app/Modules/Inventory/Domain/Entities/StockMovement.php
T
2026-11-26 16:18:59 +02:00

226 lines
7.3 KiB
PHP

<?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);
}
}