Code cleanup

This commit is contained in:
James Cole
2021-03-21 09:15:40 +01:00
parent da1751940e
commit 206845575c
317 changed files with 7418 additions and 7362 deletions

View File

@@ -50,7 +50,7 @@ class AutoCompleteController extends Controller
*/
public function allJournalsWithID(Request $request): JsonResponse
{
$search = (string) $request->get('search');
$search = (string)$request->get('search');
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
@@ -61,7 +61,7 @@ class AutoCompleteController extends Controller
$array = [];
if (is_numeric($search)) {
// search for group, not journal.
$firstResult = $groupRepos->find((int) $search);
$firstResult = $groupRepos->find((int)$search);
if (null !== $firstResult) {
// group may contain multiple journals, each a result:
foreach ($firstResult->transactionJournals as $journal) {

View File

@@ -69,7 +69,7 @@ class BoxController extends Controller
$end = session('end', Carbon::now()->endOfMonth());
$today = today(config('app.timezone'));
$display = 2; // see method docs.
$boxTitle = (string) trans('firefly.spent');
$boxTitle = (string)trans('firefly.spent');
$cache = new CacheProperties;
$cache->addProperty($start);
@@ -96,21 +96,21 @@ class BoxController extends Controller
// spent in this period, in budgets, for default currency.
// also calculate spent per day.
$spent = $opsRepository->sumExpenses($start, $end, null, null, $currency);
$spentAmount = $spent[(int) $currency->id]['sum'] ?? '0';
$spentAmount = $spent[(int)$currency->id]['sum'] ?? '0';
$days = $today->between($start, $end) ? $today->diffInDays($start) + 1 : $end->diffInDays($start) + 1;
$spentPerDay = bcdiv($spentAmount, (string) $days);
$days = $today->between($start, $end) ? $today->diffInDays($start) + 1 : $end->diffInDays($start) + 1;
$spentPerDay = bcdiv($spentAmount, (string)$days);
if ($availableBudgets->count() > 0) {
$display = 0; // assume user overspent
$boxTitle = (string) trans('firefly.overspent');
$totalAvailableSum = (string) $availableBudgets->sum('amount');
$boxTitle = (string)trans('firefly.overspent');
$totalAvailableSum = (string)$availableBudgets->sum('amount');
// calculate with available budget.
$leftToSpendAmount = bcadd($totalAvailableSum, $spentAmount);
if (1 === bccomp($leftToSpendAmount, '0')) {
$boxTitle = (string) trans('firefly.left_to_spend');
$boxTitle = (string)trans('firefly.left_to_spend');
$days = $today->diffInDays($end) + 1;
$display = 1; // not overspent
$leftPerDayAmount = bcdiv($leftToSpendAmount, (string) $days);
$leftPerDayAmount = bcdiv($leftToSpendAmount, (string)$days);
}
}
@@ -164,7 +164,7 @@ class BoxController extends Controller
$set = $collector->getExtractedJournals();
/** @var array $journal */
foreach ($set as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$amount = $journal['amount'] ?? '0';
$incomes[$currencyId] = $incomes[$currencyId] ?? '0';
$incomes[$currencyId] = bcadd($incomes[$currencyId], app('steam')->positive($amount));
@@ -180,7 +180,7 @@ class BoxController extends Controller
$set = $collector->getExtractedJournals();
/** @var array $journal */
foreach ($set as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$expenses[$currencyId] = $expenses[$currencyId] ?? '0';
$expenses[$currencyId] = bcadd($expenses[$currencyId], $journal['amount'] ?? '0');
$sums[$currencyId] = $sums[$currencyId] ?? '0';

View File

@@ -42,6 +42,7 @@ use Illuminate\Http\JsonResponse;
class BudgetController extends Controller
{
use DateCalculation;
/** @var AvailableBudgetRepositoryInterface */
private $abRepository;
/** @var BudgetLimitRepositoryInterface */
@@ -64,7 +65,7 @@ class BudgetController extends Controller
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string) trans('firefly.budgets'));
app('view')->share('title', (string)trans('firefly.budgets'));
app('view')->share('mainTitleIcon', 'fa-pie-chart');
$this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);

View File

@@ -115,7 +115,7 @@ class IntroController
app('preferences')->set($key, false);
app('preferences')->mark();
return response()->json(['message' => (string) trans('firefly.intro_boxes_after_refresh')]);
return response()->json(['message' => (string)trans('firefly.intro_boxes_after_refresh')]);
}
/**

View File

@@ -64,7 +64,7 @@ class ReconcileController extends Controller
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-credit-card');
app('view')->share('title', (string) trans('firefly.accounts'));
app('view')->share('title', (string)trans('firefly.accounts'));
$this->repository = app(JournalRepositoryInterface::class);
$this->accountRepos = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
@@ -172,61 +172,6 @@ class ReconcileController extends Controller
return response()->json($return);
}
/**
* Returns a list of transactions in a modal.
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*
*/
public function transactions(Account $account, Carbon $start, Carbon $end)
{
if ($end->lt($start)) {
[$end, $start] = [$start, $end];
}
$startDate = clone $start;
$startDate->subDay();
$currency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
$startBalance = round((float) app('steam')->balance($account, $startDate), $currency->decimal_places);
$endBalance = round((float) app('steam')->balance($account, $end), $currency->decimal_places);
// get the transactions
$selectionStart = clone $start;
$selectionStart->subDays(3);
$selectionEnd = clone $end;
$selectionEnd->addDays(3);
// grab transactions:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))
->setRange($selectionStart, $selectionEnd)
->withBudgetInformation()->withCategoryInformation()->withAccountInformation();
$array = $collector->getExtractedJournals();
$journals = $this->processTransactions($account, $array);
try {
$html = prefixView(
'accounts.reconcile.transactions',
compact('account', 'journals', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd')
)->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render: %s', $e->getMessage()));
$html = sprintf('Could not render accounts.reconcile.transactions: %s', $e->getMessage());
}
// @codeCoverageIgnoreEnd
return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]);
}
/**
* @param Account $account
* @param TransactionCurrency $currency
@@ -267,11 +212,66 @@ class ReconcileController extends Controller
return $amount;
}
/**
* Returns a list of transactions in a modal.
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*
*/
public function transactions(Account $account, Carbon $start, Carbon $end)
{
if ($end->lt($start)) {
[$end, $start] = [$start, $end];
}
$startDate = clone $start;
$startDate->subDay();
$currency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
$startBalance = round((float)app('steam')->balance($account, $startDate), $currency->decimal_places);
$endBalance = round((float)app('steam')->balance($account, $end), $currency->decimal_places);
// get the transactions
$selectionStart = clone $start;
$selectionStart->subDays(3);
$selectionEnd = clone $end;
$selectionEnd->addDays(3);
// grab transactions:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))
->setRange($selectionStart, $selectionEnd)
->withBudgetInformation()->withCategoryInformation()->withAccountInformation();
$array = $collector->getExtractedJournals();
$journals = $this->processTransactions($account, $array);
try {
$html = prefixView(
'accounts.reconcile.transactions',
compact('account', 'journals', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd')
)->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render: %s', $e->getMessage()));
$html = sprintf('Could not render accounts.reconcile.transactions: %s', $e->getMessage());
}
// @codeCoverageIgnoreEnd
return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]);
}
/**
* "fix" amounts to make it easier on the reconciliation overview:
*
* @param Account $account
* @param array $array
*
* @return array
*/
private function processTransactions(Account $account, array $array): array
@@ -305,6 +305,7 @@ class ReconcileController extends Controller
$journals[] = $journal;
}
return $journals;
}
}

View File

@@ -63,9 +63,9 @@ class RecurrenceController extends Controller
*
* @param Request $request
*
* @throws FireflyException
* @return JsonResponse
*
* @throws FireflyException
*/
public function events(Request $request): JsonResponse
{
@@ -73,10 +73,10 @@ class RecurrenceController extends Controller
$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');
$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');
$repetitions = (int)$request->get('reps');
$repetitionMoment = '';
$start->startOfDay();
@@ -100,8 +100,8 @@ class RecurrenceController extends Controller
$repetition = new RecurrenceRepetition;
$repetition->repetition_type = $repetitionType;
$repetition->repetition_moment = $repetitionMoment;
$repetition->repetition_skip = (int) $request->get('skip');
$repetition->weekend = (int) $request->get('weekend');
$repetition->repetition_skip = (int)$request->get('skip');
$repetition->weekend = (int)$request->get('weekend');
$actualEnd = clone $end;
switch ($endsAt) {
@@ -151,30 +151,30 @@ class RecurrenceController extends Controller
$string = $request->get('date') ?? date('Y-m-d');
$today = Carbon::now()->startOfDay();
$date = Carbon::createFromFormat('Y-m-d', $string)->startOfDay();
$preSelected = (string) $request->get('pre_select');
$locale = app('steam')->getLocale();
$preSelected = (string)$request->get('pre_select');
$locale = app('steam')->getLocale();
Log::debug(sprintf('date = %s, today = %s. date > today? %s', $date->toAtomString(), $today->toAtomString(), var_export($date > $today, true)));
Log::debug(sprintf('past = true? %s', var_export('true' === (string) $request->get('past'), true)));
Log::debug(sprintf('past = true? %s', var_export('true' === (string)$request->get('past'), true)));
$result = [];
if ($date > $today || 'true' === (string) $request->get('past')) {
if ($date > $today || 'true' === (string)$request->get('past')) {
Log::debug('Will fill dropdown.');
$weekly = sprintf('weekly,%s', $date->dayOfWeekIso);
$monthly = sprintf('monthly,%s', $date->day);
$dayOfWeek = (string) trans(sprintf('config.dow_%s', $date->dayOfWeekIso));
$dayOfWeek = (string)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((string) trans('config.month_and_day_no_year', [], $locale));
$yearlyDate = $date->formatLocalized((string)trans('config.month_and_day_no_year', [], $locale));
$result = [
'daily' => ['label' => (string) trans('firefly.recurring_daily'), 'selected' => 0 === strpos($preSelected, 'daily')],
$weekly => ['label' => (string) trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),
'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]),
$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]),
$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]),
$yearly => ['label' => (string)trans('firefly.recurring_yearly', ['date' => $yearlyDate]),
'selected' => 0 === strpos($preSelected, 'yearly')],
];
}

View File

@@ -44,11 +44,11 @@ class RuleController extends Controller
*/
public function action(Request $request): JsonResponse
{
$count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1;
$count = (int)$request->get('count') > 0 ? (int)$request->get('count') : 1;
$keys = array_keys(config('firefly.rule-actions'));
$actions = [];
foreach ($keys as $key) {
$actions[$key] = (string) trans('firefly.rule_action_' . $key . '_choice');
$actions[$key] = (string)trans('firefly.rule_action_' . $key . '_choice');
}
try {
$view = prefixView('rules.partials.action', compact('actions', 'count'))->render();
@@ -72,13 +72,13 @@ class RuleController extends Controller
*/
public function trigger(Request $request): JsonResponse
{
$count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1;
$count = (int)$request->get('count') > 0 ? (int)$request->get('count') : 1;
$operators = config('firefly.search.operators');
$triggers = [];
foreach ($operators as $key => $operator) {
if ('user_action' !== $key && false === $operator['alias']) {
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
$triggers[$key] = (string)trans(sprintf('firefly.rule_trigger_%s_choice', $key));
}
}
asort($triggers);