Files
CKB/app/Models/MutationDetail.php

98 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MutationDetail extends Model
{
use HasFactory;
protected $fillable = [
'mutation_id',
'product_id',
'quantity_requested',
'quantity_approved',
'notes'
];
protected $casts = [
'quantity_requested' => 'decimal:2',
'quantity_approved' => 'decimal:2'
];
public function mutation()
{
return $this->belongsTo(Mutation::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
// Helper methods
public function getQuantityDifferenceAttribute()
{
return $this->quantity_approved - $this->quantity_requested;
}
public function isFullyApproved()
{
return $this->quantity_approved == $this->quantity_requested;
}
public function isPartiallyApproved()
{
return $this->quantity_approved > 0 && $this->quantity_approved < $this->quantity_requested;
}
public function isRejected()
{
return $this->quantity_approved == 0;
}
public function getApprovalStatusAttribute()
{
if ($this->isFullyApproved()) {
return 'Disetujui Penuh';
} elseif ($this->isPartiallyApproved()) {
return 'Disetujui Sebagian';
} elseif ($this->isRejected()) {
return 'Ditolak';
} else {
return 'Menunggu';
}
}
public function getApprovalStatusColorAttribute()
{
if ($this->isFullyApproved()) {
return 'success';
} elseif ($this->isPartiallyApproved()) {
return 'warning';
} elseif ($this->isRejected()) {
return 'danger';
} else {
return 'info';
}
}
// Scope untuk filter berdasarkan status approval
public function scopeFullyApproved($query)
{
return $query->whereColumn('quantity_approved', '=', 'quantity_requested');
}
public function scopePartiallyApproved($query)
{
return $query->where('quantity_approved', '>', 0)
->whereColumn('quantity_approved', '<', 'quantity_requested');
}
public function scopeRejected($query)
{
return $query->where('quantity_approved', '=', 0);
}
}