Firefly is now capable of adding a transaction. Untested [skip-ci]

This commit is contained in:
James Cole 2014-07-15 11:04:53 +02:00
parent c2948b2a29
commit a1fb4bdb60
9 changed files with 164 additions and 25 deletions

View File

@ -2,25 +2,75 @@
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
use Firefly\Storage\Budget\BudgetRepositoryInterface as Bud;
use Firefly\Storage\Category\CategoryRepositoryInterface as Cat;
use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;
class TransactionController extends BaseController {
class TransactionController extends BaseController
{
protected $accounts;
protected $budgets;
protected $categories;
protected $tj;
public function __construct(ARI $accounts) {
public function __construct(ARI $accounts, Bud $budgets, Cat $categories, TJRI $tj)
{
$this->accounts = $accounts;
$this->budgets = $budgets;
$this->categories = $categories;
$this->tj = $tj;
View::share('menu','home');
View::share('menu', 'home');
}
public function createWithdrawal() {
public function createWithdrawal()
{
// get accounts with names and id's.
$accounts =$this->accounts->getActiveDefaultAsSelectList();
$accounts = $this->accounts->getActiveDefaultAsSelectList();
$budgets = $this->budgets->getAsSelectList();
$budgets[0] = '(no budget)';
return View::make('transactions.withdrawal')->with('accounts',$accounts);
return View::make('transactions.withdrawal')->with('accounts', $accounts)->with('budgets', $budgets);
}
public function postCreateWithdrawal()
{
// create or find beneficiary:
$beneficiary = $this->accounts->createOrFindBeneficiary(Input::get('beneficiary'));
// create or find category:
$category = $this->categories->createOrFind(Input::get('category'));
// find budget:
$budget = $this->budgets->find(intval(Input::get('budget_id')));
// find account:
$account = $this->accounts->find(intval(Input::get('account_id')));
// find amount & description:
$description = trim(Input::get('description'));
$amount = floatval(Input::get('amount'));
$date = new \Carbon\Carbon(Input::get('date'));
// create journal
/** @var \TransactionJournal $journal */
$journal = $this->tj->createSimpleJournal($account,$beneficiary,$description,$amount,$date);
var_dump($journal);
// attach bud/cat (?)
$journal->budgets()->save($budget);
$journal->categories()->save($category);
Session::flash('success','Transaction saved');
return Redirect::route('index');
}
}

View File

@ -15,6 +15,8 @@ interface AccountRepositoryInterface
public function find($id);
public function findByName($name);
public function getByIds($ids);
public function getDefault();
@ -27,4 +29,8 @@ interface AccountRepositoryInterface
public function storeWithInitialBalance($data, \Carbon\Carbon $date, $amount = 0);
public function createOrFindBeneficiary($name);
public function createOrFind($name, \AccountType $type);
}

View File

@ -16,7 +16,8 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return \Auth::user()->accounts()->with('accounttype')->get();
}
public function getBeneficiaries() {
public function getBeneficiaries()
{
$list = \Auth::user()->accounts()->leftJoin(
'account_types', 'account_types.id', '=', 'accounts.account_type_id'
)
@ -126,4 +127,28 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $account;
}
public function createOrFindBeneficiary($name)
{
$type = \AccountType::where('description', 'Beneficiary account')->first();
return $this->createOrFind($name, $type);
}
public function createOrFind($name, \AccountType $type)
{
$beneficiary = $this->findByName($name);
if (!$beneficiary) {
$data = [
'name' => $name,
'account_type' => $type
];
return $this->store($data);
}
return $beneficiary;
}
public function findByName($name)
{
return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first();
}
}

View File

@ -3,6 +3,9 @@
namespace Firefly\Storage\Budget;
interface BudgetRepositoryInterface {
interface BudgetRepositoryInterface
{
public function getAsSelectList();
public function find($id);
}

View File

@ -3,6 +3,23 @@
namespace Firefly\Storage\Budget;
class EloquentBudgetRepository implements BudgetRepositoryInterface {
class EloquentBudgetRepository implements BudgetRepositoryInterface
{
public function getAsSelectList()
{
$list = \Auth::user()->budgets()->get();
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
}
return $return;
}
public function find($id)
{
return \Auth::user()->budgets()->find($id);
}
}

View File

@ -3,8 +3,15 @@
namespace Firefly\Storage\Category;
interface CategoryRepositoryInterface {
interface CategoryRepositoryInterface
{
public function get();
public function createOrFind($name);
public function findByName($name);
public function store($name);
}

View File

@ -3,9 +3,37 @@
namespace Firefly\Storage\Category;
class EloquentCategoryRepository implements CategoryRepositoryInterface {
public function get() {
class EloquentCategoryRepository implements CategoryRepositoryInterface
{
public function get()
{
return \Auth::user()->categories()->get();
}
public function createOrFind($name)
{
$category = $this->findByName($name);
if (!$category) {
return $this->store($name);
}
return $category;
}
public function findByName($name)
{
return \Auth::user()->categories()->where('name', 'LIKE', '%' . $name . '%')->first();
}
public function store($name)
{
$category = new \Category();
$category->name = $name;
$category->user()->associate(\Auth::user());
$category->save();
return $category;
}
}

View File

@ -55,6 +55,11 @@ Route::group(['before' => 'csrf|auth'], function () {
// account controller:
Route::get('/accounts/store', ['uses' => 'AccountController@store', 'as' => 'accounts.store']);
// transaction controller:
Route::post('/transactions/add/withdrawal', ['uses' => 'TransactionController@postCreateWithdrawal']);
Route::post('/transactions/add/deposit', ['uses' => 'TransactionController@postCreateDeposit']);
Route::post('/transactions/add/transfer', ['uses' => 'TransactionController@postCreateTransfer']);
}
);

View File

@ -26,21 +26,21 @@
<h4>Mandatory fields</h4>
<div class="form-group">
<label for="account" class="col-sm-4 control-label">Description</label>
<label for="description" class="col-sm-4 control-label">Description</label>
<div class="col-sm-8">
<input type="text" name="description" value="{{{Input::old('description')}}}" autocomplete="off" class="form-control" placeholder="Description" />
</div>
</div>
<div class="form-group">
<label for="account" class="col-sm-4 control-label">Account</label>
<label for="account_id" class="col-sm-4 control-label">Account</label>
<div class="col-sm-8">
{{Form::select('account_id',$accounts,Input::old('account_id'),['class' => 'form-control'])}}
</div>
</div>
<div class="form-group">
<label for="account" class="col-sm-4 control-label">Beneficiary</label>
<label for="beneficiary" class="col-sm-4 control-label">Beneficiary</label>
<div class="col-sm-8">
<input type="text" name="beneficiary" value="{{{Input::old('beneficiary')}}}" autocomplete="off" class="form-control" placeholder="Beneficiary" />
<span class="help-block">This field will auto-complete your existing beneficiaries (if any), but you can type freely to create new ones.</span>
@ -50,21 +50,21 @@
<div class="form-group">
<label for="account" class="col-sm-4 control-label">Amount spent</label>
<label for="amount" class="col-sm-4 control-label">Amount spent</label>
<div class="col-sm-8">
<input type="number" min="0.01" step="any" class="form-control" />
<input type="number" name="amount" min="0.01" step="any" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="account" class="col-sm-4 control-label">Date</label>
<label for="date" class="col-sm-4 control-label">Date</label>
<div class="col-sm-8">
<input type="date" class="form-control" />
<input type="date" name="date" value="{{Input::old('date') ?: date('Y-m-d')}}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="account" class="col-sm-4 control-label">&nbsp;</label>
<label for="submit" class="col-sm-4 control-label">&nbsp;</label>
<div class="col-sm-8">
<input type="submit" name="submit" value="Create withdrawal" class="btn btn-info" />
</div>
@ -75,17 +75,15 @@
<h4>Optional fields</h4>
<div class="form-group">
<label for="account" class="col-sm-4 control-label">Budget</label>
<label for="budget_id" class="col-sm-4 control-label">Budget</label>
<div class="col-sm-8">
<select class="form-control">
<option>1</option>
</select>
{{Form::select('budget_id',$budgets,Input::old('budget_id') ?: 0,['class' => 'form-control'])}}
<span class="help-block">Select one of your budgets to make this transaction a part of it.</span>
</div>
</div>
<div class="form-group">
<label for="account" class="col-sm-4 control-label">Category</label>
<label for="category" class="col-sm-4 control-label">Category</label>
<div class="col-sm-8">
<input type="text" name="category" value="" autocomplete="off" class="form-control" placeholder="Category" />
<span class="help-block">Add more fine-grained information to this transaction by entering a category.