2014-07-29 13:30:50 -05:00
|
|
|
<?php
|
|
|
|
|
2014-09-28 01:47:51 -05:00
|
|
|
use Firefly\Exception\FireflyException;
|
2014-08-06 10:02:02 -05:00
|
|
|
use Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface as RTR;
|
2014-10-06 12:32:09 -05:00
|
|
|
use Firefly\Helper\Controllers\RecurringInterface as RI;
|
2014-08-06 10:02:02 -05:00
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* Class RecurringController
|
2014-09-09 13:00:04 -05:00
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
|
2014-08-10 08:01:46 -05:00
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
class RecurringController extends BaseController
|
|
|
|
{
|
2014-08-06 10:02:02 -05:00
|
|
|
protected $_repository;
|
2014-10-06 12:32:09 -05:00
|
|
|
protected $_helper;
|
2014-08-07 00:44:37 -05:00
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param RTR $repository
|
2014-10-06 12:32:09 -05:00
|
|
|
* @param RI $helper
|
2014-08-10 08:01:46 -05:00
|
|
|
*/
|
2014-10-06 12:32:09 -05:00
|
|
|
public function __construct(RTR $repository, RI $helper)
|
2014-08-06 10:02:02 -05:00
|
|
|
{
|
|
|
|
$this->_repository = $repository;
|
2014-10-12 01:19:18 -05:00
|
|
|
$this->_helper = $helper;
|
2014-09-17 01:55:51 -05:00
|
|
|
|
2014-10-05 12:29:25 -05:00
|
|
|
View::share('title', 'Recurring transactions');
|
|
|
|
View::share('mainTitleIcon', 'fa-rotate-right');
|
2014-08-06 10:02:02 -05:00
|
|
|
}
|
2014-08-07 00:44:37 -05:00
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-29 13:30:50 -05:00
|
|
|
public function create()
|
|
|
|
{
|
2014-08-07 00:44:37 -05:00
|
|
|
$periods = \Config::get('firefly.periods_to_text');
|
|
|
|
|
2014-11-09 09:57:19 -06:00
|
|
|
return View::make('recurring.create')
|
|
|
|
->with('periods', $periods)
|
|
|
|
->with('subTitle', 'Create new');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-07 00:44:37 -05:00
|
|
|
public function delete(RecurringTransaction $recurringTransaction)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-11-09 09:57:19 -06:00
|
|
|
return View::make('recurring.delete')
|
|
|
|
->with('recurringTransaction', $recurringTransaction)
|
|
|
|
->with('subTitle', 'Delete "' . $recurringTransaction->name . '"');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-07 00:44:37 -05:00
|
|
|
public function destroy(RecurringTransaction $recurringTransaction)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-09-28 01:47:51 -05:00
|
|
|
//Event::fire('recurring.destroy', [$recurringTransaction]);
|
2014-08-07 00:44:37 -05:00
|
|
|
$result = $this->_repository->destroy($recurringTransaction);
|
|
|
|
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 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-08 23:12:49 -05:00
|
|
|
public function edit(RecurringTransaction $recurringTransaction)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-08 23:12:49 -05:00
|
|
|
$periods = \Config::get('firefly.periods_to_text');
|
|
|
|
|
2014-11-09 09:57:19 -06:00
|
|
|
return View::make('recurring.edit')
|
|
|
|
->with('periods', $periods)
|
|
|
|
->with('recurringTransaction', $recurringTransaction)
|
|
|
|
->with('subTitle', 'Edit "' . $recurringTransaction->name . '"');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-29 13:30:50 -05:00
|
|
|
public function index()
|
|
|
|
{
|
2014-09-28 01:47:51 -05:00
|
|
|
return View::make('recurring.index');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2014-08-21 08:15:39 -05:00
|
|
|
public function show(RecurringTransaction $recurringTransaction)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-11-09 09:57:19 -06:00
|
|
|
return View::make('recurring.show')
|
|
|
|
->with('recurring', $recurringTransaction)
|
|
|
|
->with('subTitle', $recurringTransaction->name);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-10-12 01:19:18 -05:00
|
|
|
/**
|
|
|
|
* @param RecurringTransaction $recurringTransaction
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function rescan(RecurringTransaction $recurringTransaction)
|
|
|
|
{
|
2014-10-13 10:54:20 -05:00
|
|
|
if (intval($recurringTransaction->active) == 0) {
|
|
|
|
Session::flash('warning', 'Inactive recurring transactions cannot be scanned.');
|
|
|
|
return Redirect::back();
|
|
|
|
}
|
2014-10-12 01:19:18 -05:00
|
|
|
// do something!
|
|
|
|
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $repo */
|
|
|
|
$repo = App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
|
|
|
|
$set = $repo->get();
|
|
|
|
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
foreach ($set as $journal) {
|
|
|
|
Event::fire('recurring.rescan', [$recurringTransaction, $journal]);
|
|
|
|
}
|
2014-10-13 10:54:20 -05:00
|
|
|
Session::flash('success', 'Rescanned everything.');
|
2014-10-12 01:19:18 -05:00
|
|
|
return Redirect::back();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-29 13:30:50 -05:00
|
|
|
public function store()
|
|
|
|
{
|
2014-10-06 12:32:09 -05:00
|
|
|
$data = Input::except(['_token', 'post_submit_action']);
|
2014-09-28 01:47:51 -05:00
|
|
|
switch (Input::get('post_submit_action')) {
|
|
|
|
default:
|
|
|
|
throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
|
|
|
|
break;
|
2014-10-05 12:29:25 -05:00
|
|
|
case 'store':
|
|
|
|
case 'create_another':
|
|
|
|
/*
|
|
|
|
* Try to store:
|
|
|
|
*/
|
2014-10-06 12:32:09 -05:00
|
|
|
$messageBag = $this->_repository->store($data);
|
2014-10-05 12:29:25 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Failure!
|
|
|
|
*/
|
|
|
|
if ($messageBag->count() > 0) {
|
|
|
|
Session::flash('error', 'Could not save recurring transaction: ' . $messageBag->first());
|
|
|
|
return Redirect::route('recurring.create')->withInput()->withErrors($messageBag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Success!
|
|
|
|
*/
|
|
|
|
Session::flash('success', 'Recurring transaction "' . e(Input::get('name')) . '" saved!');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Redirect to original location or back to the form.
|
|
|
|
*/
|
|
|
|
if (Input::get('post_submit_action') == 'create_another') {
|
|
|
|
return Redirect::route('recurring.create')->withInput();
|
|
|
|
} else {
|
|
|
|
return Redirect::route('recurring.index');
|
|
|
|
}
|
|
|
|
break;
|
2014-10-06 12:32:09 -05:00
|
|
|
case 'validate_only':
|
|
|
|
$messageBags = $this->_helper->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 01:47:51 -05:00
|
|
|
}
|
|
|
|
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-08 23:12:49 -05:00
|
|
|
public function update(RecurringTransaction $recurringTransaction)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-10-06 12:32:09 -05:00
|
|
|
$data = Input::except(['_token', 'post_submit_action']);
|
|
|
|
switch (Input::get('post_submit_action')) {
|
|
|
|
case 'update':
|
|
|
|
case 'return_to_edit':
|
|
|
|
$messageBag = $this->_repository->update($recurringTransaction, $data);
|
|
|
|
if ($messageBag->count() == 0) {
|
|
|
|
// has been saved, return to index:
|
|
|
|
Session::flash('success', 'Recurring transaction updated!');
|
|
|
|
|
|
|
|
if (Input::get('post_submit_action') == 'return_to_edit') {
|
|
|
|
return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput();
|
|
|
|
} else {
|
|
|
|
return Redirect::route('recurring.index');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not update recurring transaction: ' . $messageBag->first());
|
2014-08-23 15:32:12 -05:00
|
|
|
|
2014-10-06 12:32:09 -05:00
|
|
|
return Redirect::route('transactions.edit', $recurringTransaction->id)->withInput()
|
2014-10-12 01:19:18 -05:00
|
|
|
->withErrors($messageBag);
|
2014-10-06 12:32:09 -05:00
|
|
|
}
|
2014-08-23 15:32:12 -05:00
|
|
|
|
2014-10-06 12:32:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'validate_only':
|
2014-10-12 01:19:18 -05:00
|
|
|
$data = Input::all();
|
|
|
|
$data['id'] = $recurringTransaction->id;
|
2014-10-06 12:32:09 -05:00
|
|
|
$messageBags = $this->_helper->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;
|
|
|
|
// update
|
|
|
|
default:
|
|
|
|
throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
|
|
|
|
break;
|
2014-08-21 08:15:39 -05:00
|
|
|
}
|
2014-10-06 12:32:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
// /** @var \RecurringTransaction $recurringTransaction */
|
|
|
|
// $recurringTransaction = $this->_repository->update($recurringTransaction, Input::all());
|
|
|
|
// if ($recurringTransaction->errors()->count() == 0) {
|
|
|
|
// Session::flash('success', 'The recurring transaction has been updated.');
|
|
|
|
// //Event::fire('recurring.update', [$recurringTransaction]);
|
|
|
|
//
|
|
|
|
// return Redirect::route('recurring.index');
|
|
|
|
// } else {
|
|
|
|
// Session::flash(
|
|
|
|
// 'error', 'Could not update the recurring transaction: ' . $recurringTransaction->errors()->first()
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput()->withErrors(
|
|
|
|
// $recurringTransaction->errors()
|
|
|
|
// );
|
|
|
|
// }
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
2014-08-21 08:15:39 -05:00
|
|
|
}
|