- Added labels for leave request statuses in LeaveRequestStatus enum. - Introduced label methods for PayrollAdjustmentType, PayrollPeriodStatus, PayrollStatus, RawMaterialUnit, and StokOpnameStatus enums. - Implemented formatted attributes for Attendance, CashAccount, CashTransaction, Category, Customer, Cutting, Employee, EmployeeAdvance, Expense, LeaveRequest, Order, OrderItem, Payroll, PayrollAdjustment, PayrollPeriod, Product, ProductPrice, ProductVariant, Purchase, PurchaseItem, RawMaterial, RawMaterialPrice, Restock, RestockItem, StokOpname, StokOpnameItem, Supplier, User, and UserProfile models. - Enhanced phone number formatting for Customer and Supplier models. - Added date formatting for various models to improve readability.
88 lines
2.2 KiB
PHP
88 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\LeaveRequestStatus;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
#[Appends(['formatted_end_date', 'formatted_start_date', 'status_label'])]
|
|
#[Guarded(['id'])]
|
|
class LeaveRequest extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => LeaveRequestStatus::class,
|
|
'start_date' => 'date:Y-m-d',
|
|
'end_date' => 'date:Y-m-d',
|
|
'total_days' => 'integer',
|
|
'verified_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
protected function formattedEndDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->end_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function formattedStartDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->start_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function statusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->status->label(),
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function approved(Builder $query): void
|
|
{
|
|
$query->where('status', LeaveRequestStatus::APPROVED);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function cancelled(Builder $query): void
|
|
{
|
|
$query->where('status', LeaveRequestStatus::CANCELLED);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function pending(Builder $query): void
|
|
{
|
|
$query->where('status', LeaveRequestStatus::PENDING);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function rejected(Builder $query): void
|
|
{
|
|
$query->where('status', LeaveRequestStatus::REJECTED);
|
|
}
|
|
|
|
public function employee(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Employee::class);
|
|
}
|
|
|
|
public function verifiedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'verified_by_id');
|
|
}
|
|
}
|