StocksNew/Modules/Leave/app/Models/Leave.php
Sampanna Rimal 53c0140f58 first commit
2024-08-27 17:48:06 +05:45

77 lines
1.8 KiB
PHP

<?php
namespace Modules\Leave\Models;
use App\Models\Scopes\CreatedByScope;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Modules\Employee\Models\Employee;
class Leave extends Model
{
protected $table = 'tbl_leaves';
protected $primaryKey = 'leave_id';
protected $guarded = [];
protected $appends = ['status_name'];
const PROGRESS_STATUS = [
1 => 'Pending',
2 => 'Approved',
3 => 'Rejected',
];
const DURATION = [
1 => 'Full Day',
2 => 'Half Day',
3 => 'Multiple',
];
protected static function booted(): void
{
static::addGlobalScope(new CreatedByScope);
}
protected function statusName(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes) {
switch ($attributes['status']) {
case '1':
$color = 'dark';
break;
case '2':
$color = 'success';
break;
case '3':
$color = 'danger';
break;
default:
$color = 'light';
break;
}
return collect([
'status' => self::PROGRESS_STATUS[$attributes['status']],
'color' => $color]);
},
set: fn($value) => $value,
);
}
public function getDuration()
{
return self::DURATION[$this->duration] ?? null;
}
public function employee()
{
return $this->belongsTo(Employee::class, 'employee_id');
}
public function leaveType()
{
return $this->belongsTo(LeaveType::class, 'leave_type_id');
}
}