firefly-iii/app/controllers/BudgetController.php

255 lines
8.1 KiB
PHP
Raw Normal View History

<?php
use Carbon\Carbon;
2014-12-13 15:54:52 -06:00
use FireflyIII\Database\Budget\Budget as BudgetRepository;
2014-12-12 00:13:40 -06:00
use FireflyIII\Shared\Preferences\PreferencesInterface as Pref;
2014-11-04 13:37:00 -06:00
2014-11-08 12:11:51 -06:00
/**
* Class BudgetController
2014-12-12 00:13:40 -06:00
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
2014-12-12 09:22:16 -06:00
*
2014-11-08 12:11:51 -06:00
*/
class BudgetController extends BaseController
{
2014-12-12 00:13:40 -06:00
/** @var Pref */
protected $_preferences;
2014-12-07 13:45:52 -06:00
/** @var BudgetRepository */
protected $_repository;
2014-12-06 10:53:25 -06:00
/**
2014-12-07 13:45:52 -06:00
* @param BudgetRepository $repository
2014-12-12 00:13:40 -06:00
* @param Pref $preferences
2014-12-06 10:53:25 -06:00
*/
2014-12-12 00:13:40 -06:00
public function __construct(BudgetRepository $repository, Pref $preferences)
{
2014-12-12 00:13:40 -06:00
$this->_repository = $repository;
$this->_preferences = $preferences;
2014-09-28 02:20:25 -05:00
View::share('title', 'Budgets');
2014-09-15 10:46:01 -05:00
View::share('mainTitleIcon', 'fa-tasks');
}
2014-08-10 08:01:46 -05:00
/**
* @param Budget $budget
2014-12-06 10:53:25 -06:00
*
* @return \Illuminate\Http\JsonResponse
* @throws Exception
2014-08-10 08:01:46 -05:00
*/
2014-11-04 13:37:00 -06:00
public function amount(Budget $budget)
2014-07-28 14:33:32 -05:00
{
2014-12-12 00:13:40 -06:00
$amount = intval(Input::get('amount'));
2014-12-13 14:59:02 -06:00
$date = Session::get('start', Carbon::now()->startOfMonth());
2014-12-12 00:13:40 -06:00
$limitRepetition = $this->_repository->updateLimitAmount($budget, $date, $amount);
2015-01-25 01:28:59 -06:00
return Response::json(['name' => $budget->name, 'repetition' => $limitRepetition ? $limitRepetition->id : 0]);
}
2014-11-12 15:36:02 -06:00
/**
* @return $this
*/
public function create()
{
return View::make('budgets.create')->with('subTitle', 'Create a new budget');
}
/**
* @param Budget $budget
*
* @return $this
*/
public function delete(Budget $budget)
{
2014-12-07 13:45:52 -06:00
$subTitle = 'Delete budget "' . e($budget->name) . '"';
return View::make('budgets.delete', compact('budget', 'subTitle'));
2014-11-12 15:36:02 -06:00
}
2014-12-06 10:53:25 -06:00
/**
* @param Budget $budget
*
* @return \Illuminate\Http\RedirectResponse
*/
2014-11-12 15:36:02 -06:00
public function destroy(Budget $budget)
{
2014-12-12 00:13:40 -06:00
Session::flash('success', 'Budget "' . e($budget->name) . '" was deleted.');
2014-12-07 13:45:52 -06:00
$this->_repository->destroy($budget);
2014-12-12 00:13:40 -06:00
2014-11-12 15:36:02 -06:00
return Redirect::route('budgets.index');
}
/**
* @param Budget $budget
*
* @return $this
*/
public function edit(Budget $budget)
{
2014-12-12 00:13:40 -06:00
$subTitle = 'Edit budget "' . e($budget->name) . '"';
2014-11-12 15:36:02 -06:00
2014-12-07 13:45:52 -06:00
return View::make('budgets.edit', compact('budget', 'subTitle'));
2014-11-12 15:36:02 -06:00
}
/**
2014-12-12 00:13:40 -06:00
* The index of the budget controller contains all budgets and the current relevant limit repetition.
*
* @return $this
*/
2014-11-04 13:37:00 -06:00
public function index()
{
2014-12-12 00:13:40 -06:00
$budgets = $this->_repository->get();
// loop the budgets:
$budgets->each(
function (Budget $budget) {
$budget->spent = $this->_repository->spentInMonth($budget, \Session::get('start', Carbon::now()->startOfMonth()));
$budget->currentRep = $this->_repository->getRepetitionByDate($budget, \Session::get('start', Carbon::now()->startOfMonth()));
2014-11-04 13:37:00 -06:00
}
2014-12-12 00:13:40 -06:00
);
$spent = $budgets->sum('spent');
$amount = $this->_preferences->get('budgetIncomeTotal' . \Session::get('start', Carbon::now()->startOfMonth())->format('FY'), 1000)->data;
$overspent = $spent > $amount;
$spentPCT = $overspent ? ceil($amount / $spent * 100) : ceil($spent / $amount * 100);
$budgetMax = $this->_preferences->get('budgetMaximum', 1000);
$budgetMaximum = $budgetMax->data;
return View::make('budgets.index', compact('budgetMaximum', 'budgets', 'spent', 'spentPCT', 'overspent', 'amount'));
}
/**
* @return \Illuminate\View\View
*/
public function noBudget()
{
$start = \Session::get('start', Carbon::now()->startOfMonth());
$end = \Session::get('end', Carbon::now()->startOfMonth());
$list = $this->_repository->journalsNoBudget($start, $end);
$subTitle = 'Transactions without a budget in ' . $start->format('F Y');
return View::make('budgets.noBudget', compact('list', 'subTitle'));
}
/**
2014-11-12 15:36:02 -06:00
* @return \Illuminate\Http\RedirectResponse
*/
2014-11-12 15:36:02 -06:00
public function postUpdateIncome()
{
2014-12-13 14:59:02 -06:00
$this->_preferences->set('budgetIncomeTotal' . Session::get('start', Carbon::now()->startOfMonth())->format('FY'), intval(Input::get('amount')));
2014-11-12 15:36:02 -06:00
return Redirect::route('budgets.index');
}
/**
2015-01-17 03:05:43 -06:00
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
* @param Budget $budget
* @param LimitRepetition $repetition
*
* @return \Illuminate\View\View
*/
public function show(Budget $budget, LimitRepetition $repetition = null)
{
2014-12-13 15:54:52 -06:00
if (!is_null($repetition) && $repetition->budgetLimit->budget->id != $budget->id) {
2014-12-20 09:06:25 -06:00
return View::make('error')->with('message', 'Invalid selection.');
}
2014-12-12 00:42:38 -06:00
$hideBudget = true; // used in transaction list.
$journals = $this->_repository->getJournals($budget, $repetition);
2014-12-13 15:54:52 -06:00
$limits = $repetition ? [$repetition->budgetLimit] : $budget->budgetLimits()->orderBy('startdate', 'DESC')->get();
2014-12-12 00:42:38 -06:00
$subTitle = $repetition ? e($budget->name) . ' in ' . $repetition->startdate->format('F Y') : e($budget->name);
2014-11-08 12:11:51 -06:00
2014-12-12 00:42:38 -06:00
return View::make('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle', 'hideBudget'));
}
2014-11-06 00:38:15 -06:00
/**
2014-12-12 00:13:40 -06:00
* @return $this|\Illuminate\Http\RedirectResponse
2014-11-06 00:38:15 -06:00
*/
public function store()
{
2014-12-20 09:06:25 -06:00
$data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
2014-12-12 00:13:40 -06:00
// always validate:
$messages = $this->_repository->validate($data);
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not validate budget: ' . $messages['errors']->first());
2015-01-17 03:05:43 -06:00
return Redirect::route('budgets.create')->withInput();
2014-12-12 00:13:40 -06:00
}
// return to create screen:
2015-01-17 03:05:43 -06:00
if ($data['post_submit_action'] == 'validate_only') {
2014-12-12 00:13:40 -06:00
return Redirect::route('budgets.create')->withInput();
}
2015-01-01 22:52:38 -06:00
// store
2014-12-12 00:13:40 -06:00
$this->_repository->store($data);
Session::flash('success', 'Budget "' . e($data['name']) . '" stored.');
if ($data['post_submit_action'] == 'store') {
return Redirect::route('budgets.index');
2014-11-06 00:38:15 -06:00
}
2014-12-12 00:42:38 -06:00
2014-12-12 00:13:40 -06:00
// create another.
2014-12-20 09:06:25 -06:00
return Redirect::route('budgets.create')->withInput();
2014-11-06 00:38:15 -06:00
}
2014-11-08 12:11:51 -06:00
/**
* @param Budget $budget
*
2014-12-12 00:13:40 -06:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function update(Budget $budget)
{
2014-12-20 09:06:25 -06:00
$data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
2014-12-12 00:13:40 -06:00
// always validate:
$messages = $this->_repository->validate($data);
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not update budget: ' . $messages['errors']->first());
2015-01-17 03:05:43 -06:00
return Redirect::route('budgets.edit', $budget->id)->withInput();
2014-12-12 00:13:40 -06:00
}
// return to update screen:
2015-01-17 03:05:43 -06:00
if ($data['post_submit_action'] == 'validate_only') {
2014-12-12 00:13:40 -06:00
return Redirect::route('budgets.edit', $budget->id)->withInput();
}
// update
$this->_repository->update($budget, $data);
Session::flash('success', 'Budget "' . e($data['name']) . '" updated.');
// go back to list
if ($data['post_submit_action'] == 'update') {
return Redirect::route('budgets.index');
}
2014-12-12 00:13:40 -06:00
2014-12-20 09:06:25 -06:00
return Redirect::route('budgets.edit', $budget->id)->withInput(['post_submit_action' => 'return_to_edit']);
}
2014-11-12 15:36:02 -06:00
/**
* @return $this
*/
public function updateIncome()
{
2014-12-13 14:59:02 -06:00
$budgetAmount = $this->_preferences->get('budgetIncomeTotal' . Session::get('start', Carbon::now()->startOfMonth())->format('FY'), 1000);
2014-12-12 00:42:38 -06:00
2014-12-12 00:13:40 -06:00
return View::make('budgets.income')->with('amount', $budgetAmount);
2014-11-12 15:36:02 -06:00
}
2014-12-12 00:13:40 -06:00
}