More refactoring.

This commit is contained in:
James Cole 2016-05-13 15:58:30 +02:00
parent 20e1e50032
commit 3e36a29c23
5 changed files with 6 additions and 173 deletions

View File

@ -10,7 +10,6 @@ use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Console\Command;
@ -249,9 +248,7 @@ having transaction_count = 0
/** @var User $user */
foreach ($userRepository->all() as $user) {
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class, [$user]);
$sum = $repository->sumOfEverything();
$sum = $user->transactions()->sum('amount');
if (bccomp($sum, '0') !== 0) {
$this->error('Error: Transactions for user #' . $user->id . ' (' . $user->email . ') are off by ' . $sum . '!');
}

View File

@ -127,12 +127,6 @@ class HomeController extends Controller
$savingsTotal = bcadd($savingsTotal, Steam::balance($savingAccount, $end));
}
$sum = $repository->sumOfEverything();
if (bccomp($sum, '0') !== 0) {
Session::flash('error', strval(trans('firefly.unbalanced_error', ['amount' => Amount::format($sum, false)])));
}
foreach ($accounts as $account) {
$set = $repository->journalsInPeriod(new Collection([$account]), [], $start, $end);
$set = $set->splice(0, 10);

View File

@ -169,7 +169,7 @@ class ReportController extends Controller
*/
if ($start->between($first, $last) || $end->between($first, $last)) {
$exists = true;
$journals = $repos->getJournalsInRange($account, $start, $end);
$journals = $repos->journalsInPeriod($accounts, [], $start, $end);
}
/*

View File

@ -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

View File

@ -89,34 +89,6 @@ interface AccountRepositoryInterface
public function getFirstTransaction(TransactionJournal $journal, Account $account): Transaction;
/**
* @deprecated
*
* SEE OTHER GETJOURNALS METHODS.
*
* @param Account $account
* @param int $page
* @param int $pageSize
*
* @return LengthAwarePaginator
*/
public function getJournals(Account $account, int $page, int $pageSize = 50): LengthAwarePaginator;
/**
* @deprecated
*
* SEE OTHER GETJOURNALS METHODS.
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getJournalsInRange(Account $account, Carbon $start, Carbon $end): Collection;
/**
* @deprecated
*
* Get the accounts of a user that have piggy banks connected to them.
*
* @return Collection
@ -124,8 +96,6 @@ interface AccountRepositoryInterface
public function getPiggyBankAccounts(): Collection;
/**
* @deprecated
*
* Get savings accounts and the balance difference in the period.
*
* @return Collection
@ -143,7 +113,6 @@ interface AccountRepositoryInterface
public function journalsInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): Collection;
/**
* @deprecated
*
* @param Account $account
* @param Carbon $date
@ -171,7 +140,6 @@ interface AccountRepositoryInterface
public function oldestJournalDate(Account $account): Carbon;
/**
*
*
* @param Account $account
*
@ -204,13 +172,6 @@ interface AccountRepositoryInterface
*/
public function storeMeta(Account $account, string $name, $value): AccountMeta;
/**
* @deprecated
*
* @return string
*/
public function sumOfEverything() : string;
/**
* @param Account $account
* @param array $data