mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +00:00
More refactoring.
This commit is contained in:
@@ -12,13 +12,10 @@ use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Session;
|
||||
use Steam;
|
||||
|
||||
|
||||
@@ -216,65 +213,6 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param int $page
|
||||
* @param int $pageSize
|
||||
*
|
||||
* @return LengthAwarePaginator
|
||||
*/
|
||||
public function getJournals(Account $account, int $page, int $pageSize = 50): LengthAwarePaginator
|
||||
{
|
||||
$offset = ($page - 1) * $pageSize;
|
||||
$query = $this->user
|
||||
->transactionJournals()
|
||||
->sortCorrectly()
|
||||
->expanded();
|
||||
|
||||
// expand query:
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
}
|
||||
)->where('source.account_id', $account->id);
|
||||
|
||||
|
||||
$count = $query->count();
|
||||
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
|
||||
$paginator = new LengthAwarePaginator($set, $count, $pageSize, $page);
|
||||
|
||||
return $paginator;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getJournalsInRange(Account $account, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$query = $this->user
|
||||
->transactionJournals()
|
||||
->expanded()
|
||||
->sortCorrectly()
|
||||
->where(
|
||||
function (Builder $q) use ($account) {
|
||||
$q->where('destination_account.id', $account->id);
|
||||
$q->orWhere('source_account.id', $account->id);
|
||||
}
|
||||
)
|
||||
->after($start)
|
||||
->before($end);
|
||||
|
||||
$set = $query->get(TransactionJournal::queryFields());
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the accounts of a user that have piggy banks connected to them.
|
||||
*
|
||||
@@ -282,36 +220,14 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
*/
|
||||
public function getPiggyBankAccounts(): Collection
|
||||
{
|
||||
$start = clone Session::get('start', new Carbon);
|
||||
$end = clone Session::get('end', new Carbon);
|
||||
$collection = new Collection(DB::table('piggy_banks')->distinct()->get(['piggy_banks.account_id']));
|
||||
$ids = $collection->pluck('account_id')->toArray();
|
||||
$accountIds = $collection->pluck('account_id')->toArray();
|
||||
$accounts = new Collection;
|
||||
|
||||
$ids = array_unique($ids);
|
||||
if (count($ids) > 0) {
|
||||
$accounts = $this->user->accounts()->whereIn('id', $ids)->where('accounts.active', 1)->get();
|
||||
$accountIds = array_unique($accountIds);
|
||||
if (count($accountIds) > 0) {
|
||||
$accounts = $this->user->accounts()->whereIn('id', $accountIds)->where('accounts.active', 1)->get();
|
||||
}
|
||||
|
||||
$accounts->each(
|
||||
function (Account $account) use ($start, $end) {
|
||||
$account->startBalance = Steam::balanceIgnoreVirtual($account, $start);
|
||||
$account->endBalance = Steam::balanceIgnoreVirtual($account, $end);
|
||||
$account->piggyBalance = 0;
|
||||
/** @var PiggyBank $piggyBank */
|
||||
foreach ($account->piggyBanks as $piggyBank) {
|
||||
$account->piggyBalance += $piggyBank->currentRelevantRep()->currentamount;
|
||||
}
|
||||
// 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;
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
return $accounts;
|
||||
|
||||
}
|
||||
@@ -329,33 +245,6 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
->where('accounts.active', 1)
|
||||
->where('account_meta.data', '"savingAsset"')
|
||||
->get(['accounts.*']);
|
||||
$start = clone Session::get('start', new Carbon);
|
||||
$end = clone Session::get('end', new Carbon);
|
||||
|
||||
$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;
|
||||
} else {
|
||||
if ($diff >= 0 && $account->startBalance > 0) {
|
||||
$pct = ($diff / $account->startBalance) * 100;
|
||||
} else {
|
||||
$pct = 100;
|
||||
}
|
||||
}
|
||||
$pct = $pct > 100 ? 100 : $pct;
|
||||
$account->difference = $diff;
|
||||
$account->percentage = round($pct);
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
return $accounts;
|
||||
}
|
||||
@@ -543,14 +432,6 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return AccountMeta::create(['name' => $name, 'data' => $value, 'account_id' => $account->id,]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function sumOfEverything(): string
|
||||
{
|
||||
return strval($this->user->transactions()->sum('amount'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
|
Reference in New Issue
Block a user