2014-07-29 13:30:50 -05:00
|
|
|
<?php
|
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;
|
|
|
|
|
|
|
|
public function __construct(PRI $repository, ARI $accounts)
|
|
|
|
{
|
|
|
|
$this->_repository = $repository;
|
|
|
|
$this->_accounts = $accounts;
|
|
|
|
View::share('menu', 'home');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-29 13:30:50 -05:00
|
|
|
public function create()
|
|
|
|
{
|
2014-07-31 15:01:52 -05:00
|
|
|
$accounts = $this->_accounts->getActiveDefaultAsSelectList();
|
|
|
|
|
|
|
|
return View::make('piggybanks.create')->with('accounts', $accounts);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
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-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-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
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2014-07-31 15:01:52 -05:00
|
|
|
$count = $this->_repository->count();
|
|
|
|
$piggybanks = $this->_repository->get();
|
|
|
|
$accounts = [];
|
|
|
|
// get accounts:
|
2014-08-01 00:04:36 -05:00
|
|
|
foreach ($piggybanks as $piggyBank) {
|
2014-07-31 15:01:52 -05:00
|
|
|
$account = $piggyBank->account;
|
2014-08-01 05:58:54 -05:00
|
|
|
$piggyBank->pct = round(($piggyBank->amount / $piggyBank->target) * 100, 0) . '%';
|
2014-07-31 15:01:52 -05:00
|
|
|
$id = $account->id;
|
2014-08-01 00:04:36 -05:00
|
|
|
if (!isset($accounts[$id])) {
|
2014-07-31 15:01:52 -05:00
|
|
|
$account->balance = $account->balance();
|
2014-08-01 00:04:36 -05:00
|
|
|
$account->left = $account->balance - $piggyBank->amount;
|
2014-08-02 00:34:38 -05:00
|
|
|
$account->total = $piggyBank->target;
|
2014-07-31 15:01:52 -05:00
|
|
|
} else {
|
|
|
|
$account->left -= $piggyBank->amount;
|
2014-08-02 00:34:38 -05:00
|
|
|
$account->total += $piggyBank->target;
|
2014-08-01 00:04:36 -05:00
|
|
|
|
2014-07-31 15:01:52 -05:00
|
|
|
}
|
|
|
|
$accounts[$id] = $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-01 00:04:36 -05:00
|
|
|
return View::make('piggybanks.index')->with('count', $count)->with('accounts', $accounts)->with(
|
|
|
|
'piggybanks', $piggybanks
|
|
|
|
);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function show()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
2014-07-31 15:01:52 -05:00
|
|
|
$piggyBank = $this->_repository->store(Input::all());
|
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!');
|
|
|
|
|
|
|
|
if (Input::get('create') == '1') {
|
|
|
|
return Redirect::route('piggybanks.create')->withInput();
|
|
|
|
}
|
2014-08-01 00:04:36 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateAmount(Piggybank $piggybank)
|
|
|
|
{
|
|
|
|
$this->_repository->updateAmount($piggybank, Input::get('amount'));
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
}
|