dstpabuaran.com/app/Models/Payroll.php
Yoga Pangestu 5961a60585 feat: add labels and formatting attributes to various models and enums
- 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.
2026-08-04 15:31:41 +07:00

120 lines
3.2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\PayrollStatus;
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\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
#[Appends(['formatted_base_salary', 'formatted_bonus_amount', 'formatted_deduction_amount', 'status_label', 'formatted_total_amount'])]
#[Guarded(['id'])]
class Payroll extends Model
{
use HasFactory, SoftDeletes;
protected function casts(): array
{
return [
'status' => PayrollStatus::class,
'base_salary' => 'integer',
'bonus_amount' => 'integer',
'deduction_amount' => 'integer',
'total_amount' => 'integer',
'paid_at' => 'datetime',
];
}
protected function formattedBaseSalary(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->base_salary, 0, ',', '.'),
);
}
protected function formattedBonusAmount(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->bonus_amount, 0, ',', '.'),
);
}
protected function formattedDeductionAmount(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->deduction_amount, 0, ',', '.'),
);
}
protected function formattedAmount(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->attributes['total_amount'] ?? 0, 0, ',', '.'),
);
}
protected function statusLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->status->label(),
);
}
protected function formattedTotalAmount(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->total_amount, 0, ',', '.'),
);
}
#[Scope]
protected function cancelled(Builder $query): void
{
$query->where('status', PayrollStatus::CANCELLED);
}
#[Scope]
protected function paid(Builder $query): void
{
$query->where('status', PayrollStatus::PAID);
}
#[Scope]
protected function unpaid(Builder $query): void
{
$query->where('status', PayrollStatus::UNPAID);
}
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 payrollAdjustments(): HasMany
{
return $this->hasMany(PayrollAdjustment::class);
}
public function payrollPeriod(): BelongsTo
{
return $this->belongsTo(PayrollPeriod::class);
}
}