2015-02-09 07:23:39 +01:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
|
* AccountRepository.php
|
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* 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 12:41:23 +02:00
|
|
|
*/
|
|
|
|
|
|
2016-05-20 08:57:45 +02:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
|
|
|
2015-02-09 07:23:39 +01:00
|
|
|
namespace FireflyIII\Repositories\Account;
|
|
|
|
|
|
2015-02-21 12:16:41 +01:00
|
|
|
use Carbon\Carbon;
|
2015-04-13 20:43:58 +02:00
|
|
|
use DB;
|
2016-10-09 10:53:37 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2015-02-09 07:23:39 +01:00
|
|
|
use FireflyIII\Models\Account;
|
2015-02-24 21:10:25 +01:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
2015-02-09 07:56:24 +01:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2016-03-20 11:47:10 +01:00
|
|
|
use FireflyIII\User;
|
2016-10-06 21:18:43 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2016-05-05 18:59:46 +02:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2015-03-13 08:44:07 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2015-03-21 08:51:34 +01:00
|
|
|
use Steam;
|
2015-02-09 07:23:39 +01:00
|
|
|
|
2015-05-05 20:46:13 +02:00
|
|
|
|
2015-02-09 07:23:39 +01:00
|
|
|
/**
|
2015-05-26 08:17:58 +02:00
|
|
|
*
|
2015-02-09 07:23:39 +01:00
|
|
|
* Class AccountRepository
|
|
|
|
|
*
|
|
|
|
|
* @package FireflyIII\Repositories\Account
|
|
|
|
|
*/
|
|
|
|
|
class AccountRepository implements AccountRepositoryInterface
|
|
|
|
|
{
|
|
|
|
|
|
2016-03-20 11:47:10 +01:00
|
|
|
/** @var User */
|
|
|
|
|
private $user;
|
2016-03-30 17:47:13 +02:00
|
|
|
|
2016-03-20 11:47:10 +01:00
|
|
|
/**
|
|
|
|
|
* AttachmentRepository constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param User $user
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(User $user)
|
|
|
|
|
{
|
|
|
|
|
$this->user = $user;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 08:18:47 +02:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 08:20:29 +02:00
|
|
|
/**
|
|
|
|
|
* 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-05-13 15:53:39 +02:00
|
|
|
|
2015-04-03 09:30:44 +02:00
|
|
|
/**
|
2016-10-09 10:53:37 +02:00
|
|
|
* Returns the transaction from a journal that is related to a given account. Since a journal generally only contains
|
|
|
|
|
* two transactions, this will return one of the two. This method fails horribly when the journal has more than two transactions,
|
|
|
|
|
* but luckily it isn't used for such folly.
|
|
|
|
|
*
|
2015-04-03 09:30:44 +02:00
|
|
|
* @param TransactionJournal $journal
|
|
|
|
|
* @param Account $account
|
|
|
|
|
*
|
|
|
|
|
* @return Transaction
|
2016-10-09 10:53:37 +02:00
|
|
|
* @throws FireflyException
|
2015-04-03 09:30:44 +02:00
|
|
|
*/
|
2016-03-19 17:28:04 +01:00
|
|
|
public function getFirstTransaction(TransactionJournal $journal, Account $account): Transaction
|
2015-04-03 09:30:44 +02:00
|
|
|
{
|
2016-10-09 10:53:37 +02:00
|
|
|
$count = $journal->transactions()->count();
|
|
|
|
|
if ($count > 2) {
|
|
|
|
|
throw new FireflyException(sprintf('Cannot use getFirstTransaction on journal #%d', $journal->id));
|
|
|
|
|
}
|
2015-12-26 08:44:34 +01:00
|
|
|
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
|
2016-03-19 17:28:04 +01:00
|
|
|
if (is_null($transaction)) {
|
|
|
|
|
$transaction = new Transaction;
|
|
|
|
|
}
|
2015-04-03 09:30:44 +02:00
|
|
|
|
2015-12-26 08:44:34 +01:00
|
|
|
return $transaction;
|
2015-04-03 09:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-08 14:50:18 +02:00
|
|
|
/**
|
|
|
|
|
* Returns the date of the very last transaction in this account.
|
|
|
|
|
*
|
|
|
|
|
* @param Account $account
|
|
|
|
|
*
|
|
|
|
|
* @return Carbon
|
|
|
|
|
*/
|
|
|
|
|
public function newestJournalDate(Account $account): Carbon
|
|
|
|
|
{
|
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
|
$journal = TransactionJournal::
|
|
|
|
|
leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
|
->where('transactions.account_id', $account->id)
|
2016-05-11 07:57:16 +02:00
|
|
|
->sortCorrectly()
|
2016-04-08 14:50:18 +02:00
|
|
|
->first(['transaction_journals.*']);
|
|
|
|
|
if (is_null($journal)) {
|
2016-05-13 19:40:13 +02:00
|
|
|
return new Carbon('1900-01-01');
|
2016-04-08 14:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-13 19:40:13 +02:00
|
|
|
return $journal->date;
|
2016-04-08 14:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the date of the very first transaction in this account.
|
|
|
|
|
*
|
|
|
|
|
* @param Account $account
|
|
|
|
|
*
|
|
|
|
|
* @return Carbon
|
|
|
|
|
*/
|
|
|
|
|
public function oldestJournalDate(Account $account): Carbon
|
|
|
|
|
{
|
2016-10-09 10:57:06 +02:00
|
|
|
$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);
|
2016-04-08 14:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-09 10:57:06 +02:00
|
|
|
return $first;
|
2016-04-08 14:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-14 14:25:29 +01:00
|
|
|
/**
|
|
|
|
|
* @param Account $account
|
|
|
|
|
*
|
|
|
|
|
* @return TransactionJournal|null
|
|
|
|
|
*/
|
2016-03-19 17:28:04 +01:00
|
|
|
public function openingBalanceTransaction(Account $account): TransactionJournal
|
2015-02-14 14:25:29 +01:00
|
|
|
{
|
2015-12-26 08:44:34 +01:00
|
|
|
$journal = TransactionJournal
|
2016-05-11 07:57:16 +02:00
|
|
|
::sortCorrectly()
|
2016-01-15 22:41:26 +01:00
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
|
->where('transactions.account_id', $account->id)
|
2015-12-09 22:39:50 -02:00
|
|
|
->transactionTypes([TransactionType::OPENING_BALANCE])
|
2015-06-05 12:18:20 +02:00
|
|
|
->first(['transaction_journals.*']);
|
2016-03-19 17:34:37 +01:00
|
|
|
if (is_null($journal)) {
|
2016-03-19 17:34:02 +01:00
|
|
|
return new TransactionJournal;
|
|
|
|
|
}
|
2015-12-26 08:44:34 +01:00
|
|
|
|
|
|
|
|
return $journal;
|
2015-02-14 14:25:29 +01:00
|
|
|
}
|
2015-03-29 08:14:32 +02:00
|
|
|
}
|