- 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.
123 lines
3.1 KiB
PHP
123 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\CashTransactionType;
|
|
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\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
|
|
#[Appends(['formatted_amount', 'formatted_balance_after', 'formatted_created_at', 'type_label'])]
|
|
#[Guarded(['id'])]
|
|
class CashTransaction extends Model implements HasMedia
|
|
{
|
|
use HasFactory, InteractsWithMedia, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'type' => CashTransactionType::class,
|
|
'amount' => 'integer',
|
|
'balance_after' => 'integer',
|
|
];
|
|
}
|
|
|
|
protected function formattedAmount(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp ' . number_format($this->amount, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedBalanceAfter(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp ' . number_format($this->balance_after, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedCreatedAt(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->created_at?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function typeLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->type->label(),
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function deposit(Builder $query): void
|
|
{
|
|
$query->where('type', CashTransactionType::DEPOSIT);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function expenseType(Builder $query): void
|
|
{
|
|
$query->where('type', CashTransactionType::EXPENSE);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function transfer(Builder $query): void
|
|
{
|
|
$query->where('type', CashTransactionType::TRANSFER);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function withdrawal(Builder $query): void
|
|
{
|
|
$query->where('type', CashTransactionType::WITHDRAWAL);
|
|
}
|
|
|
|
public function cashAccount(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CashAccount::class);
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by_id');
|
|
}
|
|
|
|
public function employeeAdvances(): HasMany
|
|
{
|
|
return $this->hasMany(EmployeeAdvance::class);
|
|
}
|
|
|
|
public function expense(): HasOne
|
|
{
|
|
return $this->hasOne(Expense::class);
|
|
}
|
|
|
|
public function order(): HasOne
|
|
{
|
|
return $this->hasOne(Order::class);
|
|
}
|
|
|
|
public function payrolls(): HasMany
|
|
{
|
|
return $this->hasMany(Payroll::class);
|
|
}
|
|
|
|
public function reference(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
}
|