Files
Toiminnanohjaus/app/Modules/Inventory/Presentation/Livewire/Concerns/FormatsStock.php
T
2026-11-26 16:18:59 +02:00

50 lines
1.6 KiB
PHP

<?php
namespace App\Modules\Inventory\Presentation\Livewire\Concerns;
use App\Modules\Inventory\Domain\Enums\StockUnit;
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
use App\Modules\Shared\Domain\ValueObjects\Money;
use Carbon\CarbonImmutable;
/**
* Formatting shared by the stock pages and the components that show stock
* elsewhere (task material, machine console). Values may be negative.
*/
trait FormatsStock
{
public function quantity(int $milli, StockUnit|string|null $unit = null): string
{
$unitKey = $unit instanceof StockUnit ? $unit->value : $unit;
return StockQuantity::format($milli).($unitKey === null ? '' : "\u{00A0}".__('inventory.units.'.$unitKey));
}
public function money(int $cents): string
{
return ($cents < 0 ? '-' : '').Money::fromCents(abs($cents))->format();
}
/**
* A unit cost in hundredths of a cent, e.g. "0,0123 €" or "12,50 €".
*/
public function unitCost(int $hundredths): string
{
$euros = number_format($hundredths / 10_000, 4, ',', "\u{00A0}");
$euros = preg_replace('/(,\d\d)00$/', '$1', $euros) ?? $euros;
$euros = preg_replace('/(,\d\d\d)0$/', '$1', $euros) ?? $euros;
return $euros."\u{00A0}€";
}
/**
* "24.11.2026" for "2026-11-24"; "24.11.2026 klo 14.05" for "2026-11-24 14:05".
*/
public function dateLabel(string $value): string
{
return strlen($value) > 10
? CarbonImmutable::createFromFormat('Y-m-d H:i', $value)->format('j.n.Y \k\l\o H.i')
: CarbonImmutable::createFromFormat('!Y-m-d', $value)->format('j.n.Y');
}
}