Skip to content

Firefly III: a personal finances manager

First attempt at storing an account.

James Cole384125977984
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index 03930f56bd..7de99154f6 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -1,10 +1,13 @@
<?php namespace FireflyIII\Http\Controllers;
use Auth;
+use Carbon\Carbon;
use Config;
use FireflyIII\Http\Requests;
use FireflyIII\Http\Requests\AccountFormRequest;
-use Illuminate\Http\Request;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use Redirect;
+use Session;
use View;
/**
@@ -46,8 +49,24 @@ class AccountController extends Controller
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'));
}
diff --git a/app/Http/Requests/AccountFormRequest.php b/app/Http/Requests/AccountFormRequest.php
index 478bf550c4..a64a8ddbb2 100644
--- a/app/Http/Requests/AccountFormRequest.php
+++ b/app/Http/Requests/AccountFormRequest.php
@@ -3,6 +3,7 @@
namespace FireflyIII\Http\Requests;
use Auth;
+use Config;
/**
* Class AccountFormRequest
@@ -19,9 +20,17 @@ class AccountFormRequest extends Request
public function rules()
{
+ $accountRoles = join(',', array_keys(Config::get('firefly.accountRoles')));
+ $types = join(',', array_keys(Config::get('firefly.subTitlesByIdentifier')));
+
return [
- 'name' => 'required|between:1,100|uniqueForUser:accounts,name',
- 'openingBalance' => 'required|numeric'
+ 'name' => 'required|between:1,100|uniqueForUser:accounts,name',
+ 'openingBalance' => 'numeric',
+ 'openingBalanceDate' => 'date',
+ 'accountRole' => 'in:' . $accountRoles,
+ 'active' => 'boolean',
+ 'balance_currency_id' => 'required|exists:transaction_currencies,id',
+ 'what' => 'in:' . $types
];
}
}
\ No newline at end of file
diff --git a/app/Models/Account.php b/app/Models/Account.php
index ce10f0a971..f3dac68a2b 100644
--- a/app/Models/Account.php
+++ b/app/Models/Account.php
@@ -3,6 +3,7 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
+use Watson\Validating\ValidatingTrait;
/**
* Class Account
@@ -11,7 +12,17 @@ use Illuminate\Database\Eloquent\SoftDeletes;
*/
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()
{
diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php
index 692249373b..a0656f4530 100644
--- a/app/Providers/FireflyServiceProvider.php
+++ b/app/Providers/FireflyServiceProvider.php
@@ -47,6 +47,10 @@ class FireflyServiceProvider extends ServiceProvider
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');
}
}
\ No newline at end of file
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
new file mode 100644
index 0000000000..fd503c84e1
--- /dev/null
+++ b/app/Repositories/Account/AccountRepository.php
@@ -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();
+ }
+
+}
\ No newline at end of file
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
new file mode 100644
index 0000000000..524d4a0545
--- /dev/null
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace FireflyIII\Repositories\Account;
+
+
+interface AccountRepositoryInterface
+{
+ public function store(array $data);
+}
\ No newline at end of file
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
new file mode 100644
index 0000000000..fe14e453eb
--- /dev/null
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace FireflyIII\Repositories\Journal;
+
+
+class JournalRepository implements JournalRepositoryInterface {
+
+}
\ No newline at end of file
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
new file mode 100644
index 0000000000..6da37e2a41
--- /dev/null
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: sander
+ * Date: 08/02/15
+ * Time: 18:15
+ */
+
+namespace FireflyIII\Repositories\Journal;
+
+
+interface JournalRepositoryInterface {
+
+}
\ No newline at end of file
diff --git a/config/firefly.php b/config/firefly.php
index 5ee38389c2..77f4e4b628 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -70,4 +70,10 @@ return [
'expense' => ['Expense account', 'Beneficiary account'],
'revenue' => ['Revenue account'],
],
+ 'accountTypeByIdentifier' =>
+ [
+ 'asset' => 'Asset account',
+ 'expense' => 'Expense account',
+ 'revenue' => 'Revenue account',
+ ],
];
diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php
index 764f05636d..db49422d54 100644
--- a/resources/lang/en/validation.php
+++ b/resources/lang/en/validation.php
@@ -2,106 +2,107 @@
return [
- /*
- |--------------------------------------------------------------------------
- | Validation Language Lines
- |--------------------------------------------------------------------------
- |
- | The following language lines contain the default error messages used by
- | the validator class. Some of these rules have multiple versions such
- | as the size rules. Feel free to tweak each of these messages here.
- |
- */
+ /*
+ |--------------------------------------------------------------------------
+ | Validation Language Lines
+ |--------------------------------------------------------------------------
+ |
+ | The following language lines contain the default error messages used by
+ | the validator class. Some of these rules have multiple versions such
+ | as the size rules. Feel free to tweak each of these messages here.
+ |
+ */
- "accepted" => "The :attribute must be accepted.",
- "active_url" => "The :attribute is not a valid URL.",
- "after" => "The :attribute must be a date after :date.",
- "alpha" => "The :attribute may only contain letters.",
- "alpha_dash" => "The :attribute may only contain letters, numbers, and dashes.",
- "alpha_num" => "The :attribute may only contain letters and numbers.",
- "array" => "The :attribute must be an array.",
- "before" => "The :attribute must be a date before :date.",
- "between" => [
- "numeric" => "The :attribute must be between :min and :max.",
- "file" => "The :attribute must be between :min and :max kilobytes.",
- "string" => "The :attribute must be between :min and :max characters.",
- "array" => "The :attribute must have between :min and :max items.",
- ],
- "boolean" => "The :attribute field must be true or false.",
- "confirmed" => "The :attribute confirmation does not match.",
- "date" => "The :attribute is not a valid date.",
- "date_format" => "The :attribute does not match the format :format.",
- "different" => "The :attribute and :other must be different.",
- "digits" => "The :attribute must be :digits digits.",
- "digits_between" => "The :attribute must be between :min and :max digits.",
- "email" => "The :attribute must be a valid email address.",
- "filled" => "The :attribute field is required.",
- "exists" => "The selected :attribute is invalid.",
- "image" => "The :attribute must be an image.",
- "in" => "The selected :attribute is invalid.",
- "integer" => "The :attribute must be an integer.",
- "ip" => "The :attribute must be a valid IP address.",
- "max" => [
- "numeric" => "The :attribute may not be greater than :max.",
- "file" => "The :attribute may not be greater than :max kilobytes.",
- "string" => "The :attribute may not be greater than :max characters.",
- "array" => "The :attribute may not have more than :max items.",
- ],
- "mimes" => "The :attribute must be a file of type: :values.",
- "min" => [
- "numeric" => "The :attribute must be at least :min.",
- "file" => "The :attribute must be at least :min kilobytes.",
- "string" => "The :attribute must be at least :min characters.",
- "array" => "The :attribute must have at least :min items.",
- ],
- "not_in" => "The selected :attribute is invalid.",
- "numeric" => "The :attribute must be a number.",
- "regex" => "The :attribute format is invalid.",
- "required" => "The :attribute field is required.",
- "required_if" => "The :attribute field is required when :other is :value.",
- "required_with" => "The :attribute field is required when :values is present.",
- "required_with_all" => "The :attribute field is required when :values is present.",
- "required_without" => "The :attribute field is required when :values is not present.",
- "required_without_all" => "The :attribute field is required when none of :values are present.",
- "same" => "The :attribute and :other must match.",
- "size" => [
- "numeric" => "The :attribute must be :size.",
- "file" => "The :attribute must be :size kilobytes.",
- "string" => "The :attribute must be :size characters.",
- "array" => "The :attribute must contain :size items.",
- ],
- "unique" => "The :attribute has already been taken.",
- "url" => "The :attribute format is invalid.",
- "timezone" => "The :attribute must be a valid zone.",
+ "accepted" => "The :attribute must be accepted.",
+ "active_url" => "The :attribute is not a valid URL.",
+ "after" => "The :attribute must be a date after :date.",
+ "alpha" => "The :attribute may only contain letters.",
+ "alpha_dash" => "The :attribute may only contain letters, numbers, and dashes.",
+ "alpha_num" => "The :attribute may only contain letters and numbers.",
+ "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.",
+ "between" => [
+ "numeric" => "The :attribute must be between :min and :max.",
+ "file" => "The :attribute must be between :min and :max kilobytes.",
+ "string" => "The :attribute must be between :min and :max characters.",
+ "array" => "The :attribute must have between :min and :max items.",
+ ],
+ "boolean" => "The :attribute field must be true or false.",
+ "confirmed" => "The :attribute confirmation does not match.",
+ "date" => "The :attribute is not a valid date.",
+ "date_format" => "The :attribute does not match the format :format.",
+ "different" => "The :attribute and :other must be different.",
+ "digits" => "The :attribute must be :digits digits.",
+ "digits_between" => "The :attribute must be between :min and :max digits.",
+ "email" => "The :attribute must be a valid email address.",
+ "filled" => "The :attribute field is required.",
+ "exists" => "The selected :attribute is invalid.",
+ "image" => "The :attribute must be an image.",
+ "in" => "The selected :attribute is invalid.",
+ "integer" => "The :attribute must be an integer.",
+ "ip" => "The :attribute must be a valid IP address.",
+ "max" => [
+ "numeric" => "The :attribute may not be greater than :max.",
+ "file" => "The :attribute may not be greater than :max kilobytes.",
+ "string" => "The :attribute may not be greater than :max characters.",
+ "array" => "The :attribute may not have more than :max items.",
+ ],
+ "mimes" => "The :attribute must be a file of type: :values.",
+ "min" => [
+ "numeric" => "The :attribute must be at least :min.",
+ "file" => "The :attribute must be at least :min kilobytes.",
+ "string" => "The :attribute must be at least :min characters.",
+ "array" => "The :attribute must have at least :min items.",
+ ],
+ "not_in" => "The selected :attribute is invalid.",
+ "numeric" => "The :attribute must be a number.",
+ "regex" => "The :attribute format is invalid.",
+ "required" => "The :attribute field is required.",
+ "required_if" => "The :attribute field is required when :other is :value.",
+ "required_with" => "The :attribute field is required when :values is present.",
+ "required_with_all" => "The :attribute field is required when :values is present.",
+ "required_without" => "The :attribute field is required when :values is not present.",
+ "required_without_all" => "The :attribute field is required when none of :values are present.",
+ "same" => "The :attribute and :other must match.",
+ "size" => [
+ "numeric" => "The :attribute must be :size.",
+ "file" => "The :attribute must be :size kilobytes.",
+ "string" => "The :attribute must be :size characters.",
+ "array" => "The :attribute must contain :size items.",
+ ],
+ "unique" => "The :attribute has already been taken.",
+ "url" => "The :attribute format is invalid.",
+ "timezone" => "The :attribute must be a valid zone.",
- /*
- |--------------------------------------------------------------------------
- | Custom Validation Language Lines
- |--------------------------------------------------------------------------
- |
- | Here you may specify custom validation messages for attributes using the
- | convention "attribute.rule" to name the lines. This makes it quick to
- | specify a specific custom language line for a given attribute rule.
- |
- */
+ /*
+ |--------------------------------------------------------------------------
+ | Custom Validation Language Lines
+ |--------------------------------------------------------------------------
+ |
+ | Here you may specify custom validation messages for attributes using the
+ | convention "attribute.rule" to name the lines. This makes it quick to
+ | specify a specific custom language line for a given attribute rule.
+ |
+ */
- 'custom' => [
- 'attribute-name' => [
- 'rule-name' => 'custom-message',
- ],
- ],
+ 'custom' => [
+ 'attribute-name' => [
+ 'rule-name' => 'custom-message',
+ ],
+ ],
- /*
- |--------------------------------------------------------------------------
- | Custom Validation Attributes
- |--------------------------------------------------------------------------
- |
- | The following language lines are used to swap attribute place-holders
- | with something more reader friendly such as E-Mail Address instead
- | of "email". This simply helps us make messages a little cleaner.
- |
- */
+ /*
+ |--------------------------------------------------------------------------
+ | Custom Validation Attributes
+ |--------------------------------------------------------------------------
+ |
+ | The following language lines are used to swap attribute place-holders
+ | with something more reader friendly such as E-Mail Address instead
+ | of "email". This simply helps us make messages a little cleaner.
+ |
+ */
- 'attributes' => [],
+ 'attributes' => [],
];
diff --git a/resources/views/accounts/create.blade.php b/resources/views/accounts/create.blade.php
index 0b0df1b5d5..68dc3ff298 100644
--- a/resources/views/accounts/create.blade.php
+++ b/resources/views/accounts/create.blade.php
@@ -27,21 +27,20 @@
<div class="col-lg-6 col-md-6 col-sm-12">
-
+ @if($what == 'asset')
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-smile-o"></i> Optional fields
</div>
<div class="panel-body">
- @if($what == 'asset')
+
{!! ExpandedForm::balance('openingBalance') !!}
{!! ExpandedForm::date('openingBalanceDate', date('Y-m-d')) !!}
- {!! ExpandedForm::select('account_role',Config::get('firefly.accountRoles')) !!}
- @endif
- {!! ExpandedForm::checkbox('active','1',true) !!}
+ {!! ExpandedForm::select('accountRole',Config::get('firefly.accountRoles')) !!}
+
</div>
</div>
-
+ @endif
<!-- panel for options -->
<div class="panel panel-default">
diff --git a/resources/views/form/options.blade.php b/resources/views/form/options.blade.php
index 7c84250eea..1cbcae442e 100644
--- a/resources/views/form/options.blade.php
+++ b/resources/views/form/options.blade.php
@@ -1,4 +1,5 @@
@if($type == 'create')
+ <!--
<div class="form-group">
<label for="{{{$name}}}_store" class="col-sm-4 control-label">Store</label>
<div class="col-sm-8">
@@ -10,8 +11,10 @@
</div>
</div>
</div>
+ -->
@endif
@if($type == 'update')
+ <!--
<div class="form-group">
<label for="{{{$name}}}_update" class="col-sm-4 control-label">Update</label>
<div class="col-sm-8">
@@ -23,8 +26,9 @@
</div>
</div>
</div>
+ -->
@endif
-
+ <!--
<div class="form-group">
<label for="{{{$name}}}_validate_only" class="col-sm-4 control-label">Validate only</label>
<div class="col-sm-8">
@@ -36,7 +40,7 @@
</div>
</div>
</div>
-
+-->
@if($type == 'create')
<div class="form-group">
<label for="{{{$name}}}_return_to_form" class="col-sm-4 control-label">
@@ -45,7 +49,7 @@
<div class="col-sm-8">
<div class="radio">
<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.
</label>
</div>
@@ -60,7 +64,7 @@
</label>
<div class="col-sm-8">
<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.
</label>
</div>