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