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

308 lines
9.7 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;
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;
2015-02-22 02:46:21 -06:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Input;
use Preferences;
2015-02-22 08:40:13 -06:00
use Redirect;
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
2015-05-26 01:17:58 -05:00
* @SuppressWarnings(PHPMD.TooManyMethods)
2015-02-22 02:46:21 -06:00
*/
class BudgetController extends Controller
{
2015-04-03 12:11:55 -05:00
/**
2015-05-23 13:49:57 -05:00
* @codeCoverageIgnore
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
{
$amount = intval(Input::get('amount'));
$date = Session::get('start', Carbon::now()->startOfMonth());
$limitRepetition = $repository->updateLimitAmount($budget, $date, $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").
if (Session::get('budgets.create.fromStore') !== true) {
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 = 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
2015-02-22 08:40:13 -06:00
Session::flash('success', 'The budget "' . e($name) . '" was deleted.');
Preferences::mark();
2015-02-22 08:40:13 -06:00
2015-05-25 01:12:31 -05:00
2015-04-01 02:40:19 -05:00
return Redirect::to(Session::get('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").
if (Session::get('budgets.edit.fromUpdate') !== true) {
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
/**
2015-05-03 05:54:39 -05:00
* @param BudgetRepositoryInterface $repository
*
* @return \Illuminate\View\View
2015-02-22 02:46:21 -06:00
*/
public function index(BudgetRepositoryInterface $repository)
{
2015-04-05 03:36:28 -05:00
$budgets = $repository->getActiveBudgets();
$inactive = $repository->getInactiveBudgets();
2015-05-24 15:54:59 -05:00
$spent = '0';
$budgeted = '0';
bcscale(2);
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) {
$date = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$budget->spent = $repository->spentInPeriodCorrected($budget, $date, $end);
$budget->currentRep = $repository->getCurrentRepetition($budget, $date);
if ($budget->currentRep) {
$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
}
$dateAsString = Session::get('start', Carbon::now()->startOfMonth())->format('FY');
$budgetIncomeTotal = Preferences::get('budgetIncomeTotal' . $dateAsString, 1000)->data;
$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(
2015-05-24 15:59:48 -05:00
'budgets.index', compact('budgetMaximum', 'budgetIncomeTotal', 'defaultCurrency', 'inactive', 'budgets', 'spent', 'budgeted')
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
{
2015-04-05 03:36:28 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->startOfMonth());
$list = $repository->getWithoutBudget($start, $end);
2015-05-25 14:17:36 -05:00
$subTitle = trans(
'firefly.without_budget_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
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()
{
$date = Session::get('start', Carbon::now()->startOfMonth())->format('FY');
Preferences::set('budgetIncomeTotal' . $date, intval(Input::get('amount')));
Preferences::mark();
2015-02-22 08:40:13 -06:00
return Redirect::route('budgets.index');
}
2015-04-05 03:36:28 -05:00
/**
2015-05-03 05:54:39 -05:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @param LimitRepetition $repetition
2015-04-05 03:36:28 -05:00
*
* @return \Illuminate\View\View
2015-04-05 03:36:28 -05:00
*/
2015-05-03 05:54:39 -05:00
public function show(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition = null)
2015-04-05 03:36:28 -05:00
{
if (!is_null($repetition->id) && $repetition->budgetLimit->budget->id != $budget->id) {
2015-05-05 03:23:01 -05:00
$message = 'Invalid selection.';
return view('error', compact('message'));
2015-04-05 03:36:28 -05:00
}
2015-04-28 08:26:30 -05:00
$journals = $repository->getJournals($budget, $repetition);
2015-06-04 10:43:50 -05:00
if (is_null($repetition->id)) {
$limits = $repository->getBudgetLimits($budget);
$subTitle = e($budget->name);
} else {
$limits = [$repetition->budgetLimit];
$subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]);
}
2015-05-02 16:28:04 -05:00
$journals->setPath('/budgets/show/' . $budget->id);
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'));
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
Session::flash('success', 'New budget "' . $budget->name . '" stored!');
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-03-28 12:22:36 -05:00
return Redirect::route('budgets.create')->withInput();
}
2015-04-01 02:40:19 -05:00
// redirect to previous URL.
return Redirect::to(Session::get('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'),
'active' => intval($request->input('active')) == 1
2015-02-22 08:40:13 -06:00
];
$repository->update($budget, $budgetData);
Session::flash('success', 'Budget "' . $budget->name . '" updated.');
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-05-26 13:37:01 -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.
return Redirect::to(Session::get('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()
{
2015-05-03 05:58:55 -05:00
$date = Session::get('start', Carbon::now()->startOfMonth())->format('FY');
2015-05-03 05:54:39 -05:00
$amount = Preferences::get('budgetIncomeTotal' . $date, 1000);
2015-02-22 08:40:13 -06:00
2015-05-03 05:58:55 -05:00
return view('budgets.income', compact('amount'));
2015-02-22 08:40:13 -06:00
}
2015-02-22 02:46:21 -06:00
}