Code cleanup.

This commit is contained in:
James Cole
2018-07-20 14:34:56 +02:00
parent cfc2181a48
commit 44fb307da4
73 changed files with 1914 additions and 1423 deletions

View File

@@ -22,13 +22,13 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use Amount;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
@@ -47,6 +47,8 @@ class BoxController extends Controller
* @param BudgetRepositoryInterface $repository
*
* @return JsonResponse
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function available(BudgetRepositoryInterface $repository): JsonResponse
{
@@ -99,6 +101,8 @@ class BoxController extends Controller
* @param CurrencyRepositoryInterface $repository
*
* @return JsonResponse
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function balance(CurrencyRepositoryInterface $repository): JsonResponse
{
@@ -150,17 +154,18 @@ class BoxController extends Controller
}
// format amounts:
foreach ($sums as $currencyId => $amount) {
$keys = array_keys($sums);
foreach ($keys as $currencyId) {
$currency = $repository->findNull($currencyId);
$sums[$currencyId] = Amount::formatAnything($currency, $sums[$currencyId], false);
$incomes[$currencyId] = Amount::formatAnything($currency, $incomes[$currencyId] ?? '0', false);
$expenses[$currencyId] = Amount::formatAnything($currency, $expenses[$currencyId] ?? '0', false);
$sums[$currencyId] = app('amount')->formatAnything($currency, $sums[$currencyId], false);
$incomes[$currencyId] = app('amount')->formatAnything($currency, $incomes[$currencyId] ?? '0', false);
$expenses[$currencyId] = app('amount')->formatAnything($currency, $expenses[$currencyId] ?? '0', false);
}
if (0 === \count($sums)) {
$currency = app('amount')->getDefaultCurrency();
$sums[$currency->id] = Amount::formatAnything($currency, '0', false);
$incomes[$currency->id] = Amount::formatAnything($currency, '0', false);
$expenses[$currency->id] = Amount::formatAnything($currency, '0', false);
$sums[$currency->id] = app('amount')->formatAnything($currency, '0', false);
$incomes[$currency->id] = app('amount')->formatAnything($currency, '0', false);
$expenses[$currency->id] = app('amount')->formatAnything($currency, '0', false);
}
$response = [
@@ -214,28 +219,23 @@ class BoxController extends Controller
/**
* @param AccountRepositoryInterface $repository
*
* @param CurrencyRepositoryInterface $currencyRepos
* @param AccountRepositoryInterface $repository
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function netWorth(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepos): JsonResponse
public function netWorth(AccountRepositoryInterface $repository): JsonResponse
{
$date = new Carbon(date('Y-m-d')); // needed so its per day.
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
// start and end in the future? use $end
if ($start->greaterThanOrEqualTo($date) && $end->greaterThanOrEqualTo($date)) {
$date = $end;
}
// start and end in the past? use $end
if ($start->lessThanOrEqualTo($date) && $end->lessThanOrEqualTo($date)) {
$date = $end;
if ($this->notInSessionRange($date)) {
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
}
// start in the past, end in the future? use $date
$cache = new CacheProperties;
$cache->addProperty($date);
@@ -245,20 +245,13 @@ class BoxController extends Controller
}
$netWorth = [];
$accounts = $repository->getActiveAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$currency = app('amount')->getDefaultCurrency();
$balances = app('steam')->balancesByAccounts($accounts, $date);
/** @var Account $account */
foreach ($accounts as $account) {
$accountCurrency = null;
$accountCurrency = $this->getCurrencyOrDefault($account);
$balance = $balances[$account->id] ?? '0';
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
if (0 !== $currencyId) {
$accountCurrency = $currencyRepos->findNull($currencyId);
}
if (null === $accountCurrency) {
$accountCurrency = $currency;
}
// if the account is a credit card, subtract the virtual balance from the balance,
// to better reflect that this is not money that is actually "yours".
@@ -287,4 +280,53 @@ class BoxController extends Controller
return response()->json($return);
}
/**
* @param Account $account
*
* @return TransactionCurrency
*/
private function getCurrencyOrDefault(Account $account): TransactionCurrency
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currency = app('amount')->getDefaultCurrency();
$accountCurrency = null;
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
if (0 !== $currencyId) {
$accountCurrency = $currencyRepos->findNull($currencyId);
}
if (null === $accountCurrency) {
$accountCurrency = $currency;
}
return $accountCurrency;
}
/**
* @param Carbon $date
*
* @return bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function notInSessionRange(Carbon $date): bool
{
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
$result = false;
if ($start->greaterThanOrEqualTo($date) && $end->greaterThanOrEqualTo($date)) {
$result = true;
}
// start and end in the past? use $end
if ($start->lessThanOrEqualTo($date) && $end->lessThanOrEqualTo($date)) {
$result = true;
}
return $result;
}
}