Files
CKB/app/Models/MutationDetail.php

116 lines
3.3 KiB
PHP
Executable File

<?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 !== null && $this->quantity_approved == $this->quantity_requested;
}
public function isPartiallyApproved()
{
return $this->quantity_approved !== null && $this->quantity_approved > 0 && $this->quantity_approved < $this->quantity_requested;
}
public function isRejected()
{
// Hanya dianggap ditolak jika mutasi sudah di-approve/reject dan quantity_approved = 0
$mutationStatus = $this->mutation->status->value ?? null;
return in_array($mutationStatus, ['approved', 'rejected']) && $this->quantity_approved == 0;
}
public function getApprovalStatusAttribute()
{
$mutationStatus = $this->mutation->status->value ?? null;
// Jika mutasi belum di-approve, semua detail statusnya "Menunggu"
if (!in_array($mutationStatus, ['approved', 'rejected'])) {
return 'Menunggu';
}
// Jika mutasi sudah di-approve, baru cek quantity_approved
if ($this->isFullyApproved()) {
return 'Disetujui Penuh';
} elseif ($this->isPartiallyApproved()) {
return 'Disetujui Sebagian';
} elseif ($this->isRejected()) {
return 'Ditolak';
} else {
return 'Menunggu';
}
}
public function getApprovalStatusColorAttribute()
{
$mutationStatus = $this->mutation->status->value ?? null;
// Jika mutasi belum di-approve, semua detail statusnya "info" (menunggu)
if (!in_array($mutationStatus, ['approved', 'rejected'])) {
return 'info';
}
// Jika mutasi sudah di-approve, baru cek quantity_approved
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);
}
}