From 14b94a5bd277c8c5cb165d4e3f1b6e9b75ee608a Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 29 Apr 2016 08:56:56 +0200 Subject: [PATCH] Simplified some code. --- .../Csv/Specifix/AbnAmroDescription.php | 13 ++-- .../Auth/ConfirmationController.php | 3 +- app/Http/Controllers/BudgetController.php | 64 ++++++++++++------- app/Http/Controllers/CategoryController.php | 33 +++++----- app/Http/Controllers/Chart/BillController.php | 8 ++- app/Http/Controllers/CurrencyController.php | 12 ++-- app/Http/Controllers/HomeController.php | 12 ++-- app/Http/Controllers/NewUserController.php | 6 +- app/Http/Controllers/PiggyBankController.php | 25 ++++---- app/Http/routes.php | 3 +- resources/views/budgets/index.twig | 4 +- resources/views/budgets/show.twig | 2 +- resources/views/reports/partials/budgets.twig | 2 +- 13 files changed, 104 insertions(+), 83 deletions(-) diff --git a/app/Helpers/Csv/Specifix/AbnAmroDescription.php b/app/Helpers/Csv/Specifix/AbnAmroDescription.php index 866f7edf72..7978ab1465 100644 --- a/app/Helpers/Csv/Specifix/AbnAmroDescription.php +++ b/app/Helpers/Csv/Specifix/AbnAmroDescription.php @@ -95,11 +95,10 @@ class AbnAmroDescription extends Specifix implements SpecifixInterface // description and opposing account will be the same. $this->data['opposing-account-name'] = $matches[4]; + $this->data['description'] = $matches[4]; if ($matches[1] == 'GEA') { $this->data['description'] = 'GEA ' . $matches[4]; - } else { - $this->data['description'] = $matches[4]; } return true; @@ -154,9 +153,8 @@ class AbnAmroDescription extends Specifix implements SpecifixInterface // Set a new description for the current transaction. If none was given // set the description to type, name and reference - if ($newDescription) { - $this->data['description'] = $newDescription; - } else { + $this->data['description'] = $newDescription; + if (strlen($newDescription) === 0) { $this->data['description'] = sprintf('%s - %s (%s)', $type, $name, $reference); } @@ -212,9 +210,8 @@ class AbnAmroDescription extends Specifix implements SpecifixInterface // Set a new description for the current transaction. If none was given // set the description to type, name and reference - if ($newDescription) { - $this->data['description'] = $newDescription; - } else { + $this->data['description'] = $newDescription; + if (strlen($newDescription) === 0) { $this->data['description'] = sprintf('%s - %s (%s)', $type, $name, $reference); } } diff --git a/app/Http/Controllers/Auth/ConfirmationController.php b/app/Http/Controllers/Auth/ConfirmationController.php index d51100630b..1c6761926c 100644 --- a/app/Http/Controllers/Auth/ConfirmationController.php +++ b/app/Http/Controllers/Auth/ConfirmationController.php @@ -55,9 +55,8 @@ class ConfirmationController extends Controller Session::flash('success', strval(trans('firefly.account_is_confirmed'))); return redirect(route('home')); - } else { - throw new FireflyException(trans('firefly.invalid_activation_code')); } + throw new FireflyException(trans('firefly.invalid_activation_code')); } /** diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 0a4570a05b..7b8164c693 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -251,34 +251,19 @@ class BudgetController extends Controller /** * @param BudgetRepositoryInterface $repository * @param Budget $budget - * @param LimitRepetition|null $repetition * * @return View * @throws FireflyException */ - public function show(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition = null) + public function show(BudgetRepositoryInterface $repository, Budget $budget) { - if (!is_null($repetition->id) && $repetition->budgetLimit->budget->id != $budget->id) { - throw new FireflyException('This budget limit is not part of this budget.'); - } - $pageSize = Preferences::get('transactionPageSize', 50)->data; - $journals = $repository->getJournals($budget, $repetition, $pageSize); - - if (is_null($repetition->id)) { - $start = $repository->firstActivity($budget); - $end = new Carbon; - $set = $budget->limitrepetitions()->orderBy('startdate', 'DESC')->get(); - $subTitle = e($budget->name); - $journals->setPath('/budgets/show/' . $budget->id); - } else { - $start = $repetition->startdate; - $end = $repetition->enddate; - $set = new Collection([$repetition]); - $subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]); - $journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id); - } - + $journals = $repository->getJournals($budget, new LimitRepetition, $pageSize); + $start = $repository->firstActivity($budget); + $end = new Carbon; + $set = $budget->limitrepetitions()->orderBy('startdate', 'DESC')->get(); + $subTitle = e($budget->name); + $journals->setPath('/budgets/show/' . $budget->id); $spentArray = $repository->spentPerDay($budget, $start, $end); $limits = new Collection(); @@ -292,6 +277,41 @@ class BudgetController extends Controller return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle')); } + /** + * @param BudgetRepositoryInterface $repository + * @param Budget $budget + * @param LimitRepetition $repetition + * + * @return View + * @throws FireflyException + */ + public function showWithRepetition(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition) + { + if ($repetition->budgetLimit->budget->id != $budget->id) { + throw new FireflyException('This budget limit is not part of this budget.'); + } + + $pageSize = Preferences::get('transactionPageSize', 50)->data; + $journals = $repository->getJournals($budget, $repetition, $pageSize); + $start = $repetition->startdate; + $end = $repetition->enddate; + $set = new Collection([$repetition]); + $subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]); + $journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id); + $spentArray = $repository->spentPerDay($budget, $start, $end); + $limits = new Collection(); + + /** @var LimitRepetition $entry */ + foreach ($set as $entry) { + $entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray); + $limits->push($entry); + } + + + return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle')); + + } + /** * @param BudgetFormRequest $request * @param BudgetRepositoryInterface $repository diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index 2189b5a22f..e949b6d53c 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -186,25 +186,26 @@ class CategoryController extends Controller if ($cache->has()) { $entries = $cache->get(); - } else { - while ($end >= $start) { - $end = Navigation::startOfPeriod($end, $range); - $currentEnd = Navigation::endOfPeriod($end, $range); - - // get data from spentArray: - $spent = $this->getSumOfRange($end, $currentEnd, $spentArray); - $earned = $this->getSumOfRange($end, $currentEnd, $earnedArray); - $dateStr = $end->format('Y-m-d'); - $dateName = Navigation::periodShow($end, $range); - $entries->push([$dateStr, $dateName, $spent, $earned]); - - $end = Navigation::subtractPeriod($end, $range, 1); - - } - $cache->store($entries); + return view('categories.show', compact('category', 'journals', 'entries', 'hideCategory', 'subTitle')); } + while ($end >= $start) { + $end = Navigation::startOfPeriod($end, $range); + $currentEnd = Navigation::endOfPeriod($end, $range); + + // get data from spentArray: + $spent = $this->getSumOfRange($end, $currentEnd, $spentArray); + $earned = $this->getSumOfRange($end, $currentEnd, $earnedArray); + $dateStr = $end->format('Y-m-d'); + $dateName = Navigation::periodShow($end, $range); + $entries->push([$dateStr, $dateName, $spent, $earned]); + + $end = Navigation::subtractPeriod($end, $range, 1); + + } + $cache->store($entries); + return view('categories.show', compact('category', 'journals', 'entries', 'hideCategory', 'subTitle')); } diff --git a/app/Http/Controllers/Chart/BillController.php b/app/Http/Controllers/Chart/BillController.php index f38af543d2..4e92a28f24 100644 --- a/app/Http/Controllers/Chart/BillController.php +++ b/app/Http/Controllers/Chart/BillController.php @@ -51,9 +51,11 @@ class BillController extends Controller // expenses are negative (bill not yet paid), $creditCardDue = bcmul($creditCardDue, '-1'); $unpaid = bcadd($unpaid, $creditCardDue); - } else { - // if more than zero, the bill has been paid: (transfer = positive). - // amount must be negative to be added to $paid: + } + + // if $creditCardDue more than zero, the bill has been paid: (transfer = positive). + // amount must be negative to be added to $paid: + if ($creditCardDue >= 0) { $paid = bcadd($paid, $creditCardDue); } diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php index 80280c94ca..40a98c2d0d 100644 --- a/app/Http/Controllers/CurrencyController.php +++ b/app/Http/Controllers/CurrencyController.php @@ -168,14 +168,16 @@ class CurrencyController extends Controller */ public function store(CurrencyFormRequest $request, CurrencyRepositoryInterface $repository) { - $data = $request->getCurrencyData(); - if (Auth::user()->hasRole('owner')) { - $currency = $repository->store($data); - Session::flash('success', trans('firefly.created_currency', ['name' => $currency->name])); - } else { + if (!Auth::user()->hasRole('owner')) { Log::error('User ' . Auth::user()->id . ' is not admin, but tried to store a currency.'); + + return redirect(session('currency.create.url')); } + $data = $request->getCurrencyData(); + $currency = $repository->store($data); + Session::flash('success', trans('firefly.created_currency', ['name' => $currency->name])); + if (intval(Input::get('create_another')) === 1) { Session::put('currency.create.fromStore', true); diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 32e54220b3..ded94b2943 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -32,16 +32,15 @@ class HomeController extends Controller public function dateRange() { - $start = new Carbon(Input::get('start')); - $end = new Carbon(Input::get('end')); - $label = Input::get('label'); + $start = new Carbon(Input::get('start')); + $end = new Carbon(Input::get('end')); + $label = Input::get('label'); + $isCustomRange = false; // check if the label is "everything" or "Custom range" which will betray // a possible problem with the budgets. if ($label === strval(trans('firefly.everything')) || $label === strval(trans('firefly.customRange'))) { - Session::put('is_custom_range', true); - } else { - Session::put('is_custom_range', false); + $isCustomRange = true; } $diff = $start->diffInDays($end); @@ -50,6 +49,7 @@ class HomeController extends Controller Session::flash('warning', strval(trans('firefly.warning_much_data', ['days' => $diff]))); } + Session::put('is_custom_range', $isCustomRange); Session::put('start', $start); Session::put('end', $end); } diff --git a/app/Http/Controllers/NewUserController.php b/app/Http/Controllers/NewUserController.php index b4015ca8d6..4977d37265 100644 --- a/app/Http/Controllers/NewUserController.php +++ b/app/Http/Controllers/NewUserController.php @@ -111,12 +111,12 @@ class NewUserController extends Controller $repository->storeMeta($creditCard, 'ccMonthlyPaymentDate', Carbon::now()->year . '-01-01'); $count++; } + $message = strval(trans('firefly.stored_new_accounts_new_user')); if ($count == 1) { - Session::flash('success', strval(trans('firefly.stored_new_account_new_user'))); - } else { - Session::flash('success', strval(trans('firefly.stored_new_accounts_new_user'))); + $message = strval(trans('firefly.stored_new_account_new_user')); } + Session::flash('success', $message); Preferences::mark(); return redirect(route('index')); diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php index 19b96bc78e..8619352932 100644 --- a/app/Http/Controllers/PiggyBankController.php +++ b/app/Http/Controllers/PiggyBankController.php @@ -127,16 +127,15 @@ class PiggyBankController extends Controller $accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account'])); $subTitle = trans('firefly.update_piggy_title', ['name' => $piggyBank->name]); $subTitleIcon = 'fa-pencil'; - + $targetDate = null; /* * Flash some data to fill the form. */ - if (is_null($piggyBank->targetdate) || $piggyBank->targetdate == '') { - $targetDate = null; - } else { + if (!is_null($piggyBank->targetdate) || !$piggyBank->targetdate == '') { $targetDate = new Carbon($piggyBank->targetdate); $targetDate = $targetDate->format('Y-m-d'); } + $preFilled = ['name' => $piggyBank->name, 'account_id' => $piggyBank->account_id, 'targetamount' => $piggyBank->targetamount, @@ -246,13 +245,13 @@ class PiggyBankController extends Controller 'success', strval(trans('firefly.added_amount_to_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])) ); Preferences::mark(); - } else { - Log::error('Cannot add ' . $amount . ' because max amount is ' . $maxAmount . ' (left on account is ' . $leftOnAccount . ')'); - Session::flash( - 'error', strval(trans('firefly.cannot_add_amount_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])) - ); + + return redirect(route('piggy-banks.index')); } + Log::error('Cannot add ' . $amount . ' because max amount is ' . $maxAmount . ' (left on account is ' . $leftOnAccount . ')'); + Session::flash('error', strval(trans('firefly.cannot_add_amount_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)]))); + return redirect(route('piggy-banks.index')); } @@ -280,12 +279,12 @@ class PiggyBankController extends Controller 'success', strval(trans('firefly.removed_amount_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])) ); Preferences::mark(); - } else { - Session::flash( - 'error', strval(trans('firefly.cannot_remove_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])) - ); + + return redirect(route('piggy-banks.index')); } + Session::flash('error', strval(trans('firefly.cannot_remove_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)]))); + return redirect(route('piggy-banks.index')); } diff --git a/app/Http/routes.php b/app/Http/routes.php index 2c36250347..0c4ce4b360 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -124,7 +124,8 @@ Route::group( Route::get('/budgets/create', ['uses' => 'BudgetController@create', 'as' => 'budgets.create']); Route::get('/budgets/edit/{budget}', ['uses' => 'BudgetController@edit', 'as' => 'budgets.edit']); Route::get('/budgets/delete/{budget}', ['uses' => 'BudgetController@delete', 'as' => 'budgets.delete']); - Route::get('/budgets/show/{budget}/{limitrepetition?}', ['uses' => 'BudgetController@show', 'as' => 'budgets.show']); + Route::get('/budgets/show/{budget}', ['uses' => 'BudgetController@show', 'as' => 'budgets.show']); + Route::get('/budgets/show/{budget}/{limitrepetition}', ['uses' => 'BudgetController@showWithRepetition', 'as' => 'budgets.showWithRepetition']); Route::get('/budgets/list/noBudget', ['uses' => 'BudgetController@noBudget', 'as' => 'budgets.noBudget']); Route::post('/budgets/income', ['uses' => 'BudgetController@postUpdateIncome', 'as' => 'budgets.postIncome']); Route::post('/budgets/store', ['uses' => 'BudgetController@store', 'as' => 'budgets.store']); diff --git a/resources/views/budgets/index.twig b/resources/views/budgets/index.twig index 37eae3558b..f72ac99270 100644 --- a/resources/views/budgets/index.twig +++ b/resources/views/budgets/index.twig @@ -107,7 +107,7 @@

{% if budget.currentRep.id %} - {{ budget.name }} {% else %} {{ budget.name }} @@ -173,7 +173,7 @@ {% for other in budget.otherRepetitions %} {% if other.id != budget.currentRep.id %}
  • Budgeted - {{ other.amount|formatAmountPlain }} + {{ other.amount|formatAmountPlain }} between {{ other.startdate.formatLocalized(monthAndDayFormat) }} and {{ other.enddate.formatLocalized(monthAndDayFormat) }}. diff --git a/resources/views/budgets/show.twig b/resources/views/budgets/show.twig index 1b04415a0d..5c0ba0c54a 100644 --- a/resources/views/budgets/show.twig +++ b/resources/views/budgets/show.twig @@ -48,7 +48,7 @@ {% for limit in limits %}
    diff --git a/resources/views/reports/partials/budgets.twig b/resources/views/reports/partials/budgets.twig index 4326d9ca7f..c48dfc87ce 100644 --- a/resources/views/reports/partials/budgets.twig +++ b/resources/views/reports/partials/budgets.twig @@ -27,7 +27,7 @@ {% if budgetLine.getRepetition.id %} - + {{ budgetLine.getRepetition.startdate.formatLocalized(monthAndDayFormat) }} — {{ budgetLine.getRepetition.enddate.formatLocalized(monthAndDayFormat) }}