dstpabuaran.com/app/Models/StockMutation.php

81 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;
#[Appends(['formatted_quantity', 'formatted_stock_after', 'formatted_stock_before', 'formatted_created_at'])]
#[Guarded(['id'])]
class StockMutation extends Model
{
use HasFactory, LogsActivity;
protected function casts(): array
{
return [
'quantity' => 'integer',
'stock_before' => 'integer',
'stock_after' => 'integer',
];
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logAll()
->logOnlyDirty()
->dontLogEmptyChanges();
}
protected function formattedQuantity(): Attribute
{
return Attribute::make(
get: fn () => number_format($this->quantity, 0, ',', '.'),
);
}
protected function formattedStockAfter(): Attribute
{
return Attribute::make(
get: fn () => number_format($this->stock_after, 0, ',', '.'),
);
}
protected function formattedStockBefore(): Attribute
{
return Attribute::make(
get: fn () => number_format($this->stock_before, 0, ',', '.'),
);
}
protected function formattedCreatedAt(): Attribute
{
return Attribute::make(
get: fn () => $this->created_at?->translatedFormat('d F Y, H:i'),
);
}
public function source(): MorphTo
{
return $this->morphTo();
}
public function stockable(): MorphTo
{
return $this->morphTo();
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}