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

@@ -39,6 +39,9 @@ use Illuminate\Http\JsonResponse;
/**
* Class AutoCompleteController.
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AutoCompleteController extends Controller
{

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;
}
}

View File

@@ -36,7 +36,7 @@ class FrontpageController extends Controller
* @param PiggyBankRepositoryInterface $repository
*
* @return JsonResponse
* @throws \Throwable
*/
public function piggyBanks(PiggyBankRepositoryInterface $repository): JsonResponse
{

View File

@@ -158,6 +158,7 @@ class IntroController
* @param string $specificPage
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function getSpecificSteps(string $route, string $specificPage): array
{

View File

@@ -42,6 +42,8 @@ use Illuminate\Support\Collection;
/**
*
* Class ReconcileController
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ReconcileController extends Controller
{
@@ -84,7 +86,10 @@ class ReconcileController extends Controller
* @return JsonResponse
*
* @throws FireflyException
* @throws \Throwable
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function overview(Request $request, Account $account, Carbon $start, Carbon $end): JsonResponse
{
@@ -140,7 +145,7 @@ class ReconcileController extends Controller
* @return mixed
*
* @throws FireflyException
* @throws \Throwable
*/
public function transactions(Account $account, Carbon $start, Carbon $end)
{

View File

@@ -0,0 +1,175 @@
<?php
/**
* RecurrenceController.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\RecurrenceRepetition;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
* Class RecurrenceController
*/
class RecurrenceController extends Controller
{
/** @var RecurringRepositoryInterface */
private $recurring;
/**
*
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
$this->recurring = app(RecurringRepositoryInterface::class);
return $next($request);
}
);
}
/**
* @param Request $request
*
* @throws FireflyException
* @return JsonResponse
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function events(Request $request): JsonResponse
{
$return = [];
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
$firstDate = Carbon::createFromFormat('Y-m-d', $request->get('first_date'));
$endDate = '' !== (string)$request->get('end_date') ? Carbon::createFromFormat('Y-m-d', $request->get('end_date')) : null;
$endsAt = (string)$request->get('ends');
$repetitionType = explode(',', $request->get('type'))[0];
$repetitions = (int)$request->get('reps');
$repetitionMoment = '';
$start->startOfDay();
// if $firstDate is beyond $end, simply return an empty array.
if ($firstDate->gt($end)) {
return response()->json([]);
}
// if $firstDate is beyond start, use that one:
$actualStart = clone $firstDate;
if ($repetitionType === 'weekly' || $repetitionType === 'monthly') {
$repetitionMoment = explode(',', $request->get('type'))[1] ?? '1';
}
if ($repetitionType === 'ndom') {
$repetitionMoment = str_ireplace('ndom,', '', $request->get('type'));
}
if ($repetitionType === 'yearly') {
$repetitionMoment = explode(',', $request->get('type'))[1] ?? '2018-01-01';
}
$repetition = new RecurrenceRepetition;
$repetition->repetition_type = $repetitionType;
$repetition->repetition_moment = $repetitionMoment;
$repetition->repetition_skip = (int)$request->get('skip');
$repetition->weekend = (int)$request->get('weekend');
$actualEnd = clone $end;
switch ($endsAt) {
default:
throw new FireflyException(sprintf('Cannot generate events for type that ends at "%s".', $endsAt));
case 'forever':
// simply generate up until $end. No change from default behavior.
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
break;
case 'until_date':
$actualEnd = $endDate ?? clone $end;
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
break;
case 'times':
$occurrences = $this->recurring->getXOccurrences($repetition, $actualStart, $repetitions);
break;
}
/** @var Carbon $current */
foreach ($occurrences as $current) {
if ($current->gte($start)) {
$event = [
'id' => $repetitionType . $firstDate->format('Ymd'),
'title' => 'X',
'allDay' => true,
'start' => $current->format('Y-m-d'),
'end' => $current->format('Y-m-d'),
'editable' => false,
'rendering' => 'background',
];
$return[] = $event;
}
}
return response()->json($return);
}
/**
* @param Request $request
*
* @return JsonResponse
*/
public function suggest(Request $request): JsonResponse
{
$today = new Carbon;
$date = Carbon::createFromFormat('Y-m-d', $request->get('date'));
$preSelected = (string)$request->get('pre_select');
$result = [];
if ($date > $today || 'true' === (string)$request->get('past')) {
$weekly = sprintf('weekly,%s', $date->dayOfWeekIso);
$monthly = sprintf('monthly,%s', $date->day);
$dayOfWeek = trans(sprintf('config.dow_%s', $date->dayOfWeekIso));
$ndom = sprintf('ndom,%s,%s', $date->weekOfMonth, $date->dayOfWeekIso);
$yearly = sprintf('yearly,%s', $date->format('Y-m-d'));
$yearlyDate = $date->formatLocalized(trans('config.month_and_day_no_year'));
$result = [
'daily' => ['label' => (string)trans('firefly.recurring_daily'), 'selected' => 0 === strpos($preSelected, 'daily')],
$weekly => ['label' => (string)trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),
'selected' => 0 === strpos($preSelected, 'weekly')],
$monthly => ['label' => (string)trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]),
'selected' => 0 === strpos($preSelected, 'monthly')],
$ndom => ['label' => (string)trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),
'selected' => 0 === strpos($preSelected, 'ndom')],
$yearly => ['label' => (string)trans('firefly.recurring_yearly', ['date' => $yearlyDate]), 'selected' => 0 === strpos($preSelected, 'yearly')],
];
}
return response()->json($result);
}
}