From 384125977984ea339736e3c0f5733e6ff60864cd Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 9 Feb 2015 07:23:39 +0100 Subject: [PATCH] First attempt at storing an account. --- app/Http/Controllers/AccountController.php | 23 ++- app/Http/Requests/AccountFormRequest.php | 13 +- app/Models/Account.php | 13 +- app/Providers/FireflyServiceProvider.php | 4 + .../Account/AccountRepository.php | 63 ++++++ .../Account/AccountRepositoryInterface.php | 9 + .../Journal/JournalRepository.php | 8 + .../Journal/JournalRepositoryInterface.php | 14 ++ config/firefly.php | 6 + resources/lang/en/validation.php | 193 +++++++++--------- resources/views/accounts/create.blade.php | 11 +- resources/views/form/options.blade.php | 12 +- 12 files changed, 258 insertions(+), 111 deletions(-) create mode 100644 app/Repositories/Account/AccountRepository.php create mode 100644 app/Repositories/Account/AccountRepositoryInterface.php create mode 100644 app/Repositories/Journal/JournalRepository.php create mode 100644 app/Repositories/Journal/JournalRepositoryInterface.php 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 @@ $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 @@ +_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 @@ + ['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 @@
- + @if($what == 'asset')
Optional fields
- @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')) !!} +
- + @endif
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') + @endif @if($type == 'update') + @endif - + @if($type == 'create')