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
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2014-07-31 15:01:52 -05:00
|
|
|
$count = $this->_repository->count();
|
|
|
|
$piggybanks = $this->_repository->get();
|
|
|
|
$accounts = [];
|
|
|
|
// get accounts:
|
|
|
|
foreach($piggybanks as $piggyBank) {
|
|
|
|
$account = $piggyBank->account;
|
|
|
|
$id = $account->id;
|
|
|
|
if(!isset($accounts[$id])) {
|
|
|
|
$account->balance = $account->balance();
|
|
|
|
$account->left = $account->balance;
|
|
|
|
} else {
|
|
|
|
$account->left -= $piggyBank->amount;
|
|
|
|
}
|
|
|
|
$accounts[$id] = $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
if(!$piggyBank->id) {
|
|
|
|
Session::flash('error','Could not save piggy bank: ' . $piggyBank->errors()->first());
|
|
|
|
return Redirect::route('piggybanks.create')->withInput();
|
|
|
|
} else {
|
|
|
|
Session::flash('success','New piggy bank created!');
|
|
|
|
return Redirect::route('piggybanks.index');
|
|
|
|
}
|
|
|
|
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|