Files
timebank-cc-public/app/Http/Livewire/FromAccount.php
Ronald Huynen 2547717edb Initial commit
2026-03-23 21:37:59 +01:00

90 lines
3.0 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;
public function mount()
{
$this->profileAccounts = $this->getProfileAccounts();
$this->preSelected();
}
public function getProfileAccounts()
{
$transactionController = new TransactionController();
$accounts = $transactionController->getAccountsInfo();
// 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()
{
$this->profileAccounts = $this->getProfileAccounts();
$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'] = __(ucfirst(strtolower($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')) . ')' : '';
// Translate the 'name' field
$selectedAccount['name'] = __(ucfirst(strtolower($selectedAccount['name']))) . ' ' . __('account') . $activeState;
$this->selectedAccount = $selectedAccount;
$this->dispatch('fromAccountId', $this->selectedAccount);
}
public function render()
{
return view('livewire.from-account');
}
}