154 lines
4.2 KiB
PHP
154 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasModuleMedia;
|
|
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 Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends([
|
|
'amount_formatted',
|
|
'balance_after_formatted',
|
|
'reference_label',
|
|
'is_incoming',
|
|
'created_at_formatted',
|
|
'created_by_name',
|
|
])]
|
|
class CashTransaction extends Model implements HasMedia
|
|
{
|
|
use HasModuleMedia;
|
|
use InteractsWithActivityLog;
|
|
use SoftDeletes;
|
|
|
|
public static function mediaModuleName(): string
|
|
{
|
|
return 'cash';
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('photos');
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'amount' => 'integer',
|
|
'balance_after' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function amountFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->amount, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function balanceAfterFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->balance_after, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function referenceLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
if ($this->reference_type === EmployeeAdvance::class && $this->reference instanceof EmployeeAdvance) {
|
|
return $this->reference->repayment_cash_transaction_id === $this->id
|
|
? 'Pelunasan Kasbon'
|
|
: 'Pencairan Kasbon';
|
|
}
|
|
|
|
return self::labelForReferenceType($this->reference_type);
|
|
},
|
|
);
|
|
}
|
|
|
|
public function isIncoming(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
if ($this->reference_type === EmployeeAdvance::class && $this->reference instanceof EmployeeAdvance) {
|
|
return $this->reference->repayment_cash_transaction_id === $this->id;
|
|
}
|
|
|
|
return self::isIncomingTransaction($this);
|
|
},
|
|
);
|
|
}
|
|
|
|
public function createdAtFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->created_at?->translatedFormat('l, d F Y H:i'),
|
|
);
|
|
}
|
|
|
|
public function createdByName(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->createdBy?->profile?->full_name ?? $this->createdBy?->username,
|
|
);
|
|
}
|
|
|
|
public static function labelForReferenceType(?string $referenceType): string
|
|
{
|
|
if ($referenceType === null) {
|
|
return 'Setor Kas';
|
|
}
|
|
|
|
return match ($referenceType) {
|
|
Expense::class => 'Pengeluaran',
|
|
EmployeeAdvance::class => 'Kasbon Pegawai',
|
|
Payroll::class => 'Gaji Pegawai',
|
|
default => class_basename($referenceType),
|
|
};
|
|
}
|
|
|
|
public static function isIncomingReference(?string $referenceType): bool
|
|
{
|
|
if ($referenceType === null) {
|
|
return true;
|
|
}
|
|
|
|
return match ($referenceType) {
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
public static function isIncomingTransaction(self $transaction): bool
|
|
{
|
|
if ($transaction->reference_type === EmployeeAdvance::class && $transaction->reference instanceof EmployeeAdvance) {
|
|
return $transaction->reference->repayment_cash_transaction_id === $transaction->id;
|
|
}
|
|
|
|
return self::isIncomingReference($transaction->reference_type);
|
|
}
|
|
|
|
public function cashAccount(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CashAccount::class);
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by_id');
|
|
}
|
|
|
|
public function reference(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
}
|