<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class PermohonanDana extends Model
{
use SoftDeletes;
protected $guarded = [];
public function prepared()
{
return $this->belongsTo(User::class,'prepared_id')->withTrashed();
}
public function reviewed()
{
return $this->belongsTo(User::class,'reviewed_id')->withTrashed();
}
public function approved()
{
return $this->belongsTo(User::class,'approved_id')->withTrashed();
}
public function rejected()
{
return $this->belongsTo(User::class,'rejected_id')->withTrashed();
}
public function bank()
{
return $this->belongsTo(Bank::class)->withTrashed();
}
public function detail()
{
return $this->hasMany(DetailPermohonanDana::class)->withTrashed();
}
public function detail_bukti()
{
return $this->hasMany(DetailBuktiPermohonanDana::class)->withTrashed();
}
public function detail_foto()
{
return $this->hasMany(DetailFotoPermohonanDana::class)->withTrashed();
}
public function getTotalAttribute()
{
// Jika Jenis Permohonan Petty Cash, Maka Detail Pertama Sebagai Total
// Kemudian total dikurang Detail Selanjutnya
$total = ($this->jenis_permohonan == "Petty Cash") ? $this->detail[0]->jumlah : 0 ;
for ($i=0; $i < count($this->detail); $i++) {
if ($this->jenis_permohonan == "Petty Cash") {
if ($i != 0) {
$total -= $this->detail[$i]->jumlah;
}
}else{
$total += $this->detail[$i]->jumlah;
}
}
return $total;
}
public function getStatusAttribute()
{
$status = 'Prepared';
if ($this->approved_id) {
$status = 'Approved';
if ($this->reviewed_id) {
$status = 'Reviewed';
}
}
if ($this->rejected_id) {
$status = 'Rejected';
}
return $status;
}
public function getNextStatusAttribute()
{
$nextStatus = 'Approve';
if ($this->approved_id) {
$nextStatus = 'Review';
}
return $nextStatus;
}
public function delete(){
$this->detail()->delete();
parent::delete();
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]