Move some methods around, refactoring.

This commit is contained in:
James Cole
2016-10-09 07:58:27 +02:00
parent ea7ee7ee9a
commit c64771b76b
19 changed files with 207 additions and 343 deletions

View File

@@ -20,6 +20,7 @@ use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Account\AccountTaskerInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
@@ -196,12 +197,14 @@ class AccountController extends Controller
}
/**
* @param ARI $repository
* @param Account $account
* @param AccountTaskerInterface $tasker
* @param AccountCrudInterface $crud
* @param ARI $repository
* @param Account $account
*
* @return View
*/
public function show(ARI $repository, Account $account)
public function show(AccountTaskerInterface $tasker, AccountCrudInterface $crud, ARI $repository, Account $account)
{
// show journals from current period only:
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
@@ -240,16 +243,15 @@ class AccountController extends Controller
if ($cache->has()) {
$entries = $cache->get();
return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
// $entries = $cache->get();
// return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
}
$assets = $crud->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
while ($end >= $start) {
$end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range);
$spent = $this->spentInPeriod($account, $end, $currentEnd);
$earned = $this->earnedInPeriod($account, $end, $currentEnd);
$spent = $tasker->amountOutInPeriod(new Collection([$account]), $assets, $end, $currentEnd);
$earned = $tasker->amountInInPeriod(new Collection([$account]), $assets, $end, $currentEnd);
$dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range);
$entries->push([$dateStr, $dateName, $spent, $earned]);
@@ -391,69 +393,4 @@ class AccountController extends Controller
return '';
}
/**
* Asset accounts actually earn money by being the destination of a deposit or the destination
* of a transfer. The money moves to them.
*
* A revenue account doesn't really earn money itself. Money is earned "from" the revenue account.
* So, the call to find out how many money has been earned by/from a revenue account is slightly different.
*
*
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
private function earnedInPeriod(Account $account, Carbon $start, Carbon $end)
{
/** @var ARI $repository */
$repository = app(ARI::class);
$collection = new Collection([$account]);
$type = $account->accountType->type;
switch ($type) {
case AccountType::DEFAULT:
case AccountType::ASSET:
return $repository->earnedInPeriod($collection, $start, $end);
case AccountType::REVENUE:
return $repository->earnedFromInPeriod($collection, $start, $end);
default:
return '0';
}
}
/**
* Asset accounts actually spend money by being the source of a withdrawal or the source
* of a transfer. The money moves away from them.
*
* An expense account doesn't really spend money itself. Money is spent "at" the expense account.
* So, the call to find out how many money has been spent on/at an expense account is slightly different.
*
*
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
private function spentInPeriod(Account $account, Carbon $start, Carbon $end): string
{
/** @var ARI $repository */
$repository = app(ARI::class);
$collection = new Collection([$account]);
$type = $account->accountType->type;
switch ($type) {
case AccountType::DEFAULT:
case AccountType::ASSET:
return $repository->spentInPeriod($collection, $start, $end);
case AccountType::EXPENSE:
return $repository->spentAtInPeriod($collection, $start, $end);
default:
return '0';
}
}
}