89 lines
2.3 KiB
PHP
89 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends([
|
|
'material_usage_formatted',
|
|
'material_usage_input',
|
|
'remaining_material_formatted',
|
|
'remaining_material_input',
|
|
'unit_abbreviation',
|
|
])]
|
|
class CuttingMaterial extends Model
|
|
{
|
|
use InteractsWithActivityLog;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'material_usage' => 'decimal:4',
|
|
'remaining_material' => 'decimal:4',
|
|
];
|
|
}
|
|
|
|
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 cutting(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Cutting::class);
|
|
}
|
|
|
|
public function rawMaterialPrice(): BelongsTo
|
|
{
|
|
return $this->belongsTo(RawMaterialPrice::class);
|
|
}
|
|
|
|
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'), '.');
|
|
}
|
|
}
|