84 lines
2.3 KiB
PHP
84 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\OwnerVerificationStatus;
|
|
use App\Enums\RawMaterialUnit;
|
|
use App\Models\Concerns\HasPendingOwnerVerification;
|
|
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\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['unit_abbreviation', 'unit_label'])]
|
|
class RawMaterial extends Model
|
|
{
|
|
// 1. Use Trait
|
|
use HasFactory, HasPendingOwnerVerification, InteractsWithActivityLog, SoftDeletes;
|
|
|
|
// 2. Casting
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_active' => 'boolean',
|
|
'unit' => RawMaterialUnit::class,
|
|
];
|
|
}
|
|
|
|
// 3. Scope (grouped by column, then alphabetical)
|
|
// Column Group: is_active
|
|
#[Scope]
|
|
protected function active(Builder $query): void
|
|
{
|
|
$query->where('is_active', true);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function inactive(Builder $query): void
|
|
{
|
|
$query->where('is_active', false);
|
|
}
|
|
|
|
// 4. Attribute
|
|
public function unitAbbreviation(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->unit->abbreviation(),
|
|
);
|
|
}
|
|
|
|
public function unitLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->unit->label(),
|
|
);
|
|
}
|
|
|
|
// 5. Relation
|
|
public function ownerVerificationRequests(): MorphMany
|
|
{
|
|
return $this->morphMany(OwnerVerificationRequest::class, 'subject');
|
|
}
|
|
|
|
public function pendingOwnerVerificationRequest(): MorphOne
|
|
{
|
|
return $this->morphOne(OwnerVerificationRequest::class, 'subject')
|
|
->where('status', OwnerVerificationStatus::PENDING)
|
|
->latestOfMany();
|
|
}
|
|
|
|
public function prices(): HasMany
|
|
{
|
|
return $this->hasMany(RawMaterialPrice::class);
|
|
}
|
|
}
|