2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* BudgetController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 12:27:31 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-20 08:57:45 +02:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-22 09:46:21 +01:00
|
|
|
|
2015-05-24 22:54:59 +02:00
|
|
|
use Amount;
|
2015-02-22 09:46:21 +01:00
|
|
|
use Carbon\Carbon;
|
2016-02-06 05:01:34 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-12-28 11:34:00 +01:00
|
|
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
2015-02-22 15:40:13 +01:00
|
|
|
use FireflyIII\Http\Requests\BudgetFormRequest;
|
2016-12-22 16:36:56 +01:00
|
|
|
use FireflyIII\Http\Requests\BudgetIncomeRequest;
|
2016-05-20 11:02:07 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
2015-02-22 09:46:21 +01:00
|
|
|
use FireflyIII\Models\Budget;
|
2016-12-29 20:48:33 +01:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2016-10-10 07:49:39 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2015-02-22 09:46:21 +01:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2017-01-08 10:19:10 +01:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2016-12-28 13:02:56 +01:00
|
|
|
use Illuminate\Http\Request;
|
2015-12-31 17:46:34 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-22 09:46:21 +01:00
|
|
|
use Preferences;
|
2015-02-22 15:40:13 +01:00
|
|
|
use Response;
|
2015-04-05 10:36:28 +02:00
|
|
|
use View;
|
|
|
|
|
2015-02-22 09:46:21 +01:00
|
|
|
/**
|
|
|
|
* Class BudgetController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers
|
|
|
|
*/
|
|
|
|
class BudgetController extends Controller
|
|
|
|
{
|
|
|
|
|
2016-12-22 16:36:56 +01:00
|
|
|
/** @var BudgetRepositoryInterface */
|
|
|
|
private $repository;
|
|
|
|
|
2015-04-03 19:11:55 +02:00
|
|
|
/**
|
2016-02-04 07:27:03 +01:00
|
|
|
*
|
2015-04-03 19:11:55 +02:00
|
|
|
*/
|
2015-02-22 09:46:21 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-04-28 15:26:30 +02:00
|
|
|
parent::__construct();
|
2016-10-29 07:44:46 +02:00
|
|
|
|
2015-04-28 15:26:30 +02:00
|
|
|
View::share('hideBudgets', true);
|
2016-10-29 07:44:46 +02:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
View::share('title', trans('firefly.budgets'));
|
|
|
|
View::share('mainTitleIcon', 'fa-tasks');
|
2016-12-22 16:36:56 +01:00
|
|
|
$this->repository = app(BudgetRepositoryInterface::class);
|
2016-10-29 07:44:46 +02:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2015-02-22 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-08 10:19:10 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Budget $budget
|
2015-02-22 09:46:21 +01:00
|
|
|
*
|
2016-12-28 13:02:56 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-02-22 09:46:21 +01:00
|
|
|
*/
|
2017-01-08 10:19:10 +01:00
|
|
|
public function amount(Request $request, Budget $budget)
|
2015-02-22 09:46:21 +01:00
|
|
|
{
|
2016-12-28 13:02:56 +01:00
|
|
|
$amount = intval($request->get('amount'));
|
2016-04-28 10:59:36 +02:00
|
|
|
/** @var Carbon $start */
|
|
|
|
$start = session('start', Carbon::now()->startOfMonth());
|
|
|
|
/** @var Carbon $end */
|
2016-12-30 07:39:42 +01:00
|
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
2017-01-08 10:19:10 +01:00
|
|
|
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
|
2015-05-24 22:54:59 +02:00
|
|
|
if ($amount == 0) {
|
2016-12-29 20:52:02 +01:00
|
|
|
$budgetLimit = null;
|
2015-05-24 22:54:59 +02:00
|
|
|
}
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-02-22 09:46:21 +01:00
|
|
|
|
2016-12-29 20:52:02 +01:00
|
|
|
return Response::json(['name' => $budget->name, 'limit' => $budgetLimit ? $budgetLimit->id : 0, 'amount' => $amount]);
|
2015-02-22 09:46:21 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-22 15:40:13 +01:00
|
|
|
/**
|
2017-02-17 20:14:22 +01:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2017-02-17 20:14:22 +01:00
|
|
|
public function create(Request $request)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2015-04-01 09:40:19 +02:00
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
2016-02-04 07:27:03 +01:00
|
|
|
if (session('budgets.create.fromStore') !== true) {
|
2017-02-05 08:26:54 +01:00
|
|
|
$this->rememberPreviousUri('budgets.create.uri');
|
2015-04-01 09:40:19 +02:00
|
|
|
}
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->forget('budgets.create.fromStore');
|
|
|
|
$request->session()->flash('gaEventCategory', 'budgets');
|
|
|
|
$request->session()->flash('gaEventAction', 'create');
|
2016-01-27 18:31:44 +01:00
|
|
|
$subTitle = (string)trans('firefly.create_new_budget');
|
2015-04-01 09:40:19 +02:00
|
|
|
|
2015-05-05 10:23:01 +02:00
|
|
|
return view('budgets.create', compact('subTitle'));
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2017-02-17 20:14:22 +01:00
|
|
|
public function delete(Request $request, Budget $budget)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2015-05-25 22:16:00 +02:00
|
|
|
$subTitle = trans('firefly.delete_budget', ['name' => $budget->name]);
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2015-04-01 09:40:19 +02:00
|
|
|
// put previous url in session
|
2017-02-05 08:26:54 +01:00
|
|
|
$this->rememberPreviousUri('budgets.delete.uri');
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->flash('gaEventCategory', 'budgets');
|
|
|
|
$request->session()->flash('gaEventAction', 'delete');
|
2015-04-01 09:40:19 +02:00
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
return view('budgets.delete', compact('budget', 'subTitle'));
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-17 20:14:22 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Budget $budget
|
2015-02-22 15:40:13 +01:00
|
|
|
*
|
2017-02-17 20:14:22 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2017-02-17 20:14:22 +01:00
|
|
|
public function destroy(Request $request, Budget $budget)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
|
|
|
|
2017-02-05 08:26:54 +01:00
|
|
|
$name = $budget->name;
|
2017-01-08 10:19:10 +01:00
|
|
|
$this->repository->destroy($budget);
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.deleted_budget', ['name' => e($name)])));
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2017-02-05 08:26:54 +01:00
|
|
|
return redirect($this->getPreviousUri('budgets.delete.uri'));
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-17 20:14:22 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Budget $budget
|
2015-02-22 15:40:13 +01:00
|
|
|
*
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2017-02-17 20:14:22 +01:00
|
|
|
public function edit(Request $request, Budget $budget)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2015-06-13 10:02:36 +02:00
|
|
|
$subTitle = trans('firefly.edit_budget', ['name' => $budget->name]);
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2015-04-01 09:40:19 +02:00
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
2016-02-04 07:27:03 +01:00
|
|
|
if (session('budgets.edit.fromUpdate') !== true) {
|
2017-02-05 08:26:54 +01:00
|
|
|
$this->rememberPreviousUri('budgets.edit.uri');
|
2015-04-01 09:40:19 +02:00
|
|
|
}
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->forget('budgets.edit.fromUpdate');
|
|
|
|
$request->session()->flash('gaEventCategory', 'budgets');
|
|
|
|
$request->session()->flash('gaEventAction', 'edit');
|
2015-04-01 09:40:19 +02:00
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
return view('budgets.edit', compact('budget', 'subTitle'));
|
2015-02-22 15:40:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-22 09:46:21 +01:00
|
|
|
/**
|
2016-05-20 11:02:07 +02:00
|
|
|
* @return View
|
2015-02-22 09:46:21 +01:00
|
|
|
*/
|
2016-12-22 16:36:56 +01:00
|
|
|
public function index()
|
2015-02-22 09:46:21 +01:00
|
|
|
{
|
2016-12-22 16:36:56 +01:00
|
|
|
$this->repository->cleanupBudgets();
|
2016-06-07 12:22:46 +02:00
|
|
|
|
2016-12-22 16:36:56 +01:00
|
|
|
$budgets = $this->repository->getActiveBudgets();
|
|
|
|
$inactive = $this->repository->getInactiveBudgets();
|
|
|
|
$start = session('start', new Carbon);
|
2016-04-25 13:20:42 +02:00
|
|
|
$end = session('end', new Carbon);
|
|
|
|
$periodStart = $start->formatLocalized($this->monthAndDayFormat);
|
|
|
|
$periodEnd = $end->formatLocalized($this->monthAndDayFormat);
|
2016-12-22 16:36:56 +01:00
|
|
|
$budgetInformation = $this->collectBudgetInformation($budgets, $start, $end);
|
|
|
|
$defaultCurrency = Amount::getDefaultCurrency();
|
|
|
|
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
|
|
|
|
$spent = array_sum(array_column($budgetInformation, 'spent'));
|
|
|
|
$budgeted = array_sum(array_column($budgetInformation, 'budgeted'));
|
2015-02-22 09:46:21 +01:00
|
|
|
|
2015-05-24 22:54:59 +02:00
|
|
|
return view(
|
2016-12-22 16:36:56 +01:00
|
|
|
'budgets.index',
|
2016-12-30 11:51:58 +01:00
|
|
|
compact('available', 'periodStart', 'periodEnd', 'budgetInformation', 'inactive', 'budgets', 'spent', 'budgeted')
|
2015-05-24 22:54:59 +02:00
|
|
|
);
|
2015-02-22 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
2015-02-22 15:40:13 +01:00
|
|
|
/**
|
2016-12-28 13:02:56 +01:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2016-12-28 13:02:56 +01:00
|
|
|
public function noBudget(Request $request)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2016-02-24 14:49:36 +01:00
|
|
|
/** @var Carbon $start */
|
|
|
|
$start = session('start', Carbon::now()->startOfMonth());
|
|
|
|
/** @var Carbon $end */
|
2016-11-05 18:43:18 +01:00
|
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
2016-12-28 13:02:56 +01:00
|
|
|
$page = intval($request->get('page')) == 0 ? 1 : intval($request->get('page'));
|
2016-11-05 18:08:44 +01:00
|
|
|
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
|
2016-09-16 12:07:45 +02:00
|
|
|
$subTitle = trans(
|
|
|
|
'firefly.without_budget_between',
|
2015-05-25 21:17:36 +02:00
|
|
|
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
|
|
|
|
);
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2016-11-05 18:08:44 +01:00
|
|
|
// collector
|
2016-12-28 11:34:00 +01:00
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 16:16:15 +01:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2016-11-05 18:08:44 +01:00
|
|
|
$collector->setAllAssetAccounts()->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withoutBudget();
|
2016-11-05 18:43:18 +01:00
|
|
|
$journals = $collector->getPaginatedJournals();
|
2016-11-05 18:08:44 +01:00
|
|
|
$journals->setPath('/budgets/list/noBudget');
|
|
|
|
|
|
|
|
return view('budgets.no-budget', compact('journals', 'subTitle'));
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-03 12:54:39 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2016-12-22 16:36:56 +01:00
|
|
|
public function postUpdateIncome(BudgetIncomeRequest $request)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2016-12-22 16:36:56 +01:00
|
|
|
$start = session('start', new Carbon);
|
|
|
|
$end = session('end', new Carbon);
|
|
|
|
$defaultCurrency = Amount::getDefaultCurrency();
|
|
|
|
$amount = $request->get('amount');
|
|
|
|
|
|
|
|
$this->repository->setAvailableBudget($defaultCurrency, $start, $end, $amount);
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('budgets.index'));
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
2015-04-05 10:36:28 +02:00
|
|
|
/**
|
2017-01-08 10:19:10 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Budget $budget
|
2015-04-05 10:36:28 +02:00
|
|
|
*
|
2016-02-06 05:01:34 +01:00
|
|
|
* @return View
|
2015-04-05 10:36:28 +02:00
|
|
|
*/
|
2017-01-08 10:19:10 +01:00
|
|
|
public function show(Request $request, Budget $budget)
|
2015-04-05 10:36:28 +02:00
|
|
|
{
|
2016-05-06 06:15:46 +02:00
|
|
|
/** @var Carbon $start */
|
2016-12-11 14:03:30 +01:00
|
|
|
$start = session('first', Carbon::create()->startOfYear());
|
|
|
|
$end = new Carbon;
|
2016-12-28 13:02:56 +01:00
|
|
|
$page = intval($request->get('page')) == 0 ? 1 : intval($request->get('page'));
|
2016-12-11 14:03:30 +01:00
|
|
|
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
|
2017-01-08 10:19:10 +01:00
|
|
|
$limits = $this->getLimits($budget, $start, $end);
|
2016-12-11 14:03:30 +01:00
|
|
|
$repetition = null;
|
2016-11-05 17:47:50 +01:00
|
|
|
// collector:
|
2016-12-28 11:34:00 +01:00
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 16:16:15 +01:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2016-12-28 06:14:58 +01:00
|
|
|
$collector->setAllAssetAccounts()->setRange($start, $end)->setBudget($budget)->setLimit($pageSize)->setPage($page)->withCategoryInformation();
|
2016-11-05 18:43:18 +01:00
|
|
|
$journals = $collector->getPaginatedJournals();
|
2016-05-06 06:15:46 +02:00
|
|
|
$journals->setPath('/budgets/show/' . $budget->id);
|
|
|
|
|
|
|
|
|
2016-04-29 08:56:56 +02:00
|
|
|
$subTitle = e($budget->name);
|
2015-04-05 10:36:28 +02:00
|
|
|
|
2016-04-29 08:56:56 +02:00
|
|
|
return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-30 07:39:42 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param BudgetLimit $budgetLimit
|
2016-04-29 08:56:56 +02:00
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2016-12-30 07:39:42 +01:00
|
|
|
public function showByBudgetLimit(Request $request, Budget $budget, BudgetLimit $budgetLimit)
|
2016-11-26 09:16:06 +01:00
|
|
|
{
|
2016-12-30 08:41:48 +01:00
|
|
|
if ($budgetLimit->budget->id != $budget->id) {
|
2016-04-29 08:56:56 +02:00
|
|
|
throw new FireflyException('This budget limit is not part of this budget.');
|
2015-06-04 17:43:50 +02:00
|
|
|
}
|
2016-11-26 09:16:06 +01:00
|
|
|
|
2017-02-05 08:26:54 +01:00
|
|
|
$page = intval($request->get('page')) == 0 ? 1 : intval($request->get('page'));
|
|
|
|
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
|
|
|
|
$subTitle = trans(
|
2016-12-30 07:39:42 +01:00
|
|
|
'firefly.budget_in_period', [
|
|
|
|
'name' => $budget->name,
|
|
|
|
'start' => $budgetLimit->start_date->formatLocalized($this->monthAndDayFormat),
|
|
|
|
'end' => $budgetLimit->end_date->formatLocalized($this->monthAndDayFormat),
|
|
|
|
]
|
2016-11-26 09:16:06 +01:00
|
|
|
);
|
2016-10-29 16:11:54 +02:00
|
|
|
|
2016-11-05 17:47:50 +01:00
|
|
|
// collector:
|
2016-12-28 11:34:00 +01:00
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 16:16:15 +01:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2016-12-30 07:39:42 +01:00
|
|
|
$collector->setAllAssetAccounts()->setRange($budgetLimit->start_date, $budgetLimit->end_date)
|
|
|
|
->setBudget($budget)->setLimit($pageSize)->setPage($page)->withCategoryInformation();
|
2016-11-05 18:43:18 +01:00
|
|
|
$journals = $collector->getPaginatedJournals();
|
2016-12-30 07:39:42 +01:00
|
|
|
$journals->setPath('/budgets/show/' . $budget->id . '/' . $budgetLimit->id);
|
2015-12-31 17:46:34 +01:00
|
|
|
|
|
|
|
|
2017-01-08 10:19:10 +01:00
|
|
|
$start = session('first', Carbon::create()->startOfYear());
|
|
|
|
$end = new Carbon;
|
|
|
|
$limits = $this->getLimits($budget, $start, $end);
|
2015-04-05 10:36:28 +02:00
|
|
|
|
2016-12-30 07:39:42 +01:00
|
|
|
return view('budgets.show', compact('limits', 'budget', 'budgetLimit', 'journals', 'subTitle'));
|
2016-04-29 08:56:56 +02:00
|
|
|
|
2015-04-05 10:36:28 +02:00
|
|
|
}
|
|
|
|
|
2015-03-26 17:45:03 +01:00
|
|
|
/**
|
2017-01-08 10:19:10 +01:00
|
|
|
* @param BudgetFormRequest $request
|
2015-03-26 17:45:03 +01:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2017-01-08 10:19:10 +01:00
|
|
|
public function store(BudgetFormRequest $request)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2016-10-29 07:44:46 +02:00
|
|
|
$data = $request->getBudgetData();
|
2017-01-08 10:19:10 +01:00
|
|
|
$budget = $this->repository->store($data);
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.stored_new_budget', ['name' => e($budget->name)])));
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2016-12-28 13:02:56 +01:00
|
|
|
if (intval($request->get('create_another')) === 1) {
|
2015-04-01 09:40:19 +02:00
|
|
|
// set value so create routine will not overwrite URL:
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->put('budgets.create.fromStore', true);
|
2015-04-05 10:36:28 +02:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('budgets.create'))->withInput();
|
2015-03-28 18:22:36 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:26:54 +01:00
|
|
|
return redirect($this->getPreviousUri('budgets.create.uri'));
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-08 10:19:10 +01:00
|
|
|
* @param BudgetFormRequest $request
|
|
|
|
* @param Budget $budget
|
2015-02-22 15:40:13 +01:00
|
|
|
*
|
2015-05-26 17:50:09 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2017-01-08 10:19:10 +01:00
|
|
|
public function update(BudgetFormRequest $request, Budget $budget)
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2016-10-22 22:03:00 +02:00
|
|
|
$data = $request->getBudgetData();
|
2017-01-08 10:19:10 +01:00
|
|
|
$this->repository->update($budget, $data);
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.updated_budget', ['name' => e($budget->name)])));
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2016-12-28 13:02:56 +01:00
|
|
|
if (intval($request->get('return_to_edit')) === 1) {
|
2015-04-01 09:40:19 +02:00
|
|
|
// set value so edit routine will not overwrite URL:
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->put('budgets.edit.fromUpdate', true);
|
2015-04-05 10:36:28 +02:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('budgets.edit', [$budget->id]))->withInput(['return_to_edit' => 1]);
|
2015-03-26 17:45:03 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:26:54 +01:00
|
|
|
return redirect($this->getPreviousUri('budgets.edit.uri'));
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
|
|
|
public function updateIncome()
|
|
|
|
{
|
2016-12-22 16:36:56 +01:00
|
|
|
$start = session('start', new Carbon);
|
|
|
|
$end = session('end', new Carbon);
|
|
|
|
$defaultCurrency = Amount::getDefaultCurrency();
|
|
|
|
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
|
|
|
|
|
|
|
|
|
|
|
|
return view('budgets.income', compact('available', 'start', 'end'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
// get account information
|
2017-01-05 08:56:14 +01:00
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
2016-12-22 16:36:56 +01:00
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
|
|
|
|
$return = [];
|
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
$budgetId = $budget->id;
|
|
|
|
$return[$budgetId] = [
|
2017-01-05 09:08:35 +01:00
|
|
|
'spent' => $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end),
|
2016-12-22 16:36:56 +01:00
|
|
|
'budgeted' => '0',
|
|
|
|
'currentRep' => false,
|
|
|
|
];
|
2016-12-29 20:48:33 +01:00
|
|
|
$budgetLimits = $this->repository->getBudgetLimits($budget, $start, $end);
|
|
|
|
$otherLimits = new Collection;
|
|
|
|
|
|
|
|
// get all the budget limits relevant between start and end and examine them:
|
|
|
|
/** @var BudgetLimit $limit */
|
|
|
|
foreach ($budgetLimits as $limit) {
|
|
|
|
if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end)
|
|
|
|
) {
|
|
|
|
$return[$budgetId]['currentLimit'] = $limit;
|
2016-12-30 07:39:42 +01:00
|
|
|
$return[$budgetId]['budgeted'] = $limit->amount;
|
2016-12-29 20:48:33 +01:00
|
|
|
continue;
|
2016-12-22 16:36:56 +01:00
|
|
|
}
|
2016-12-29 20:48:33 +01:00
|
|
|
// otherwise it's just one of the many relevant repetitions:
|
|
|
|
$otherLimits->push($limit);
|
2016-12-22 16:36:56 +01:00
|
|
|
}
|
2016-12-29 20:48:33 +01:00
|
|
|
$return[$budgetId]['otherLimits'] = $otherLimits;
|
2016-12-22 16:36:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 10:19:10 +01: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()) {
|
|
|
|
return $cache->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
|
|
|
|
$set = $this->repository->getBudgetLimits($budget, $start, $end);
|
|
|
|
$limits = new Collection();
|
|
|
|
|
|
|
|
/** @var BudgetLimit $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
$entry->spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $entry->start_date, $entry->end_date);
|
|
|
|
$limits->push($entry);
|
|
|
|
}
|
|
|
|
$cache->store($limits);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2015-02-22 09:46:21 +01:00
|
|
|
}
|