Files

120 lines
4.4 KiB
PHP

<?php
namespace App\Http\Livewire;
use App\Http\Controllers\TransactionController;
use Livewire\Component;
class FromAccount extends Component
{
public $profileAccounts = [];
public $fromAccountId;
public $selectedAccount = null;
public $label;
public $active = true;
#[\Livewire\Attributes\On('fromAccountRefresh')]
public function onRefresh()
{
\Log::info('[FROM-ACCOUNT] Received fromAccountRefresh event');
$this->resetForm();
}
public function mount()
{
$this->profileAccounts = $this->getProfileAccounts();
$this->preSelected();
}
public function getProfileAccounts()
{
\Log::info('[FROM-ACCOUNT] getProfileAccounts() called');
$transactionController = new TransactionController();
$accounts = $transactionController->getAccountsInfo();
\Log::info('[FROM-ACCOUNT] getAccountsInfo() returned', [
'accountCount' => $accounts->count(),
'firstAccountBalance' => $accounts->first()['balance'] ?? null,
'allBalances' => $accounts->map(fn($acc) => ['id' => $acc['id'], 'balance' => $acc['balance']])->toArray(),
]);
// Return empty collection if no accounts (e.g., Admin profiles)
if (!$accounts || $accounts->isEmpty()) {
return collect();
}
// Always filter out deleted accounts
$accounts = $accounts->filter(function ($account) {
return is_null($account['deletedAt']) || \Illuminate\Support\Carbon::parse($account['deletedAt'])->isFuture();
});
// If $active is true, also filter out inactive accounts
if ($this->active) {
$accounts = $accounts->filter(function ($account) {
return is_null($account['inactiveAt']) || \Illuminate\Support\Carbon::parse($account['inactiveAt'])->isFuture();
});
}
return $accounts;
}
public function resetForm()
{
\Log::info('[FROM-ACCOUNT] resetForm() called - refreshing account data');
// Force cache expiration to ensure fresh data
// (TransactionObserver has already cleared caches, but we ensure it here for synchronous refresh)
$profileType = session('activeProfileType');
$profileId = session('activeProfileId');
if ($profileType && $profileId) {
$cacheKey = "accounts_info_{$profileType}_{$profileId}";
// Force a fresh fetch by clearing cache right before getProfileAccounts()
\Illuminate\Support\Facades\Cache::forget($cacheKey);
\Log::info('[FROM-ACCOUNT] Cache cleared before refresh', ['cacheKey' => $cacheKey]);
}
// Now fetch fresh account data
$this->profileAccounts = $this->getProfileAccounts();
\Log::info('[FROM-ACCOUNT] Updated profileAccounts with fresh data', [
'count' => count($this->profileAccounts),
'firstBalance' => $this->profileAccounts->first()['balance'] ?? null,
]);
$this->preSelected();
}
public function preSelected()
{
if ($this->profileAccounts && count($this->profileAccounts) > 0) {
$firstActiveAccount = $this->profileAccounts->first(function ($account) {
return is_null($account['inactiveAt']) || \Illuminate\Support\Carbon::parse($account['inactiveAt'])->isFuture();
});
if ($firstActiveAccount) {
$activeState = $firstActiveAccount['inactive'] === true ? ' (' . strtolower(__('Inactive')) . ')' : '';
$firstActiveAccount['name'] = $firstActiveAccount['name'] . ' ' . __('account') . $activeState;
$this->fromAccountId = $firstActiveAccount['id'];
$this->selectedAccount = $firstActiveAccount;
$this->dispatch('fromAccountId', $this->selectedAccount);
}
}
}
public function fromAccountSelected($fromAccountId)
{
$this->fromAccountId = $fromAccountId;
$selectedAccount = collect($this->profileAccounts)->firstWhere('id', $fromAccountId);
$activeState = $selectedAccount['inactive'] === true ? ' (' . strtolower(__('Inactive')) . ')' : '';
$selectedAccount['name'] = $selectedAccount['name'] . ' ' . __('account') . $activeState;
$this->selectedAccount = $selectedAccount;
$this->dispatch('fromAccountId', $this->selectedAccount);
}
public function render()
{
return view('livewire.from-account');
}
}