user = $user; } /** * Moved here from account CRUD * * @param array $types * * @return int */ public function count(array $types):int { $count = $this->user->accounts()->accountTypeIn($types)->count(); return $count; } /** * Moved here from account CRUD. * * @param Account $account * @param Account $moveTo * * @return bool */ public function destroy(Account $account, Account $moveTo): bool { if (!is_null($moveTo->id)) { DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]); } if (!is_null($account)) { $account->delete(); } return true; } /** * @param $accountId * * @return Account */ public function find(int $accountId): Account { $account = $this->user->accounts()->find($accountId); if (is_null($account)) { return new Account; } return $account; } /** * @param string $number * @param array $types * * @return Account */ public function findByAccountNumber(string $number, array $types): Account { $query = $this->user->accounts() ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id') ->where('account_meta.name', 'accountNumber') ->where('account_meta.data', json_encode($number)); if (count($types) > 0) { $query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id'); $query->whereIn('account_types.type', $types); } /** @var Collection $accounts */ $accounts = $query->get(['accounts.*']); if ($accounts->count() > 0) { return $accounts->first(); } return new Account; } /** * @param string $iban * @param array $types * * @return Account */ public function findByIban(string $iban, array $types): Account { $query = $this->user->accounts()->where('iban', '!=', '')->whereNotNull('iban'); if (count($types) > 0) { $query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id'); $query->whereIn('account_types.type', $types); } $accounts = $query->get(['accounts.*']); /** @var Account $account */ foreach ($accounts as $account) { if ($account->iban === $iban) { return $account; } } return new Account; } /** * @param string $name * @param array $types * * @return Account */ public function findByName(string $name, array $types): Account { $query = $this->user->accounts(); if (count($types) > 0) { $query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id'); $query->whereIn('account_types.type', $types); } Log::debug(sprintf('Searching for account named %s of the following type(s)', $name), ['types' => $types]); $accounts = $query->get(['accounts.*']); /** @var Account $account */ foreach ($accounts as $account) { if ($account->name === $name) { Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id)); return $account; } } Log::debug('Found nothing.'); return new Account; } /** * @param array $accountIds * * @return Collection */ public function getAccountsById(array $accountIds): Collection { /** @var Collection $result */ $query = $this->user->accounts(); if (count($accountIds) > 0) { $query->whereIn('accounts.id', $accountIds); } $result = $query->get(['accounts.*']); $result = $result->sortBy( function (Account $account) { return strtolower($account->name); } ); return $result; } /** * @param array $types * * @return Collection */ public function getAccountsByType(array $types): Collection { /** @var Collection $result */ $query = $this->user->accounts(); if (count($types) > 0) { $query->accountTypeIn($types); } $result = $query->get(['accounts.*']); $result = $result->sortBy( function (Account $account) { return strtolower($account->name); } ); return $result; } /** * @param array $types * * @return Collection */ public function getActiveAccountsByType(array $types): Collection { /** @var Collection $result */ $query = $this->user->accounts()->with( ['accountmeta' => function (HasMany $query) { $query->where('name', 'accountRole'); }] ); if (count($types) > 0) { $query->accountTypeIn($types); } $query->where('active', 1); $result = $query->get(['accounts.*']); $result = $result->sortBy( function (Account $account) { return strtolower($account->name); } ); return $result; } /** * Returns the date of the very first transaction in this account. * * @param Account $account * * @return Carbon */ public function oldestJournalDate(Account $account): Carbon { $first = new Carbon; /** @var Transaction $first */ $date = $account->transactions() ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') ->orderBy('transaction_journals.date', 'ASC') ->orderBy('transaction_journals.order', 'DESC') ->orderBy('transaction_journals.id', 'ASC') ->first(['transaction_journals.date']); if (!is_null($date)) { $first = new Carbon($date->date); } return $first; } }