You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monitor-doc/app/Models/Document.php

114 lines
2.6 KiB
PHTML

<?php
namespace App\Models;
2 years ago
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2 years ago
use Illuminate\Support\Carbon;
class Document extends Model
{
use HasFactory;
2 years ago
const STATUS_YES = 1;
const STATUS_NO = 0;
const TYPE_TETAP = 1;
const TYPE_TIDAK_TETAP = 0;
protected $fillable = [
"no",
"no_doc",
"name",
"company_name",
"type_id",
"category_id",
"publisher",
"description",
"publish_date",
"due_date",
"status",
"type",
"group",
"region",
"document",
"user_id",
];
2 years ago
protected $casts = [
'publish_date' => 'datetime:Y-m-d',
'due_date' => 'datetime:Y-m-d'
];
2 years ago
protected $appends = ['due_status'];
public function creator()
{
return $this->belongsTo(User::class, 'user_id');
}
2 years ago
public function variety()
2 years ago
{
return $this->belongsTo(Type::class, 'type_id');
}
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
2 years ago
protected function dueStatus(): Attribute
{
return Attribute::make(
get: function() {
$diff = $this->category->duration;
if ($this->due_date == null) {
return '';
}
if (now()->toDateString() == $this->due_date->toDateString()) {
return "hari ini jatuh tempo";
}
$date = now()->diffInDays($this->due_date, false) + 1;
if ($diff >= $date && $date > 0) {
return $date . " hari mendekati jatuh tempo";
}
if ($date <= 0) {
return "jatuh tempo";
}
},
);
}
protected function isCloseDue(): Attribute
{
return Attribute::make(
get: function() {
$diff = $this->category->duration;
if ($this->due_date == null) {
return 0;
}
if (now()->toDateString() == $this->due_date->toDateString()) {
return 0;
}
$date = now()->diffInDays($this->due_date, false) + 1;
if ($diff >= $date && $date > 0) {
return $date;
}
if ($date <= 0) {
return 0;
}
return 0;
},
);
}
}