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

401 lines
14 KiB
PHP
Raw Normal View History

2015-02-22 09:46:21 +01:00
<?php namespace FireflyIII\Http\Controllers;
2015-05-24 22:54:59 +02:00
use Amount;
2015-02-22 09:46:21 +01:00
use Auth;
use Carbon\Carbon;
2016-04-25 13:20:42 +02:00
use Config;
2016-02-06 05:01:34 +01:00
use FireflyIII\Exceptions\FireflyException;
2015-02-22 15:40:13 +01:00
use FireflyIII\Http\Requests\BudgetFormRequest;
2015-02-22 09:46:21 +01:00
use FireflyIII\Models\Budget;
2015-02-22 15:40:13 +01:00
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
2015-02-22 09:46:21 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2016-05-05 22:03:35 +02:00
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2015-02-22 09:46:21 +01:00
use Input;
2015-09-27 17:44:49 +02:00
use Navigation;
2015-02-22 09:46:21 +01:00
use Preferences;
2015-02-22 15:40:13 +01:00
use Response;
2015-02-22 09:46:21 +01:00
use Session;
2015-04-01 09:40:19 +02:00
use URL;
2015-04-05 10:36:28 +02:00
use View;
2015-02-22 09:46:21 +01:00
/**
* Class BudgetController
*
* @package FireflyIII\Http\Controllers
*/
class BudgetController extends Controller
{
2015-04-03 19:11:55 +02:00
/**
2016-02-04 07:27:03 +01:00
*
2015-04-03 19:11:55 +02:00
*/
2015-02-22 09:46:21 +01:00
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2015-05-14 13:17:53 +02:00
View::share('title', trans('firefly.budgets'));
2015-02-22 09:46:21 +01:00
View::share('mainTitleIcon', 'fa-tasks');
2015-04-28 15:26:30 +02:00
View::share('hideBudgets', true);
2015-02-22 09:46:21 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
2015-02-22 09:46:21 +01:00
*
2015-05-03 12:54:39 +02:00
* @return \Symfony\Component\HttpFoundation\Response
2015-02-22 09:46:21 +01:00
*/
2015-05-03 12:54:39 +02:00
public function amount(BudgetRepositoryInterface $repository, Budget $budget)
2015-02-22 09:46:21 +01:00
{
2016-02-05 15:41:40 +01:00
$amount = intval(Input::get('amount'));
2016-04-28 10:59:36 +02:00
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
$viewRange = Preferences::get('viewRange', '1M')->data;
// is custom view range?
if (session('is_custom_range') === true) {
$viewRange = 'custom';
}
$limitRepetition = $repository->updateLimitAmount($budget, $start, $end, $viewRange, $amount);
2015-05-24 22:54:59 +02:00
if ($amount == 0) {
$limitRepetition = null;
}
Preferences::mark();
2015-02-22 09:46:21 +01:00
return Response::json(['name' => $budget->name, 'repetition' => $limitRepetition ? $limitRepetition->id : 0]);
}
2015-02-22 15:40:13 +01:00
/**
* @return \Illuminate\View\View
2015-02-22 15:40:13 +01:00
*/
public function create()
{
2015-04-01 09:40:19 +02:00
// put previous url in session if not redirect from store (not "create another").
2016-02-04 07:27:03 +01:00
if (session('budgets.create.fromStore') !== true) {
2015-04-01 09:40:19 +02:00
Session::put('budgets.create.url', URL::previous());
}
Session::forget('budgets.create.fromStore');
2015-05-25 08:12:31 +02:00
Session::flash('gaEventCategory', 'budgets');
Session::flash('gaEventAction', 'create');
$subTitle = (string)trans('firefly.create_new_budget');
2015-04-01 09:40:19 +02:00
2015-05-05 10:23:01 +02:00
return view('budgets.create', compact('subTitle'));
2015-02-22 15:40:13 +01:00
}
/**
* @param Budget $budget
*
* @return \Illuminate\View\View
*/
public function delete(Budget $budget)
{
2015-05-25 22:16:00 +02:00
$subTitle = trans('firefly.delete_budget', ['name' => $budget->name]);
2015-02-22 15:40:13 +01:00
2015-04-01 09:40:19 +02:00
// put previous url in session
Session::put('budgets.delete.url', URL::previous());
2015-05-25 08:12:31 +02:00
Session::flash('gaEventCategory', 'budgets');
Session::flash('gaEventAction', 'delete');
2015-04-01 09:40:19 +02:00
2015-02-22 16:19:32 +01:00
return view('budgets.delete', compact('budget', 'subTitle'));
2015-02-22 15:40:13 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @param Budget $budget
* @param BudgetRepositoryInterface $repository
2015-02-22 15:40:13 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Budget $budget, BudgetRepositoryInterface $repository)
{
$name = $budget->name;
$repository->destroy($budget);
2015-04-01 09:40:19 +02:00
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.deleted_budget', ['name' => e($name)])));
Preferences::mark();
2015-02-22 15:40:13 +01:00
2015-05-25 08:12:31 +02:00
2016-02-04 07:27:03 +01:00
return redirect(session('budgets.delete.url'));
2015-02-22 15:40:13 +01:00
}
/**
* @param Budget $budget
*
* @return \Illuminate\View\View
2015-02-22 15:40:13 +01:00
*/
public function edit(Budget $budget)
{
2015-06-13 10:02:36 +02:00
$subTitle = trans('firefly.edit_budget', ['name' => $budget->name]);
2015-02-22 15:40:13 +01:00
2015-04-01 09:40:19 +02:00
// put previous url in session if not redirect from store (not "return_to_edit").
2016-02-04 07:27:03 +01:00
if (session('budgets.edit.fromUpdate') !== true) {
2015-04-01 09:40:19 +02:00
Session::put('budgets.edit.url', URL::previous());
}
Session::forget('budgets.edit.fromUpdate');
2015-05-25 08:12:31 +02:00
Session::flash('gaEventCategory', 'budgets');
Session::flash('gaEventAction', 'edit');
2015-04-01 09:40:19 +02:00
2015-02-22 16:19:32 +01:00
return view('budgets.edit', compact('budget', 'subTitle'));
2015-02-22 15:40:13 +01:00
}
2015-02-22 09:46:21 +01:00
/**
* @param BudgetRepositoryInterface $repository
*
* @param ARI $accountRepository
2015-05-03 12:54:39 +02:00
*
* @return \Illuminate\View\View
2015-02-22 09:46:21 +01:00
*/
public function index(BudgetRepositoryInterface $repository, ARI $accountRepository)
2015-02-22 09:46:21 +01:00
{
2016-04-25 13:20:42 +02:00
$budgets = $repository->getActiveBudgets();
$inactive = $repository->getInactiveBudgets();
$spent = '0';
$budgeted = '0';
$range = Preferences::get('viewRange', '1M')->data;
$repeatFreq = Config::get('firefly.range_to_repeat_freq.' . $range);
2016-04-28 16:30:21 +02:00
if (session('is_custom_range') === true) {
$repeatFreq = 'custom';
}
2016-04-25 13:20:42 +02:00
/** @var Carbon $start */
$start = session('start', new Carbon);
/** @var Carbon $end */
$end = session('end', new Carbon);
2015-09-27 17:44:49 +02:00
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$budgetIncomeTotal = Preferences::get($key, 1000)->data;
$period = Navigation::periodShow($start, $range);
2016-04-25 13:20:42 +02:00
$periodStart = $start->formatLocalized($this->monthAndDayFormat);
$periodEnd = $end->formatLocalized($this->monthAndDayFormat);
2015-12-15 12:46:40 +01:00
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
2015-04-03 19:39:36 +02:00
/**
* Do some cleanup:
*/
2016-05-05 22:03:35 +02:00
// $repository->cleanupBudgets();
2015-04-03 19:39:36 +02:00
2015-02-22 09:46:21 +01:00
// loop the budgets:
2015-05-24 22:54:59 +02:00
/** @var Budget $budget */
foreach ($budgets as $budget) {
2016-05-05 22:03:35 +02:00
$budget->spent = '0';//$repository->balanceInPeriod($budget, $start, $end, $accounts); // TODO BUDGET spentInPeriod
$budget->currentRep = new LimitRepetition(
); // $repository->getCurrentRepetition($budget, $repeatFreq, $start, $end); // TODO BUDGET getBudgetLimitRepetitions
$budget->otherRepetitions = new Collection(
);//$repository->getValidRepetitions($budget, $start, $end, $budget->currentRep); // TODO BUDGET getBudgetLimitRepetitions
2016-04-05 22:00:03 +02:00
if (!is_null($budget->currentRep->id)) {
2015-05-24 22:54:59 +02:00
$budgeted = bcadd($budgeted, $budget->currentRep->amount);
2015-02-22 09:46:21 +01:00
}
2015-05-24 22:54:59 +02:00
$spent = bcadd($spent, $budget->spent);
2015-02-22 09:46:21 +01:00
2015-05-24 22:54:59 +02:00
}
2015-09-27 17:44:49 +02:00
$budgetMaximum = Preferences::get('budgetMaximum', 1000)->data;
$defaultCurrency = Amount::getDefaultCurrency();
2015-02-22 09:46:21 +01:00
2015-05-24 22:54:59 +02:00
return view(
2016-04-25 13:20:42 +02:00
'budgets.index', compact(
'budgetMaximum', 'periodStart', 'periodEnd',
'period', 'range', 'budgetIncomeTotal',
'defaultCurrency', 'inactive', 'budgets',
2016-04-28 10:59:36 +02:00
'spent', 'budgeted'
2016-04-25 13:20:42 +02:00
)
2015-05-24 22:54:59 +02:00
);
2015-02-22 09:46:21 +01:00
}
2015-02-22 15:40:13 +01:00
/**
2015-05-03 12:54:39 +02:00
* @param BudgetRepositoryInterface $repository
*
* @return \Illuminate\View\View
2015-02-22 15:40:13 +01:00
*/
2015-04-05 10:36:28 +02:00
public function noBudget(BudgetRepositoryInterface $repository)
2015-02-22 15:40:13 +01:00
{
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
2016-04-21 09:00:32 +02:00
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
$pageSize = Preferences::get('transactionPageSize', 50)->data;
2016-05-05 22:03:35 +02:00
$list = new LengthAwarePaginator([], 0, $pageSize); // $repository->getWithoutBudget($start, $end, $page, $pageSize); // TODO BUDGET journalsInPeriodWithoutBudget
2015-05-25 21:17:36 +02:00
$subTitle = trans(
'firefly.without_budget_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
2016-04-21 09:01:34 +02:00
$list->setPath('/budgets/list/noBudget');
2015-02-22 15:40:13 +01:00
2015-02-22 16:19:32 +01:00
return view('budgets.noBudget', compact('list', 'subTitle'));
2015-02-22 15:40:13 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @return \Illuminate\Http\RedirectResponse
2015-02-22 15:40:13 +01:00
*/
public function postUpdateIncome()
{
2015-09-27 17:44:49 +02:00
$range = Preferences::get('viewRange', '1M')->data;
2016-02-05 15:41:40 +01:00
/** @var Carbon $date */
$date = session('start', new Carbon);
$start = Navigation::startOfPeriod($date, $range);
2015-09-27 17:44:49 +02:00
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
2015-02-22 15:40:13 +01:00
2015-09-27 17:44:49 +02:00
Preferences::set($key, intval(Input::get('amount')));
Preferences::mark();
2015-02-22 15:40:13 +01:00
2015-07-06 16:27:21 +02:00
return redirect(route('budgets.index'));
2015-02-22 15:40:13 +01:00
}
2015-04-05 10:36:28 +02:00
/**
2015-05-03 12:54:39 +02:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
2015-04-05 10:36:28 +02:00
*
2016-02-06 05:01:34 +01:00
* @return View
* @throws FireflyException
2015-04-05 10:36:28 +02:00
*/
2016-04-29 08:56:56 +02:00
public function show(BudgetRepositoryInterface $repository, Budget $budget)
2015-04-05 10:36:28 +02:00
{
2016-04-29 08:56:56 +02:00
$pageSize = Preferences::get('transactionPageSize', 50)->data;
2016-05-05 22:03:35 +02:00
$journals = new LengthAwarePaginator(
[], 0, $pageSize
); //$repository->getJournals($budget, new LimitRepetition, $pageSize); // TODO BUDGET journalsInPeriod
$start = new Carbon; //$repository->firstActivity($budget); // TODO BUDGET getOldestJournal
2016-04-29 08:56:56 +02:00
$end = new Carbon;
$set = $budget->limitrepetitions()->orderBy('startdate', 'DESC')->get();
$subTitle = e($budget->name);
$journals->setPath('/budgets/show/' . $budget->id);
2016-05-05 22:03:35 +02:00
$spentArray = []; //$repository->spentPerDay($budget, $start, $end, new Collection); // TODO BUDGET spentInPeriod
2016-04-29 08:56:56 +02:00
$limits = new Collection();
/** @var LimitRepetition $entry */
foreach ($set as $entry) {
$entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray);
$limits->push($entry);
2015-04-05 10:36:28 +02:00
}
2015-06-04 17:43:50 +02:00
2016-04-29 08:56:56 +02:00
return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle'));
}
/**
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @param LimitRepetition $repetition
*
* @return View
* @throws FireflyException
*/
public function showWithRepetition(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition)
{
if ($repetition->budgetLimit->budget->id != $budget->id) {
throw new FireflyException('This budget limit is not part of this budget.');
2015-06-04 17:43:50 +02:00
}
2016-04-29 08:56:56 +02:00
$pageSize = Preferences::get('transactionPageSize', 50)->data;
2016-05-05 22:03:35 +02:00
$journals = new LengthAwarePaginator([], 0, $pageSize); // $repository->getJournals($budget, $repetition, $pageSize); // TODO BUDGET journalsInPeriod
2016-04-29 08:56:56 +02:00
$start = $repetition->startdate;
$end = $repetition->enddate;
$set = new Collection([$repetition]);
$subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]);
$journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id);
2016-05-05 22:03:35 +02:00
$spentArray = []; //$repository->spentPerDay($budget, $start, $end, new Collection); // TODO BUDGET spentInPeriod
$limits = new Collection();
/** @var LimitRepetition $entry */
foreach ($set as $entry) {
$entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray);
$limits->push($entry);
}
2015-04-05 10:36:28 +02:00
2015-04-28 15:26:30 +02:00
return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle'));
2016-04-29 08:56:56 +02:00
2015-04-05 10:36:28 +02:00
}
/**
* @param BudgetFormRequest $request
* @param BudgetRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-02-22 15:40:13 +01:00
public function store(BudgetFormRequest $request, BudgetRepositoryInterface $repository)
{
$budgetData = [
'name' => $request->input('name'),
'user' => Auth::user()->id,
];
2015-06-06 23:09:12 +02:00
$budget = $repository->store($budgetData);
2015-02-22 15:40:13 +01:00
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.stored_new_budget', ['name' => e($budget->name)])));
Preferences::mark();
2015-02-22 15:40:13 +01:00
2015-03-28 18:22:36 +01:00
if (intval(Input::get('create_another')) === 1) {
2015-04-01 09:40:19 +02:00
// set value so create routine will not overwrite URL:
Session::put('budgets.create.fromStore', true);
2015-04-05 10:36:28 +02:00
2015-07-06 16:27:21 +02:00
return redirect(route('budgets.create'))->withInput();
2015-03-28 18:22:36 +01:00
}
2015-04-01 09:40:19 +02:00
// redirect to previous URL.
2016-02-04 07:27:03 +01:00
return redirect(session('budgets.create.url'));
2015-02-22 15:40:13 +01:00
}
/**
* @param BudgetFormRequest $request
* @param BudgetRepositoryInterface $repository
2015-05-03 12:54:39 +02:00
* @param Budget $budget
2015-02-22 15:40:13 +01:00
*
* @return \Illuminate\Http\RedirectResponse
2015-02-22 15:40:13 +01:00
*/
2015-05-03 12:54:39 +02:00
public function update(BudgetFormRequest $request, BudgetRepositoryInterface $repository, Budget $budget)
2015-02-22 15:40:13 +01:00
{
$budgetData = [
2015-03-29 21:27:51 +02:00
'name' => $request->input('name'),
2015-12-15 12:46:40 +01:00
'active' => intval($request->input('active')) == 1,
2015-02-22 15:40:13 +01:00
];
$repository->update($budget, $budgetData);
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.updated_budget', ['name' => e($budget->name)])));
Preferences::mark();
2015-02-22 15:40:13 +01:00
if (intval(Input::get('return_to_edit')) === 1) {
2015-04-01 09:40:19 +02:00
// set value so edit routine will not overwrite URL:
Session::put('budgets.edit.fromUpdate', true);
2015-04-05 10:36:28 +02:00
2015-07-06 16:27:21 +02:00
return redirect(route('budgets.edit', [$budget->id]))->withInput(['return_to_edit' => 1]);
}
2015-04-01 09:43:19 +02:00
// redirect to previous URL.
2016-02-04 07:27:03 +01:00
return redirect(session('budgets.edit.url'));
2015-02-22 15:40:13 +01:00
}
/**
* @return \Illuminate\View\View
2015-02-22 15:40:13 +01:00
*/
public function updateIncome()
{
2016-04-25 09:12:52 +02:00
$range = Preferences::get('viewRange', '1M')->data;
$format = strval(trans('config.month_and_day'));
2016-02-05 15:41:40 +01:00
/** @var Carbon $date */
2016-04-25 09:11:09 +02:00
$date = session('start', new Carbon);
$start = Navigation::startOfPeriod($date, $range);
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$amount = Preferences::get($key, 1000);
2016-04-25 09:12:52 +02:00
$displayStart = $start->formatLocalized($format);
$displayEnd = $end->formatLocalized($format);
2016-04-25 09:11:09 +02:00
return view('budgets.income', compact('amount', 'displayStart', 'displayEnd'));
2015-02-22 15:40:13 +01:00
}
2015-02-22 09:46:21 +01:00
}