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