firefly-iii/app/controllers/BudgetController.php

218 lines
6.3 KiB
PHP
Raw Normal View History

<?php
use Carbon\Carbon;
2014-07-27 13:29:58 -05:00
use Firefly\Helper\Controllers\BudgetInterface as BI;
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
/**
* Class BudgetController
*
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*
*/
class BudgetController extends BaseController
{
protected $_budgets;
2014-07-27 13:29:58 -05:00
protected $_repository;
/**
* @param BI $budgets
* @param BRI $repository
*/
2014-07-27 13:29:58 -05:00
public function __construct(BI $budgets, BRI $repository)
{
$this->_budgets = $budgets;
2014-07-27 13:29:58 -05:00
$this->_repository = $repository;
2014-09-15 10:46:01 -05:00
View::share('title','Budgets');
View::share('mainTitleIcon', 'fa-tasks');
}
/**
* @return $this|\Illuminate\View\View
*/
2014-07-27 13:29:58 -05:00
public function create()
{
2014-07-27 13:29:58 -05:00
$periods = \Config::get('firefly.periods_to_text');
return View::make('budgets.create')->with('periods', $periods)->with('subTitle', 'Create a new budget');
}
2014-08-10 08:01:46 -05:00
/**
* @param Budget $budget
*
* @return $this
*/
2014-07-28 14:33:32 -05:00
public function delete(Budget $budget)
{
return View::make('budgets.delete')->with('budget', $budget)
->with('subTitle', 'Delete budget "' . $budget->name . '"');
2014-07-28 14:33:32 -05:00
}
2014-08-10 08:01:46 -05:00
/**
* @param Budget $budget
*
* @return \Illuminate\Http\RedirectResponse
*/
2014-08-02 08:23:29 -05:00
public function destroy(Budget $budget)
2014-07-28 14:33:32 -05:00
{
// remove budget
2014-08-28 00:53:54 -05:00
Event::fire('budgets.destroy', [$budget]); // just before deletion.
$this->_repository->destroy($budget);
Session::flash('success', 'The budget was deleted.');
2014-07-28 14:33:32 -05:00
// redirect:
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
}
return Redirect::route('budgets.index.budget');
2014-07-28 14:33:32 -05:00
}
2014-08-10 08:01:46 -05:00
/**
* @param Budget $budget
*
* @return $this
*/
2014-07-28 14:33:32 -05:00
public function edit(Budget $budget)
{
return View::make('budgets.edit')->with('budget', $budget)
->with('subTitle', 'Edit budget "' . $budget->name . '"');
2014-07-28 14:33:32 -05:00
}
/**
* @return $this|\Illuminate\View\View
*/
public function indexByBudget()
{
2014-09-15 10:46:01 -05:00
View::share('subTitleIcon', 'fa-folder-open');
2014-07-27 13:29:58 -05:00
$budgets = $this->_repository->get();
return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', new Carbon)
2014-09-15 10:46:01 -05:00
->with('subTitle', 'Grouped by budget');
}
/**
* @return $this
*/
2014-07-27 13:29:58 -05:00
public function indexByDate()
{
2014-09-15 10:46:01 -05:00
View::share('subTitleIcon', 'fa-calendar');
2014-07-27 13:29:58 -05:00
// get a list of dates by getting all repetitions:
$set = $this->_repository->get();
2014-07-27 13:29:58 -05:00
$budgets = $this->_budgets->organizeByDate($set);
return View::make('budgets.indexByDate')->with('budgets', $budgets)
2014-09-15 10:46:01 -05:00
->with('subTitle', 'Grouped by date');
}
/**
* Three use cases for this view:
*
* - Show everything.
* - Show a specific repetition.
* - Show everything shows NO repetition.
*
* @param Budget $budget
* @param LimitRepetition $repetition
*
* @return int
*/
public function show(Budget $budget, \LimitRepetition $repetition = null)
{
$useSessionDates = Input::get('useSession') == 'true' ? true : false;
$view = null;
$title = null;
\Log::debug('Is envelope true? ' . (Input::get('noenvelope') == 'true'));
switch (true) {
case (!is_null($repetition)):
$data = $this->_budgets->organizeRepetition($repetition);
$view = 1;
$title = $budget->name . ', ' . $repetition->periodShow() . ', ' . mf(
$repetition->limit->amount,
false
);
break;
case (Input::get('noenvelope') == 'true'):
$data = $this->_budgets->outsideRepetitions($budget);
$view = 2;
$title = $budget->name . ', transactions outside an envelope';
break;
default:
$data = $this->_budgets->organizeRepetitions($budget, $useSessionDates);
$view = $useSessionDates ? 3 : 4;
$title = $useSessionDates ? $budget->name . ' in session period' : $budget->name;
break;
2014-07-28 14:33:32 -05:00
}
return View::make('budgets.show')
->with('budget', $budget)
->with('repetitions', $data)
->with('view', $view)
->with('highlight', Input::get('highlight'))
->with('useSessionDates', $useSessionDates)
2014-09-15 10:46:01 -05:00
->with('subTitle', 'Overview for '.$title);
}
2014-07-27 13:29:58 -05:00
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function store()
{
$budget = $this->_repository->store(Input::all());
2014-08-10 04:30:14 -05:00
if ($budget->validate()) {
2014-08-28 00:53:54 -05:00
Event::fire('budgets.store', [$budget]);
Session::flash('success', 'Budget created!');
2014-07-27 13:29:58 -05:00
if (Input::get('create') == '1') {
return Redirect::route('budgets.create', ['from' => Input::get('from')]);
}
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.index.budget');
}
} else {
Session::flash('error', 'Could not save the new budget');
2014-08-02 08:23:29 -05:00
return Redirect::route('budgets.create')->withInput()->withErrors($budget->errors());
}
2014-07-27 13:29:58 -05:00
}
2014-08-10 08:01:46 -05:00
/**
* @param Budget $budget
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
2014-08-02 08:23:29 -05:00
public function update(Budget $budget)
2014-07-28 14:33:32 -05:00
{
2014-08-02 08:23:29 -05:00
$budget = $this->_repository->update($budget, Input::all());
if ($budget->validate()) {
2014-08-28 00:53:54 -05:00
Event::fire('budgets.update', [$budget]);
2014-08-02 08:23:29 -05:00
Session::flash('success', 'Budget "' . $budget->name . '" updated.');
2014-07-28 14:33:32 -05:00
2014-08-02 08:23:29 -05:00
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.index.budget');
}
2014-07-28 14:33:32 -05:00
} else {
2014-08-02 08:23:29 -05:00
Session::flash('error', 'Could not update budget: ' . $budget->errors()->first());
return Redirect::route('budgets.edit', $budget->id)->withInput()->withErrors($budget->errors());
2014-07-28 14:33:32 -05:00
}
}
2014-07-23 01:16:04 -05:00
}