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