store/app/Models/EmployeeAdvance.php

152 lines
3.7 KiB
PHP

<?php
namespace App\Models;
use App\Enums\EmployeeAdvanceStatus;
use App\Models\Concerns\HasModuleMedia;
use App\Models\Concerns\HasRejection;
use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\MediaLibrary\HasMedia;
#[Guarded(['id'])]
#[Appends([
'amount_formatted',
'due_date_formatted',
'due_date_input',
'status_label',
'employee_name',
'rejection_reason',
'created_at_formatted',
'is_editable',
'can_verify',
'can_pay',
])]
class EmployeeAdvance extends Model implements HasMedia
{
use HasModuleMedia;
use HasRejection;
use InteractsWithActivityLog;
public static function mediaModuleName(): string
{
return 'employee-advance';
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('photos');
}
protected function casts(): array
{
return [
'amount' => 'integer',
'due_date' => 'date',
'status' => EmployeeAdvanceStatus::class,
'verified_at' => 'datetime',
'paid_at' => 'datetime',
];
}
public function amountFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format($this->amount, 0, ',', '.'),
);
}
public function dueDateFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->due_date?->translatedFormat('l, d F Y'),
);
}
public function dueDateInput(): Attribute
{
return Attribute::make(
get: fn () => $this->due_date?->format('Y-m-d'),
);
}
public function statusLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->status?->label(),
);
}
public function employeeName(): Attribute
{
return Attribute::make(
get: fn () => $this->employee?->user?->profile?->full_name
?? $this->employee?->user?->username,
);
}
public function rejectionReason(): Attribute
{
return Attribute::make(
get: fn () => $this->rejection?->reason,
);
}
public function createdAtFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->created_at?->translatedFormat('l, d F Y H:i'),
);
}
public function isEditable(): Attribute
{
return Attribute::make(
get: fn () => $this->status === EmployeeAdvanceStatus::PENDING,
);
}
public function canVerify(): Attribute
{
return Attribute::make(
get: fn () => $this->status === EmployeeAdvanceStatus::PENDING,
);
}
public function canPay(): Attribute
{
return Attribute::make(
get: fn () => $this->status === EmployeeAdvanceStatus::APPROVED,
);
}
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class);
}
public function cashTransaction(): BelongsTo
{
return $this->belongsTo(CashTransaction::class);
}
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');
}
public function paidBy(): BelongsTo
{
return $this->belongsTo(User::class, 'paid_by_id');
}
}