diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index d7488c1bdb..fb38a501c0 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -117,6 +117,7 @@ class AccountController extends BaseController $openingBalance = $this->_repository->openingBalanceTransaction($account); $subTitleIcon = $this->_subIconsByIdentifier[$account->accountType->type]; $subTitle = 'Edit ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"'; + $what = $this->_shortNamesByFullName[$account->accountType->type]; // pre fill some useful values. $preFilled = [ @@ -126,7 +127,7 @@ class AccountController extends BaseController ]; Session::flash('preFilled', $preFilled); - return View::make('accounts.edit', compact('account', 'subTitle', 'openingBalance', 'subTitleIcon')); + return View::make('accounts.edit', compact('account', 'what', 'subTitle', 'openingBalance', 'subTitleIcon')); } /** @@ -168,17 +169,10 @@ class AccountController extends BaseController */ public function store() { - /** @var \FireflyIII\Database\AccountType\AccountType $accountTypes */ - $accountTypes = App::make('FireflyIII\Database\AccountType\AccountType'); - - - $data = Input::except('_token'); - $type = $accountTypes->findByWhat($data['what']); - $data['user_id'] = \Auth::user()->id; - $data['account_type_id'] = $type->id; - // always validate: + $data = Input::except('_token'); $messages = $this->_repository->validate($data); + // flash messages: Session::flash('successes', $messages['successes']); Session::flash('errors', $messages['errors']); @@ -209,16 +203,10 @@ class AccountController extends BaseController */ public function update(Account $account) { - $data = Input::except('_token'); - $data['account_type_id'] = $account->account_type_id; - $data['user_id'] = \Auth::user()->id; - - - // always validate: + $data = Input::except('_token'); $messages = $this->_repository->validate($data); // flash messages: - Session::flash('warnings', $messages['warnings']); Session::flash('successes', $messages['successes']); Session::flash('errors', $messages['errors']); if ($messages['errors']->count() > 0) { diff --git a/app/lib/FireflyIII/Database/Account/Account.php b/app/lib/FireflyIII/Database/Account/Account.php index 37b5576fed..f450f293e2 100644 --- a/app/lib/FireflyIII/Database/Account/Account.php +++ b/app/lib/FireflyIII/Database/Account/Account.php @@ -127,6 +127,7 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $currency->id, 'amount' => $balance, + 'what' => 'opening', 'from' => $fromAccount, 'completed' => 0, 'to' => $toAccount, @@ -312,24 +313,8 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte $model->save(); if (isset($data['openingBalance']) && isset($data['openingBalanceDate']) && strlen($data['openingBalanceDate']) > 0) { - /** @noinspection PhpParamsInspection */ - $openingBalance = $this->openingBalanceTransaction($model); - if (is_null($openingBalance)) { - $this->storeInitialBalance($model, $data); - } else { - $openingBalance->date = new Carbon($data['openingBalanceDate']); - $openingBalance->save(); - $amount = floatval($data['openingBalance']); - /** @var \Transaction $transaction */ - foreach ($openingBalance->transactions as $transaction) { - if ($transaction->account_id == $model->id) { - $transaction->amount = $amount; - } else { - $transaction->amount = $amount * -1; - } - $transaction->save(); - } - } + $this->updateInitialBalance($model, $data); + } \Event::fire('account.update', [$model]); @@ -348,7 +333,7 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte { $successes = new MessageBag; $account = new \Account($model); - $account->isValid(); + $account->isValid('form_input', false); $errors = $account->getErrors(); if (isset($model['account_role']) && !in_array($model['account_role'], array_keys(\Config::get('firefly.accountRoles')))) { @@ -379,6 +364,32 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte return ['errors' => $errors, 'successes' => $successes]; } + /** + * @param Eloquent $model + * @param array $data + */ + public function updateInitialBalance(Eloquent $model, array $data) + { + /** @noinspection PhpParamsInspection */ + $openingBalance = $this->openingBalanceTransaction($model); + if (is_null($openingBalance)) { + $this->storeInitialBalance($model, $data); + } else { + $openingBalance->date = new Carbon($data['openingBalanceDate']); + $openingBalance->save(); + $amount = floatval($data['openingBalance']); + /** @var \Transaction $transaction */ + foreach ($openingBalance->transactions as $transaction) { + if ($transaction->account_id == $model->id) { + $transaction->amount = $amount; + } else { + $transaction->amount = $amount * -1; + } + $transaction->save(); + } + } + } + /** * Returns an object with id $id. * diff --git a/app/models/Account.php b/app/models/Account.php index 98e93478b4..e8150ce3d1 100644 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -19,12 +19,18 @@ class Account extends Eloquent * * @var array */ - protected $rules + protected $rulesets = [ - 'name' => 'required|between:1,100', - 'user_id' => 'required|exists:users,id', - 'account_type_id' => 'required|exists:account_types,id', - 'active' => 'required|boolean' + 'saving' => [ + 'name' => 'required|between:1,100', + 'user_id' => 'required|exists:users,id', + 'account_type_id' => 'required|exists:account_types,id', + 'active' => 'required|boolean' + ], + 'form_input' => [ + 'name' => 'required|between:1,100', + 'active' => 'required|boolean', + ] ]; /** diff --git a/app/views/accounts/edit.blade.php b/app/views/accounts/edit.blade.php index b7db423d70..62d246ec3c 100644 --- a/app/views/accounts/edit.blade.php +++ b/app/views/accounts/edit.blade.php @@ -29,6 +29,7 @@ {{Form::ffBalance('openingBalance',null, ['currency' => $openingBalance ? $openingBalance->transactionCurrency : null])}} {{Form::ffDate('openingBalanceDate')}} {{Form::ffSelect('account_role',Config::get('firefly.accountRoles'))}} + {{Form::hidden('what',$what)}} @endif