firefly-iii/app/Repositories/Account/AccountRepository.php

207 lines
5.3 KiB
PHP
Raw Normal View History

2015-02-09 00:23:39 -06:00
<?php
/**
* AccountRepository.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
2015-02-09 00:23:39 -06:00
namespace FireflyIII\Repositories\Account;
2015-02-21 05:16:41 -06:00
use Carbon\Carbon;
use DB;
2015-02-09 00:23:39 -06:00
use FireflyIII\Models\Account;
2015-02-09 00:56:24 -06:00
use FireflyIII\Models\Transaction;
2016-03-20 05:47:10 -05:00
use FireflyIII\User;
2016-10-10 00:16:05 -05:00
use Illuminate\Support\Collection;
2016-10-10 00:20:49 -05:00
use Log;
2015-02-09 00:23:39 -06:00
2015-05-05 13:46:13 -05:00
2015-02-09 00:23:39 -06:00
/**
2015-05-26 01:17:58 -05:00
*
2015-02-09 00:23:39 -06:00
* Class AccountRepository
*
* @package FireflyIII\Repositories\Account
*/
class AccountRepository implements AccountRepositoryInterface
{
2016-03-20 05:47:10 -05:00
/** @var User */
private $user;
2016-03-30 10:47:13 -05:00
2016-03-20 05:47:10 -05:00
/**
* AttachmentRepository constructor.
*
* @param User $user
*/
public function __construct(User $user)
{
$this->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;
}
2016-10-10 00:12:39 -05:00
/**
* @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;
}
2016-10-10 00:14:01 -05:00
/**
* @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;
}
2016-10-10 00:16:05 -05:00
/**
* @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;
}
2016-10-10 00:20:49 -05:00
/**
* @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;
}
/**
* 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;
}
2015-03-29 01:14:32 -05:00
}