mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
First attempt at storing an account.
This commit is contained in:
parent
169d1065cc
commit
3841259779
@ -1,10 +1,13 @@
|
|||||||
<?php namespace FireflyIII\Http\Controllers;
|
<?php namespace FireflyIII\Http\Controllers;
|
||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Config;
|
use Config;
|
||||||
use FireflyIII\Http\Requests;
|
use FireflyIII\Http\Requests;
|
||||||
use FireflyIII\Http\Requests\AccountFormRequest;
|
use FireflyIII\Http\Requests\AccountFormRequest;
|
||||||
use Illuminate\Http\Request;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
|
use Redirect;
|
||||||
|
use Session;
|
||||||
use View;
|
use View;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,8 +49,24 @@ class AccountController extends Controller
|
|||||||
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts'));
|
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(AccountFormRequest $request)
|
public function store(AccountFormRequest $request, AccountRepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
|
$accountData = [
|
||||||
|
'name' => $request->input('name'),
|
||||||
|
'accountType' => Config::get('firefly.accountTypeByIdentifier.' . $request->input('what')),
|
||||||
|
'active' => true,
|
||||||
|
'user' => Auth::user()->id,
|
||||||
|
'accountRole' => $request->input('accountRole'),
|
||||||
|
'openingBalance' => floatval($request->input('openingBalance')),
|
||||||
|
'openingBalanceDate' => new Carbon($request->input('openingBalanceDate')),
|
||||||
|
'openingBalanceCurrency' => intval($request->input('balance_currency_id')),
|
||||||
|
|
||||||
|
];
|
||||||
|
$account = $repository->store($accountData);
|
||||||
|
|
||||||
|
Session::flash('success', 'New account "' . $account->name . '" stored!');
|
||||||
|
|
||||||
|
return Redirect::route('accounts.index', $request->input('what'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace FireflyIII\Http\Requests;
|
namespace FireflyIII\Http\Requests;
|
||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
|
use Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AccountFormRequest
|
* Class AccountFormRequest
|
||||||
@ -19,9 +20,17 @@ class AccountFormRequest extends Request
|
|||||||
|
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
|
$accountRoles = join(',', array_keys(Config::get('firefly.accountRoles')));
|
||||||
|
$types = join(',', array_keys(Config::get('firefly.subTitlesByIdentifier')));
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => 'required|between:1,100|uniqueForUser:accounts,name',
|
'name' => 'required|between:1,100|uniqueForUser:accounts,name',
|
||||||
'openingBalance' => 'required|numeric'
|
'openingBalance' => 'numeric',
|
||||||
|
'openingBalanceDate' => 'date',
|
||||||
|
'accountRole' => 'in:' . $accountRoles,
|
||||||
|
'active' => 'boolean',
|
||||||
|
'balance_currency_id' => 'required|exists:transaction_currencies,id',
|
||||||
|
'what' => 'in:' . $types
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,6 +3,7 @@
|
|||||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Watson\Validating\ValidatingTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Account
|
* Class Account
|
||||||
@ -11,7 +12,17 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||||||
*/
|
*/
|
||||||
class Account extends Model
|
class Account extends Model
|
||||||
{
|
{
|
||||||
use SoftDeletes;
|
use SoftDeletes, ValidatingTrait;
|
||||||
|
|
||||||
|
protected $rules
|
||||||
|
= [
|
||||||
|
'user_id' => 'required|exists:users,id',
|
||||||
|
'account_type_id' => 'required|exists:account_types,id',
|
||||||
|
'name' => 'required|between:1,100|uniqueForUser:accounts,name',
|
||||||
|
'active' => 'required|boolean'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $fillable = ['user_id','account_type_id','name','active'];
|
||||||
|
|
||||||
public function accountMeta()
|
public function accountMeta()
|
||||||
{
|
{
|
||||||
|
@ -47,6 +47,10 @@ class FireflyServiceProvider extends ServiceProvider
|
|||||||
return new \FireflyIII\Support\ExpandedForm;
|
return new \FireflyIII\Support\ExpandedForm;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// preferences
|
||||||
|
$this->app->bind('FireflyIII\Repositories\Account\AccountRepositoryInterface', 'FireflyIII\Repositories\Account\AccountRepository');
|
||||||
|
$this->app->bind('FireflyIII\Repositories\Journal\JournalRepositoryInterface', 'FireflyIII\Repositories\Journal\JournalRepository');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
63
app/Repositories/Account/AccountRepository.php
Normal file
63
app/Repositories/Account/AccountRepository.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Repositories\Account;
|
||||||
|
|
||||||
|
use App;
|
||||||
|
use FireflyIII\Models\Account;
|
||||||
|
use FireflyIII\Models\AccountType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AccountRepository
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Repositories\Account
|
||||||
|
*/
|
||||||
|
class AccountRepository implements AccountRepositoryInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return Account;
|
||||||
|
*/
|
||||||
|
public function store(array $data)
|
||||||
|
{
|
||||||
|
$newAccount = $this->_store($data);
|
||||||
|
|
||||||
|
|
||||||
|
// continue with the opposing account:
|
||||||
|
if ($data['openingBalance'] != 0) {
|
||||||
|
$type = $data['openingBalance'] < 0 ? 'expense' : 'revenue';
|
||||||
|
$opposing = [
|
||||||
|
'user' => $data['user'],
|
||||||
|
'accountType' => $type,
|
||||||
|
'name' => $data['name'] . ' initial balance',
|
||||||
|
'active' => false,
|
||||||
|
];
|
||||||
|
$this->_store($opposing);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $newAccount;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*/
|
||||||
|
protected function _store(array $data)
|
||||||
|
{
|
||||||
|
$accountType = AccountType::whereType($data['accountType'])->first();
|
||||||
|
$newAccount = new Account(
|
||||||
|
[
|
||||||
|
'user_id' => $data['user'],
|
||||||
|
'account_type_id' => $accountType->id,
|
||||||
|
'name' => $data['name'],
|
||||||
|
'active' => $data['active'] === true ? true : false,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
if (!$newAccount->isValid()) {
|
||||||
|
App::abort(500);
|
||||||
|
}
|
||||||
|
$newAccount->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
app/Repositories/Account/AccountRepositoryInterface.php
Normal file
9
app/Repositories/Account/AccountRepositoryInterface.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Repositories\Account;
|
||||||
|
|
||||||
|
|
||||||
|
interface AccountRepositoryInterface
|
||||||
|
{
|
||||||
|
public function store(array $data);
|
||||||
|
}
|
8
app/Repositories/Journal/JournalRepository.php
Normal file
8
app/Repositories/Journal/JournalRepository.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Repositories\Journal;
|
||||||
|
|
||||||
|
|
||||||
|
class JournalRepository implements JournalRepositoryInterface {
|
||||||
|
|
||||||
|
}
|
14
app/Repositories/Journal/JournalRepositoryInterface.php
Normal file
14
app/Repositories/Journal/JournalRepositoryInterface.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: sander
|
||||||
|
* Date: 08/02/15
|
||||||
|
* Time: 18:15
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Repositories\Journal;
|
||||||
|
|
||||||
|
|
||||||
|
interface JournalRepositoryInterface {
|
||||||
|
|
||||||
|
}
|
@ -70,4 +70,10 @@ return [
|
|||||||
'expense' => ['Expense account', 'Beneficiary account'],
|
'expense' => ['Expense account', 'Beneficiary account'],
|
||||||
'revenue' => ['Revenue account'],
|
'revenue' => ['Revenue account'],
|
||||||
],
|
],
|
||||||
|
'accountTypeByIdentifier' =>
|
||||||
|
[
|
||||||
|
'asset' => 'Asset account',
|
||||||
|
'expense' => 'Expense account',
|
||||||
|
'revenue' => 'Revenue account',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
@ -20,6 +20,7 @@ return [
|
|||||||
"alpha_dash" => "The :attribute may only contain letters, numbers, and dashes.",
|
"alpha_dash" => "The :attribute may only contain letters, numbers, and dashes.",
|
||||||
"alpha_num" => "The :attribute may only contain letters and numbers.",
|
"alpha_num" => "The :attribute may only contain letters and numbers.",
|
||||||
"array" => "The :attribute must be an array.",
|
"array" => "The :attribute must be an array.",
|
||||||
|
"unique_for_user" => "There already is an entry with this :attribute.",
|
||||||
"before" => "The :attribute must be a date before :date.",
|
"before" => "The :attribute must be a date before :date.",
|
||||||
"between" => [
|
"between" => [
|
||||||
"numeric" => "The :attribute must be between :min and :max.",
|
"numeric" => "The :attribute must be between :min and :max.",
|
||||||
|
@ -27,21 +27,20 @@
|
|||||||
|
|
||||||
<div class="col-lg-6 col-md-6 col-sm-12">
|
<div class="col-lg-6 col-md-6 col-sm-12">
|
||||||
|
|
||||||
|
@if($what == 'asset')
|
||||||
<div class="panel panel-default">
|
<div class="panel panel-default">
|
||||||
<div class="panel-heading">
|
<div class="panel-heading">
|
||||||
<i class="fa fa-smile-o"></i> Optional fields
|
<i class="fa fa-smile-o"></i> Optional fields
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-body">
|
<div class="panel-body">
|
||||||
@if($what == 'asset')
|
|
||||||
{!! ExpandedForm::balance('openingBalance') !!}
|
{!! ExpandedForm::balance('openingBalance') !!}
|
||||||
{!! ExpandedForm::date('openingBalanceDate', date('Y-m-d')) !!}
|
{!! ExpandedForm::date('openingBalanceDate', date('Y-m-d')) !!}
|
||||||
{!! ExpandedForm::select('account_role',Config::get('firefly.accountRoles')) !!}
|
{!! ExpandedForm::select('accountRole',Config::get('firefly.accountRoles')) !!}
|
||||||
@endif
|
|
||||||
{!! ExpandedForm::checkbox('active','1',true) !!}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
<!-- panel for options -->
|
<!-- panel for options -->
|
||||||
<div class="panel panel-default">
|
<div class="panel panel-default">
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
@if($type == 'create')
|
@if($type == 'create')
|
||||||
|
<!--
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="{{{$name}}}_store" class="col-sm-4 control-label">Store</label>
|
<label for="{{{$name}}}_store" class="col-sm-4 control-label">Store</label>
|
||||||
<div class="col-sm-8">
|
<div class="col-sm-8">
|
||||||
@ -10,8 +11,10 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
-->
|
||||||
@endif
|
@endif
|
||||||
@if($type == 'update')
|
@if($type == 'update')
|
||||||
|
<!--
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="{{{$name}}}_update" class="col-sm-4 control-label">Update</label>
|
<label for="{{{$name}}}_update" class="col-sm-4 control-label">Update</label>
|
||||||
<div class="col-sm-8">
|
<div class="col-sm-8">
|
||||||
@ -23,8 +26,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
-->
|
||||||
@endif
|
@endif
|
||||||
|
<!--
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="{{{$name}}}_validate_only" class="col-sm-4 control-label">Validate only</label>
|
<label for="{{{$name}}}_validate_only" class="col-sm-4 control-label">Validate only</label>
|
||||||
<div class="col-sm-8">
|
<div class="col-sm-8">
|
||||||
@ -36,7 +40,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
-->
|
||||||
@if($type == 'create')
|
@if($type == 'create')
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="{{{$name}}}_return_to_form" class="col-sm-4 control-label">
|
<label for="{{{$name}}}_return_to_form" class="col-sm-4 control-label">
|
||||||
@ -45,7 +49,7 @@
|
|||||||
<div class="col-sm-8">
|
<div class="col-sm-8">
|
||||||
<div class="radio">
|
<div class="radio">
|
||||||
<label>
|
<label>
|
||||||
{!! Form::radio('post_submit_action', 'create_another', $previousValue == 'create_another', ['id' => $name . '_create_another']) !!}
|
{!! Form::checkbox('create_another', '1') !!}
|
||||||
After storing, return here to create another one.
|
After storing, return here to create another one.
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
@ -60,7 +64,7 @@
|
|||||||
</label>
|
</label>
|
||||||
<div class="col-sm-8">
|
<div class="col-sm-8">
|
||||||
<div class="radio"><label>
|
<div class="radio"><label>
|
||||||
{!! Form::radio('post_submit_action', 'return_to_edit', $previousValue == 'return_to_edit', ['id' => $name . '_return_to_edit']) !!}
|
{!! Form::checkbox('return_to_edit', '1') !!}
|
||||||
After updating, return here.
|
After updating, return here.
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user