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

396 lines
14 KiB
PHP
Raw Normal View History

2015-02-22 02:46:21 -06:00
<?php namespace FireflyIII\Http\Controllers;
2015-05-24 15:54:59 -05:00
use Amount;
2015-02-22 02:46:21 -06:00
use Auth;
use Carbon\Carbon;
2016-04-25 06:20:42 -05:00
use Config;
2016-02-05 22:01:34 -06:00
use FireflyIII\Exceptions\FireflyException;
2015-02-22 08:40:13 -06:00
use FireflyIII\Http\Requests\BudgetFormRequest;
2015-02-22 02:46:21 -06:00
use FireflyIII\Models\Budget;
2015-02-22 08:40:13 -06:00
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
2015-02-22 02:46:21 -06:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
2015-02-22 02:46:21 -06:00
use Input;
2015-09-27 10:44:49 -05:00
use Navigation;
2015-02-22 02:46:21 -06:00
use Preferences;
2015-02-22 08:40:13 -06:00
use Response;
2015-02-22 02:46:21 -06:00
use Session;
2015-04-01 02:40:19 -05:00
use URL;
2015-04-05 03:36:28 -05:00
use View;
2015-02-22 02:46:21 -06:00
/**
* Class BudgetController
*
* @package FireflyIII\Http\Controllers
*/
class BudgetController extends Controller
{
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();
2015-05-14 06:17:53 -05:00
View::share('title', trans('firefly.budgets'));
2015-02-22 02:46:21 -06:00
View::share('mainTitleIcon', 'fa-tasks');
2015-04-28 08:26:30 -05:00
View::share('hideBudgets', true);
2015-02-22 02:46:21 -06:00
}
/**
2015-05-03 05:54:39 -05:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
2015-02-22 02:46:21 -06:00
*
2015-05-03 05:54:39 -05:00
* @return \Symfony\Component\HttpFoundation\Response
2015-02-22 02:46:21 -06:00
*/
2015-05-03 05:54:39 -05:00
public function amount(BudgetRepositoryInterface $repository, Budget $budget)
2015-02-22 02:46:21 -06:00
{
2016-02-05 08:41:40 -06:00
$amount = intval(Input::get('amount'));
2016-04-28 03:59:36 -05:00
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
$viewRange = Preferences::get('viewRange', '1M')->data;
// is custom view range?
if (session('is_custom_range') === true) {
$viewRange = 'custom';
}
$limitRepetition = $repository->updateLimitAmount($budget, $start, $end, $viewRange, $amount);
2015-05-24 15:54:59 -05:00
if ($amount == 0) {
$limitRepetition = null;
}
Preferences::mark();
2015-02-22 02:46:21 -06:00
return Response::json(['name' => $budget->name, 'repetition' => $limitRepetition ? $limitRepetition->id : 0]);
}
2015-02-22 08:40:13 -06:00
/**
* @return \Illuminate\View\View
2015-02-22 08:40:13 -06:00
*/
public function create()
{
2015-04-01 02:40:19 -05:00
// put previous url in session if not redirect from store (not "create another").
2016-02-04 00:27:03 -06:00
if (session('budgets.create.fromStore') !== true) {
2015-04-01 02:40:19 -05:00
Session::put('budgets.create.url', URL::previous());
}
Session::forget('budgets.create.fromStore');
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'budgets');
Session::flash('gaEventAction', 'create');
$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
}
/**
* @param Budget $budget
*
* @return \Illuminate\View\View
*/
public function delete(Budget $budget)
{
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
Session::put('budgets.delete.url', URL::previous());
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'budgets');
Session::flash('gaEventAction', 'delete');
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
}
/**
2015-05-03 05:54:39 -05:00
* @param Budget $budget
* @param BudgetRepositoryInterface $repository
2015-02-22 08:40:13 -06:00
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Budget $budget, BudgetRepositoryInterface $repository)
{
$name = $budget->name;
$repository->destroy($budget);
2015-04-01 02:40:19 -05:00
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.deleted_budget', ['name' => e($name)])));
Preferences::mark();
2015-02-22 08:40:13 -06:00
2015-05-25 01:12:31 -05:00
2016-02-04 00:27:03 -06:00
return redirect(session('budgets.delete.url'));
2015-02-22 08:40:13 -06:00
}
/**
* @param Budget $budget
*
* @return \Illuminate\View\View
2015-02-22 08:40:13 -06:00
*/
public function edit(Budget $budget)
{
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").
2016-02-04 00:27:03 -06:00
if (session('budgets.edit.fromUpdate') !== true) {
2015-04-01 02:40:19 -05:00
Session::put('budgets.edit.url', URL::previous());
}
Session::forget('budgets.edit.fromUpdate');
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'budgets');
Session::flash('gaEventAction', 'edit');
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
/**
* @param BudgetRepositoryInterface $repository
*
* @param ARI $accountRepository
2015-05-03 05:54:39 -05:00
*
* @return \Illuminate\View\View
2015-02-22 02:46:21 -06:00
*/
public function index(BudgetRepositoryInterface $repository, ARI $accountRepository)
2015-02-22 02:46:21 -06:00
{
2016-04-25 06:20:42 -05:00
$budgets = $repository->getActiveBudgets();
$inactive = $repository->getInactiveBudgets();
$spent = '0';
$budgeted = '0';
$range = Preferences::get('viewRange', '1M')->data;
$repeatFreq = Config::get('firefly.range_to_repeat_freq.' . $range);
2016-04-28 09:30:21 -05:00
if (session('is_custom_range') === true) {
$repeatFreq = 'custom';
}
2016-04-25 06:20:42 -05:00
/** @var Carbon $start */
$start = session('start', new Carbon);
/** @var Carbon $end */
$end = session('end', new Carbon);
2015-09-27 10:44:49 -05:00
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$budgetIncomeTotal = Preferences::get($key, 1000)->data;
$period = Navigation::periodShow($start, $range);
2016-04-25 06:20:42 -05:00
$periodStart = $start->formatLocalized($this->monthAndDayFormat);
$periodEnd = $end->formatLocalized($this->monthAndDayFormat);
2015-12-15 05:46:40 -06:00
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
2015-04-03 12:39:36 -05:00
/**
* Do some cleanup:
*/
$repository->cleanupBudgets();
2015-02-22 02:46:21 -06:00
// loop the budgets:
2015-05-24 15:54:59 -05:00
/** @var Budget $budget */
foreach ($budgets as $budget) {
2016-04-25 06:20:42 -05:00
$budget->spent = $repository->balanceInPeriod($budget, $start, $end, $accounts);
$budget->currentRep = $repository->getCurrentRepetition($budget, $repeatFreq, $start, $end);
$budget->otherRepetitions = $repository->getValidRepetitions($budget, $start, $end, $budget->currentRep);
2016-04-05 15:00:03 -05:00
if (!is_null($budget->currentRep->id)) {
2015-05-24 15:54:59 -05:00
$budgeted = bcadd($budgeted, $budget->currentRep->amount);
2015-02-22 02:46:21 -06:00
}
2015-05-24 15:54:59 -05:00
$spent = bcadd($spent, $budget->spent);
2015-02-22 02:46:21 -06:00
2015-05-24 15:54:59 -05:00
}
2015-09-27 10:44:49 -05:00
$budgetMaximum = Preferences::get('budgetMaximum', 1000)->data;
$defaultCurrency = Amount::getDefaultCurrency();
2015-02-22 02:46:21 -06:00
2015-05-24 15:54:59 -05:00
return view(
2016-04-25 06:20:42 -05:00
'budgets.index', compact(
'budgetMaximum', 'periodStart', 'periodEnd',
'period', 'range', 'budgetIncomeTotal',
'defaultCurrency', 'inactive', 'budgets',
2016-04-28 03:59:36 -05:00
'spent', 'budgeted'
2016-04-25 06:20:42 -05:00
)
2015-05-24 15:54:59 -05:00
);
2015-02-22 02:46:21 -06:00
}
2015-02-22 08:40:13 -06:00
/**
2015-05-03 05:54:39 -05:00
* @param BudgetRepositoryInterface $repository
*
* @return \Illuminate\View\View
2015-02-22 08:40:13 -06:00
*/
2015-04-05 03:36:28 -05:00
public function noBudget(BudgetRepositoryInterface $repository)
2015-02-22 08:40:13 -06:00
{
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
2016-04-21 02:00:32 -05:00
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
$pageSize = Preferences::get('transactionPageSize', 50)->data;
$list = $repository->getWithoutBudget($start, $end, $page, $pageSize);
2015-05-25 14:17:36 -05:00
$subTitle = trans(
'firefly.without_budget_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
2016-04-21 02:01:34 -05:00
$list->setPath('/budgets/list/noBudget');
2015-02-22 08:40:13 -06:00
2015-02-22 09:19:32 -06:00
return view('budgets.noBudget', compact('list', 'subTitle'));
2015-02-22 08:40:13 -06:00
}
/**
2015-05-03 05:54:39 -05:00
* @return \Illuminate\Http\RedirectResponse
2015-02-22 08:40:13 -06:00
*/
public function postUpdateIncome()
{
2015-09-27 10:44:49 -05:00
$range = Preferences::get('viewRange', '1M')->data;
2016-02-05 08:41:40 -06:00
/** @var Carbon $date */
$date = session('start', new Carbon);
$start = Navigation::startOfPeriod($date, $range);
2015-09-27 10:44:49 -05:00
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
2015-02-22 08:40:13 -06:00
2015-09-27 10:44:49 -05:00
Preferences::set($key, intval(Input::get('amount')));
Preferences::mark();
2015-02-22 08:40:13 -06:00
2015-07-06 09:27:21 -05:00
return redirect(route('budgets.index'));
2015-02-22 08:40:13 -06:00
}
2015-04-05 03:36:28 -05:00
/**
2015-05-03 05:54:39 -05:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
2015-04-05 03:36:28 -05:00
*
2016-02-05 22:01:34 -06:00
* @return View
* @throws FireflyException
2015-04-05 03:36:28 -05:00
*/
2016-04-29 01:56:56 -05:00
public function show(BudgetRepositoryInterface $repository, Budget $budget)
2015-04-05 03:36:28 -05:00
{
2016-04-29 01:56:56 -05:00
$pageSize = Preferences::get('transactionPageSize', 50)->data;
$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();
/** @var LimitRepetition $entry */
foreach ($set as $entry) {
$entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray);
$limits->push($entry);
2015-04-05 03:36:28 -05:00
}
2015-06-04 10:43:50 -05:00
2016-04-29 01:56:56 -05:00
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.');
2015-06-04 10:43:50 -05:00
}
2016-04-29 01:56:56 -05:00
$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);
}
2015-04-05 03:36:28 -05:00
2015-04-28 08:26:30 -05:00
return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle'));
2016-04-29 01:56:56 -05:00
2015-04-05 03:36:28 -05:00
}
/**
* @param BudgetFormRequest $request
* @param BudgetRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-02-22 08:40:13 -06:00
public function store(BudgetFormRequest $request, BudgetRepositoryInterface $repository)
{
$budgetData = [
'name' => $request->input('name'),
'user' => Auth::user()->id,
];
2015-06-06 16:09:12 -05:00
$budget = $repository->store($budgetData);
2015-02-22 08:40:13 -06:00
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.stored_new_budget', ['name' => e($budget->name)])));
Preferences::mark();
2015-02-22 08:40:13 -06:00
2015-03-28 12:22:36 -05:00
if (intval(Input::get('create_another')) === 1) {
2015-04-01 02:40:19 -05:00
// set value so create routine will not overwrite URL:
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();
2015-03-28 12:22:36 -05:00
}
2015-04-01 02:40:19 -05:00
// redirect to previous URL.
2016-02-04 00:27:03 -06:00
return redirect(session('budgets.create.url'));
2015-02-22 08:40:13 -06:00
}
/**
* @param BudgetFormRequest $request
* @param BudgetRepositoryInterface $repository
2015-05-03 05:54:39 -05:00
* @param Budget $budget
2015-02-22 08:40:13 -06:00
*
* @return \Illuminate\Http\RedirectResponse
2015-02-22 08:40:13 -06:00
*/
2015-05-03 05:54:39 -05:00
public function update(BudgetFormRequest $request, BudgetRepositoryInterface $repository, Budget $budget)
2015-02-22 08:40:13 -06:00
{
$budgetData = [
2015-03-29 14:27:51 -05:00
'name' => $request->input('name'),
2015-12-15 05:46:40 -06:00
'active' => intval($request->input('active')) == 1,
2015-02-22 08:40:13 -06:00
];
$repository->update($budget, $budgetData);
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.updated_budget', ['name' => e($budget->name)])));
Preferences::mark();
2015-02-22 08:40:13 -06:00
if (intval(Input::get('return_to_edit')) === 1) {
2015-04-01 02:40:19 -05:00
// set value so edit routine will not overwrite URL:
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]);
}
2015-04-01 02:43:19 -05:00
// redirect to previous URL.
2016-02-04 00:27:03 -06:00
return redirect(session('budgets.edit.url'));
2015-02-22 08:40:13 -06:00
}
/**
* @return \Illuminate\View\View
2015-02-22 08:40:13 -06:00
*/
public function updateIncome()
{
2016-04-25 02:12:52 -05:00
$range = Preferences::get('viewRange', '1M')->data;
$format = strval(trans('config.month_and_day'));
2016-02-05 08:41:40 -06:00
/** @var Carbon $date */
2016-04-25 02:11:09 -05:00
$date = session('start', new Carbon);
$start = Navigation::startOfPeriod($date, $range);
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$amount = Preferences::get($key, 1000);
2016-04-25 02:12:52 -05:00
$displayStart = $start->formatLocalized($format);
$displayEnd = $end->formatLocalized($format);
2016-04-25 02:11:09 -05:00
return view('budgets.income', compact('amount', 'displayStart', 'displayEnd'));
2015-02-22 08:40:13 -06:00
}
2015-02-22 02:46:21 -06:00
}