'decimal:4', 'remaining_material' => 'decimal:4', ]; } public function user(): BelongsTo { return $this->belongsTo(User::class); } public function cutting(): BelongsTo { return $this->belongsTo(Cutting::class); } public function rawMaterialPrice(): BelongsTo { return $this->belongsTo(RawMaterialPrice::class); } public function materialUsageFormatted(): Attribute { return Attribute::make( get: fn () => $this->formatQuantity($this->material_usage), ); } public function materialUsageInput(): Attribute { return Attribute::make( get: fn () => $this->formatQuantityInput($this->material_usage), ); } public function remainingMaterialFormatted(): Attribute { return Attribute::make( get: fn () => $this->formatQuantity($this->remaining_material), ); } public function remainingMaterialInput(): Attribute { return Attribute::make( get: fn () => $this->formatQuantityInput($this->remaining_material), ); } public function unitAbbreviation(): Attribute { return Attribute::make( get: fn () => $this->rawMaterialPrice?->rawMaterial?->unit?->abbreviation(), ); } public function materialCost(): int { $price = $this->rawMaterialPrice; if ($price === null) { return 0; } return (int) round((float) $this->material_usage * (int) $price->price); } private function formatQuantity(float|string|null $value): string { $formatted = rtrim(rtrim(number_format((float) $value, 4, ',', '.'), '0'), ','); return "{$formatted} {$this->unitAbbreviation}"; } private function formatQuantityInput(float|string|null $value): string { return rtrim(rtrim(number_format((float) $value, 4, '.', ''), '0'), '.'); } }