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

277 lines
8.7 KiB
PHP
Raw Normal View History

2015-02-22 02:46:21 -06:00
<?php namespace FireflyIII\Http\Controllers;
use Auth;
use Carbon\Carbon;
use FireflyIII\Http\Requests;
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
*/
class BudgetController extends Controller
{
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);
return Response::json(['name' => $budget->name, 'repetition' => $limitRepetition ? $limitRepetition->id : 0]);
}
2015-02-22 08:40:13 -06:00
/**
* @return $this
*/
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-05 03:23:01 -05:00
$subTitle = 'Create a 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)
{
$subTitle = 'Delete budget' . e($budget->name) . '"';
2015-04-01 02:40:19 -05:00
// put previous url in session
Session::put('budgets.delete.url', URL::previous());
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.');
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 $this
*/
public function edit(Budget $budget)
{
$subTitle = 'Edit budget "' . e($budget->name) . '"';
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-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 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-02-22 02:46:21 -06:00
2015-04-03 12:39:36 -05:00
/**
* Do some cleanup:
*/
$repository->cleanupBudgets();
2015-02-22 02:46:21 -06:00
// loop the budgets:
$budgets->each(
function (Budget $budget) use ($repository) {
$date = Session::get('start', Carbon::now()->startOfMonth());
2015-05-16 02:09:52 -05:00
$end = Session::get('end', Carbon::now()->endOfMonth());
2015-05-19 23:49:51 -05:00
$budget->spent = $repository->spentInPeriodCorrected($budget, $date, $end);
2015-04-05 03:36:28 -05:00
$budget->currentRep = $repository->getCurrentRepetition($budget, $date);
2015-02-22 02:46:21 -06:00
}
);
2015-04-05 03:36:28 -05:00
$dateAsString = Session::get('start', Carbon::now()->startOfMonth())->format('FY');
2015-02-22 02:46:21 -06:00
$spent = $budgets->sum('spent');
2015-04-05 03:36:28 -05:00
$amount = Preferences::get('budgetIncomeTotal' . $dateAsString, 1000)->data;
2015-02-22 02:46:21 -06:00
$overspent = $spent > $amount;
$spentPCT = $overspent ? ceil($amount / $spent * 100) : ceil($spent / $amount * 100);
$budgetMax = Preferences::get('budgetMaximum', 1000);
$budgetMaximum = $budgetMax->data;
2015-03-29 14:27:51 -05:00
return view('budgets.index', compact('budgetMaximum', 'inactive', 'budgets', 'spent', 'spentPCT', 'overspent', 'amount'));
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 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);
$subTitle = 'Transactions without a budget between ' . $start->format('jS F Y') . ' and ' . $end->format('jS F Y');
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')));
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
*
2015-05-03 05:54:39 -05:00
* @return 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);
$limits = !is_null($repetition->id) ? [$repetition->budgetLimit] : $repository->getBudgetLimits($budget);
$subTitle = !is_null($repetition->id) ? e($budget->name) . ' in ' . $repetition->startdate->format('F Y') : e($budget->name);
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,
];
$budget = $repository->store($budgetData);
Session::flash('success', 'New budget "' . $budget->name . '" stored!');
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
*
2015-05-03 05:54:39 -05:00
* @return $this|\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.');
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-04-01 02:40:19 -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
}
/**
2015-05-03 05:54:39 -05:00
* @return 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
}