2014-07-29 20:30:50 +02:00
|
|
|
<?php
|
2014-11-13 11:17:39 +01:00
|
|
|
use FireflyIII\Exception\FireflyException;
|
|
|
|
use Illuminate\Support\MessageBag;
|
2014-07-29 20:30:50 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* Class RecurringController
|
2014-09-09 20:00:04 +02:00
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
|
2014-08-10 15:01:46 +02:00
|
|
|
*/
|
2014-08-02 07:34:38 +02:00
|
|
|
class RecurringController extends BaseController
|
|
|
|
{
|
2014-11-12 10:54:53 +01:00
|
|
|
public function __construct()
|
2014-08-06 17:02:02 +02:00
|
|
|
{
|
2014-09-17 08:55:51 +02:00
|
|
|
|
2014-10-05 19:29:25 +02:00
|
|
|
View::share('title', 'Recurring transactions');
|
|
|
|
View::share('mainTitleIcon', 'fa-rotate-right');
|
2014-08-06 17:02:02 +02:00
|
|
|
}
|
2014-08-07 07:44:37 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-29 20:30:50 +02:00
|
|
|
public function create()
|
|
|
|
{
|
2014-08-07 07:44:37 +02:00
|
|
|
$periods = \Config::get('firefly.periods_to_text');
|
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
return View::make('recurring.create')->with('periods', $periods)->with('subTitle', 'Create new');
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-07 07:44:37 +02:00
|
|
|
public function delete(RecurringTransaction $recurringTransaction)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-11-12 22:36:02 +01:00
|
|
|
return View::make('recurring.delete')->with('recurringTransaction', $recurringTransaction)->with(
|
2014-11-13 16:13:32 +01:00
|
|
|
'subTitle', 'Delete "' . $recurringTransaction->name . '"'
|
|
|
|
);
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-07 07:44:37 +02:00
|
|
|
public function destroy(RecurringTransaction $recurringTransaction)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-09-28 08:47:51 +02:00
|
|
|
//Event::fire('recurring.destroy', [$recurringTransaction]);
|
2014-11-12 10:54:53 +01:00
|
|
|
|
2014-11-13 16:13:32 +01:00
|
|
|
/** @var \FireflyIII\Database\Recurring $repository */
|
|
|
|
$repository = App::make('FireflyIII\Database\Recurring');
|
2014-11-12 10:54:53 +01:00
|
|
|
|
|
|
|
$result = $repository->destroy($recurringTransaction);
|
2014-08-07 07:44:37 +02:00
|
|
|
if ($result === true) {
|
|
|
|
Session::flash('success', 'The recurring transaction was deleted.');
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not delete the recurring transaction. Check the logs to be sure.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::route('recurring.index');
|
|
|
|
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-09 06:12:49 +02:00
|
|
|
public function edit(RecurringTransaction $recurringTransaction)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-08-09 06:12:49 +02:00
|
|
|
$periods = \Config::get('firefly.periods_to_text');
|
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
return View::make('recurring.edit')->with('periods', $periods)->with('recurringTransaction', $recurringTransaction)->with(
|
2014-11-13 16:13:32 +01:00
|
|
|
'subTitle', 'Edit "' . $recurringTransaction->name . '"'
|
|
|
|
);
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-29 20:30:50 +02:00
|
|
|
public function index()
|
|
|
|
{
|
2014-09-28 08:47:51 +02:00
|
|
|
return View::make('recurring.index');
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 08:19:18 +02:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
2014-11-12 22:36:02 +01:00
|
|
|
*
|
2014-10-12 08:19:18 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function rescan(RecurringTransaction $recurringTransaction)
|
|
|
|
{
|
2014-10-13 17:54:20 +02:00
|
|
|
if (intval($recurringTransaction->active) == 0) {
|
|
|
|
Session::flash('warning', 'Inactive recurring transactions cannot be scanned.');
|
2014-11-12 22:36:02 +01:00
|
|
|
|
2014-10-13 17:54:20 +02:00
|
|
|
return Redirect::back();
|
|
|
|
}
|
2014-11-13 16:13:32 +01:00
|
|
|
|
|
|
|
/** @var \FireflyIII\Database\Recurring $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Recurring');
|
|
|
|
$repos->scanEverything($recurringTransaction);
|
|
|
|
|
2014-10-13 17:54:20 +02:00
|
|
|
Session::flash('success', 'Rescanned everything.');
|
2014-11-12 22:36:02 +01:00
|
|
|
|
2014-10-12 08:19:18 +02:00
|
|
|
return Redirect::back();
|
|
|
|
}
|
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function show(RecurringTransaction $recurringTransaction)
|
|
|
|
{
|
|
|
|
return View::make('recurring.show')->with('recurring', $recurringTransaction)->with('subTitle', $recurringTransaction->name);
|
|
|
|
}
|
|
|
|
|
2014-07-29 20:30:50 +02:00
|
|
|
public function store()
|
|
|
|
{
|
2014-11-13 16:13:32 +01:00
|
|
|
$data = Input::except('_token');
|
2014-11-13 11:17:39 +01:00
|
|
|
/** @var \FireflyIII\Database\Recurring $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Recurring');
|
|
|
|
|
|
|
|
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 recurring transaction: ' . $messages['errors']->first());
|
|
|
|
|
|
|
|
return Redirect::route('recurring.create')->withInput()->withErrors($messages['errors']);
|
|
|
|
}
|
|
|
|
// store!
|
|
|
|
$repos->store($data);
|
|
|
|
Session::flash('success', 'New recurring transaction stored!');
|
|
|
|
|
|
|
|
if ($data['post_submit_action'] == 'create_another') {
|
|
|
|
return Redirect::route('recurring.create')->withInput();
|
|
|
|
} else {
|
|
|
|
return Redirect::route('recurring.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('recurring.create')->withInput();
|
|
|
|
break;
|
|
|
|
}
|
2014-09-28 08:47:51 +02:00
|
|
|
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-09 06:12:49 +02:00
|
|
|
public function update(RecurringTransaction $recurringTransaction)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-11-13 16:13:32 +01:00
|
|
|
/** @var \FireflyIII\Database\Recurring $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Recurring');
|
|
|
|
$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;
|
|
|
|
case 'create_another':
|
|
|
|
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']);
|
|
|
|
Session::flash('error', 'Could not save recurring transaction: ' . $messages['errors']->first());
|
|
|
|
|
|
|
|
return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput()->withErrors($messages['errors']);
|
|
|
|
}
|
|
|
|
// store!
|
|
|
|
$repos->update($recurringTransaction, $data);
|
|
|
|
Session::flash('success', 'Recurring transaction updated!');
|
|
|
|
|
|
|
|
if ($data['post_submit_action'] == 'create_another') {
|
|
|
|
return Redirect::route('recurring.edit', $recurringTransaction->id);
|
|
|
|
} else {
|
|
|
|
return Redirect::route('recurring.index');
|
|
|
|
}
|
|
|
|
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('recurring.edit', $recurringTransaction->id)->withInput();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
2014-08-21 15:15:39 +02:00
|
|
|
}
|