2014-07-20 11:24:27 -05:00
|
|
|
<?php
|
|
|
|
|
2014-11-12 00:31:48 -06:00
|
|
|
use FireflyIII\Exception\FireflyException;
|
2014-11-05 12:57:56 -06:00
|
|
|
use Illuminate\Support\MessageBag;
|
2014-11-04 13:37:00 -06:00
|
|
|
|
2014-07-27 15:48:13 -05:00
|
|
|
|
2014-11-08 12:11:51 -06:00
|
|
|
/**
|
|
|
|
* Class BudgetController
|
|
|
|
*/
|
2014-07-20 11:24:27 -05:00
|
|
|
class BudgetController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2014-11-04 13:37:00 -06:00
|
|
|
public function __construct()
|
2014-07-20 11:24:27 -05:00
|
|
|
{
|
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-07-20 11:24:27 -05:00
|
|
|
}
|
|
|
|
|
2014-11-06 00:38:15 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-11-04 13:37:00 -06:00
|
|
|
public function postUpdateIncome()
|
2014-07-20 11:24:27 -05:00
|
|
|
{
|
2014-11-12 03:54:53 -06:00
|
|
|
/** @var \FireflyIII\Shared\Preferences\PreferencesInterface $preferences */
|
|
|
|
$preferences = App::make('FireflyIII\Shared\Preferences\PreferencesInterface');
|
2014-11-04 13:37:00 -06:00
|
|
|
$date = Session::get('start');
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-11-04 13:37:00 -06:00
|
|
|
$value = intval(Input::get('amount'));
|
|
|
|
$preferences->set('budgetIncomeTotal' . $date->format('FY'), $value);
|
|
|
|
return Redirect::route('budgets.index');
|
2014-07-28 14:33:32 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
2014-11-04 13:37:00 -06:00
|
|
|
* Update the amount for a budget's limitrepetition and/or create it.
|
2014-08-10 08:01:46 -05:00
|
|
|
*
|
|
|
|
* @param Budget $budget
|
|
|
|
*/
|
2014-11-04 13:37:00 -06:00
|
|
|
public function amount(Budget $budget)
|
2014-07-28 14:33:32 -05:00
|
|
|
{
|
2014-11-04 13:37:00 -06:00
|
|
|
$amount = intval(Input::get('amount'));
|
2014-11-05 12:57:56 -06:00
|
|
|
$date = Session::get('start');
|
2014-11-04 13:37:00 -06:00
|
|
|
/** @var \Limit $limit */
|
|
|
|
$limit = $budget->limits()->where('startdate', $date->format('Y-m-d'))->first();
|
|
|
|
if (!$limit) {
|
|
|
|
// create one!
|
|
|
|
$limit = new Limit;
|
|
|
|
$limit->budget()->associate($budget);
|
|
|
|
$limit->startdate = $date;
|
|
|
|
$limit->amount = $amount;
|
|
|
|
$limit->repeat_freq = 'monthly';
|
|
|
|
$limit->repeats = 0;
|
|
|
|
$limit->save();
|
2014-11-12 15:21:48 -06:00
|
|
|
/*
|
|
|
|
* A newly stored limit also created a limit repetition.
|
|
|
|
*/
|
|
|
|
Event::fire('limits.store', [$limit]); // Still in use.
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-11-04 13:37:00 -06:00
|
|
|
} else {
|
2014-11-05 14:37:24 -06:00
|
|
|
if ($amount > 0) {
|
|
|
|
$limit->amount = $amount;
|
|
|
|
$limit->save();
|
2014-11-12 15:21:48 -06:00
|
|
|
/*
|
|
|
|
* An updated limit also updates the associated limit repetitions.
|
|
|
|
*/
|
|
|
|
Event::fire('limits.update', [$limit]); // Still in use.
|
2014-11-05 14:37:24 -06:00
|
|
|
} else {
|
|
|
|
$limit->delete();
|
|
|
|
}
|
2014-11-04 13:37:00 -06:00
|
|
|
}
|
|
|
|
// try to find the limit repetition for this limit:
|
|
|
|
$repetition = $limit->limitrepetitions()->first();
|
2014-11-05 12:57:56 -06:00
|
|
|
if ($repetition) {
|
|
|
|
return Response::json(['name' => $budget->name, 'repetition' => $repetition->id]);
|
2014-11-04 13:37:00 -06:00
|
|
|
} else {
|
2014-11-05 12:57:56 -06:00
|
|
|
return Response::json(['name' => $budget->name, 'repetition' => null]);
|
2014-11-04 13:37:00 -06:00
|
|
|
}
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-07-24 15:16:42 -05:00
|
|
|
}
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-11-12 03:54:53 -06:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-11-04 13:37:00 -06:00
|
|
|
public function index()
|
2014-07-24 15:16:42 -05:00
|
|
|
{
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-11-12 03:54:53 -06:00
|
|
|
/** @var \FireflyIII\Shared\Preferences\PreferencesInterface $preferences */
|
|
|
|
$preferences = App::make('FireflyIII\Shared\Preferences\PreferencesInterface');
|
2014-11-04 13:37:00 -06:00
|
|
|
$date = Session::get('start');
|
|
|
|
|
|
|
|
/** @var \FireflyIII\Database\Budget $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Budget');
|
|
|
|
$budgets = $repos->get();
|
|
|
|
|
|
|
|
// get the limits for the current month.
|
2014-11-10 14:55:22 -06:00
|
|
|
$date = \Session::get('start');
|
2014-11-09 01:02:47 -06:00
|
|
|
$spent = 0;
|
2014-11-04 13:37:00 -06:00
|
|
|
/** @var \Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
|
|
|
|
$budget->spent = $repos->spentInMonth($budget, $date);
|
2014-11-09 01:02:47 -06:00
|
|
|
$spent += $budget->spent;
|
2014-11-05 12:57:56 -06:00
|
|
|
$budget->pct = 0;
|
2014-11-04 13:37:00 -06:00
|
|
|
$budget->limit = 0;
|
|
|
|
|
|
|
|
/** @var \Limit $limit */
|
|
|
|
foreach ($budget->limits as $limit) {
|
|
|
|
/** @var \LimitRepetition $repetition */
|
|
|
|
foreach ($limit->limitrepetitions as $repetition) {
|
|
|
|
if ($repetition->startdate == $date) {
|
|
|
|
$budget->currentRep = $repetition;
|
2014-11-05 12:57:56 -06:00
|
|
|
$budget->limit = floatval($repetition->amount);
|
|
|
|
if ($budget->limit > $budget->spent) {
|
2014-11-04 13:37:00 -06:00
|
|
|
// not overspent:
|
|
|
|
$budget->pct = 30;
|
|
|
|
} else {
|
|
|
|
$budget->pct = 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-11-04 13:37:00 -06:00
|
|
|
$budgetAmount = $preferences->get('budgetIncomeTotal' . $date->format('FY'), 1000);
|
2014-11-10 14:55:22 -06:00
|
|
|
$amount = floatval($budgetAmount->data);
|
|
|
|
$overspent = $spent > $amount;
|
|
|
|
if ($overspent) {
|
2014-11-09 01:02:47 -06:00
|
|
|
// overspent on total amount
|
|
|
|
$spentPCT = ceil($amount / $spent * 100);
|
|
|
|
} else {
|
|
|
|
// not overspent on total amount.
|
|
|
|
$spentPCT = ceil($spent / $amount * 100);
|
|
|
|
}
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-11-10 14:55:22 -06:00
|
|
|
return View::make('budgets.index', compact('budgets', 'spent', 'spentPCT', 'overspent'))->with('budgetAmount', $budgetAmount);
|
2014-07-20 11:24:27 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 12:57:56 -06:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-11-04 13:37:00 -06:00
|
|
|
public function updateIncome()
|
2014-07-20 11:24:27 -05:00
|
|
|
{
|
2014-11-04 13:37:00 -06:00
|
|
|
$date = Session::get('start');
|
2014-11-12 03:54:53 -06:00
|
|
|
/** @var \FireflyIII\Shared\Preferences\PreferencesInterface $preferences */
|
|
|
|
$preferences = App::make('FireflyIII\Shared\Preferences\PreferencesInterface');
|
2014-11-04 13:37:00 -06:00
|
|
|
$budgetAmount = $preferences->get('budgetIncomeTotal' . $date->format('FY'), 1000);
|
|
|
|
return View::make('budgets.income')->with('amount', $budgetAmount)->with('date', $date);
|
2014-07-20 11:24:27 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 12:57:56 -06:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param LimitRepetition $repetition
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function show(Budget $budget, LimitRepetition $repetition = null)
|
|
|
|
{
|
|
|
|
if (!is_null($repetition) && $repetition->limit->budget->id != $budget->id) {
|
|
|
|
App::abort(500);
|
|
|
|
}
|
|
|
|
|
2014-11-08 12:11:51 -06:00
|
|
|
if (is_null($repetition)) {
|
|
|
|
// get all other repetitions:
|
|
|
|
$limits = $budget->limits()->orderBy('startdate', 'DESC')->get();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// get nothing? i dunno
|
|
|
|
$limits = [$repetition->limit];
|
|
|
|
}
|
|
|
|
|
|
|
|
return View::make('budgets.show', compact('limits', 'budget', 'repetition'));
|
2014-11-05 12:57:56 -06:00
|
|
|
}
|
|
|
|
|
2014-11-06 00:38:15 -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)
|
|
|
|
{
|
|
|
|
return View::make('budgets.delete')->with('budget', $budget)->with('subTitle', 'Delete budget "' . $budget->name . '"');
|
|
|
|
}
|
2014-07-27 13:29:58 -05:00
|
|
|
|
2014-11-06 00:38:15 -06:00
|
|
|
public function destroy(Budget $budget)
|
|
|
|
{
|
|
|
|
/** @var \FireflyIII\Database\Budget $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Budget');
|
|
|
|
// remove budget
|
|
|
|
$repos->destroy($budget);
|
|
|
|
Session::flash('success', 'The budget was deleted.');
|
|
|
|
return Redirect::route('budgets.index');
|
2014-08-02 08:23:29 -05:00
|
|
|
|
2014-11-06 00:38:15 -06:00
|
|
|
}
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-11-05 12:57:56 -06:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function edit(Budget $budget)
|
|
|
|
{
|
|
|
|
Session::flash('prefilled', ['name' => $budget->name]);
|
|
|
|
return View::make('budgets.edit')->with('budget', $budget)->with('subTitle', 'Edit budget "' . $budget->name . '"');
|
|
|
|
|
|
|
|
}
|
2014-11-04 13:37:00 -06:00
|
|
|
|
2014-11-06 00:38:15 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
/** @var \FireflyIII\Database\Budget $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Budget');
|
|
|
|
$data = Input::except('_token');
|
|
|
|
|
|
|
|
switch ($data['post_submit_action']) {
|
|
|
|
default:
|
|
|
|
throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"');
|
|
|
|
break;
|
|
|
|
case 'create_another':
|
|
|
|
case 'store':
|
|
|
|
$messages = $repos->validate($data);
|
|
|
|
/** @var MessageBag $messages ['errors'] */
|
|
|
|
if ($messages['errors']->count() > 0) {
|
|
|
|
Session::flash('warnings', $messages['warnings']);
|
|
|
|
Session::flash('successes', $messages['successes']);
|
|
|
|
Session::flash('error', 'Could not save budget: ' . $messages['errors']->first());
|
|
|
|
return Redirect::route('budgets.create')->withInput()->withErrors($messages['errors']);
|
|
|
|
}
|
|
|
|
// store!
|
|
|
|
$repos->store($data);
|
|
|
|
Session::flash('success', 'New budget stored!');
|
|
|
|
|
|
|
|
if ($data['post_submit_action'] == 'create_another') {
|
|
|
|
return Redirect::route('budgets.create');
|
|
|
|
} else {
|
|
|
|
return Redirect::route('budgets.index');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'validate_only':
|
|
|
|
$messageBags = $repos->validate($data);
|
|
|
|
Session::flash('warnings', $messageBags['warnings']);
|
|
|
|
Session::flash('successes', $messageBags['successes']);
|
|
|
|
Session::flash('errors', $messageBags['errors']);
|
|
|
|
return Redirect::route('budgets.create')->withInput();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 12:11:51 -06:00
|
|
|
|
2014-11-05 12:57:56 -06:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function update(Budget $budget)
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var \FireflyIII\Database\Budget $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Budget');
|
|
|
|
$data = Input::except('_token');
|
|
|
|
|
|
|
|
switch (Input::get('post_submit_action')) {
|
|
|
|
default:
|
|
|
|
throw new FireflyException('Cannot handle post_submit_action "' . e(Input::get('post_submit_action')) . '"');
|
|
|
|
break;
|
2014-11-10 12:03:03 -06:00
|
|
|
case 'return_to_edit':
|
2014-11-05 12:57:56 -06:00
|
|
|
case 'update':
|
|
|
|
$messages = $repos->validate($data);
|
|
|
|
/** @var MessageBag $messages ['errors'] */
|
|
|
|
if ($messages['errors']->count() > 0) {
|
|
|
|
Session::flash('warnings', $messages['warnings']);
|
|
|
|
Session::flash('successes', $messages['successes']);
|
2014-11-06 00:38:15 -06:00
|
|
|
Session::flash('error', 'Could not save budget: ' . $messages['errors']->first());
|
2014-11-05 12:57:56 -06:00
|
|
|
return Redirect::route('budgets.edit', $budget->id)->withInput()->withErrors($messages['errors']);
|
|
|
|
}
|
|
|
|
// store!
|
|
|
|
$repos->update($budget, $data);
|
2014-11-06 13:33:37 -06:00
|
|
|
Session::flash('success', 'Budget updated!');
|
2014-11-05 12:57:56 -06:00
|
|
|
|
2014-11-10 12:03:03 -06:00
|
|
|
if ($data['post_submit_action'] == 'return_to_edit') {
|
2014-11-05 12:57:56 -06:00
|
|
|
return Redirect::route('budgets.edit', $budget->id);
|
|
|
|
} else {
|
2014-11-06 13:33:37 -06:00
|
|
|
return Redirect::route('budgets.index');
|
2014-11-05 12:57:56 -06:00
|
|
|
}
|
|
|
|
case 'validate_only':
|
|
|
|
$messageBags = $repos->validate($data);
|
|
|
|
Session::flash('warnings', $messageBags['warnings']);
|
|
|
|
Session::flash('successes', $messageBags['successes']);
|
|
|
|
Session::flash('errors', $messageBags['errors']);
|
|
|
|
return Redirect::route('budgets.edit', $budget->id)->withInput();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 12:11:51 -06:00
|
|
|
}
|