- Added roles property to Employee type and a new column for displaying employee roles in the employee table. - Updated leave request columns to use formatted start and end dates instead of raw date strings. - Enhanced leave request index to accept filter options for status dynamically. - Refactored transaction index to support dynamic filter options for status, channel, and payment type. - Introduced AGENTS.md for session notes detailing model relationships, casting, scopes, reorganizations, and conventions.
124 lines
3.2 KiB
PHP
124 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\EmployeeAdvanceStatus;
|
|
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_amount', 'formatted_created_at', 'formatted_due_date', 'formatted_paid_amount', 'status_label'])]
|
|
#[Guarded(['id'])]
|
|
class EmployeeAdvance extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => EmployeeAdvanceStatus::class,
|
|
'amount' => 'integer',
|
|
'paid_amount' => 'integer',
|
|
'due_date' => 'date:Y-m-d',
|
|
'verified_at' => 'datetime',
|
|
'paid_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
protected function formattedAmount(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp ' . number_format($this->amount, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedCreatedAt(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->created_at?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function formattedDueDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->due_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function formattedPaidAmount(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp ' . number_format($this->paid_amount, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function statusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->status->label(),
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function approved(Builder $query): void
|
|
{
|
|
$query->where('status', EmployeeAdvanceStatus::APPROVED);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function cancelled(Builder $query): void
|
|
{
|
|
$query->where('status', EmployeeAdvanceStatus::CANCELLED);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function paid(Builder $query): void
|
|
{
|
|
$query->where('status', EmployeeAdvanceStatus::PAID);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function pending(Builder $query): void
|
|
{
|
|
$query->where('status', EmployeeAdvanceStatus::PENDING);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function rejected(Builder $query): void
|
|
{
|
|
$query->where('status', EmployeeAdvanceStatus::REJECTED);
|
|
}
|
|
|
|
public function cashTransaction(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CashTransaction::class);
|
|
}
|
|
|
|
public function employee(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Employee::class);
|
|
}
|
|
|
|
public function paidBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'paid_by_id');
|
|
}
|
|
|
|
public function repaymentCashTransaction(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CashTransaction::class, 'repayment_cash_transaction_id');
|
|
}
|
|
|
|
public function verifiedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'verified_by_id');
|
|
}
|
|
}
|