Some code cleanup in the account code.

This commit is contained in:
James Cole 2015-01-17 07:25:44 +01:00
parent 7d4006b205
commit a854b2c17e
10 changed files with 40 additions and 223 deletions

View File

@ -88,7 +88,7 @@ class GoogleChartController extends BaseController
/** @var \FireflyIII\Database\Account\Account $acct */
$acct = App::make('FireflyIII\Database\Account\Account');
$accounts = count($pref->data) > 0 ? $acct->getByIds($pref->data) : $acct->getAssetAccounts();
$accounts = count($pref->data) > 0 ? $acct->getByIds($pref->data) : $acct->getAccountsByType(['Default account', 'Asset account']);
/** @var Account $account */
foreach ($accounts as $account) {

View File

@ -33,7 +33,7 @@ class HomeController extends BaseController
/** @var \FireflyIII\Shared\Preferences\PreferencesInterface $preferences */
$preferences = App::make('FireflyIII\Shared\Preferences\PreferencesInterface');
$count = $acct->countAssetAccounts();
$count = $acct->countAccountsByType(['Default account', 'Asset account']);
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
@ -42,7 +42,7 @@ class HomeController extends BaseController
// get the preference for the home accounts to show:
$frontPage = $preferences->get('frontPageAccounts', []);
if ($frontPage->data == []) {
$accounts = $acct->getAssetAccounts();
$accounts = $acct->getAccountsByType(['Default account', 'Asset account']);
} else {
$accounts = $acct->getByIds($frontPage->data);
}

View File

@ -36,7 +36,7 @@ class JsonController extends BaseController
{
/** @var \FireflyIII\Database\Account\Account $accounts */
$accounts = App::make('FireflyIII\Database\Account\Account');
$list = $accounts->getExpenseAccounts();
$list = $accounts->getAccountsByType(['Expense account', 'Beneficiary account']);
$return = [];
foreach ($list as $entry) {
$return[] = $entry->name;
@ -53,7 +53,7 @@ class JsonController extends BaseController
{
/** @var \FireflyIII\Database\Account\Account $accounts */
$accounts = App::make('FireflyIII\Database\Account\Account');
$list = $accounts->getRevenueAccounts();
$list = $accounts->getAccountsByType(['Revenue account']);
$return = [];
foreach ($list as $entry) {
$return[] = $entry->name;

View File

@ -62,7 +62,7 @@ class PiggyBankController extends BaseController
$acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggy_bank_periods');
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$accounts = FFForm::makeSelectList($acct->getAccountsByType(['Default account', 'Asset account']));
$subTitle = 'Create new piggy bank';
$subTitleIcon = 'fa-plus';
@ -107,7 +107,7 @@ class PiggyBankController extends BaseController
$acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggy_bank_periods');
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$accounts = FFForm::makeSelectList($acct->getAccountsByType(['Default account', 'Asset account']));
$subTitle = 'Edit piggy bank "' . e($piggyBank->name) . '"';
$subTitleIcon = 'fa-pencil';

View File

@ -29,7 +29,7 @@ class PreferencesController extends BaseController
/** @var \FireflyIII\Shared\Preferences\Preferences $preferences */
$preferences = App::make('FireflyIII\Shared\Preferences\Preferences');
$accounts = $acct->getAssetAccounts();
$accounts = $acct->getAccountsByType(['Default account', 'Asset account']);
$viewRange = $preferences->get('viewRange', '1M');
$viewRangeValue = $viewRange->data;
$frontPage = $preferences->get('frontPageAccounts', []);

View File

@ -34,7 +34,7 @@ class RepeatedExpenseController extends BaseController
/** @var \FireflyIII\Database\Account\Account $acct */
$acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggy_bank_periods');
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$accounts = FFForm::makeSelectList($acct->getAccountsByType(['Default account', 'Asset account']));
return View::make('repeatedExpense.create', compact('accounts', 'periods'))->with('subTitle', 'Create new repeated expense')->with(
'subTitleIcon', 'fa-plus'
@ -79,7 +79,7 @@ class RepeatedExpenseController extends BaseController
$acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggy_bank_periods');
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$accounts = FFForm::makeSelectList($acct->getAccountsByType(['Default account', 'Asset account']));
$subTitle = 'Edit repeated expense "' . e($repeatedExpense->name) . '"';
$subTitleIcon = 'fa-pencil';

View File

@ -78,9 +78,10 @@ class UserController extends BaseController
if ($user) {
$result = $email->sendVerificationMail($user);
if($result === false) {
if ($result === false) {
$user->delete();
return View::make('error')->with('message','The email message could not be send. See the log files.');
return View::make('error')->with('message', 'The email message could not be send. See the log files.');
}
return View::make('user.verification-pending');
@ -126,7 +127,7 @@ class UserController extends BaseController
public function register()
{
if (Config::get('mail.from.address') == '@gmail.com' || Config::get('mail.from.address') == '') {
return View::make('error')->with('message', 'Configuration error in <code>app/config/'.App::environment().'/mail.php</code>');
return View::make('error')->with('message', 'Configuration error in <code>app/config/' . App::environment() . '/mail.php</code>');
}
return View::make('user.register');

View File

@ -41,47 +41,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
return $this->getUser()->accounts()->accountTypeIn($types)->count();
}
/**
* @return int
*/
public function countAssetAccounts()
{
return $this->countAccountsByType(['Default account', 'Asset account']);
}
/**
* @return int
*/
public function countExpenseAccounts()
{
return $this->countAccountsByType(['Expense account', 'Beneficiary account']);
}
/**
* Counts the number of total revenue accounts. Useful for DataTables.
*
* @return int
*/
public function countRevenueAccounts()
{
return $this->countAccountsByType(['Revenue account']);
}
/**
* @param \Account $account
*
* @return \Account|null
*/
public function findInitialBalanceAccount(\Account $account)
{
/** @var \FireflyIII\Database\AccountType\AccountType $acctType */
$acctType = \App::make('FireflyIII\Database\AccountType\AccountType');
$accountType = $acctType->findByWhat('initial');
return $this->getUser()->accounts()->where('account_type_id', $accountType->id)->where('name', 'LIKE', $account->name . '%')->first();
}
/**
* @param array $types
*
@ -107,57 +66,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
return $set;
}
/**
* Get all asset accounts. Optional JSON based parameters.
*
* @param array $metaFilter
*
* @return Collection
*/
public function getAssetAccounts($metaFilter = [])
{
$list = $this->getAccountsByType(['Default account', 'Asset account']);
$list->each(
function (\Account $account) {
// get accountRole:
/** @var \AccountMeta $entry */
$accountRole = $account->accountmeta()->whereName('accountRole')->first();
if (!$accountRole) {
$accountRole = new \AccountMeta;
$accountRole->account_id = $account->id;
$accountRole->name = 'accountRole';
$accountRole->data = 'defaultExpense';
$accountRole->save();
}
$account->accountRole = $accountRole->data;
}
);
return $list;
}
/**
* @return Collection
*/
public function getExpenseAccounts()
{
return $this->getAccountsByType(['Expense account', 'Beneficiary account']);
}
/**
* Get all revenue accounts.
*
* @return Collection
*/
public function getRevenueAccounts()
{
return $this->getAccountsByType(['Revenue account']);
}
/**
* @param \Account $account
*
@ -235,6 +143,31 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
}
/**
* @param \Account $account
*
* @return int
*/
public function getLastActivity(\Account $account)
{
$lastActivityKey = 'account.' . $account->id . '.lastActivityDate';
if (\Cache::has($lastActivityKey)) {
return \Cache::get($lastActivityKey);
}
$transaction = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'DESC')->first();
if ($transaction) {
$date = $transaction->transactionJournal->date;
} else {
$date = 0;
}
\Cache::forever($lastActivityKey, $date);
return $date;
}
/**
* @param Eloquent $model
*
@ -571,57 +504,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
}
/**
* @param \Account $account
* @param int $limit
*
* @return \Illuminate\Pagination\Paginator
*/
public function getAllTransactionJournals(\Account $account, $limit = 50)
{
$offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0;
$set = $this->getUser()->transactionJournals()->withRelevantData()->leftJoin(
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
)->where('transactions.account_id', $account->id)->take($limit)->offset($offset)->orderBy('date', 'DESC')->get(
['transaction_journals.*']
);
$count = $this->getUser()->transactionJournals()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->orderBy('date', 'DESC')->where('transactions.account_id', $account->id)->count();
$items = [];
foreach ($set as $entry) {
$items[] = $entry;
}
return \Paginator::make($items, $count, $limit);
}
/**
* @param \Account $account
*
* @return int
*/
public function getLastActivity(\Account $account)
{
$lastActivityKey = 'account.' . $account->id . '.lastActivityDate';
if (\Cache::has($lastActivityKey)) {
return \Cache::get($lastActivityKey);
}
$transaction = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'DESC')->first();
if ($transaction) {
$date = $transaction->transactionJournal->date;
} else {
$date = 0;
}
\Cache::forever($lastActivityKey, $date);
return $date;
}
/**
* @param \Account $account
* @param int $limit
@ -656,24 +538,4 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
}
/**
* @param \Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Pagination\Paginator
*/
public function getTransactionJournalsInRange(\Account $account, Carbon $start, Carbon $end)
{
$set = $this->getUser()->transactionJournals()->transactionTypes(['Withdrawal'])->withRelevantData()->leftJoin(
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
)->where('transactions.account_id', $account->id)->before($end)->after($start)->orderBy('date', 'DESC')->get(
['transaction_journals.*']
);
return $set;
}
}

View File

@ -21,34 +21,6 @@ interface AccountInterface
*/
public function countAccountsByType(array $types);
/**
* Counts the number of total asset accounts. Useful for DataTables.
*
* @return int
*/
public function countAssetAccounts();
/**
* Counts the number of total expense accounts. Useful for DataTables.
*
* @return int
*/
public function countExpenseAccounts();
/**
* Counts the number of total revenue accounts. Useful for DataTables.
*
* @return int
*/
public function countRevenueAccounts();
/**
* @param \Account $account
*
* @return \Account|null
*/
public function findInitialBalanceAccount(\Account $account);
/**
* Get all accounts of the selected types. Is also capable of handling DataTables' parameters.
*
@ -58,24 +30,6 @@ interface AccountInterface
*/
public function getAccountsByType(array $types);
/**
* Get all asset accounts. The parameters are optional and are provided by the DataTables plugin.
*
* @return Collection
*/
public function getAssetAccounts();
/**
* @return Collection
*/
public function getExpenseAccounts();
/**
* Get all revenue accounts.
*
* @return Collection
*/
public function getRevenueAccounts();
/**
* @param \Account $account

View File

@ -387,7 +387,7 @@ class Report implements ReportInterface
$sharedAccounts[] = $account->id;
}
$accounts = $this->_accounts->getAssetAccounts()->filter(
$accounts = $this->_accounts->getAccountsByType(['Default account', 'Asset account'])->filter(
function (\Account $account) use ($sharedAccounts) {
if (!in_array($account->id, $sharedAccounts)) {
return $account;