firefly-iii/app/controllers/BudgetController.php

181 lines
4.7 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
*/
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;
View::share('menu', 'budgets');
}
/**
* @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');
2014-07-27 13:29:58 -05:00
return View::make('budgets.create')->with('periods', $periods);
}
/**
* @return $this|\Illuminate\View\View
*/
public function indexByBudget()
{
2014-07-27 13:29:58 -05:00
$budgets = $this->_repository->get();
$today = new Carbon;
return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', $today);
}
/**
* @return $this|\Illuminate\View\View
2014-07-27 13:29:58 -05:00
* @throws Firefly\Exception\FireflyException
*/
2014-07-27 13:29:58 -05:00
public function indexByDate()
{
2014-07-27 13:29:58 -05:00
// get a list of dates by getting all repetitions:
$set = $this->_repository->get();
$budgets = $this->_budgets->organizeByDate($set);
2014-07-27 13:29:58 -05:00
return View::make('budgets.indexByDate')->with('budgets', $budgets);
}
public function edit(Budget $budget)
{
return View::make('budgets.edit')->with('budget', $budget);
}
public function update()
{
$budget = $this->_repository->update(Input::all());
Session::flash('success', 'Budget "' . $budget->name . '" updated.');
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.index.budget');
}
return Redirect::route('budgets.index');
}
public function delete(Budget $budget)
{
return View::make('budgets.delete')->with('budget', $budget);
}
public function destroy()
{
$result = $this->_repository->destroy(Input::get('id'));
if ($result === true) {
Session::flash('success', 'The budget was deleted.');
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.index.budget');
}
} else {
Session::flash('error', 'Could not delete the budget. Check the logs to be sure.');
}
return Redirect::route('budgets.index');
}
/**
* @param Budget $budget
*
* @return int
*/
public function show(Budget $budget)
{
return $budget->id;
// /** @var \Budget $budget */
// $budget = $this->_budgets->find($budgetId);
//
// $list = $budget->transactionjournals()->get();
// $return = [];
// /** @var \TransactionJournal $entry */
// foreach ($list as $entry) {
// $month = $entry->date->format('F Y');
// $return[$month] = isset($return[$month]) ? $return[$month] : [];
// $return[$month][] = $entry;
//
// }
// $str = '';
//
// foreach ($return as $month => $set) {
// $str .= '<h1>' . $month . '</h1>';
// /** @var \TransactionJournal $tj */
// $sum = 0;
// foreach ($set as $tj) {
// $str .= '#' . $tj->id . ' ' . $tj->description . ': ';
//
// foreach ($tj->transactions as $index => $t) {
// $str .= $t->amount . ', ';
// if ($index == 0) {
// $sum += $t->amount;
//
// }
// }
// $str .= '<br>';
//
// }
// $str .= 'sum: ' . $sum . '<br><br>';
// }
//
// return $str;
}
2014-07-27 13:29:58 -05:00
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function store()
{
$budget = $this->_repository->store(Input::all());
if ($budget->id) {
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');
return Redirect::route('budgets.create')->withInput();
}
2014-07-27 13:29:58 -05:00
}
2014-07-23 01:16:04 -05:00
}