84 lines
2.2 KiB
PHP
84 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasModuleMedia;
|
|
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;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['price_formatted', 'stock_formatted', 'price_input', 'stock_input'])]
|
|
class RawMaterialPrice extends Model implements HasMedia
|
|
{
|
|
use HasModuleMedia;
|
|
use InteractsWithActivityLog;
|
|
use SoftDeletes;
|
|
|
|
public static function mediaModuleName(): string
|
|
{
|
|
return 'raw-material';
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('images');
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'price' => 'decimal:2',
|
|
'stock' => 'decimal:4',
|
|
];
|
|
}
|
|
|
|
public function priceFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format((float) $this->price, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function stockFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
$formatted = rtrim(rtrim(number_format((float) $this->stock, 4, ',', '.'), '0'), ',');
|
|
|
|
return "{$formatted} {$this->rawMaterial->unit->abbreviation()}";
|
|
},
|
|
);
|
|
}
|
|
|
|
public function priceInput(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => (string) (int) $this->price,
|
|
);
|
|
}
|
|
|
|
public function stockInput(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => rtrim(rtrim(number_format((float) $this->stock, 4, '.', ''), '0'), '.'),
|
|
);
|
|
}
|
|
|
|
public function rawMaterial(): BelongsTo
|
|
{
|
|
return $this->belongsTo(RawMaterial::class);
|
|
}
|
|
|
|
public function cuttingMaterials(): HasMany
|
|
{
|
|
return $this->hasMany(CuttingMaterial::class);
|
|
}
|
|
}
|