2014-07-29 13:30:50 -05:00
|
|
|
<?php
|
2014-07-31 15:01:52 -05:00
|
|
|
|
2014-08-14 11:17:02 -05:00
|
|
|
use Carbon\Carbon;
|
2014-07-31 15:01:52 -05:00
|
|
|
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
|
|
|
|
use Firefly\Storage\Piggybank\PiggybankRepositoryInterface as PRI;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggybankController
|
|
|
|
*/
|
|
|
|
class PiggybankController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $_repository;
|
|
|
|
protected $_accounts;
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param PRI $repository
|
|
|
|
* @param ARI $accounts
|
|
|
|
*/
|
2014-07-31 15:01:52 -05:00
|
|
|
public function __construct(PRI $repository, ARI $accounts)
|
|
|
|
{
|
|
|
|
$this->_repository = $repository;
|
|
|
|
$this->_accounts = $accounts;
|
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-13 13:36:32 -05:00
|
|
|
public function createPiggybank()
|
|
|
|
{
|
2014-08-13 14:35:05 -05:00
|
|
|
$periods = Config::get('firefly.piggybank_periods');
|
2014-08-13 13:36:32 -05:00
|
|
|
$accounts = $this->_accounts->getActiveDefaultAsSelectList();
|
|
|
|
|
2014-08-13 14:35:05 -05:00
|
|
|
return View::make('piggybanks.create-piggybank')->with('accounts', $accounts)->with('periods',$periods);
|
2014-08-13 13:36:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function createRepeated()
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-14 11:02:31 -05:00
|
|
|
$periods = Config::get('firefly.piggybank_periods');
|
2014-07-31 15:01:52 -05:00
|
|
|
$accounts = $this->_accounts->getActiveDefaultAsSelectList();
|
|
|
|
|
2014-08-14 11:02:31 -05:00
|
|
|
return View::make('piggybanks.create-repeated')->with('accounts', $accounts)->with('periods',$periods);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-13 13:36:32 -05:00
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function delete(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-02 00:34:38 -05:00
|
|
|
return View::make('piggybanks.delete')->with('piggybank', $piggyBank);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function destroy(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-02 00:34:38 -05:00
|
|
|
$piggyBank->delete();
|
|
|
|
Session::flash('success', 'Piggy bank deleted.');
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.index');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function edit(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-02 00:34:38 -05:00
|
|
|
$accounts = $this->_accounts->getActiveDefaultAsSelectList();
|
|
|
|
|
|
|
|
return View::make('piggybanks.edit')->with('piggybank', $piggyBank)->with('accounts', $accounts);
|
2014-08-01 00:04:36 -05:00
|
|
|
}
|
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-08-13 13:36:32 -05:00
|
|
|
$countRepeating = $this->_repository->countRepeating();
|
|
|
|
$countNonRepeating = $this->_repository->countNonrepeating();
|
2014-07-31 15:01:52 -05:00
|
|
|
$piggybanks = $this->_repository->get();
|
2014-08-13 13:36:32 -05:00
|
|
|
return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
|
|
|
|
->with('countRepeating',$countRepeating)
|
|
|
|
->with('countNonRepeating',$countNonRepeating);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2014-08-11 00:28:47 -05:00
|
|
|
public function show(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-13 00:02:13 -05:00
|
|
|
return View::make('piggybanks.show')->with('piggyBank', $piggyBank);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-13 13:36:32 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function storePiggybank()
|
|
|
|
{
|
|
|
|
$data = Input::all();
|
|
|
|
unset($data['_token']);
|
|
|
|
|
|
|
|
// extend the data array with the settings needed to create a piggy bank:
|
|
|
|
$data['repeats'] = 0;
|
|
|
|
$data['rep_times'] = 0;
|
|
|
|
$data['order'] = 0;
|
|
|
|
|
|
|
|
$piggyBank = $this->_repository->store($data);
|
2014-08-14 00:40:15 -05:00
|
|
|
if (!is_null($piggyBank->id)) {
|
2014-08-13 13:36:32 -05:00
|
|
|
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.index');
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.create.piggybank')->withInput()->withErrors($piggyBank->errors());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-14 11:17:02 -05:00
|
|
|
public function storeRepeated()
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-14 11:17:02 -05:00
|
|
|
|
|
|
|
$data = Input::all();
|
|
|
|
unset($data['_token']);
|
|
|
|
|
|
|
|
// extend the data array with the settings needed to create a repeated:
|
|
|
|
$data['repeats'] = 1;
|
|
|
|
$data['startdate'] = new Carbon;
|
|
|
|
$data['order'] = 0;
|
|
|
|
|
|
|
|
$piggyBank = $this->_repository->store($data);
|
2014-08-10 04:30:14 -05:00
|
|
|
if ($piggyBank->validate()) {
|
2014-08-02 00:34:38 -05:00
|
|
|
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
|
|
|
|
|
2014-07-31 15:01:52 -05:00
|
|
|
return Redirect::route('piggybanks.index');
|
2014-08-10 04:30:14 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.create')->withInput();
|
2014-07-31 15:01:52 -05:00
|
|
|
}
|
|
|
|
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-07-29 13:30:50 -05:00
|
|
|
public function update()
|
|
|
|
{
|
2014-08-02 00:34:38 -05:00
|
|
|
|
|
|
|
$piggyBank = $this->_repository->update(Input::all());
|
2014-08-10 04:30:14 -05:00
|
|
|
if ($piggyBank->validate()) {
|
|
|
|
Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.');
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.index');
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not update piggy bank: ' . $piggyBank->errors()->first());
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.edit', $piggyBank->id)->withErrors($piggyBank->errors())->withInput();
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:34:38 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggybank
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function updateAmount(Piggybank $piggybank)
|
|
|
|
{
|
|
|
|
$this->_repository->updateAmount($piggybank, Input::get('amount'));
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
}
|