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:
|
Contoh buruk:
|
||||||
|
|
||||||
```php
|
```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()) {
|
if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) {
|
||||||
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
|
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
|
||||||
} else {
|
} else {
|
||||||
return $this->first_name[0] . '. ' . $this->last_name;
|
return $this->first_name[0] . '. ' . $this->last_name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Contoh terbaik:
|
Contoh terbaik:
|
||||||
|
|
||||||
```php
|
```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
|
public function isVerifiedClient(): bool
|
||||||
|
|||||||
@ -90,13 +90,9 @@ public function createdByName(): Attribute
|
|||||||
public function isIncoming(): Attribute
|
public function isIncoming(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: function () {
|
get: fn () => $this->reference_type === EmployeeAdvance::class
|
||||||
if ($this->reference_type === EmployeeAdvance::class && $this->reference instanceof EmployeeAdvance) {
|
? $this->isEmployeeAdvanceRepayment()
|
||||||
return $this->reference->repayment_cash_transaction_id === $this->id;
|
: $this->type === CashTransactionType::DEPOSIT,
|
||||||
}
|
|
||||||
|
|
||||||
return $this->type === CashTransactionType::DEPOSIT;
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,13 +115,11 @@ public function referenceLabel(): Attribute
|
|||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: function () {
|
get: function () {
|
||||||
if ($this->reference_type === null) {
|
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) {
|
if ($this->reference_type === EmployeeAdvance::class) {
|
||||||
return $this->reference->repayment_cash_transaction_id === $this->id
|
return $this->getEmployeeAdvanceLabel();
|
||||||
? 'Pelunasan Kasbon'
|
|
||||||
: 'Pencairan Kasbon';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::labelForReferenceType($this->reference_type);
|
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
|
public static function mediaModuleName(): string
|
||||||
{
|
{
|
||||||
return 'cash';
|
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
|
public static function isIncomingTransaction(self $transaction): bool
|
||||||
{
|
{
|
||||||
if ($transaction->reference_type === EmployeeAdvance::class && $transaction->reference instanceof EmployeeAdvance) {
|
return (bool) $transaction->is_incoming;
|
||||||
return $transaction->reference->repayment_cash_transaction_id === $transaction->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::isIncomingReference($transaction->reference_type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user