laravel12/app/Models/StockLog.php
2025-12-07 12:45:41 +09:00

40 lines
903 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class StockLog extends Model
{
protected $table = 'stock_logs';
protected $guarded = [];
protected $casts = [
'change_amount' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
public function scopeLatestFirst($qurey)
{
return $qurey->orderByDesc('created_at')->orderByDesc('id');
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
public function getChangeTypeLabelAttribute(): string
{
return $this->change_type === 'in' ? '입고' : '출고';
}
public function getSignedAmountAttribute(): int
{
return $this->change_type === 'in' ? (int) $this->change_amount : -(int) $this->change_amount;
}
}