refactor: refactor CashTransaction model logic into helper methods and update best practice documentation for Eloquent attributes
This commit is contained in:
parent
016e8d95d9
commit
1ffb913fe8
@ -45,22 +45,32 @@ ### **Prinsip _single responsibility_**
|
||||
Contoh buruk:
|
||||
|
||||
```php
|
||||
public function getFullNameAttribute(): string
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
public function fullName(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function () {
|
||||
if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) {
|
||||
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
|
||||
} else {
|
||||
return $this->first_name[0] . '. ' . $this->last_name;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
Contoh terbaik:
|
||||
|
||||
```php
|
||||
public function getFullNameAttribute(): string
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
public function fullName(): Attribute
|
||||
{
|
||||
return $this->isVerifiedClient() ? $this->getFullNameLong() : $this->getFullNameShort();
|
||||
return Attribute::make(
|
||||
get: fn () => $this->isVerifiedClient() ? $this->getFullNameLong() : $this->getFullNameShort()
|
||||
);
|
||||
}
|
||||
|
||||
public function isVerifiedClient(): bool
|
||||
|
||||
@ -90,13 +90,9 @@ public function createdByName(): Attribute
|
||||
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 $this->type === CashTransactionType::DEPOSIT;
|
||||
},
|
||||
get: fn () => $this->reference_type === EmployeeAdvance::class
|
||||
? $this->isEmployeeAdvanceRepayment()
|
||||
: $this->type === CashTransactionType::DEPOSIT,
|
||||
);
|
||||
}
|
||||
|
||||
@ -119,13 +115,11 @@ public function referenceLabel(): Attribute
|
||||
return Attribute::make(
|
||||
get: function () {
|
||||
if ($this->reference_type === null) {
|
||||
return $this->type === CashTransactionType::WITHDRAWAL ? 'Tarik Kas' : 'Setor Kas';
|
||||
return $this->getManualTransactionLabel();
|
||||
}
|
||||
|
||||
if ($this->reference_type === EmployeeAdvance::class && $this->reference instanceof EmployeeAdvance) {
|
||||
return $this->reference->repayment_cash_transaction_id === $this->id
|
||||
? 'Pelunasan Kasbon'
|
||||
: 'Pencairan Kasbon';
|
||||
if ($this->reference_type === EmployeeAdvance::class) {
|
||||
return $this->getEmployeeAdvanceLabel();
|
||||
}
|
||||
|
||||
return self::labelForReferenceType($this->reference_type);
|
||||
@ -133,6 +127,22 @@ public function referenceLabel(): Attribute
|
||||
);
|
||||
}
|
||||
|
||||
public function isEmployeeAdvanceRepayment(): bool
|
||||
{
|
||||
return $this->reference instanceof EmployeeAdvance
|
||||
&& $this->reference->repayment_cash_transaction_id === $this->id;
|
||||
}
|
||||
|
||||
public function getManualTransactionLabel(): string
|
||||
{
|
||||
return $this->type === CashTransactionType::WITHDRAWAL ? 'Tarik Kas' : 'Setor Kas';
|
||||
}
|
||||
|
||||
public function getEmployeeAdvanceLabel(): string
|
||||
{
|
||||
return $this->isEmployeeAdvanceRepayment() ? 'Pelunasan Kasbon' : 'Pencairan Kasbon';
|
||||
}
|
||||
|
||||
public static function mediaModuleName(): string
|
||||
{
|
||||
return 'cash';
|
||||
@ -158,23 +168,8 @@ public static function labelForReferenceType(?string $referenceType): string
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
return (bool) $transaction->is_incoming;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user