firefly-iii/app/Http/Controllers/BudgetController.php

614 lines
23 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* BudgetController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-22 02:46:21 -06:00
use Carbon\Carbon;
2017-06-06 12:30:31 -05:00
use Exception;
2016-02-05 22:01:34 -06:00
use FireflyIII\Exceptions\FireflyException;
2016-12-28 04:34:00 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2015-02-22 08:40:13 -06:00
use FireflyIII\Http\Requests\BudgetFormRequest;
2016-12-22 09:36:56 -06:00
use FireflyIII\Http\Requests\BudgetIncomeRequest;
2015-02-22 02:46:21 -06:00
use FireflyIII\Models\Budget;
2016-12-29 13:48:33 -06:00
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionType;
2015-02-22 02:46:21 -06:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2017-01-08 03:19:10 -06:00
use FireflyIII\Support\CacheProperties;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Log;
2015-02-22 02:46:21 -06:00
use Preferences;
2015-02-22 08:40:13 -06:00
use Response;
2015-04-05 03:36:28 -05:00
use View;
2015-02-22 02:46:21 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class BudgetController.
2015-02-22 02:46:21 -06:00
*
2017-09-16 02:24:48 -05:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-10-22 13:13:02 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2015-02-22 02:46:21 -06:00
*/
class BudgetController extends Controller
{
2017-11-15 05:25:49 -06:00
/** @var BudgetRepositoryInterface */
2016-12-22 09:36:56 -06:00
private $repository;
2015-04-03 12:11:55 -05:00
/**
2016-02-04 00:27:03 -06:00
*
2015-04-03 12:11:55 -05:00
*/
2015-02-22 02:46:21 -06:00
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
2015-04-28 08:26:30 -05:00
View::share('hideBudgets', true);
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2017-12-16 12:46:36 -06:00
app('view')->share('title', trans('firefly.budgets'));
app('view')->share('mainTitleIcon', 'fa-tasks');
2016-12-22 09:36:56 -06:00
$this->repository = app(BudgetRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-22 02:46:21 -06:00
}
/**
2017-12-17 07:30:53 -06:00
* @param Request $request
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
2015-02-22 02:46:21 -06:00
*
2016-12-28 06:02:56 -06:00
* @return \Illuminate\Http\JsonResponse
2015-02-22 02:46:21 -06:00
*/
2017-11-24 10:05:22 -06:00
public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget)
2015-02-22 02:46:21 -06:00
{
2017-11-18 04:32:35 -06:00
$amount = strval($request->get('amount'));
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
2017-01-08 03:19:10 -06:00
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
2017-12-16 12:48:31 -06:00
if (bccomp($amount, '0') === 0) {
2016-12-29 13:52:02 -06:00
$budgetLimit = null;
2015-05-24 15:54:59 -05:00
}
2017-11-24 10:05:22 -06:00
// calculate left in budget:
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
$currency = app('amount')->getDefaultCurrency();
$left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);
Preferences::mark();
2015-02-22 02:46:21 -06:00
2017-11-24 10:05:22 -06:00
return Response::json(['left' => $left, 'name' => $budget->name, 'limit' => $budgetLimit ? $budgetLimit->id : 0, 'amount' => $amount]);
2015-02-22 02:46:21 -06:00
}
2015-02-22 08:40:13 -06:00
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
*
* @return View
2015-02-22 08:40:13 -06:00
*/
2017-02-17 13:14:22 -06:00
public function create(Request $request)
2015-02-22 08:40:13 -06:00
{
2015-04-01 02:40:19 -05:00
// put previous url in session if not redirect from store (not "create another").
2017-11-15 05:25:49 -06:00
if (true !== session('budgets.create.fromStore')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('budgets.create.uri');
2015-04-01 02:40:19 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('budgets.create.fromStore');
$subTitle = (string)trans('firefly.create_new_budget');
2015-04-01 02:40:19 -05:00
2015-05-05 03:23:01 -05:00
return view('budgets.create', compact('subTitle'));
2015-02-22 08:40:13 -06:00
}
/**
2017-02-24 22:57:01 -06:00
* @param Request $request
* @param Budget $budget
2015-02-22 08:40:13 -06:00
*
* @return View
2015-02-22 08:40:13 -06:00
*/
2017-02-17 13:14:22 -06:00
public function delete(Request $request, Budget $budget)
2015-02-22 08:40:13 -06:00
{
2015-05-25 15:16:00 -05:00
$subTitle = trans('firefly.delete_budget', ['name' => $budget->name]);
2015-02-22 08:40:13 -06:00
2015-04-01 02:40:19 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('budgets.delete.uri');
2015-04-01 02:40:19 -05:00
2015-02-22 09:19:32 -06:00
return view('budgets.delete', compact('budget', 'subTitle'));
2015-02-22 08:40:13 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
* @param Budget $budget
2015-02-22 08:40:13 -06:00
*
2017-02-17 13:14:22 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 08:40:13 -06:00
*/
2017-02-17 13:14:22 -06:00
public function destroy(Request $request, Budget $budget)
2015-02-22 08:40:13 -06:00
{
2017-02-05 01:26:54 -06:00
$name = $budget->name;
2017-01-08 03:19:10 -06:00
$this->repository->destroy($budget);
2017-10-19 11:08:50 -05:00
$request->session()->flash('success', strval(trans('firefly.deleted_budget', ['name' => $name])));
Preferences::mark();
2015-02-22 08:40:13 -06:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('budgets.delete.uri'));
2015-02-22 08:40:13 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
* @param Budget $budget
2015-02-22 08:40:13 -06:00
*
* @return View
2015-02-22 08:40:13 -06:00
*/
2017-02-17 13:14:22 -06:00
public function edit(Request $request, Budget $budget)
2015-02-22 08:40:13 -06:00
{
2015-06-13 03:02:36 -05:00
$subTitle = trans('firefly.edit_budget', ['name' => $budget->name]);
2015-02-22 08:40:13 -06:00
2015-04-01 02:40:19 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
2017-11-15 05:25:49 -06:00
if (true !== session('budgets.edit.fromUpdate')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('budgets.edit.uri');
2015-04-01 02:40:19 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('budgets.edit.fromUpdate');
2015-04-01 02:40:19 -05:00
2015-02-22 09:19:32 -06:00
return view('budgets.edit', compact('budget', 'subTitle'));
2015-02-22 08:40:13 -06:00
}
2015-02-22 02:46:21 -06:00
/**
2017-06-06 12:30:31 -05:00
* @param string|null $moment
*
2016-05-20 04:02:07 -05:00
* @return View
2017-09-16 02:24:48 -05:00
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity) complex because of while loop
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2015-02-22 02:46:21 -06:00
*/
2017-07-26 11:40:48 -05:00
public function index(string $moment = null)
2015-02-22 02:46:21 -06:00
{
2017-06-06 12:30:31 -05:00
$range = Preferences::get('viewRange', '1M')->data;
$start = session('start', new Carbon);
$end = session('end', new Carbon);
// make date if present:
2017-11-15 05:25:49 -06:00
if (null !== $moment || 0 !== strlen(strval($moment))) {
2017-06-06 12:30:31 -05:00
try {
$start = new Carbon($moment);
$end = app('navigation')->endOfPeriod($start, $range);
2017-06-06 12:30:31 -05:00
} catch (Exception $e) {
2017-06-07 00:38:58 -05:00
// start and end are already defined.
2017-06-06 12:30:31 -05:00
}
}
$next = clone $end;
$next->addDay();
$prev = clone $start;
$prev->subDay();
$prev = app('navigation')->startOfPeriod($prev, $range);
2016-12-22 09:36:56 -06:00
$this->repository->cleanupBudgets();
$budgets = $this->repository->getActiveBudgets();
$inactive = $this->repository->getInactiveBudgets();
2016-04-25 06:20:42 -05:00
$periodStart = $start->formatLocalized($this->monthAndDayFormat);
$periodEnd = $end->formatLocalized($this->monthAndDayFormat);
2017-09-27 01:45:27 -05:00
$budgetInformation = $this->repository->collectBudgetInformation($budgets, $start, $end);
2017-10-05 04:49:06 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
2016-12-22 09:36:56 -06:00
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
$spent = array_sum(array_column($budgetInformation, 'spent'));
$budgeted = array_sum(array_column($budgetInformation, 'budgeted'));
2015-02-22 02:46:21 -06:00
2017-06-06 13:35:39 -05:00
// select thing for last 12 periods:
$previousLoop = [];
$previousDate = clone $start;
$count = 0;
while ($count < 12) {
$previousDate->subDay();
$previousDate = app('navigation')->startOfPeriod($previousDate, $range);
2017-06-06 13:35:39 -05:00
$format = $previousDate->format('Y-m-d');
$previousLoop[$format] = app('navigation')->periodShow($previousDate, $range);
2017-11-15 05:25:49 -06:00
++$count;
2017-06-06 13:35:39 -05:00
}
// select thing for next 12 periods:
$nextLoop = [];
$nextDate = clone $end;
$nextDate->addDay();
$count = 0;
while ($count < 12) {
$format = $nextDate->format('Y-m-d');
$nextLoop[$format] = app('navigation')->periodShow($nextDate, $range);
$nextDate = app('navigation')->endOfPeriod($nextDate, $range);
2017-11-15 05:25:49 -06:00
++$count;
2017-06-06 13:35:39 -05:00
$nextDate->addDay();
}
2017-06-06 12:30:31 -05:00
// display info
$currentMonth = app('navigation')->periodShow($start, $range);
$nextText = app('navigation')->periodShow($next, $range);
$prevText = app('navigation')->periodShow($prev, $range);
2017-06-06 12:30:31 -05:00
2015-05-24 15:54:59 -05:00
return view(
2016-12-22 09:36:56 -06:00
'budgets.index',
2017-06-06 12:30:31 -05:00
compact(
2017-11-15 03:52:29 -06:00
'available',
'currentMonth',
'next',
'nextText',
'prev',
'prevText',
'periodStart',
'periodEnd',
'budgetInformation',
'inactive',
'budgets',
'spent',
'budgeted',
'previousLoop',
'nextLoop',
'start',
'end'
2017-06-06 12:30:31 -05:00
)
2015-05-24 15:54:59 -05:00
);
2015-02-22 02:46:21 -06:00
}
2017-09-29 01:52:15 -05:00
/**
* @param Carbon $start
* @param Carbon $end
2017-10-22 13:13:02 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2017-09-29 01:52:15 -05:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function infoIncome(Carbon $start, Carbon $end)
{
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('info-income');
if ($cache->has()) {
// @codeCoverageIgnoreStart
$result = $cache->get();
2017-09-29 01:52:15 -05:00
2017-10-22 13:13:02 -05:00
return view('budgets.info', compact('result', 'begin', 'currentEnd'));
// @codeCoverageIgnoreEnd
2017-10-22 13:13:02 -05:00
}
$result = [
'available' => '0',
'earned' => '0',
'suggested' => '0',
];
$currency = app('amount')->getDefaultCurrency();
$range = Preferences::get('viewRange', '1M')->data;
$begin = app('navigation')->subtractPeriod($start, $range, 3);
2017-10-22 13:13:02 -05:00
// get average amount available.
$total = '0';
$count = 0;
$currentStart = clone $begin;
while ($currentStart < $start) {
$currentEnd = app('navigation')->endOfPeriod($currentStart, $range);
2017-10-22 13:13:02 -05:00
$total = bcadd($total, $this->repository->getAvailableBudget($currency, $currentStart, $currentEnd));
$currentStart = app('navigation')->addPeriod($currentStart, $range, 0);
2017-11-15 05:25:49 -06:00
++$count;
2017-10-22 13:13:02 -05:00
}
$result['available'] = bcdiv($total, strval($count));
2017-10-22 13:13:02 -05:00
// amount earned in this period:
$subDay = clone $end;
$subDay->subDay();
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($begin, $subDay)->setTypes([TransactionType::DEPOSIT])->withOpposingAccount();
$result['earned'] = bcdiv(strval($collector->getJournals()->sum('transaction_amount')), strval($count));
2017-10-22 13:13:02 -05:00
// amount spent in period
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($begin, $subDay)->setTypes([TransactionType::WITHDRAWAL])->withOpposingAccount();
$result['spent'] = bcdiv(strval($collector->getJournals()->sum('transaction_amount')), strval($count));
// suggestion starts with the amount spent
$result['suggested'] = bcmul($result['spent'], '-1');
2017-11-15 05:25:49 -06:00
$result['suggested'] = 1 === bccomp($result['suggested'], $result['earned']) ? $result['earned'] : $result['suggested'];
2017-10-22 13:13:02 -05:00
// unless it's more than you earned. So min() of suggested/earned
2017-10-22 13:13:02 -05:00
$cache->store($result);
2017-09-29 01:52:15 -05:00
return view('budgets.info', compact('result', 'begin', 'currentEnd'));
}
2015-02-22 08:40:13 -06:00
/**
2017-04-09 00:56:46 -05:00
* @param Request $request
* @param JournalRepositoryInterface $repository
* @param string $moment
2016-12-28 06:02:56 -06:00
*
* @return View
2017-09-16 02:24:48 -05:00
*
2017-09-27 01:45:27 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2015-02-22 08:40:13 -06:00
*/
2017-03-09 14:05:37 -06:00
public function noBudget(Request $request, JournalRepositoryInterface $repository, string $moment = '')
2015-02-22 08:40:13 -06:00
{
// default values:
$range = Preferences::get('viewRange', '1M')->data;
$start = null;
$end = null;
$periods = new Collection;
// prep for "all" view.
2017-11-15 05:25:49 -06:00
if ('all' === $moment) {
$subTitle = trans('firefly.all_journals_without_budget');
2017-03-09 14:05:37 -06:00
$first = $repository->first();
$start = $first->date ?? new Carbon;
$end = new Carbon;
}
// prep for "specific date" view.
2017-11-15 05:25:49 -06:00
if (strlen($moment) > 0 && 'all' !== $moment) {
$start = new Carbon($moment);
$end = app('navigation')->endOfPeriod($start, $range);
$subTitle = trans(
'firefly.without_budget_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
$periods = $this->getPeriodOverview();
}
// prep for current period
2017-11-15 05:25:49 -06:00
if (0 === strlen($moment)) {
$start = clone session('start', app('navigation')->startOfPeriod(new Carbon, $range));
$end = clone session('end', app('navigation')->endOfPeriod(new Carbon, $range));
$periods = $this->getPeriodOverview();
$subTitle = trans(
'firefly.without_budget_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
}
2017-07-30 06:42:30 -05:00
$page = intval($request->get('page'));
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
2015-02-22 08:40:13 -06:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setLimit($pageSize)->setPage($page)
->withoutBudget()->withOpposingAccount();
2017-11-05 14:16:20 -06:00
$transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('budgets.no-budget'));
2017-03-10 09:08:58 -06:00
2017-11-05 14:16:20 -06:00
return view('budgets.no-budget', compact('transactions', 'subTitle', 'moment', 'periods', 'start', 'end'));
2015-02-22 08:40:13 -06:00
}
/**
2017-02-24 22:57:01 -06:00
* @param BudgetIncomeRequest $request
*
2015-05-03 05:54:39 -05:00
* @return \Illuminate\Http\RedirectResponse
2015-02-22 08:40:13 -06:00
*/
2016-12-22 09:36:56 -06:00
public function postUpdateIncome(BudgetIncomeRequest $request)
2015-02-22 08:40:13 -06:00
{
$start = Carbon::createFromFormat('Y-m-d', $request->string('start'));
$end = Carbon::createFromFormat('Y-m-d', $request->string('end'));
2017-10-05 04:49:06 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
2016-12-22 09:36:56 -06:00
$amount = $request->get('amount');
$this->repository->setAvailableBudget($defaultCurrency, $start, $end, $amount);
Preferences::mark();
2015-02-22 08:40:13 -06:00
2017-08-31 00:29:23 -05:00
return redirect(route('budgets.index', [$start->format('Y-m-d')]));
2015-02-22 08:40:13 -06:00
}
2015-04-05 03:36:28 -05:00
/**
2017-01-08 03:19:10 -06:00
* @param Request $request
* @param Budget $budget
2015-04-05 03:36:28 -05:00
*
2016-02-05 22:01:34 -06:00
* @return View
2015-04-05 03:36:28 -05:00
*/
2017-01-08 03:19:10 -06:00
public function show(Request $request, Budget $budget)
2015-04-05 03:36:28 -05:00
{
2016-05-05 23:15:46 -05:00
/** @var Carbon $start */
2016-12-11 07:03:30 -06:00
$start = session('first', Carbon::create()->startOfYear());
$end = new Carbon;
2017-07-30 06:42:30 -05:00
$page = intval($request->get('page'));
2016-12-11 07:03:30 -06:00
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
2017-01-08 03:19:10 -06:00
$limits = $this->getLimits($budget, $start, $end);
2016-12-11 07:03:30 -06:00
$repetition = null;
// collector:
2016-12-28 04:34:00 -06:00
/** @var JournalCollectorInterface $collector */
2017-02-05 09:16:15 -06:00
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end)->setBudget($budget)->setLimit($pageSize)->setPage($page)->withBudgetInformation();
2017-11-05 14:16:20 -06:00
$transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('budgets.show', [$budget->id]));
2016-05-05 23:15:46 -05:00
$subTitle = trans('firefly.all_journals_for_budget', ['name' => $budget->name]);
2015-04-05 03:36:28 -05:00
2017-11-05 14:16:20 -06:00
return view('budgets.show', compact('limits', 'budget', 'repetition', 'transactions', 'subTitle'));
2016-04-29 01:56:56 -05:00
}
/**
2016-12-30 00:39:42 -06:00
* @param Request $request
* @param Budget $budget
* @param BudgetLimit $budgetLimit
2016-04-29 01:56:56 -05:00
*
* @return View
2017-11-15 05:25:49 -06:00
*
2016-04-29 01:56:56 -05:00
* @throws FireflyException
*/
2016-12-30 00:39:42 -06:00
public function showByBudgetLimit(Request $request, Budget $budget, BudgetLimit $budgetLimit)
2016-11-26 02:16:06 -06:00
{
2017-07-15 09:41:07 -05:00
if ($budgetLimit->budget->id !== $budget->id) {
2016-04-29 01:56:56 -05:00
throw new FireflyException('This budget limit is not part of this budget.');
2015-06-04 10:43:50 -05:00
}
2016-11-26 02:16:06 -06:00
2017-07-30 06:42:30 -05:00
$page = intval($request->get('page'));
2017-02-05 01:26:54 -06:00
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
$subTitle = trans(
2017-11-15 03:52:29 -06:00
'firefly.budget_in_period',
[
2017-11-15 04:33:07 -06:00
'name' => $budget->name,
'start' => $budgetLimit->start_date->formatLocalized($this->monthAndDayFormat),
'end' => $budgetLimit->end_date->formatLocalized($this->monthAndDayFormat),
]
2016-11-26 02:16:06 -06:00
);
2016-10-29 09:11:54 -05:00
// collector:
2016-12-28 04:34:00 -06:00
/** @var JournalCollectorInterface $collector */
2017-02-05 09:16:15 -06:00
$collector = app(JournalCollectorInterface::class);
2016-12-30 00:39:42 -06:00
$collector->setAllAssetAccounts()->setRange($budgetLimit->start_date, $budgetLimit->end_date)
->setBudget($budget)->setLimit($pageSize)->setPage($page)->withBudgetInformation();
2017-11-05 14:16:20 -06:00
$transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('budgets.show', [$budget->id, $budgetLimit->id]));
2017-01-08 03:19:10 -06:00
$start = session('first', Carbon::create()->startOfYear());
$end = new Carbon;
$limits = $this->getLimits($budget, $start, $end);
2015-04-05 03:36:28 -05:00
2017-11-05 14:16:20 -06:00
return view('budgets.show', compact('limits', 'budget', 'budgetLimit', 'transactions', 'subTitle'));
2015-04-05 03:36:28 -05:00
}
/**
2017-01-08 03:19:10 -06:00
* @param BudgetFormRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
2017-01-08 03:19:10 -06:00
public function store(BudgetFormRequest $request)
2015-02-22 08:40:13 -06:00
{
2016-10-29 00:44:46 -05:00
$data = $request->getBudgetData();
2017-01-08 03:19:10 -06:00
$budget = $this->repository->store($data);
2015-02-22 08:40:13 -06:00
2017-10-19 11:08:50 -05:00
$request->session()->flash('success', strval(trans('firefly.stored_new_budget', ['name' => $budget->name])));
Preferences::mark();
2015-02-22 08:40:13 -06:00
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('create_another'))) {
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('budgets.create.fromStore', true);
2015-04-05 03:36:28 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('budgets.create'))->withInput();
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreEnd
2015-03-28 12:22:36 -05:00
}
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('budgets.create.uri'));
2015-02-22 08:40:13 -06:00
}
/**
2017-01-08 03:19:10 -06:00
* @param BudgetFormRequest $request
* @param Budget $budget
2015-02-22 08:40:13 -06:00
*
* @return \Illuminate\Http\RedirectResponse
2015-02-22 08:40:13 -06:00
*/
2017-01-08 03:19:10 -06:00
public function update(BudgetFormRequest $request, Budget $budget)
2015-02-22 08:40:13 -06:00
{
2016-10-22 15:03:00 -05:00
$data = $request->getBudgetData();
2017-01-08 03:19:10 -06:00
$this->repository->update($budget, $data);
2015-02-22 08:40:13 -06:00
2017-10-19 11:08:50 -05:00
$request->session()->flash('success', strval(trans('firefly.updated_budget', ['name' => $budget->name])));
Preferences::mark();
2015-02-22 08:40:13 -06:00
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('return_to_edit'))) {
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('budgets.edit.fromUpdate', true);
2015-04-05 03:36:28 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('budgets.edit', [$budget->id]))->withInput(['return_to_edit' => 1]);
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreEnd
}
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('budgets.edit.uri'));
2015-02-22 08:40:13 -06:00
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 08:40:13 -06:00
*/
public function updateIncome(Carbon $start, Carbon $end)
2015-02-22 08:40:13 -06:00
{
2017-10-05 04:49:06 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
2016-12-22 09:36:56 -06:00
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
2017-08-26 03:01:48 -05:00
$available = round($available, $defaultCurrency->decimal_places);
2016-12-22 09:36:56 -06:00
return view('budgets.income', compact('available', 'start', 'end'));
}
2017-01-08 03:19:10 -06:00
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
private function getLimits(Budget $budget, Carbon $start, Carbon $end): Collection
{
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($budget->id);
$cache->addProperty('get-limits');
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2017-01-08 03:19:10 -06:00
}
2017-10-14 00:58:29 -05:00
$set = $this->repository->getBudgetLimits($budget, $start, $end);
$limits = new Collection();
2017-01-08 03:19:10 -06:00
/** @var BudgetLimit $entry */
foreach ($set as $entry) {
2017-10-14 00:58:29 -05:00
$entry->spent = $this->repository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->start_date, $entry->end_date);
2017-01-08 03:19:10 -06:00
$limits->push($entry);
}
$cache->store($limits);
return $set;
}
/**
* @return Collection
*/
private function getPeriodOverview(): Collection
{
$repository = app(JournalRepositoryInterface::class);
$first = $repository->first();
$start = $first->date ?? new Carbon;
$range = Preferences::get('viewRange', '1M')->data;
$start = app('navigation')->startOfPeriod($start, $range);
$end = app('navigation')->endOfX(new Carbon, $range, null);
$entries = new Collection;
2017-10-22 13:13:02 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('no-budget-period-entries');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
Log::debug('Going to get period expenses and incomes.');
while ($end >= $start) {
$end = app('navigation')->startOfPeriod($end, $range);
$currentEnd = app('navigation')->endOfPeriod($end, $range);
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutBudget()->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL]);
$set = $collector->getJournals();
2017-12-06 12:21:17 -06:00
$sum = strval($set->sum('transaction_amount') ?? '0');
$journals = $set->count();
$dateStr = $end->format('Y-m-d');
$dateName = app('navigation')->periodShow($end, $range);
2017-11-15 05:25:49 -06:00
$entries->push(['string' => $dateStr, 'name' => $dateName, 'count' => $journals, 'sum' => $sum, 'date' => clone $end]);
$end = app('navigation')->subtractPeriod($end, $range, 1);
}
$cache->store($entries);
return $entries;
}
2015-02-22 02:46:21 -06:00
}