Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Reordered the account repository and fixed a small bug.
James Cole9015d6ca16a8
diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.phpindex a5f0b4a4a7..2f4b4acfc7 100644--- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php+++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php@@ -25,6 +25,56 @@ class EloquentAccountRepository implements AccountRepositoryInterface$this->_user = \Auth::user();}+ /**+ * @return mixed+ */+ public function count()+ {+ return $this->_user->accounts()->count();++ }++ /**+ * @param \Account $account+ *+ * @return bool|mixed+ */+ public function destroy(\Account $account)+ {+ // find all transaction journals related to this account:+ $journals = \TransactionJournal::withRelevantData()->accountIs($account)->get(['transaction_journals.*']);+ $accountIDs = [];++ /** @var \TransactionJournal $journal */+ foreach ($journals as $journal) {+ // remember the account id's of the transactions involved:+ foreach ($journal->transactions as $t) {+ $accountIDs[] = $t->account_id;+ }+ $journal->delete();++ }+ $accountIDs = array_unique($accountIDs);+ if (count($accountIDs) > 0) {+ // find the "initial balance" type accounts in this list. Should be just 1.+ $query = $this->_user->accounts()->accountTypeIn(['Initial balance account'])+ ->whereIn('accounts.id', $accountIDs);+ if ($query->count() == 1) {+ $iba = $query->first(['accounts.*']);+ $iba->delete();+ }+ }+ $account->delete();++ /**+ *+ * TODO+ * Also delete: initial balance, initial balance account, and transactions+ */++ return true;+ }+/*** @param $id*@@ -45,10 +95,12 @@ class EloquentAccountRepository implements AccountRepositoryInterface*/public function findExpenseAccountByName($name){+ $cashType = $this->findAccountType('Cash account');+ $importType = $this->findAccountType('Import account');// catch Import account:if ($name == 'Import account') {- $importType = $this->findAccountType('Import account');- $import = $this->firstOrCreate(++ $import = $this->firstOrCreate(['name' => 'Import account','user_id' => $this->_user->id,@@ -77,10 +129,11 @@ class EloquentAccountRepository implements AccountRepositoryInterface$account = $this->firstOrCreate($set);}+// find cash account as fall back:if (is_null($account)) {- $cashType = $this->findAccountType('Cash account');- $account = $this->_user->accounts()->where('account_type_id', $cashType->id)->first();++ $account = $this->_user->accounts()->where('account_type_id', $cashType->id)->first();}// create cash account as ultimate fall back:@@ -94,7 +147,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface$account = $this->firstOrCreate($set);}- if ($account->active == 0) {+ if ($account->active == 0 && $account->account_type_id != $cashType->id) {return null;}@@ -176,150 +229,108 @@ class EloquentAccountRepository implements AccountRepositoryInterfacereturn $account;}+ public function getByAccountType(\AccountType $type)+ {+ return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')+ ->where('account_type_id', $type->id)->get();+ }+/**- * Takes a transaction/account component and updates the transaction journal to match.- *- * @param Job $job- * @param array $payload+ * @param $ids*- * @return mixed+ * @return array|mixed*/- public function importUpdateTransaction(Job $job, array $payload)+ public function getByIds(array $ids){- /** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */- $repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');-- /** @var \Importmap $importMap */- $importMap = $repository->findImportmap($payload['mapID']);- $user = $importMap->user;- $this->overruleUser($user);-- if ($job->attempts() > 10) {- \Log::error('Never found budget/account combination "' . $payload['data']['transaction_id'] . '"');-- $importMap->jobsdone++;- $importMap->save();-- $job->delete(); // count fixed.- return;- }-- /** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */- $journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');- $journals->overruleUser($user);-- /*- * Prep some vars from the payload- */- $transactionId = intval($payload['data']['transaction_id']);- $componentId = intval($payload['data']['component_id']);-- /*- * Find the import map for both:- */- $accountMap = $repository->findImportEntry($importMap, 'Account', $componentId);- $transactionMap = $repository->findImportEntry($importMap, 'Transaction', $transactionId);-- /*- * Either may be null:- */- if (is_null($accountMap) || is_null($transactionMap)) {- \Log::notice('No map found in account/transaction mapper. Release.');- if (\Config::get('queue.default') == 'sync') {- $importMap->jobsdone++;- $importMap->save();- $job->delete(); // count fixed- } else {- $job->release(300); // proper release.- }- return;- }-- /*- * Find the account and the transaction:- */- $account = $this->find($accountMap->new);- /** @var \TransactionJournal $journal */- $journal = $journals->find($transactionMap->new);-- /*- * If either is null, release:- */- if (is_null($account) || is_null($journal)) {- \Log::notice('Map is incorrect in account/transaction mapper. Release.');- if (\Config::get('queue.default') == 'sync') {- $importMap->jobsdone++;- $importMap->save();- $job->delete(); // count fixed- } else {- $job->release(300); // proper release.- }- return;- }-- /*- * Update one of the journal's transactions to have the right account:- */- $importType = $this->findAccountType('Import account');- /** @var \Transaction $transaction */- $updated = false;- \Log::debug(- 'Connect "' . $account->name . '" (#' . $account->id . ') to "' . $journal->description . '" (#'- . $journal->id . ')'- );- foreach ($journal->transactions as $index => $transaction) {- /*- * If it's of the right type, update it!- */- \Log::debug(- 'Transaction ' . $index . ' (#' . $transaction->id . '): [' . $transaction->account->account_type_id- . ' vs. ' . $importType->id . ']'- );- if ($transaction->account->account_type_id == $importType->id) {- $transaction->account()->associate($account);- $transaction->save();- $updated = true;- \Log::debug(- 'Connected expense account "' . $account->name . '" to journal "' . $journal->description . '"'- );- }- }- if ($updated === false) {- \Log::error(- 'Did not connect transactions of journal #' . $journal->id . ' to expense account ' . $account->id- );-+ if (count($ids) > 0) {+ return $this->_user->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();+ } else {+ return $this->getActiveDefault();}+ }+//+// /**+// * @param $name+// *+// * @return \Account|mixed|null+// */+// public function createOrFindBeneficiary($name)+// {+// if (is_null($name) || strlen($name) == 0) {+// return null;+// }+// $type = \AccountType::where('type', 'Expense account')->first();+// return $this->createOrFind($name, $type);+// }+//+// /**+// * @param $name+// * @param \AccountType $type+// *+// * @return \Account|mixed+// */+// public function createOrFind($name, \AccountType $type = null)+// {+// $account = $this->findByName($name, $type);+// if (!$account) {+// $data = [+// 'name' => $name,+// 'account_type' => $type+// ];+//+// return $this->store($data);+// }+//+// return $account;+// }+//+// /**+// * @param $name+// * @param \AccountType $type+// *+// * @return mixed+// */+// public function findByName($name, \AccountType $type = null)+// {+// $type = is_null($type) ? \AccountType::where('type', 'Asset account')->first() : $type;+//+// return $this->_user->accounts()->where('account_type_id', $type->id)+// ->where('name', 'like', '%' . $name . '%')+// ->first();+// }- $journal->save();--- $importMap->jobsdone++;- $importMap->save();-- $job->delete(); // count fixed-+ /**+ * @return mixed+ */+ public function getActiveDefault()+ {+ return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])->where(+ 'accounts.active', 1+ )+ ->get(['accounts.*']);}/**- * @param \User $user- *- * @return mixed|void+ * @return mixed*/- public function overruleUser(\User $user)+ public function getDefault(){- $this->_user = $user;- return true;+ return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])+ ->orderBy('accounts.name', 'ASC')->get(['accounts.*']);}/**- * @param $accountId+ * Gets a list of accounts that have the mentioned type. Will automatically convert+ * strings in this array to actual (model) account types.*- * @return mixed+ * @param array $types+ *+ * @return Collection*/- public function find($accountId)+ public function getOfTypes(array $types){- return $this->_user->accounts()->where('id', $accountId)->first();+ $accounts = $this->_user->accounts()->accountTypeIn($types)->get(['accounts.*']);+ return $accounts;}/**@@ -403,53 +414,17 @@ class EloquentAccountRepository implements AccountRepositoryInterface$job->delete(); // count fixed.return;}-//-// /**-// * @param $name-// *-// * @return \Account|mixed|null-// */-// public function createOrFindBeneficiary($name)-// {-// if (is_null($name) || strlen($name) == 0) {-// return null;-// }-// $type = \AccountType::where('type', 'Expense account')->first();-// return $this->createOrFind($name, $type);-// }-//+// /**-// * @param $name-// * @param \AccountType $type+// * Used for import// *-// * @return \Account|mixed-// */-// public function createOrFind($name, \AccountType $type = null)-// {-// $account = $this->findByName($name, $type);-// if (!$account) {-// $data = [-// 'name' => $name,-// 'account_type' => $type-// ];-//-// return $this->store($data);-// }-//-// return $account;-// }-//-// /**// * @param $name-// * @param \AccountType $type// *// * @return mixed// */-// public function findByName($name, \AccountType $type = null)+// public function findByNameAny($name)// {-// $type = is_null($type) ? \AccountType::where('type', 'Asset account')->first() : $type;-//-// return $this->_user->accounts()->where('account_type_id', $type->id)+// return $this->_user->accounts()// ->where('name', 'like', '%' . $name . '%')// ->first();// }@@ -489,10 +464,9 @@ class EloquentAccountRepository implements AccountRepositoryInterface$account->name = $data['name'];$account->active- =- isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(- $data['active']- ) : 1;+ = isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(+ $data['active']+ ) : 1;// try to save it:try {@@ -570,121 +544,149 @@ class EloquentAccountRepository implements AccountRepositoryInterface}/**- * Gets a list of accounts that have the mentioned type. Will automatically convert- * strings in this array to actual (model) account types.+ * Takes a transaction/account component and updates the transaction journal to match.*- * @param array $types+ * @param Job $job+ * @param array $payload*- * @return Collection- */- public function getOfTypes(array $types)- {- $accounts = $this->_user->accounts()->accountTypeIn($types)->get(['accounts.*']);- return $accounts;- }-- /*** @return mixed*/- public function count()+ public function importUpdateTransaction(Job $job, array $payload){- return $this->_user->accounts()->count();-- }+ /** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */+ $repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');-// /**-// * Used for import-// *-// * @param $name-// *-// * @return mixed-// */-// public function findByNameAny($name)-// {-// return $this->_user->accounts()-// ->where('name', 'like', '%' . $name . '%')-// ->first();-// }+ /** @var \Importmap $importMap */+ $importMap = $repository->findImportmap($payload['mapID']);+ $user = $importMap->user;+ $this->overruleUser($user);- /**- * @param \Account $account- *- * @return bool|mixed- */- public function destroy(\Account $account)- {- // find all transaction journals related to this account:- $journals = \TransactionJournal::withRelevantData()->accountIs($account)->get(['transaction_journals.*']);- $accountIDs = [];+ if ($job->attempts() > 10) {+ \Log::error('Never found budget/account combination "' . $payload['data']['transaction_id'] . '"');- /** @var \TransactionJournal $journal */- foreach ($journals as $journal) {- // remember the account id's of the transactions involved:- foreach ($journal->transactions as $t) {- $accountIDs[] = $t->account_id;- }- $journal->delete();+ $importMap->jobsdone++;+ $importMap->save();+ $job->delete(); // count fixed.+ return;}- $accountIDs = array_unique($accountIDs);- if (count($accountIDs) > 0) {- // find the "initial balance" type accounts in this list. Should be just 1.- $query = $this->_user->accounts()->accountTypeIn(['Initial balance account'])- ->whereIn('accounts.id', $accountIDs);- if ($query->count() == 1) {- $iba = $query->first(['accounts.*']);- $iba->delete();++ /** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */+ $journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');+ $journals->overruleUser($user);++ /*+ * Prep some vars from the payload+ */+ $transactionId = intval($payload['data']['transaction_id']);+ $componentId = intval($payload['data']['component_id']);++ /*+ * Find the import map for both:+ */+ $accountMap = $repository->findImportEntry($importMap, 'Account', $componentId);+ $transactionMap = $repository->findImportEntry($importMap, 'Transaction', $transactionId);++ /*+ * Either may be null:+ */+ if (is_null($accountMap) || is_null($transactionMap)) {+ \Log::notice('No map found in account/transaction mapper. Release.');+ if (\Config::get('queue.default') == 'sync') {+ $importMap->jobsdone++;+ $importMap->save();+ $job->delete(); // count fixed+ } else {+ $job->release(300); // proper release.}+ return;}- $account->delete();- /**- *- * TODO- * Also delete: initial balance, initial balance account, and transactions+ /*+ * Find the account and the transaction:*/+ $account = $this->find($accountMap->new);+ /** @var \TransactionJournal $journal */+ $journal = $journals->find($transactionMap->new);- return true;- }+ /*+ * If either is null, release:+ */+ if (is_null($account) || is_null($journal)) {+ \Log::notice('Map is incorrect in account/transaction mapper. Release.');+ if (\Config::get('queue.default') == 'sync') {+ $importMap->jobsdone++;+ $importMap->save();+ $job->delete(); // count fixed+ } else {+ $job->release(300); // proper release.+ }+ return;+ }- public function getByAccountType(\AccountType $type)- {- return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')- ->where('account_type_id', $type->id)->get();- }+ /*+ * Update one of the journal's transactions to have the right account:+ */+ $importType = $this->findAccountType('Import account');+ /** @var \Transaction $transaction */+ $updated = false;+ \Log::debug(+ 'Connect "' . $account->name . '" (#' . $account->id . ') to "' . $journal->description . '" (#'+ . $journal->id . ')'+ );+ foreach ($journal->transactions as $index => $transaction) {+ /*+ * If it's of the right type, update it!+ */+ \Log::debug(+ 'Transaction ' . $index . ' (#' . $transaction->id . '): [' . $transaction->account->account_type_id+ . ' vs. ' . $importType->id . ']'+ );+ if ($transaction->account->account_type_id == $importType->id) {+ $transaction->account()->associate($account);+ $transaction->save();+ $updated = true;+ \Log::debug(+ 'Connected expense account "' . $account->name . '" to journal "' . $journal->description . '"'+ );+ }+ }+ if ($updated === false) {+ \Log::error(+ 'Did not connect transactions of journal #' . $journal->id . ' to expense account ' . $account->id+ );- /**- * @param $ids- *- * @return array|mixed- */- public function getByIds(array $ids)- {- if (count($ids) > 0) {- return $this->_user->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();- } else {- return $this->getActiveDefault();}++ $journal->save();+++ $importMap->jobsdone++;+ $importMap->save();++ $job->delete(); // count fixed+}/**- * @return mixed+ * @param \User $user+ *+ * @return mixed|void*/- public function getActiveDefault()+ public function overruleUser(\User $user){- return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])->where(- 'accounts.active', 1- )- ->get(['accounts.*']);+ $this->_user = $user;+ return true;}/**+ * @param $accountId+ ** @return mixed*/- public function getDefault()+ public function find($accountId){- return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])- ->orderBy('accounts.name', 'ASC')->get(['accounts.*']);+ return $this->_user->accounts()->where('id', $accountId)->first();}/**