2015-02-09 00:23:39 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* AccountRepository.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05: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 05:41:23 -05:00
|
|
|
*/
|
|
|
|
|
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;
|
2015-04-13 13:43:58 -05:00
|
|
|
use DB;
|
2016-10-09 03:53:37 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2015-02-09 00:23:39 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2015-02-24 14:10:25 -06:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
2015-02-09 00:56:24 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2016-03-20 05:47:10 -05:00
|
|
|
use FireflyIII\User;
|
2016-10-06 14:18:43 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2016-05-05 11:59:46 -05:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2015-03-13 02:44:07 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2015-03-21 02:51:34 -05:00
|
|
|
use Steam;
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-09 01:18:47 -05: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 01:20:29 -05: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 08:53:39 -05:00
|
|
|
/**
|
2016-10-09 03:53:37 -05:00
|
|
|
* Returns the date of the first time the Account has been used, or today if it has never been used.
|
|
|
|
*
|
2016-05-13 08:53:39 -05:00
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function firstUseDate(Account $account): Carbon
|
|
|
|
{
|
2016-10-09 03:53:37 -05:00
|
|
|
$first = new Carbon;
|
2016-05-13 08:53:39 -05:00
|
|
|
|
|
|
|
/** @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-04-03 02:30:44 -05:00
|
|
|
/**
|
2016-10-09 03:53:37 -05: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 02:30:44 -05:00
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return Transaction
|
2016-10-09 03:53:37 -05:00
|
|
|
* @throws FireflyException
|
2015-04-03 02:30:44 -05:00
|
|
|
*/
|
2016-03-19 11:28:04 -05:00
|
|
|
public function getFirstTransaction(TransactionJournal $journal, Account $account): Transaction
|
2015-04-03 02:30:44 -05:00
|
|
|
{
|
2016-10-09 03:53:37 -05:00
|
|
|
$count = $journal->transactions()->count();
|
|
|
|
if ($count > 2) {
|
|
|
|
throw new FireflyException(sprintf('Cannot use getFirstTransaction on journal #%d', $journal->id));
|
|
|
|
}
|
2015-12-26 01:44:34 -06:00
|
|
|
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
|
2016-03-19 11:28:04 -05:00
|
|
|
if (is_null($transaction)) {
|
|
|
|
$transaction = new Transaction;
|
|
|
|
}
|
2015-04-03 02:30:44 -05:00
|
|
|
|
2015-12-26 01:44:34 -06:00
|
|
|
return $transaction;
|
2015-04-03 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
2015-04-13 13:43:58 -05:00
|
|
|
/**
|
|
|
|
* Get the accounts of a user that have piggy banks connected to them.
|
|
|
|
*
|
2016-05-13 10:22:24 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
2015-04-13 13:43:58 -05:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-05-13 10:22:24 -05:00
|
|
|
public function getPiggyBankAccounts(Carbon $start, Carbon $end): Collection
|
2015-04-13 13:43:58 -05:00
|
|
|
{
|
2015-12-30 02:30:06 -06:00
|
|
|
$collection = new Collection(DB::table('piggy_banks')->distinct()->get(['piggy_banks.account_id']));
|
2016-05-13 08:58:30 -05:00
|
|
|
$accountIds = $collection->pluck('account_id')->toArray();
|
2016-01-01 05:41:00 -06:00
|
|
|
$accounts = new Collection;
|
2016-05-13 08:58:30 -05:00
|
|
|
$accountIds = array_unique($accountIds);
|
|
|
|
if (count($accountIds) > 0) {
|
|
|
|
$accounts = $this->user->accounts()->whereIn('id', $accountIds)->where('accounts.active', 1)->get();
|
2015-04-13 13:43:58 -05:00
|
|
|
}
|
|
|
|
|
2016-05-13 10:22:24 -05:00
|
|
|
$accounts->each(
|
|
|
|
function (Account $account) use ($start, $end) {
|
|
|
|
$account->startBalance = Steam::balanceIgnoreVirtual($account, $start);
|
|
|
|
$account->endBalance = Steam::balanceIgnoreVirtual($account, $end);
|
2016-05-20 02:25:17 -05:00
|
|
|
$account->piggyBalance = '0';
|
2016-05-13 10:22:24 -05:00
|
|
|
/** @var PiggyBank $piggyBank */
|
|
|
|
foreach ($account->piggyBanks as $piggyBank) {
|
2016-05-20 02:25:17 -05:00
|
|
|
$account->piggyBalance = bcadd($account->piggyBalance, $piggyBank->currentRelevantRep()->currentamount);
|
2016-05-13 10:22:24 -05:00
|
|
|
}
|
|
|
|
// sum of piggy bank amounts on this account:
|
|
|
|
// diff between endBalance and piggyBalance.
|
|
|
|
// then, percentage.
|
|
|
|
$difference = bcsub($account->endBalance, $account->piggyBalance);
|
|
|
|
$account->difference = $difference;
|
|
|
|
$account->percentage = $difference != 0 && $account->endBalance != 0 ? round((($difference / $account->endBalance) * 100)) : 100;
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2015-04-13 13:43:58 -05:00
|
|
|
return $accounts;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-21 02:51:34 -05:00
|
|
|
/**
|
2016-05-13 10:22:24 -05:00
|
|
|
* Get savings accounts.
|
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2015-03-21 02:51:34 -05:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-05-13 10:22:24 -05:00
|
|
|
public function getSavingsAccounts(Carbon $start, Carbon $end): Collection
|
2015-03-21 02:51:34 -05:00
|
|
|
{
|
2016-03-20 10:47:53 -05:00
|
|
|
$accounts = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')
|
|
|
|
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
|
|
|
|
->where('account_meta.name', 'accountRole')
|
|
|
|
->where('accounts.active', 1)
|
|
|
|
->where('account_meta.data', '"savingAsset"')
|
|
|
|
->get(['accounts.*']);
|
2015-03-21 02:51:34 -05:00
|
|
|
|
2016-05-13 10:22:24 -05:00
|
|
|
$accounts->each(
|
|
|
|
function (Account $account) use ($start, $end) {
|
|
|
|
$account->startBalance = Steam::balance($account, $start);
|
|
|
|
$account->endBalance = Steam::balance($account, $end);
|
|
|
|
|
|
|
|
// diff (negative when lost, positive when gained)
|
|
|
|
$diff = bcsub($account->endBalance, $account->startBalance);
|
|
|
|
|
|
|
|
if ($diff < 0 && $account->startBalance > 0) {
|
|
|
|
// percentage lost compared to start.
|
|
|
|
$pct = (($diff * -1) / $account->startBalance) * 100;
|
2016-05-20 10:53:03 -05:00
|
|
|
|
|
|
|
$pct = $pct > 100 ? 100 : $pct;
|
|
|
|
$account->difference = $diff;
|
|
|
|
$account->percentage = round($pct);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($diff >= 0 && $account->startBalance > 0) {
|
|
|
|
$pct = ($diff / $account->startBalance) * 100;
|
|
|
|
$pct = $pct > 100 ? 100 : $pct;
|
|
|
|
$account->difference = $diff;
|
|
|
|
$account->percentage = round($pct);
|
|
|
|
|
|
|
|
return;
|
2016-05-13 10:22:24 -05:00
|
|
|
}
|
|
|
|
$account->difference = $diff;
|
2016-05-20 10:53:03 -05:00
|
|
|
$account->percentage = 100;
|
2016-05-13 10:22:24 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2015-03-21 02:51:34 -05:00
|
|
|
return $accounts;
|
|
|
|
}
|
2016-05-15 10:46:53 -05:00
|
|
|
|
2015-03-02 04:42:27 -06:00
|
|
|
/**
|
2016-05-13 08:53:39 -05:00
|
|
|
* @param Collection $accounts
|
|
|
|
* @param array $types
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function journalsInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
|
|
|
// first collect actual transaction journals (fairly easy)
|
2016-08-26 02:30:52 -05:00
|
|
|
$query = $this->user->transactionJournals()->expanded()->sortCorrectly();
|
2016-05-13 08:53:39 -05:00
|
|
|
|
|
|
|
if ($end >= $start) {
|
|
|
|
$query->before($end)->after($start);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($types) > 0) {
|
|
|
|
$query->transactionTypes($types);
|
|
|
|
}
|
|
|
|
if ($accounts->count() > 0) {
|
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
2016-05-14 15:11:49 -05:00
|
|
|
$query->leftJoin(
|
|
|
|
'transactions as source', function (JoinClause $join) {
|
2016-05-14 15:21:08 -05:00
|
|
|
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
2016-05-14 15:11:49 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
$query->leftJoin(
|
|
|
|
'transactions as destination', function (JoinClause $join) {
|
2016-05-14 15:21:08 -05:00
|
|
|
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
2016-05-14 15:11:49 -05:00
|
|
|
}
|
|
|
|
);
|
2016-10-06 14:18:43 -05:00
|
|
|
$query->where(
|
2016-10-06 22:44:21 -05:00
|
|
|
// source.account_id in accountIds XOR destination.account_id in accountIds
|
|
|
|
function (Builder $query) use ($accountIds) {
|
|
|
|
$query->where(
|
|
|
|
function (Builder $q1) use ($accountIds) {
|
|
|
|
$q1->whereIn('source.account_id', $accountIds)
|
|
|
|
->whereNotIn('destination.account_id', $accountIds);
|
2016-10-06 14:18:43 -05:00
|
|
|
}
|
|
|
|
)->orWhere(
|
2016-10-06 22:44:21 -05:00
|
|
|
function (Builder $q2) use ($accountIds) {
|
|
|
|
$q2->whereIn('destination.account_id', $accountIds)
|
|
|
|
->whereNotIn('source.account_id', $accountIds);
|
2016-10-06 14:18:43 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2016-05-13 08:53:39 -05:00
|
|
|
}
|
2016-10-06 22:44:21 -05:00
|
|
|
|
2016-05-13 08:53:39 -05:00
|
|
|
// that should do it:
|
2016-05-15 10:46:53 -05:00
|
|
|
$fields = TransactionJournal::queryFields();
|
|
|
|
$complete = $query->get($fields);
|
2016-05-13 08:53:39 -05:00
|
|
|
|
|
|
|
return $complete;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2015-03-02 04:42:27 -06:00
|
|
|
* @param Account $account
|
2015-05-20 12:55:53 -05:00
|
|
|
* @param Carbon $date
|
2015-03-02 04:42:27 -06:00
|
|
|
*
|
2016-04-27 12:22:15 -05:00
|
|
|
* @return string
|
2015-03-02 04:42:27 -06:00
|
|
|
*/
|
2016-03-19 11:28:04 -05:00
|
|
|
public function leftOnAccount(Account $account, Carbon $date): string
|
2015-03-02 04:42:27 -06:00
|
|
|
{
|
2015-05-17 02:35:49 -05:00
|
|
|
|
2016-04-27 12:21:47 -05:00
|
|
|
$balance = Steam::balanceIgnoreVirtual($account, $date);
|
2015-03-02 04:42:27 -06:00
|
|
|
/** @var PiggyBank $p */
|
2016-08-26 02:30:52 -05:00
|
|
|
foreach ($account->piggyBanks()->get() as $p) {
|
2016-08-24 13:06:45 -05:00
|
|
|
$currentAmount = $p->currentRelevantRep()->currentamount ?? '0';
|
|
|
|
|
|
|
|
$balance = bcsub($balance, $currentAmount);
|
2015-03-02 04:42:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $balance;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-08 07:50:18 -05: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 00:57:16 -05:00
|
|
|
->sortCorrectly()
|
2016-04-08 07:50:18 -05:00
|
|
|
->first(['transaction_journals.*']);
|
|
|
|
if (is_null($journal)) {
|
2016-05-13 12:40:13 -05:00
|
|
|
return new Carbon('1900-01-01');
|
2016-04-08 07:50:18 -05:00
|
|
|
}
|
|
|
|
|
2016-05-13 12:40:13 -05:00
|
|
|
return $journal->date;
|
2016-04-08 07:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the date of the very first transaction in this account.
|
|
|
|
*
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function oldestJournalDate(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-13 12:40:13 -05:00
|
|
|
->orderBy('transaction_journals.date', 'ASC')
|
|
|
|
->orderBy('transaction_journals.order', 'DESC')
|
|
|
|
->orderBy('transaction_journals.id', 'ÅSC')
|
2016-04-08 07:50:18 -05:00
|
|
|
->first(['transaction_journals.*']);
|
|
|
|
if (is_null($journal)) {
|
2016-05-13 12:40:13 -05:00
|
|
|
return new Carbon('1900-01-01');
|
2016-04-08 07:50:18 -05:00
|
|
|
}
|
|
|
|
|
2016-05-13 12:40:13 -05:00
|
|
|
return $journal->date;
|
2016-04-08 07:50:18 -05:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:25:29 -06:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return TransactionJournal|null
|
|
|
|
*/
|
2016-03-19 11:28:04 -05:00
|
|
|
public function openingBalanceTransaction(Account $account): TransactionJournal
|
2015-02-14 07:25:29 -06:00
|
|
|
{
|
2015-12-26 01:44:34 -06:00
|
|
|
$journal = TransactionJournal
|
2016-05-11 00:57:16 -05:00
|
|
|
::sortCorrectly()
|
2016-01-15 15:41:26 -06:00
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->where('transactions.account_id', $account->id)
|
2015-12-09 18:39:50 -06:00
|
|
|
->transactionTypes([TransactionType::OPENING_BALANCE])
|
2015-06-05 05:18:20 -05:00
|
|
|
->first(['transaction_journals.*']);
|
2016-03-19 11:34:37 -05:00
|
|
|
if (is_null($journal)) {
|
2016-03-19 11:34:02 -05:00
|
|
|
return new TransactionJournal;
|
|
|
|
}
|
2015-12-26 01:44:34 -06:00
|
|
|
|
|
|
|
return $journal;
|
2015-02-14 07:25:29 -06:00
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|