Skip to content

Firefly III: a personal finances manager

Some cleaning up.

James Colec39c59fff5cf
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 @@
<?php
-namespace Firefly\Helper;
+namespace Firefly\Exception;
/**
diff --git a/app/lib/Firefly/Exception/ValidationException.php b/app/lib/Firefly/Exception/ValidationException.php
new file mode 100644
index 0000000000..415a7119f3
--- /dev/null
+++ b/app/lib/Firefly/Exception/ValidationException.php
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: sander
+ * Date: 19/09/14
+ * Time: 21:42
+ */
+
+namespace Firefly\Exception;
+
+/**
+ * Class ValidationException
+ *
+ * @package Firefly\Exception
+ */
+class ValidationException extends \Exception {
+
+}
\ No newline at end of file
diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
index 618ace476e..295d9902fe 100644
--- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
+++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
@@ -2,6 +2,7 @@
namespace Firefly\Storage\Account;
+
use Illuminate\Queue\Jobs\Job;
/**
@@ -66,6 +67,7 @@ interface AccountRepositoryInterface
/**
* @param $type
+ *
* @return mixed
*/
public function findAccountType($type);
@@ -80,6 +82,28 @@ interface AccountRepositoryInterface
*/
public function importUpdateTransaction(Job $job, array $payload);
+ /**
+ * @param $id
+ *
+ * @return |Account|null
+ */
+ public function findAssetAccountById($id);
+
+ /**
+ * @param $name
+ *
+ * @return |Account|null
+ */
+ public function findExpenseAccountByName($name);
+
+ /**
+ * @param \Account $from
+ * @param \Account $to
+ *
+ * @return \TransactionType|null
+ */
+ public function transactionTypeByAccounts(\Account $from, \Account $to);
+
/**
* @return mixed
*/
@@ -109,12 +133,14 @@ interface AccountRepositoryInterface
/**
* @param \AccountType $type
+ *
* @return mixed
*/
public function getByAccountType(\AccountType $type);
/**
* @param \User $user
+ *
* @return mixed
*/
public function overruleUser(\User $user);
diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
index 0c451a7555..0c925e12ec 100644
--- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
+++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
@@ -4,6 +4,7 @@
namespace Firefly\Storage\Account;
use Carbon\Carbon;
+use Firefly\Exception\FireflyException;
use Illuminate\Database\QueryException;
use Illuminate\Queue\Jobs\Job;
@@ -25,6 +26,114 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$this->_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;
}