- 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.
77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Gender;
|
|
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_birth_date', 'gender_label', 'formatted_phone_number'])]
|
|
#[Guarded(['id'])]
|
|
class UserProfile extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'gender' => Gender::class,
|
|
'birth_date' => 'date:Y-m-d',
|
|
];
|
|
}
|
|
|
|
protected function formattedBirthDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->birth_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function genderLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->gender->label(),
|
|
);
|
|
}
|
|
|
|
protected function formattedPhoneNumber(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->phone_number
|
|
? substr($this->phone_number, 0, 4) . ' ' . substr($this->phone_number, 4, 4) . ' ' . substr($this->phone_number, 8)
|
|
: null,
|
|
set: fn (?string $value) => $value ? preg_replace('/\s/', '', $value) : null,
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function female(Builder $query): void
|
|
{
|
|
$query->where('gender', Gender::FEMALE);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function hasPhoneNumber(Builder $query): void
|
|
{
|
|
$query->whereNotNull('phone_number');
|
|
}
|
|
|
|
#[Scope]
|
|
protected function male(Builder $query): void
|
|
{
|
|
$query->where('gender', Gender::MALE);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
}
|