More code cleanup [skip ci]

This commit is contained in:
James Cole 2015-02-01 09:30:27 +01:00
parent 6873336aca
commit 13d3d40376
4 changed files with 47 additions and 41 deletions

View File

@ -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) {

View File

@ -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.
*

View File

@ -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',
]
];
/**

View File

@ -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
</div>
</div>