Expand model to get meta data

This commit is contained in:
James Cole 2014-12-05 21:39:26 +01:00
parent 2134e87c31
commit ac765b7e4c

View File

@ -89,7 +89,7 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
/* /*
* Basic query: * Basic query:
*/ */
$query = $this->getUser()->accounts()->accountTypeIn($types); $query = $this->getUser()->accounts()->accountTypeIn($types)->withMeta();
/* /*
@ -128,7 +128,22 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
*/ */
public function getAssetAccounts() public function getAssetAccounts()
{ {
return $this->getAccountsByType(['Default account', 'Asset account']); $list = $this->getAccountsByType(['Default account', 'Asset account']);
$list->each(
function (\Account $account) {
/** @var \AccountMeta $entry */
foreach ($account->accountmeta as $entry) {
if ($entry->name == 'accountRole') {
$account->accountRole = \Config::get('firefly.accountRoles.' . $entry->data);
}
}
if (!isset($account->accountRole)) {
$account->accountRole = 'Default expense account';
}
}
);
return $list;
} }
@ -276,25 +291,38 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
{ {
$model->name = $data['name']; $model->name = $data['name'];
$model->active = isset($data['active']) ? intval($data['active']) : 0; $model->active = isset($data['active']) ? intval($data['active']) : 0;
switch ($model->accountType->type) {
case 'Asset account':
case 'Default account':
$model->updateMeta('accountRole', $data['account_role']);
break;
}
$model->save(); $model->save();
if (isset($data['openingbalance']) && isset($data['openingbalancedate']) && strlen($data['openingbalancedate']) > 0) { if (isset($data['openingbalance']) && isset($data['openingbalancedate']) && strlen($data['openingbalancedate']) > 0) {
$openingBalance = $this->openingBalanceTransaction($model); $openingBalance = $this->openingBalanceTransaction($model);
$openingBalance->date = new Carbon($data['openingbalancedate']); if (is_null($openingBalance)) {
$openingBalance->save(); $this->storeInitialBalance($model, $data);
$amount = floatval($data['openingbalance']); } else {
/** @var \Transaction $transaction */ $openingBalance->date = new Carbon($data['openingbalancedate']);
foreach ($openingBalance->transactions as $transaction) { $openingBalance->save();
if ($transaction->account_id == $model->id) { $amount = floatval($data['openingbalance']);
$transaction->amount = $amount; /** @var \Transaction $transaction */
} else { foreach ($openingBalance->transactions as $transaction) {
$transaction->amount = $amount * -1; if ($transaction->account_id == $model->id) {
$transaction->amount = $amount;
} else {
$transaction->amount = $amount * -1;
}
$transaction->save();
} }
$transaction->save();
} }
} }
\Event::fire('account.update', [$model]); \Event::fire('account.update', [$model]);
return true; return true;
} }
@ -330,6 +358,12 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
$errors->merge($errors); $errors->merge($errors);
} }
if (isset($model['account_role']) && !in_array($model['account_role'], array_keys(\Config::get('firefly.accountRoles')))) {
$errors->add('account_role', 'Invalid account role');
} else {
$successes->add('account_role', 'OK');
}
/* /*
* type validation. * type validation.
*/ */