From c39c59fff5cf0ac985063089417e7c253ddff9cc Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 19 Sep 2014 23:05:57 +0200 Subject: [PATCH] Some cleaning up. --- .../Firefly/Exception/MigrationException.php | 2 +- .../Firefly/Exception/ValidationException.php | 18 ++ .../Account/AccountRepositoryInterface.php | 26 ++ .../Account/EloquentAccountRepository.php | 225 +++++++++++++----- .../Budget/EloquentBudgetRepository.php | 8 - .../Piggybank/EloquentPiggybankRepository.php | 4 - 6 files changed, 204 insertions(+), 79 deletions(-) create mode 100644 app/lib/Firefly/Exception/ValidationException.php diff --git a/app/lib/Firefly/Exception/MigrationException.php b/app/lib/Firefly/Exception/MigrationException.php index f6d85835a0..7bf4a6fa3a 100644 --- a/app/lib/Firefly/Exception/MigrationException.php +++ b/app/lib/Firefly/Exception/MigrationException.php @@ -1,6 +1,6 @@ _user = \Auth::user(); } + /** + * @param $id + * + * @return |Account|null + */ + public function findAssetAccountById($id) + { + return $this->_user->accounts()->find($id); + } + /** + * @param \Account $from + * @param \Account $to + * @param int $amount + * + * @throws FireflyException + * + * @return \TransactionType|null + */ + public function transactionTypeByAccounts(\Account $from, \Account $to, $amount = 0) { + // account types for both: + $toAT = $to->accountType->type; + $fromAT = $from->accountType->type; + + $journalType = null; + + switch (true) { + case ($from->transactions()->count() == 0 && $to->transactions()->count() == 0): + $journalType = \TransactionType::where('type', 'Opening balance')->first(); + break; + + case (in_array($fromAT, ['Default account', 'Asset account']) + && in_array( + $toAT, ['Default account', 'Asset account'] + )): // both are yours: + // determin transaction type. If both accounts are new, it's an initial balance transfer. + $journalType = \TransactionType::where('type', 'Transfer')->first(); + break; + case ($amount < 0): + $journalType = \TransactionType::where('type', 'Deposit')->first(); + break; + // is deposit into one of your own accounts: + case ($toAT == 'Default account' || $toAT == 'Asset account'): + $journalType = \TransactionType::where('type', 'Deposit')->first(); + break; + // is withdrawal from one of your own accounts: + case ($fromAT == 'Default account' || $fromAT == 'Asset account'): + $journalType = \TransactionType::where('type', 'Withdrawal')->first(); + break; + } + + if (is_null($journalType)) { + throw new FireflyException('Could not figure out transaction type.'); + } + return $journalType; + } + + + + /** + * @param $name + * + * @return |Account|null + */ + public function findExpenseAccountByName($name) + { + // find account: + $type = $this->findAccountType('Expense account'); + $account = $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first(); + + // 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(); + } + + // create cash account as ultimate fall back: + if (is_null($account)) { + $set = [ + 'name' => 'Cash account', + 'user_id' => $this->_user->id, + 'active' => 1, + 'account_type_id' => $cashType->id + ]; + $account = $this->firstOrCreate($set); + } + + if ($account->active == 0) { + return null; + } + + return $account; + } + + /** + * @param $type + * + * @return mixed + */ + public function findAccountType($type) + { + return \AccountType::where('type', $type)->first(); + } + + public function firstOrCreate(array $data) + { + return \Account::firstOrCreate($data); + } + /** * Takes a transaction/account component and updates the transaction journal to match. * @@ -152,16 +261,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface return $this->_user->accounts()->where('id', $accountId)->first(); } - /** - * @param $type - * - * @return mixed - */ - public function findAccountType($type) - { - return \AccountType::where('type', $type)->first(); - } - /** * @param Job $job * @param array $payload @@ -243,6 +342,56 @@ 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 +// * +// * @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(); +// } /** * @param $data @@ -304,56 +453,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface // whatever the result, return the account. return $account; } -// -// /** -// * @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(); -// } /** * @param \Account $account @@ -393,11 +492,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface return false; } - public function firstOrCreate(array $data) - { - return \Account::firstOrCreate($data); - } - /** * Gets a list of accounts that have the mentioned type. Will automatically convert * strings in this array to actual (model) account types. @@ -551,7 +645,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])->where( 'accounts.active', 1 ) - ->get(['accounts.*']); } diff --git a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php index 2dd5ff6bf0..0f177f12ab 100644 --- a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php +++ b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php @@ -378,14 +378,6 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface $q->orderBy('limit_repetitions.startdate', 'ASC'); }] )->orderBy('name', 'ASC')->get(); - foreach ($set as $budget) { - foreach ($budget->limits as $limit) { - foreach ($limit->limitrepetitions as $rep) { - $rep->left = $rep->left(); - } - } - } - return $set; } diff --git a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php index b6be8be98f..5e4299b4a0 100644 --- a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php +++ b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php @@ -213,10 +213,6 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface { $piggies = $this->_user->piggybanks()->with(['account', 'piggybankrepetitions'])->get(); - foreach ($piggies as $pig) { - $pig->leftInAccount = $this->leftOnAccount($pig->account); - } - return $piggies; }