Files
firefly-iii/app/controllers/RepeatedExpenseController.php

125 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2014-11-24 10:12:34 +01:00
use Carbon\Carbon;
use FireflyIII\Database\PiggyBank\RepeatedExpense as Repository;
2014-11-22 23:31:06 +01:00
use FireflyIII\Exception\FireflyException;
2014-12-06 17:53:25 +01:00
/**
* Class RepeatedExpenseController
*/
class RepeatedExpenseController extends BaseController
{
/** @var Repository */
protected $_repository;
2014-12-06 17:53:25 +01:00
/**
* @param Repository $repository
2014-12-06 17:53:25 +01:00
*/
public function __construct(Repository $repository)
2014-11-22 23:31:06 +01:00
{
View::share('title', 'Repeated expenses');
View::share('mainTitleIcon', 'fa-rotate-left');
$this->_repository = $repository;
2014-11-22 23:31:06 +01:00
}
2014-12-06 17:53:25 +01:00
/**
* @return $this
*/
2014-11-22 23:31:06 +01:00
public function create()
{
2014-12-13 22:54:52 +01:00
/** @var \FireflyIII\Database\Account\Account $acct */
$acct = App::make('FireflyIII\Database\Account\Account');
2014-11-22 23:31:06 +01:00
$periods = Config::get('firefly.piggybank_periods');
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
return View::make('repeatedexpense.create', compact('accounts', 'periods'))->with('subTitle', 'Create new repeated expense')->with(
'subTitleIcon', 'fa-plus'
);
}
2014-11-22 23:31:06 +01:00
2014-12-06 17:53:25 +01:00
/**
* @return \Illuminate\View\View
*/
public function index()
{
$subTitle = 'Overview';
2014-11-22 23:31:06 +01:00
2014-12-13 22:54:52 +01:00
/** @var \FireflyIII\Database\PiggyBank\RepeatedExpense $repository */
$repository = App::make('FireflyIII\Database\PiggyBank\RepeatedExpense');
2014-11-22 23:31:06 +01:00
$expenses = $repository->get();
$expenses->each(
2014-11-24 10:12:34 +01:00
function (Piggybank $piggyBank) use ($repository) {
$piggyBank->currentRelevantRep();
2014-11-22 23:31:06 +01:00
}
);
return View::make('repeatedexpense.index', compact('expenses', 'subTitle'));
}
2014-12-06 17:53:25 +01:00
/**
* @param Piggybank $piggyBank
*
* @return \Illuminate\View\View
*/
2014-11-24 10:12:34 +01:00
public function show(Piggybank $piggyBank)
{
$subTitle = $piggyBank->name;
$today = Carbon::now();
2014-12-13 22:54:52 +01:00
/** @var \FireflyIII\Database\PiggyBank\RepeatedExpense $repository */
$repository = App::make('FireflyIII\Database\PiggyBank\RepeatedExpense');
2014-11-24 17:01:37 +01:00
$repetitions = $piggyBank->piggybankrepetitions()->get();
$repetitions->each(
function (PiggybankRepetition $repetition) use ($repository) {
2014-12-14 20:40:02 +01:00
$repetition->bars = $repository->calculateParts($repetition);
2014-11-24 17:01:37 +01:00
}
);
2014-12-06 17:34:39 +01:00
return View::make('repeatedexpense.show', compact('repetitions', 'piggyBank', 'today', 'subTitle'));
2014-11-24 10:12:34 +01:00
}
2014-11-22 23:31:06 +01:00
/**
2014-12-06 17:53:25 +01:00
* @return $this
* @throws FireflyException
2014-11-22 23:31:06 +01:00
*/
public function store()
{
$data = Input::except('_token');
2014-11-22 23:31:06 +01:00
$data['repeats'] = 1;
// 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 repeated expense: ' . $messages['errors']->first());
}
// return to create screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
return Redirect::route('repeated.create')->withInput();
2014-11-22 23:31:06 +01:00
}
// store:
$this->_repository->store($data);
Session::flash('success', 'Budget "' . e($data['name']) . '" stored.');
if ($data['post_submit_action'] == 'store') {
return Redirect::route('repeated.index');
}
// create another.
if ($data['post_submit_action'] == 'create_another') {
return Redirect::route('repeated.create')->withInput();
}
return Redirect::route('repeated.index');
}
}