dstpabuaran.com/app/Services/Admin/Finance/CashAccountService.php
Yoga Pangestu c3d8ea2f66 feat: implement cash account management with deposit and withdrawal functionality
- Introduced CashAccountController for managing cash accounts.
- Created CashTransactionRequest and CashAccountRequest for transaction validation.
- Developed CashAccountService to handle business logic for cash transactions.
- Added UI components for cash account management, including deposit and withdrawal dialogs.
- Implemented data tables for displaying transactions and cash account details.
- Updated routes to include cash account management endpoints.
- Added tests for cash account functionality, including deposit and withdrawal operations.
2026-07-29 02:08:39 +07:00

72 lines
2.0 KiB
PHP

<?php
namespace App\Services\Admin\Finance;
use App\Enums\CashTransactionType;
use App\Models\CashAccount;
use App\Models\CashTransaction;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
class CashAccountService
{
public function get(): ?CashAccount
{
return CashAccount::first();
}
public function getAllTransactions(): Collection
{
$cashAccount = $this->get();
if (! $cashAccount) {
return collect();
}
return $cashAccount->cashTransactions()
->with('createdBy')
->latest()
->get();
}
public function deposit(array $data): CashTransaction
{
$cashAccount = CashAccount::firstOrFail();
$newBalance = $cashAccount->balance + $data['amount'];
$cashAccount->update(['balance' => $newBalance]);
return CashTransaction::create([
'cash_account_id' => $cashAccount->id,
'created_by_id' => auth()->id(),
'amount' => $data['amount'],
'balance_after' => $newBalance,
'type' => CashTransactionType::DEPOSIT,
'description' => $data['description'],
]);
}
public function withdrawal(array $data): CashTransaction
{
$cashAccount = CashAccount::firstOrFail();
if ($cashAccount->balance < $data['amount']) {
throw ValidationException::withMessages([
'amount' => 'Saldo tidak mencukupi.',
]);
}
$newBalance = $cashAccount->balance - $data['amount'];
$cashAccount->update(['balance' => $newBalance]);
return CashTransaction::create([
'cash_account_id' => $cashAccount->id,
'created_by_id' => auth()->id(),
'amount' => $data['amount'],
'balance_after' => $newBalance,
'type' => CashTransactionType::WITHDRAWAL,
'description' => $data['description'],
]);
}
}