64 lines
1.5 KiB
PHP
64 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\RawMaterialUnit;
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['unit_label', 'unit_abbreviation'])]
|
|
class RawMaterial extends Model
|
|
{
|
|
use HasFactory;
|
|
use InteractsWithActivityLog;
|
|
use SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_active' => 'boolean',
|
|
'unit' => RawMaterialUnit::class,
|
|
];
|
|
}
|
|
|
|
#[Scope]
|
|
public function active(Builder $query): void
|
|
{
|
|
$query->where('is_active', true);
|
|
}
|
|
|
|
#[Scope]
|
|
public function inactive(Builder $query): void
|
|
{
|
|
$query->where('is_active', false);
|
|
}
|
|
|
|
public function prices(): HasMany
|
|
{
|
|
return $this->hasMany(RawMaterialPrice::class);
|
|
}
|
|
|
|
public function unitAbbreviation(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->unit->abbreviation(),
|
|
);
|
|
}
|
|
|
|
public function unitLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->unit->label(),
|
|
);
|
|
}
|
|
}
|